This project has retired. For details please refer to its Attic page.
AbstractServiceImpl 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.server;
20  
21  import org.apache.chemistry.opencmis.commons.PropertyIds;
22  import org.apache.chemistry.opencmis.commons.data.Properties;
23  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
24  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinitionContainer;
25  import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
26  import org.apache.chemistry.opencmis.commons.exceptions.CmisUpdateConflictException;
27  import org.apache.chemistry.opencmis.inmemory.storedobj.api.DocumentVersion;
28  import org.apache.chemistry.opencmis.inmemory.storedobj.api.ObjectStore;
29  import org.apache.chemistry.opencmis.inmemory.storedobj.api.StoreManager;
30  import org.apache.chemistry.opencmis.inmemory.storedobj.api.StoredObject;
31  import org.apache.chemistry.opencmis.inmemory.storedobj.api.VersionedDocument;
32  
33  /**
34   * Common functionality for all service implementations
35   *
36   * @author Jens
37   *
38   */
39  public class AbstractServiceImpl {
40  
41      protected final StoreManager fStoreManager;
42  
43      protected AbstractServiceImpl(StoreManager storeManager) {
44          fStoreManager = storeManager;
45      }
46  
47      /**
48       * check if repository is known and that object exists. To avoid later calls
49       * to again retrieve the object from the id return the retrieved object for
50       * later use.
51       *
52       * @param repositoryId
53       *            repository id
54       * @param objectId
55       *            object id
56       * @return object for objectId
57       */
58      protected StoredObject checkStandardParameters(String repositoryId, String objectId) {
59  
60          ObjectStore objStore = fStoreManager.getObjectStore(repositoryId);
61  
62          if (objStore == null) {
63              throw new CmisObjectNotFoundException("Unknown repository id: " + repositoryId);
64          }
65  
66          StoredObject so = objStore.getObjectById(objectId);
67  
68          if (so == null) {
69              throw new CmisObjectNotFoundException("Unknown object id: " + objectId);
70          }
71  
72          return so;
73      }
74  
75      protected StoredObject checkExistingObjectId(ObjectStore objStore, String objectId) {
76          StoredObject so = objStore.getObjectById(objectId);
77  
78          if (so == null) {
79              throw new CmisObjectNotFoundException("Unknown object id: " + objectId);
80          }
81  
82          return so;
83      }
84  
85      protected void checkRepositoryId(String repositoryId) {
86          ObjectStore objStore = fStoreManager.getObjectStore(repositoryId);
87  
88          if (objStore == null) {
89              throw new CmisObjectNotFoundException("Unknown repository id: " + repositoryId);
90          }
91      }
92  
93      protected TypeDefinition getTypeDefinition(String repositoryId, Properties properties) {
94          String typeId = (String) properties.getProperties().get(PropertyIds.OBJECT_TYPE_ID).getFirstValue();
95          TypeDefinitionContainer typeDefC = fStoreManager.getTypeById(repositoryId, typeId);
96          if (typeDefC == null) {
97              throw new RuntimeException("Cannot create object, a type with id " + typeId + " is unknown");
98          }
99  
100         return typeDefC.getTypeDefinition();
101     }
102 
103     protected TypeDefinition getTypeDefinition(String repositoryId, StoredObject obj) {
104 
105         TypeDefinitionContainer typeDefC = fStoreManager.getTypeById(repositoryId, obj.getTypeId());
106         return typeDefC.getTypeDefinition();
107     }
108 
109     /**
110      * We allow checkin, cancel, checkout operations on a single version as well
111      * as on a version series This method returns the versioned document
112      * (version series) in each case
113      *
114      * @param value
115      *            version or version series id of a document
116      * @return version series id
117      */
118     protected VersionedDocument getVersionedDocumentOfObjectId(StoredObject so) {
119         VersionedDocument verDoc;
120         if (so instanceof DocumentVersion) {
121             // get document the version is contained in to c
122             verDoc = ((DocumentVersion) so).getParentDocument();
123         } else {
124             verDoc = (VersionedDocument) so;
125         }
126 
127         return verDoc;
128     }
129 
130     protected VersionedDocument testIsNotCheckedOutBySomeoneElse(StoredObject so, String user) {
131         checkIsVersionableObject(so);
132         VersionedDocument verDoc = getVersionedDocumentOfObjectId(so);
133         if (verDoc.isCheckedOut()) {
134             testCheckedOutByCurrentUser(user, verDoc);
135         }
136 
137         return verDoc;
138     }
139 
140     protected VersionedDocument testHasProperCheckedOutStatus(StoredObject so, String user) {
141         checkIsVersionableObject(so);
142         VersionedDocument verDoc = getVersionedDocumentOfObjectId(so);
143 
144         checkHasUser(user);
145 
146         testIsCheckedOut(verDoc);
147         testCheckedOutByCurrentUser(user, verDoc);
148 
149         return verDoc;
150     }
151 
152     protected void checkIsVersionableObject(StoredObject so) {
153         if (!(so instanceof VersionedDocument || so instanceof DocumentVersion)) {
154             throw new RuntimeException(
155                     "Object is of a versionable type but not instance of VersionedDocument or DocumentVersion.");
156         }
157     }
158 
159     protected void checkHasUser(String user) {
160         if (null == user || user.length() == 0) {
161             throw new CmisUpdateConflictException("Object can't be checked-in, no user is given.");
162         }
163     }
164 
165     protected void testCheckedOutByCurrentUser(String user, VersionedDocument verDoc) {
166         if (!user.equals(verDoc.getCheckedOutBy())) {
167             throw new CmisUpdateConflictException("Object can't be checked-in, user " + verDoc.getCheckedOutBy()
168                     + " has checked out the document.");
169         }
170     }
171 
172     protected void testIsCheckedOut(VersionedDocument verDoc) {
173         if (!verDoc.isCheckedOut()) {
174             throw new CmisUpdateConflictException("Canot check-in: Document " + verDoc.getId() + " is not checked out.");
175         }
176     }
177 
178 }