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.bindings.impl;
20  
21  import static org.apache.chemistry.opencmis.commons.impl.CollectionsHelper.isNotEmpty;
22  
23  import java.io.Serializable;
24  import java.math.BigInteger;
25  import java.util.List;
26  
27  import org.apache.chemistry.opencmis.client.bindings.cache.TypeDefinitionCache;
28  import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
29  import org.apache.chemistry.opencmis.client.bindings.spi.CmisSpi;
30  import org.apache.chemistry.opencmis.commons.data.ExtensionsData;
31  import org.apache.chemistry.opencmis.commons.data.RepositoryInfo;
32  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
33  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinitionContainer;
34  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinitionList;
35  import org.apache.chemistry.opencmis.commons.spi.ExtendedRepositoryService;
36  import org.apache.chemistry.opencmis.commons.spi.RepositoryService;
37  
38  /**
39   * Repository Service implementation.
40   * 
41   * Passes requests to the SPI and handles caching.
42   */
43  public class RepositoryServiceImpl implements RepositoryService, ExtendedRepositoryService, Serializable {
44  
45      private static final long serialVersionUID = 1L;
46  
47      private final BindingSession session;
48  
49      /**
50       * Constructor.
51       */
52      public RepositoryServiceImpl(BindingSession session) {
53          assert session != null;
54  
55          this.session = session;
56      }
57  
58      @Override
59      public RepositoryInfo getRepositoryInfo(String repositoryId, ExtensionsData extension) {
60          RepositoryInfo result = null;
61          boolean hasExtension = (extension != null) && isNotEmpty(extension.getExtensions());
62  
63          RepositoryInfoCache cache = CmisBindingsHelper.getRepositoryInfoCache(session);
64  
65          // if extension is not set, check the cache first
66          if (!hasExtension) {
67              result = cache.get(repositoryId);
68              if (result != null) {
69                  return result;
70              }
71          }
72  
73          // it was not in the cache -> get the SPI and fetch the repository info
74          CmisSpi spi = CmisBindingsHelper.getSPI(session);
75          result = spi.getRepositoryService().getRepositoryInfo(repositoryId, extension);
76  
77          // put it into the cache
78          if (!hasExtension) {
79              cache.put(result);
80          }
81  
82          return result;
83      }
84  
85      @Override
86      public List<RepositoryInfo> getRepositoryInfos(ExtensionsData extension) {
87          List<RepositoryInfo> result = null;
88          boolean hasExtension = (extension != null) && isNotEmpty(extension.getExtensions());
89  
90          // get the SPI and fetch the repository infos
91          CmisSpi spi = CmisBindingsHelper.getSPI(session);
92          result = spi.getRepositoryService().getRepositoryInfos(extension);
93  
94          // put it into the cache
95          if (!hasExtension && (result != null)) {
96              RepositoryInfoCache cache = CmisBindingsHelper.getRepositoryInfoCache(session);
97              for (RepositoryInfo rid : result) {
98                  cache.put(rid);
99              }
100         }
101 
102         return result;
103     }
104 
105     @Override
106     public TypeDefinitionList getTypeChildren(String repositoryId, String typeId, Boolean includePropertyDefinitions,
107             BigInteger maxItems, BigInteger skipCount, ExtensionsData extension) {
108         TypeDefinitionList result = null;
109         boolean hasExtension = (extension != null) && isNotEmpty(extension.getExtensions());
110         boolean propDefs = (includePropertyDefinitions == null ? false : includePropertyDefinitions.booleanValue());
111 
112         // get the SPI and fetch the type definitions
113         CmisSpi spi = CmisBindingsHelper.getSPI(session);
114         result = spi.getRepositoryService().getTypeChildren(repositoryId, typeId, includePropertyDefinitions, maxItems,
115                 skipCount, extension);
116 
117         // put it into the cache
118         if (!hasExtension && propDefs && (result != null)) {
119             TypeDefinitionCache cache = CmisBindingsHelper.getTypeDefinitionCache(session);
120 
121             for (TypeDefinition tdd : result.getList()) {
122                 cache.put(repositoryId, tdd);
123             }
124         }
125 
126         return result;
127     }
128 
129     @Override
130     public TypeDefinition getTypeDefinition(String repositoryId, String typeId, ExtensionsData extension) {
131         return getTypeDefinition(repositoryId, typeId, extension, true);
132     }
133 
134     @Override
135     public TypeDefinition getTypeDefinition(String repositoryId, String typeId, ExtensionsData extension,
136             boolean useCache) {
137         TypeDefinition result = null;
138         boolean hasExtension = (extension != null) && isNotEmpty(extension.getExtensions());
139 
140         TypeDefinitionCache cache = CmisBindingsHelper.getTypeDefinitionCache(session);
141 
142         // if the cache should be used and the extension is not set,
143         // check the cache first
144         if (useCache && !hasExtension) {
145             result = cache.get(repositoryId, typeId);
146             if (result != null) {
147                 return result;
148             }
149         }
150 
151         // it was not in the cache -> get the SPI and fetch the type definition
152         CmisSpi spi = CmisBindingsHelper.getSPI(session);
153         result = spi.getRepositoryService().getTypeDefinition(repositoryId, typeId, extension);
154 
155         // put it into the cache
156         if (!hasExtension && (result != null)) {
157             cache.put(repositoryId, result);
158         }
159 
160         return result;
161     }
162 
163     @Override
164     public List<TypeDefinitionContainer> getTypeDescendants(String repositoryId, String typeId, BigInteger depth,
165             Boolean includePropertyDefinitions, ExtensionsData extension) {
166         List<TypeDefinitionContainer> result = null;
167         boolean hasExtension = (extension != null) && isNotEmpty(extension.getExtensions());
168         boolean propDefs = includePropertyDefinitions == null ? false : includePropertyDefinitions.booleanValue();
169 
170         // get the SPI and fetch the type definitions
171         CmisSpi spi = CmisBindingsHelper.getSPI(session);
172         result = spi.getRepositoryService().getTypeDescendants(repositoryId, typeId, depth, includePropertyDefinitions,
173                 extension);
174 
175         // put it into the cache
176         if (!hasExtension && propDefs && (result != null)) {
177             TypeDefinitionCache cache = CmisBindingsHelper.getTypeDefinitionCache(session);
178             addToTypeCache(cache, repositoryId, result);
179         }
180 
181         return result;
182     }
183 
184     private void addToTypeCache(TypeDefinitionCache cache, String repositoryId, List<TypeDefinitionContainer> containers) {
185         if (containers == null) {
186             return;
187         }
188 
189         for (TypeDefinitionContainer container : containers) {
190             cache.put(repositoryId, container.getTypeDefinition());
191             addToTypeCache(cache, repositoryId, container.getChildren());
192         }
193     }
194 
195     @Override
196     public TypeDefinition createType(String repositoryId, TypeDefinition type, ExtensionsData extension) {
197         // get the SPI and create the type definitions
198         CmisSpi spi = CmisBindingsHelper.getSPI(session);
199         TypeDefinition result = spi.getRepositoryService().createType(repositoryId, type, extension);
200 
201         // add the type to cache
202         TypeDefinitionCache cache = CmisBindingsHelper.getTypeDefinitionCache(session);
203         cache.put(repositoryId, result);
204 
205         return result;
206     }
207 
208     @Override
209     public TypeDefinition updateType(String repositoryId, TypeDefinition type, ExtensionsData extension) {
210         // get the SPI and update the type definitions
211         CmisSpi spi = CmisBindingsHelper.getSPI(session);
212         TypeDefinition result = spi.getRepositoryService().updateType(repositoryId, type, extension);
213 
214         // update the type in cache
215         TypeDefinitionCache cache = CmisBindingsHelper.getTypeDefinitionCache(session);
216         cache.put(repositoryId, result);
217 
218         return result;
219     }
220 
221     @Override
222     public void deleteType(String repositoryId, String typeId, ExtensionsData extension) {
223         // get the SPI and delete the type definitions
224         CmisSpi spi = CmisBindingsHelper.getSPI(session);
225         spi.getRepositoryService().deleteType(repositoryId, typeId, extension);
226 
227         // remove the type from cache
228         TypeDefinitionCache cache = CmisBindingsHelper.getTypeDefinitionCache(session);
229         cache.remove(repositoryId, typeId);
230     }
231 
232 }