This project has retired. For details please refer to its Attic page.
AbstractFilableCmisObject 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.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      public List<Folder> getParents() {
49          return getParents(getSession().getDefaultContext());
50      }
51  
52      public List<Folder> getParents(OperationContext context) {
53          String objectId = getObjectId();
54  
55          // get object ids of the parent folders
56          List<ObjectParentData> bindingParents = getBinding().getNavigationService().getObjectParents(getRepositoryId(),
57                  objectId, getPropertyQueryName(PropertyIds.OBJECT_ID), false, IncludeRelationships.NONE, null, false,
58                  null);
59  
60          List<Folder> parents = new ArrayList<Folder>();
61  
62          for (ObjectParentData p : bindingParents) {
63              if ((p == null) || (p.getObject() == null) || (p.getObject().getProperties() == null)) {
64                  // should not happen...
65                  throw new CmisRuntimeException("Repository sent invalid data!");
66              }
67  
68              // get id property
69              PropertyData<?> idProperty = p.getObject().getProperties().getProperties().get(PropertyIds.OBJECT_ID);
70              if (!(idProperty instanceof PropertyId)) {
71                  // the repository sent an object without a valid object id...
72                  throw new CmisRuntimeException("Repository sent invalid data! No object id!");
73              }
74  
75              // fetch the object and make sure it is a folder
76              CmisObject parentFolder = getSession().getObject((String) idProperty.getFirstValue(), context);
77              if (!(parentFolder instanceof Folder)) {
78                  // the repository sent an object that is not a folder...
79                  throw new CmisRuntimeException("Repository sent invalid data! Object is not a folder!");
80              }
81  
82              parents.add((Folder) parentFolder);
83          }
84  
85          return parents;
86      }
87  
88      public List<String> getPaths() {
89          String objectId = getObjectId();
90  
91          ObjectType folderType = getSession().getTypeDefinition(BaseTypeId.CMIS_FOLDER.value());
92          PropertyDefinition<?> propDef = folderType.getPropertyDefinitions().get(PropertyIds.PATH);
93          String pathQueryName = (propDef == null ? null : propDef.getQueryName());
94  
95          // get object paths of the parent folders
96          List<ObjectParentData> bindingParents = getBinding().getNavigationService().getObjectParents(getRepositoryId(),
97                  objectId, pathQueryName, false, IncludeRelationships.NONE, null, true, null);
98  
99          List<String> paths = new ArrayList<String>();
100 
101         for (ObjectParentData p : bindingParents) {
102             if ((p == null) || (p.getObject() == null) || (p.getObject().getProperties() == null)) {
103                 // should not happen...
104                 throw new CmisRuntimeException("Repository sent invalid data!");
105             }
106 
107             // get path property
108             PropertyData<?> pathProperty = p.getObject().getProperties().getProperties().get(PropertyIds.PATH);
109             if (!(pathProperty instanceof PropertyString)) {
110                 // the repository sent a folder without a valid path...
111                 throw new CmisRuntimeException("Repository sent invalid data! No path property!");
112             }
113 
114             if (p.getRelativePathSegment() == null) {
115                 // the repository didn't send a relative path segment
116                 throw new CmisRuntimeException("Repository sent invalid data! No relative path segement!");
117             }
118 
119             String folderPath = ((String) pathProperty.getFirstValue());
120             paths.add(folderPath + (folderPath.endsWith("/") ? "" : "/") + p.getRelativePathSegment());
121         }
122 
123         return paths;
124     }
125 
126     public FileableCmisObject move(ObjectId sourceFolderId, ObjectId targetFolderId) {
127         return move(sourceFolderId, targetFolderId, getSession().getDefaultContext());
128     }
129 
130     public FileableCmisObject move(ObjectId sourceFolderId, ObjectId targetFolderId, OperationContext context) {
131         String objectId = getObjectId();
132         Holder<String> objectIdHolder = new Holder<String>(objectId);
133 
134         if ((sourceFolderId == null) || (sourceFolderId.getId() == null)) {
135             throw new IllegalArgumentException("Source folder id must be set!");
136         }
137 
138         if ((targetFolderId == null) || (targetFolderId.getId() == null)) {
139             throw new IllegalArgumentException("Target folder id must be set!");
140         }
141 
142         getBinding().getObjectService().moveObject(getRepositoryId(), objectIdHolder, targetFolderId.getId(),
143                 sourceFolderId.getId(), null);
144 
145         if (objectIdHolder.getValue() == null) {
146             return null;
147         }
148 
149         CmisObject movedObject = getSession().getObject(objectIdHolder.getValue(), context);
150         if (!(movedObject instanceof FileableCmisObject)) {
151             throw new CmisRuntimeException("Moved object is invalid!");
152         }
153 
154         return (FileableCmisObject) movedObject;
155     }
156 
157     public void addToFolder(ObjectId folderId, boolean allVersions) {
158         String objectId = getObjectId();
159 
160         if ((folderId == null) || (folderId.getId() == null)) {
161             throw new IllegalArgumentException("Folder Id must be set!");
162         }
163 
164         getBinding().getMultiFilingService().addObjectToFolder(getRepositoryId(), objectId, folderId.getId(),
165                 allVersions, null);
166     }
167 
168     public void removeFromFolder(ObjectId folderId) {
169         String objectId = getObjectId();
170 
171         if ((folderId == null) || (folderId.getId() == null)) {
172             throw new IllegalArgumentException("Folder Id must be set!");
173         }
174 
175         getBinding().getMultiFilingService()
176                 .removeObjectFromFolder(getRepositoryId(), objectId, folderId.getId(), null);
177     }
178 }