This project has retired. For details please refer to its Attic page.
AbstractMultiFilingImpl 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.ArrayList;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
26  import org.apache.chemistry.opencmis.commons.exceptions.CmisNameConstraintViolationException;
27  import org.apache.chemistry.opencmis.inmemory.NameValidator;
28  import org.apache.chemistry.opencmis.inmemory.storedobj.api.Folder;
29  import org.apache.chemistry.opencmis.inmemory.storedobj.api.MultiFiling;
30  
31  /**
32   * AbstractMultiPathImpl is the common superclass of all objects hold in the
33   * repository that have multiple parent folders, these are: Folders
34   *
35   * @author Jens
36   */
37  public abstract class AbstractMultiFilingImpl extends StoredObjectImpl implements MultiFiling {
38  
39      protected List<Folder> fParents = new ArrayList<Folder>(1);
40  
41      AbstractMultiFilingImpl(ObjectStoreImpl objStore) {
42          super(objStore);
43      }
44  
45      public void addParent(Folder parent) {
46        try {
47            fObjStore.lock();
48            addParentIntern(parent);
49        } finally {
50          fObjStore.unlock();
51        }
52      }
53  
54      private void addParentIntern(Folder parent) {
55          if (parent.hasChild(getName())) {
56              throw new IllegalArgumentException(
57                      "Cannot assign new parent folder, this name already exists in target folder.");
58          }
59  
60          if (null == fParents) {
61              fParents = new ArrayList<Folder>();
62          }
63  
64          fParents.add(parent);
65      }
66  
67      public void removeParent(Folder parent) {
68          try {
69              fObjStore.lock();
70              removeParentIntern(parent);
71          } finally {
72            fObjStore.unlock();
73          }
74      }
75  
76      private void removeParentIntern(Folder parent) {
77          fParents.remove(parent);
78          if (fParents.isEmpty()) {
79              fParents = null;
80          }
81      }
82  
83      public List<Folder> getParents() {
84          if (null == fParents)
85              return Collections.emptyList();
86          else
87              return fParents;
88      }
89  
90      /*
91       * (non-Javadoc)
92       * 
93       * @see
94       * org.apache.opencmis.inmemory.storedobj.api.MultiParentPath#getParents()
95       */
96      public List<Folder> getParents(String user) {
97          if (null == fParents)
98              return Collections.emptyList();
99          else if (null == user)
100             return Collections.unmodifiableList(fParents);
101         else {
102             List<Folder> visibleParents = new ArrayList<Folder>(fParents.size());
103             for (Folder folder : fParents)
104                 if (fObjStore.hasReadAccess(user, folder))
105                     visibleParents.add(folder);
106             return visibleParents;
107         }
108     }
109 
110     public boolean hasParent() {
111       return null != fParents && !fParents.isEmpty();
112     }
113 
114     public String getPathSegment() {
115         return getName();
116     }
117 
118     public void move(Folder oldParent, Folder newParent) {
119         try {
120             fObjStore.lock();
121             addParentIntern(newParent);
122             removeParentIntern(oldParent);
123         } finally {
124           fObjStore.unlock();
125         }
126     }
127 
128     @Override
129     public void rename(String newName) {
130         try {
131             if (!NameValidator.isValidId(newName)) {
132                 throw new CmisInvalidArgumentException(NameValidator.ERROR_ILLEGAL_NAME);
133             }
134             fObjStore.lock();
135             for (Folder folder : fParents) {
136               if (folder == null) {
137                 throw new CmisInvalidArgumentException("Root folder cannot be renamed.");
138             }
139               if (folder.hasChild(newName)) {
140                 throw new CmisNameConstraintViolationException("Cannot rename object to " + newName
141                           + ". This path already exists in parent " + folder.getPath() + ".");
142             }
143             }
144             setName(newName);
145         } finally {
146           fObjStore.unlock();
147         }
148     }
149 
150 }