This project has retired. For details please refer to its Attic page.
InMemoryAbstractServiceImpl 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.CmisInvalidArgumentException;
26  import org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException;
27  import org.apache.chemistry.opencmis.commons.exceptions.CmisUpdateConflictException;
28  import org.apache.chemistry.opencmis.inmemory.storedobj.api.CmisServiceValidator;
29  import org.apache.chemistry.opencmis.inmemory.storedobj.api.DocumentVersion;
30  import org.apache.chemistry.opencmis.inmemory.storedobj.api.StoreManager;
31  import org.apache.chemistry.opencmis.inmemory.storedobj.api.StoredObject;
32  import org.apache.chemistry.opencmis.inmemory.storedobj.api.VersionedDocument;
33  
34  /**
35   * Common functionality for all service implementations
36   *
37   * @author Jens
38   *
39   */
40  public class InMemoryAbstractServiceImpl {
41  
42      protected final StoreManager fStoreManager;
43      protected final CmisServiceValidator validator;
44  
45      protected InMemoryAbstractServiceImpl(StoreManager storeManager, CmisServiceValidator validator) {
46          this.fStoreManager = storeManager;
47          this.validator = validator;
48      }
49  
50      protected InMemoryAbstractServiceImpl(StoreManager storeManager) {
51          this.fStoreManager = storeManager;
52          this.validator = storeManager.getServiceValidator();
53      }
54  
55      protected TypeDefinition getTypeDefinition(String repositoryId, Properties properties) {
56          if (null == properties)
57              return null;
58          
59          String typeId = (String) properties.getProperties().get(PropertyIds.OBJECT_TYPE_ID).getFirstValue();
60          TypeDefinitionContainer typeDefC = fStoreManager.getTypeById(repositoryId, typeId);
61          if (typeDefC == null) {
62              throw new CmisInvalidArgumentException("Cannot create object, a type with id " + typeId + " is unknown");
63          }
64  
65          return typeDefC.getTypeDefinition();
66      }
67  
68      protected TypeDefinition getTypeDefinition(String repositoryId, StoredObject obj) {
69  
70          TypeDefinitionContainer typeDefC = fStoreManager.getTypeById(repositoryId, obj.getTypeId());
71          return typeDefC.getTypeDefinition();
72      }
73  
74      /**
75       * We allow checkin, cancel, checkout operations on a single version as well
76       * as on a version series This method returns the versioned document
77       * (version series) in each case
78       *
79       * @param value
80       *            version or version series id of a document
81       * @return version series id
82       */
83      protected VersionedDocument getVersionedDocumentOfObjectId(StoredObject so) {
84  
85          VersionedDocument verDoc;
86          if (so instanceof DocumentVersion) {
87              // get document the version is contained in to c
88              verDoc = ((DocumentVersion) so).getParentDocument();
89          } else {
90              verDoc = (VersionedDocument) so;
91          }
92  
93          return verDoc;
94      }
95  
96      protected VersionedDocument testIsNotCheckedOutBySomeoneElse(StoredObject so, String user) {
97          checkIsVersionableObject(so);
98          VersionedDocument verDoc = getVersionedDocumentOfObjectId(so);
99          if (verDoc.isCheckedOut()) {
100             testCheckedOutByCurrentUser(user, verDoc);
101         }
102 
103         return verDoc;
104     }
105 
106     protected VersionedDocument testHasProperCheckedOutStatus(StoredObject so, String user) {
107 
108         checkIsVersionableObject(so);
109         VersionedDocument verDoc = getVersionedDocumentOfObjectId(so);
110 
111         checkHasUser(user);
112 
113         testIsCheckedOut(verDoc);
114         testCheckedOutByCurrentUser(user, verDoc);
115 
116         return verDoc;
117     }
118 
119     protected void checkIsVersionableObject(StoredObject so) {
120         if (!(so instanceof VersionedDocument || so instanceof DocumentVersion)) {
121             throw new CmisInvalidArgumentException(
122                     "Object is of a versionable type but not instance of VersionedDocument or DocumentVersion.");
123         }
124     }
125 
126     protected void checkHasUser(String user) {
127         if (null == user || user.length() == 0) {
128             throw new CmisPermissionDeniedException("Object can't be checked-in, no user is given.");
129         }
130     }
131 
132     protected void testCheckedOutByCurrentUser(String user, VersionedDocument verDoc) {
133         if (!user.equals(verDoc.getCheckedOutBy())) {
134             throw new CmisUpdateConflictException("User " + verDoc.getCheckedOutBy()
135                     + " has checked out the document.");
136         }
137     }
138 
139     protected void testIsCheckedOut(VersionedDocument verDoc) {
140         if (!verDoc.isCheckedOut()) {
141             throw new CmisUpdateConflictException("Document " + verDoc.getId() + " is not checked out.");
142         }
143     }
144     
145     protected boolean isCheckedOut(StoredObject so, String user) {
146         if (so instanceof VersionedDocument || so instanceof DocumentVersion) {
147             VersionedDocument verDoc = getVersionedDocumentOfObjectId(so);
148             return verDoc.isCheckedOut() && user.equals(verDoc.getCheckedOutBy());
149         } else
150             return false;
151         
152 
153     }
154 
155 }