This project has retired. For details please refer to its Attic page.
AbstractSingleFilingImpl xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.inmemory.storedobj.impl;
20  
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
25  import org.apache.chemistry.opencmis.commons.exceptions.CmisNameConstraintViolationException;
26  import org.apache.chemistry.opencmis.inmemory.NameValidator;
27  import org.apache.chemistry.opencmis.inmemory.storedobj.api.Document;
28  import org.apache.chemistry.opencmis.inmemory.storedobj.api.Folder;
29  import org.apache.chemistry.opencmis.inmemory.storedobj.api.SingleFiling;
30  import org.apache.chemistry.opencmis.inmemory.storedobj.api.VersionedDocument;
31  
32  /**
33   * AbstractPathImpl is the common superclass of all objects hold in the
34   * repository that have a single parent, these are: Folders.
35   *
36   * @author Jens
37   */
38  public abstract class AbstractSingleFilingImpl extends StoredObjectImpl implements SingleFiling {
39  
40      protected FolderImpl fParent;
41  
42      protected AbstractSingleFilingImpl(ObjectStoreImpl objStore) {
43          super(objStore);
44      }
45  
46      public String getPath() {
47          StringBuffer path = new StringBuffer(getName());
48          if (null == getParent()) {
49              path.replace(0, path.length(), PATH_SEPARATOR);
50          } else {
51              // root folder-->
52              Folder f = getParent();
53              while (f.getParent() != null) {
54                  path.insert(0, PATH_SEPARATOR);
55                  path.insert(0, f.getName());
56                  f = f.getParent();
57              }
58              path.insert(0, PATH_SEPARATOR);
59          }
60          // if (LOG.isDebugEnabled())
61          // LOG.debug("getPath() returns: " + path.toString());
62          return path.toString();
63      }
64  
65      public Folder getParent() {
66          return fParent;
67      }
68  
69      public boolean hasParent() {
70        return null != fParent;
71      }
72  
73      public List<Folder> getParents() {
74          if (null == fParent) {
75              return Collections.emptyList();
76          } else {
77              return Collections.singletonList((Folder) fParent);
78          }
79      }
80  
81      public List<Folder> getParents(String user) {
82          return getParents();
83      }
84      
85      public void setParent(Folder parent) {
86          try {
87              fObjStore.lock();
88              fParent = (FolderImpl) parent;
89          } finally {
90            fObjStore.unlock();
91          }
92      }
93  
94      @Override
95      public void rename(String newName) {
96          if (!NameValidator.isValidId(newName)) {
97              throw new CmisInvalidArgumentException(NameValidator.ERROR_ILLEGAL_NAME);
98          }
99          try {
100             fObjStore.lock();
101             if (getParent() == null) {
102                 throw new CmisInvalidArgumentException("Root folder cannot be renamed.");
103             }
104             if (getParent().hasChild(newName)) {
105                 throw new CmisNameConstraintViolationException("Cannot rename object to " + newName
106                         + ". This path already exists.");
107             }
108 
109             setName(newName);
110         } finally {
111           fObjStore.unlock();
112         }
113     }
114 
115     public void move(Folder oldParent, Folder newParent) {
116         try {
117             fObjStore.lock();
118             if (this instanceof Document || this instanceof VersionedDocument) {
119                 fParent.moveChildDocument(this, oldParent, newParent);
120             } else {// it must be a folder
121                 if (getParent() == null) {
122                     throw new IllegalArgumentException("Root folder cannot be moved.");
123                 }
124                 if (newParent == null) {
125                     throw new IllegalArgumentException("null is not a valid move target.");
126                 }
127                 if (newParent.hasChild(getName())) {
128                     throw new IllegalArgumentException("Cannot move folder, this name already exists in target.");
129                 }
130 
131                 setParent(newParent);
132             }
133         } finally {
134           fObjStore.unlock();
135         }
136     }
137 
138 }