View Javadoc

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.client.runtime;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.chemistry.opencmis.client.api.CmisObject;
25  import org.apache.chemistry.opencmis.client.api.FileableCmisObject;
26  import org.apache.chemistry.opencmis.client.api.Folder;
27  import org.apache.chemistry.opencmis.client.api.ObjectId;
28  import org.apache.chemistry.opencmis.client.api.ObjectType;
29  import org.apache.chemistry.opencmis.client.api.OperationContext;
30  import org.apache.chemistry.opencmis.commons.PropertyIds;
31  import org.apache.chemistry.opencmis.commons.data.ObjectParentData;
32  import org.apache.chemistry.opencmis.commons.data.PropertyData;
33  import org.apache.chemistry.opencmis.commons.data.PropertyId;
34  import org.apache.chemistry.opencmis.commons.data.PropertyString;
35  import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
36  import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;
37  import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
38  import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
39  import org.apache.chemistry.opencmis.commons.spi.Holder;
40  
41  /**
42   * Base class for all filable persistent session object impl classes.
43   */
44  public abstract class AbstractFilableCmisObject extends AbstractCmisObject implements FileableCmisObject {
45  
46      private static final long serialVersionUID = 1L;
47  
48      @Override
49      public List<Folder> getParents() {
50          return getParents(getSession().getDefaultContext());
51      }
52  
53      @Override
54      public List<Folder> getParents(OperationContext context) {
55          String objectId = getObjectId();
56  
57          // get object ids of the parent folders
58          List<ObjectParentData> bindingParents = getBinding().getNavigationService().getObjectParents(getRepositoryId(),
59                  objectId, getPropertyQueryName(PropertyIds.OBJECT_ID), false, IncludeRelationships.NONE, null, false,
60                  null);
61  
62          List<Folder> parents = new ArrayList<Folder>();
63  
64          for (ObjectParentData p : bindingParents) {
65              if (p == null || p.getObject() == null || p.getObject().getProperties() == null) {
66                  // should not happen...
67                  throw new CmisRuntimeException("Repository sent invalid data!");
68              }
69  
70              // get id property
71              PropertyData<?> idProperty = p.getObject().getProperties().getProperties().get(PropertyIds.OBJECT_ID);
72              if (!(idProperty instanceof PropertyId) && !(idProperty instanceof PropertyString)) {
73                  // the repository sent an object without a valid object id...
74                  throw new CmisRuntimeException("Repository sent invalid data! No object id!");
75              }
76  
77              // fetch the object and make sure it is a folder
78              CmisObject parentFolder = getSession().getObject((String) idProperty.getFirstValue(), context);
79              if (!(parentFolder instanceof Folder)) {
80                  // the repository sent an object that is not a folder...
81                  throw new CmisRuntimeException("Repository sent invalid data! Object is not a folder!");
82              }
83  
84              parents.add((Folder) parentFolder);
85          }
86  
87          return parents;
88      }
89  
90      @Override
91      public List<String> getPaths() {
92          String objectId = getObjectId();
93  
94          // determine filter
95          String filter;
96  
97          ObjectType folderType = getSession().getTypeDefinition(BaseTypeId.CMIS_FOLDER.value());
98          PropertyDefinition<?> idPropDef = folderType.getPropertyDefinitions().get(PropertyIds.OBJECT_ID);
99          PropertyDefinition<?> pathPropDef = folderType.getPropertyDefinitions().get(PropertyIds.PATH);
100         if (idPropDef != null && pathPropDef != null) {
101             filter = idPropDef.getQueryName() + "," + pathPropDef.getQueryName();
102         } else {
103             filter = "*";
104         }
105 
106         // get object paths of the parent folders
107         List<ObjectParentData> bindingParents = getBinding().getNavigationService().getObjectParents(getRepositoryId(),
108                 objectId, filter, false, IncludeRelationships.NONE, null, true, null);
109 
110         List<String> paths = new ArrayList<String>();
111 
112         for (ObjectParentData p : bindingParents) {
113             if (p == null || p.getObject() == null || p.getObject().getProperties() == null) {
114                 // should not happen...
115                 throw new CmisRuntimeException("Repository sent invalid data!");
116             }
117 
118             // get path property
119             PropertyData<?> pathProperty = p.getObject().getProperties().getProperties().get(PropertyIds.PATH);
120             if (!(pathProperty instanceof PropertyString)) {
121                 // the repository sent a folder without a valid path...
122                 throw new CmisRuntimeException("Repository sent invalid data! No path property!");
123             }
124 
125             if (p.getRelativePathSegment() == null) {
126                 // the repository didn't send a relative path segment
127                 throw new CmisRuntimeException("Repository sent invalid data! No relative path segement!");
128             }
129 
130             String folderPath = ((String) pathProperty.getFirstValue());
131             if (folderPath == null) {
132                 // the repository sent a folder without a valid path...
133                 throw new CmisRuntimeException("Repository sent invalid data! No path property value!");
134             }
135             paths.add(folderPath + (folderPath.endsWith("/") ? "" : "/") + p.getRelativePathSegment());
136         }
137 
138         return paths;
139     }
140 
141     @Override
142     public FileableCmisObject move(ObjectId sourceFolderId, ObjectId targetFolderId) {
143         return move(sourceFolderId, targetFolderId, getSession().getDefaultContext());
144     }
145 
146     @Override
147     public FileableCmisObject move(ObjectId sourceFolderId, ObjectId targetFolderId, OperationContext context) {
148         String objectId = getObjectId();
149         Holder<String> objectIdHolder = new Holder<String>(objectId);
150 
151         if (sourceFolderId == null || sourceFolderId.getId() == null) {
152             throw new IllegalArgumentException("Source folder id must be set!");
153         }
154 
155         if (targetFolderId == null || targetFolderId.getId() == null) {
156             throw new IllegalArgumentException("Target folder id must be set!");
157         }
158 
159         getBinding().getObjectService().moveObject(getRepositoryId(), objectIdHolder, targetFolderId.getId(),
160                 sourceFolderId.getId(), null);
161 
162         // invalidate path cache
163         getSession().removeObjectFromCache(objectId);
164 
165         if (objectIdHolder.getValue() == null) {
166             return null;
167         }
168 
169         CmisObject movedObject = getSession().getObject(objectIdHolder.getValue(), context);
170         if (!(movedObject instanceof FileableCmisObject)) {
171             throw new CmisRuntimeException("Moved object is invalid!");
172         }
173 
174         return (FileableCmisObject) movedObject;
175     }
176 
177     @Override
178     public void addToFolder(ObjectId folderId, boolean allVersions) {
179         String objectId = getObjectId();
180 
181         if (folderId == null || folderId.getId() == null) {
182             throw new IllegalArgumentException("Folder Id must be set!");
183         }
184 
185         getBinding().getMultiFilingService().addObjectToFolder(getRepositoryId(), objectId, folderId.getId(),
186                 allVersions, null);
187 
188         // invalidate path cache
189         getSession().removeObjectFromCache(objectId);
190     }
191 
192     @Override
193     public void removeFromFolder(ObjectId folderId) {
194         String objectId = getObjectId();
195 
196         getBinding().getMultiFilingService().removeObjectFromFolder(getRepositoryId(), objectId,
197                 (folderId != null ? folderId.getId() : null), null);
198 
199         // invalidate path cache
200         getSession().removeObjectFromCache(objectId);
201     }
202 }