This project has retired. For details please refer to its Attic page.
InMemoryRepositoryServiceImpl 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 java.math.BigInteger;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.List;
25  import java.util.ListIterator;
26  
27  import org.apache.chemistry.opencmis.commons.data.ExtensionsData;
28  import org.apache.chemistry.opencmis.commons.data.RepositoryInfo;
29  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
30  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinitionContainer;
31  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinitionList;
32  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
33  import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
34  import org.apache.chemistry.opencmis.commons.impl.dataobjects.TypeDefinitionListImpl;
35  import org.apache.chemistry.opencmis.commons.server.CallContext;
36  import org.apache.chemistry.opencmis.commons.spi.Holder;
37  import org.apache.chemistry.opencmis.inmemory.TypeValidator;
38  import org.apache.chemistry.opencmis.inmemory.storedobj.api.ObjectStore;
39  import org.apache.chemistry.opencmis.inmemory.storedobj.api.StoreManager;
40  import org.apache.chemistry.opencmis.inmemory.storedobj.api.TypeManagerCreatable;
41  
42  public class InMemoryRepositoryServiceImpl extends InMemoryAbstractServiceImpl {
43  
44      public InMemoryRepositoryServiceImpl(StoreManager storeManager) {
45          super(storeManager);
46      }
47  
48      public RepositoryInfo getRepositoryInfo(CallContext context, String repositoryId, ExtensionsData extension) {
49  
50          validator.getRepositoryInfo(context, repositoryId, extension);
51  
52          RepositoryInfo repoInfo = getRepositoryInfoFromStoreManager(repositoryId);
53  
54          return repoInfo;
55      }
56  
57      public List<RepositoryInfo> getRepositoryInfos(CallContext context, ExtensionsData extension) {
58  
59          validator.getRepositoryInfos(context, extension);
60          List<RepositoryInfo> res = new ArrayList<RepositoryInfo>();
61          List<String> repIds = fStoreManager.getAllRepositoryIds();
62          for (String repId : repIds) {
63              res.add(fStoreManager.getRepositoryInfo(repId));
64          }
65          return res;
66      }
67  
68      public TypeDefinitionList getTypeChildren(CallContext context, String repositoryId, String typeId,
69              Boolean includePropertyDefinitions, BigInteger maxItems, BigInteger skipCount, ExtensionsData extension) {
70  
71          validator.getTypeChildren(context, repositoryId, typeId, extension);
72  
73          boolean inclPropDefs = includePropertyDefinitions == null ? false : includePropertyDefinitions;
74          getRepositoryInfoFromStoreManager(repositoryId); // just to check if
75          // repository
76          // exists
77  
78          int skip = skipCount == null ? 0 : skipCount.intValue();
79          int max = maxItems == null ? -1 : maxItems.intValue();
80  
81          TypeDefinitionListImpl result = new TypeDefinitionListImpl();
82          List<TypeDefinitionContainer> children;
83          if (typeId == null) {
84              // spec says that base types must be returned in this case
85              children = fStoreManager.getRootTypes(repositoryId);
86          } else {
87              children = getTypeDescendants(context, repositoryId, typeId, BigInteger.valueOf(1),
88                      inclPropDefs, null);
89          }
90          result.setNumItems(BigInteger.valueOf(children.size()));
91          result.setHasMoreItems(children.size() > max - skip);
92          List<TypeDefinition> childrenTypes = new ArrayList<TypeDefinition>();
93          ListIterator<TypeDefinitionContainer> it = children.listIterator(skip);
94          if (max < 0) {
95              max = children.size();
96          }
97          for (int i = skip; i < max + skip && it.hasNext(); i++) {
98              childrenTypes.add(it.next().getTypeDefinition());
99          }
100 
101         result.setList(childrenTypes);
102         return result;
103     }
104 
105     public TypeDefinition getTypeDefinition(CallContext context, String repositoryId, String typeId,
106             ExtensionsData extension) {
107 
108         validator.getTypeDefinition(context, repositoryId, typeId, extension);
109 
110         TypeDefinitionContainer tc = fStoreManager.getTypeById(repositoryId, typeId);
111         if (tc != null) {
112             return tc.getTypeDefinition();
113         } else {
114             throw new CmisObjectNotFoundException("unknown type id: " + typeId);
115         }
116     }
117 
118     public List<TypeDefinitionContainer> getTypeDescendants(CallContext context, String repositoryId, String typeId,
119             BigInteger depth, Boolean includePropertyDefinitions, ExtensionsData extension) {
120 
121         validator.getTypeDescendants(context, repositoryId, typeId, extension);
122 
123         boolean inclPropDefs = includePropertyDefinitions == null ? false : includePropertyDefinitions;
124 
125         if (depth != null && depth.intValue() == 0) {
126             throw new CmisInvalidArgumentException("depth == 0 is illegal in getTypeDescendants");
127         }
128 
129         List<TypeDefinitionContainer> result = null;
130         if (typeId == null) {
131             // spec says that depth must be ignored in this case
132             Collection<TypeDefinitionContainer> tmp = fStoreManager.getTypeDefinitionList(repositoryId,
133                     inclPropDefs);
134             result = new ArrayList<TypeDefinitionContainer>(tmp);
135         } else {
136             TypeDefinitionContainer tc = fStoreManager.getTypeById(repositoryId, typeId, inclPropDefs,
137                     depth == null ? -1 : depth.intValue());
138             if (tc == null) {
139                 throw new CmisInvalidArgumentException("unknown type id: " + typeId);
140             } else {
141                 result = tc.getChildren();
142             }
143         }
144 
145         return result;
146     }
147 
148     public void createTypeDefinition(String repositoryId, Holder<TypeDefinition> type, ExtensionsData extension) {
149 
150         if (null == repositoryId)
151             throw new CmisInvalidArgumentException("Repository id may not be null");
152 
153         TypeManagerCreatable typeManager = fStoreManager.getTypeManager(repositoryId);
154         if (null == typeManager)
155             throw new CmisInvalidArgumentException("Unknown repository " + repositoryId);
156         
157         TypeDefinition td = type.getValue();
158         TypeValidator.checkType(typeManager, td);
159         TypeValidator.checkProperties(typeManager, td.getPropertyDefinitions().values());
160         
161         typeManager.addTypeDefinition(type.getValue());
162     }
163 
164     public void updateTypeDefinition(String repositoryId, Holder<TypeDefinition> type, ExtensionsData extension) {
165         String typeId = type.getValue().getId();
166         TypeManagerCreatable typeManager = fStoreManager.getTypeManager(repositoryId);
167         if (null == typeManager)
168             throw new CmisInvalidArgumentException("Unknown repository " + repositoryId);
169         
170         TypeDefinitionContainer typeDefC = typeManager.getTypeById(typeId);
171         if (null == typeDefC)
172             throw new CmisInvalidArgumentException("Cannot update type unknown type id: " + typeId);
173 
174         typeManager.updateTypeDefinition(type.getValue());
175     }
176 
177     public void deleteTypeDefinition(String repositoryId, String typeId, ExtensionsData extension) {
178         
179         TypeManagerCreatable typeManager = fStoreManager.getTypeManager(repositoryId);
180         if (null == typeManager)
181             throw new CmisInvalidArgumentException("Unknown repository " + repositoryId);
182         
183         TypeDefinitionContainer typeDefC = typeManager.getTypeById(typeId);
184         if (null == typeDefC)
185             throw new CmisInvalidArgumentException("Cannot delete type unknown type id: " + typeId);
186 
187         TypeDefinition typeDef =  typeDefC.getTypeDefinition();
188         // TODO: re-enable when CMIS 1.1 is supported.
189 //        if (!typeDef.getTypeMutability().supportsDelete()) {
190 //            throw new CmisInvalidArgumentException("type definition " + typeId + " cannot be deleted, deletion is not supported for type id " + typeId);            
191 //        }
192 
193         ObjectStore objectStore = fStoreManager.getObjectStore(repositoryId);
194         if (objectStore.isTypeInUse(typeId)) {
195             throw new CmisInvalidArgumentException("type definition " + typeId + " cannot be deleted, type is in use.");            
196         }
197         
198         typeManager.deleteTypeDefinition(typeId);        
199     }
200     
201     private RepositoryInfo getRepositoryInfoFromStoreManager(String repositoryId) {
202         RepositoryInfo repoInfo = fStoreManager.getRepositoryInfo(repositoryId);
203         if (null == repoInfo || !repoInfo.getId().equals(repositoryId)) {
204             throw new CmisInvalidArgumentException("Unknown repository: " + repositoryId);
205         }
206         return repoInfo;
207     }
208 
209 }