This project has retired. For details please refer to its Attic page.
RepositoryServiceImpl 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  
20  package org.apache.chemistry.opencmis.client.bindings.impl;
21  
22  import java.io.Serializable;
23  import java.math.BigInteger;
24  import java.util.List;
25  
26  import org.apache.chemistry.opencmis.client.bindings.spi.CmisSpi;
27  import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
28  import org.apache.chemistry.opencmis.commons.data.ExtensionsData;
29  import org.apache.chemistry.opencmis.commons.data.RepositoryInfo;
30  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
31  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinitionContainer;
32  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinitionList;
33  import org.apache.chemistry.opencmis.commons.spi.RepositoryService;
34  
35  /**
36   * Repository Service implementation.
37   * 
38   * Passes requests to the SPI and handles caching.
39   */
40  public class RepositoryServiceImpl implements RepositoryService, Serializable {
41  
42      private static final long serialVersionUID = 1L;
43  
44      private final BindingSession session;
45  
46      /**
47       * Constructor.
48       */
49      public RepositoryServiceImpl(BindingSession session) {
50          this.session = session;
51      }
52  
53      public RepositoryInfo getRepositoryInfo(String repositoryId, ExtensionsData extension) {
54          RepositoryInfo result = null;
55          boolean hasExtension = (extension != null) && (!extension.getExtensions().isEmpty());
56  
57          RepositoryInfoCache cache = CmisBindingsHelper.getRepositoryInfoCache(session);
58  
59          // if extension is not set, check the cache first
60          if (!hasExtension) {
61              result = cache.get(repositoryId);
62              if (result != null) {
63                  return result;
64              }
65          }
66  
67          // it was not in the cache -> get the SPI and fetch the repository info
68          CmisSpi spi = CmisBindingsHelper.getSPI(session);
69          result = spi.getRepositoryService().getRepositoryInfo(repositoryId, extension);
70  
71          // put it into the cache
72          if (!hasExtension) {
73              cache.put(result);
74          }
75  
76          return result;
77      }
78  
79      public List<RepositoryInfo> getRepositoryInfos(ExtensionsData extension) {
80          List<RepositoryInfo> result = null;
81          boolean hasExtension = (extension != null) && (!extension.getExtensions().isEmpty());
82  
83          // get the SPI and fetch the repository infos
84          CmisSpi spi = CmisBindingsHelper.getSPI(session);
85          result = spi.getRepositoryService().getRepositoryInfos(extension);
86  
87          // put it into the cache
88          if (!hasExtension && (result != null)) {
89              RepositoryInfoCache cache = CmisBindingsHelper.getRepositoryInfoCache(session);
90              for (RepositoryInfo rid : result) {
91                  cache.put(rid);
92              }
93          }
94  
95          return result;
96      }
97  
98      public TypeDefinitionList getTypeChildren(String repositoryId, String typeId, Boolean includePropertyDefinitions,
99              BigInteger maxItems, BigInteger skipCount, ExtensionsData extension) {
100         TypeDefinitionList result = null;
101         boolean hasExtension = (extension != null) && (!extension.getExtensions().isEmpty());
102         boolean propDefs = (includePropertyDefinitions == null ? false : includePropertyDefinitions.booleanValue());
103 
104         // get the SPI and fetch the type definitions
105         CmisSpi spi = CmisBindingsHelper.getSPI(session);
106         result = spi.getRepositoryService().getTypeChildren(repositoryId, typeId, includePropertyDefinitions, maxItems,
107                 skipCount, extension);
108 
109         // put it into the cache
110         if (!hasExtension && propDefs && (result != null)) {
111             TypeDefinitionCache cache = CmisBindingsHelper.getTypeDefinitionCache(session);
112 
113             for (TypeDefinition tdd : result.getList()) {
114                 cache.put(repositoryId, tdd);
115             }
116         }
117 
118         return result;
119     }
120 
121     public TypeDefinition getTypeDefinition(String repositoryId, String typeId, ExtensionsData extension) {
122         TypeDefinition result = null;
123         boolean hasExtension = (extension != null) && (!extension.getExtensions().isEmpty());
124 
125         TypeDefinitionCache cache = CmisBindingsHelper.getTypeDefinitionCache(session);
126 
127         // if extension is not set, check the cache first
128         if (!hasExtension) {
129             result = cache.get(repositoryId, typeId);
130             if (result != null) {
131                 return result;
132             }
133         }
134 
135         // it was not in the cache -> get the SPI and fetch the type definition
136         CmisSpi spi = CmisBindingsHelper.getSPI(session);
137         result = spi.getRepositoryService().getTypeDefinition(repositoryId, typeId, extension);
138 
139         // put it into the cache
140         if (!hasExtension && (result != null)) {
141             cache.put(repositoryId, result);
142         }
143 
144         return result;
145     }
146 
147     public List<TypeDefinitionContainer> getTypeDescendants(String repositoryId, String typeId, BigInteger depth,
148             Boolean includePropertyDefinitions, ExtensionsData extension) {
149         List<TypeDefinitionContainer> result = null;
150         boolean hasExtension = (extension != null) && (!extension.getExtensions().isEmpty());
151         boolean propDefs = (includePropertyDefinitions == null ? false : includePropertyDefinitions.booleanValue());
152 
153         // get the SPI and fetch the type definitions
154         CmisSpi spi = CmisBindingsHelper.getSPI(session);
155         result = spi.getRepositoryService().getTypeDescendants(repositoryId, typeId, depth, includePropertyDefinitions,
156                 extension);
157 
158         // put it into the cache
159         if (!hasExtension && propDefs && (result != null)) {
160             TypeDefinitionCache cache = CmisBindingsHelper.getTypeDefinitionCache(session);
161             addToTypeCache(cache, repositoryId, result);
162         }
163 
164         return result;
165     }
166 
167     private void addToTypeCache(TypeDefinitionCache cache, String repositoryId, List<TypeDefinitionContainer> containers) {
168         if (containers == null) {
169             return;
170         }
171 
172         for (TypeDefinitionContainer container : containers) {
173             cache.put(repositoryId, container.getTypeDefinition());
174             addToTypeCache(cache, repositoryId, container.getChildren());
175         }
176     }
177 }