This project has retired. For details please refer to its Attic page.
OperationContextImpl 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.client.runtime;
20  
21  import java.io.Serializable;
22  import java.util.Collections;
23  import java.util.Set;
24  import java.util.TreeSet;
25  
26  import org.apache.chemistry.opencmis.client.api.OperationContext;
27  import org.apache.chemistry.opencmis.commons.PropertyIds;
28  import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
29  
30  /**
31   * {@link OperationContext} implementation.
32   */
33  public class OperationContextImpl implements OperationContext, Serializable {
34  
35      public static final String PROPERTIES_STAR = "*";
36      public static final String RENDITION_NONE = "cmis:none";
37  
38      private static final long serialVersionUID = 1L;
39  
40      private TreeSet<String> filter;
41      private boolean includeAcls;
42      private boolean includeAllowableActions;
43      private boolean includePolicies;
44      private IncludeRelationships includeRelationships;
45      private TreeSet<String> renditionFilter;
46      private boolean includePathSegments;
47      private String orderBy;
48      private boolean cacheEnabled;
49      private String cacheKey;
50      private int maxItemsPerPage;
51  
52      /**
53       * Default constructor.
54       */
55      public OperationContextImpl() {
56          setFilter(null);
57          setIncludeAcls(false);
58          setIncludeAllowableActions(true);
59          setIncludePolicies(false);
60          setIncludeRelationships(IncludeRelationships.NONE);
61          setRenditionFilter(null);
62          setIncludePathSegments(true);
63          setOrderBy(null);
64          setCacheEnabled(false);
65          generateCacheKey();
66  
67          setMaxItemsPerPage(100); // default
68      }
69  
70      /**
71       * Copy constructor.
72       */
73      public OperationContextImpl(OperationContext source) {
74          setFilter(source.getFilter());
75          setIncludeAcls(source.isIncludeAcls());
76          setIncludeAllowableActions(source.isIncludeAllowableActions());
77          setIncludePolicies(source.isIncludePolicies());
78          setIncludeRelationships(source.getIncludeRelationships());
79          setRenditionFilter(source.getRenditionFilter());
80          setIncludePathSegments(source.isIncludePathSegments());
81          setOrderBy(source.getOrderBy());
82          setCacheEnabled(source.isCacheEnabled());
83          generateCacheKey();
84  
85          setMaxItemsPerPage(source.getMaxItemsPerPage());
86      }
87  
88      /**
89       * Constructor with parameters.
90       */
91      public OperationContextImpl(Set<String> propertyFilter, boolean includeAcls, boolean includeAllowableActions,
92              boolean includePolicies, IncludeRelationships includeRelationships, Set<String> renditionFilter,
93              boolean includePathSegments, String orderBy, boolean cacheEnabled, int maxItemsPerPage) {
94          setFilter(propertyFilter);
95          setIncludeAcls(includeAcls);
96          setIncludeAllowableActions(includeAllowableActions);
97          setIncludePolicies(includePolicies);
98          setIncludeRelationships(includeRelationships);
99          setRenditionFilter(renditionFilter);
100         setIncludePathSegments(includePathSegments);
101         setOrderBy(orderBy);
102         setCacheEnabled(cacheEnabled);
103         generateCacheKey();
104 
105         setMaxItemsPerPage(maxItemsPerPage);
106     }
107 
108     public Set<String> getFilter() {
109         if (this.filter == null) {
110             return null;
111         }
112 
113         return Collections.unmodifiableSet(this.filter);
114     }
115 
116     public void setFilter(Set<String> propertyFilter) {
117         if (propertyFilter != null) {
118             TreeSet<String> tempSet = new TreeSet<String>();
119 
120             for (String oid : propertyFilter) {
121                 if (oid == null) {
122                     continue;
123                 }
124 
125                 String toid = oid.trim();
126                 if (toid.length() == 0) {
127                     continue;
128                 }
129                 if (toid.equals(PROPERTIES_STAR)) {
130                     tempSet = new TreeSet<String>();
131                     tempSet.add(PROPERTIES_STAR);
132                     break;
133                 }
134                 if (toid.indexOf(',') > -1) {
135                     throw new IllegalArgumentException("Query id must not contain a comma!");
136                 }
137 
138                 tempSet.add(toid);
139             }
140 
141             if (tempSet.size() == 0) {
142                 this.filter = null;
143             } else {
144                 this.filter = tempSet;
145             }
146         } else {
147             this.filter = null;
148         }
149 
150         generateCacheKey();
151     }
152 
153     public void setFilterString(String propertyFilter) {
154         if ((propertyFilter == null) || (propertyFilter.trim().length() == 0)) {
155             setFilter(null);
156             return;
157         }
158 
159         String[] propertyIds = propertyFilter.split(",");
160         TreeSet<String> tempSet = new TreeSet<String>();
161         for (String pid : propertyIds) {
162             tempSet.add(pid);
163         }
164 
165         setFilter(tempSet);
166     }
167 
168     public String getFilterString() {
169         if (this.filter == null) {
170             return null;
171         }
172 
173         if (this.filter.contains(PROPERTIES_STAR)) {
174             return PROPERTIES_STAR;
175         }
176 
177         this.filter.add(PropertyIds.OBJECT_ID);
178         this.filter.add(PropertyIds.BASE_TYPE_ID);
179         this.filter.add(PropertyIds.OBJECT_TYPE_ID);
180 
181         StringBuilder sb = new StringBuilder();
182 
183         for (String oid : this.filter) {
184             if (sb.length() > 0) {
185                 sb.append(",");
186             }
187 
188             sb.append(oid);
189         }
190 
191         return sb.toString();
192     }
193 
194     public boolean isIncludeAcls() {
195         return includeAcls;
196     }
197 
198     public void setIncludeAcls(boolean include) {
199         this.includeAcls = include;
200         generateCacheKey();
201     }
202 
203     public boolean isIncludeAllowableActions() {
204         return this.includeAllowableActions;
205     }
206 
207     public void setIncludeAllowableActions(boolean include) {
208         this.includeAllowableActions = include;
209         generateCacheKey();
210     }
211 
212     public boolean isIncludePolicies() {
213         return this.includePolicies;
214     }
215 
216     public void setIncludePolicies(boolean include) {
217         this.includePolicies = include;
218         generateCacheKey();
219     }
220 
221     public IncludeRelationships getIncludeRelationships() {
222         return this.includeRelationships;
223     }
224 
225     public void setIncludeRelationships(IncludeRelationships include) {
226         this.includeRelationships = include;
227         generateCacheKey();
228     }
229 
230     public Set<String> getRenditionFilter() {
231         if (this.renditionFilter == null) {
232             return null;
233         }
234 
235         return Collections.unmodifiableSet(this.renditionFilter);
236     }
237 
238     public void setRenditionFilter(Set<String> renditionFilter) {
239         TreeSet<String> tempSet = new TreeSet<String>();
240 
241         if (renditionFilter != null) {
242             for (String rf : renditionFilter) {
243                 if (rf == null) {
244                     continue;
245                 }
246 
247                 String trf = rf.trim();
248                 if (trf.length() == 0) {
249                     continue;
250                 }
251                 if (trf.indexOf(',') > -1) {
252                     throw new IllegalArgumentException("Rendition must not contain a comma!");
253                 }
254 
255                 tempSet.add(trf);
256             }
257 
258             if (tempSet.size() == 0) {
259                 tempSet.add(RENDITION_NONE);
260             }
261         } else {
262             tempSet.add(RENDITION_NONE);
263         }
264 
265         this.renditionFilter = tempSet;
266         generateCacheKey();
267     }
268 
269     public void setRenditionFilterString(String renditionFilter) {
270         if ((renditionFilter == null) || (renditionFilter.trim().length() == 0)) {
271             setRenditionFilter(null);
272             return;
273         }
274 
275         String[] renditions = renditionFilter.split(",");
276         TreeSet<String> tempSet = new TreeSet<String>();
277         for (String rend : renditions) {
278             tempSet.add(rend);
279         }
280 
281         setRenditionFilter(tempSet);
282     }
283 
284     public String getRenditionFilterString() {
285         if (this.renditionFilter == null) {
286             return null;
287         }
288 
289         StringBuilder sb = new StringBuilder();
290 
291         for (String rf : this.renditionFilter) {
292             if (sb.length() > 0) {
293                 sb.append(",");
294             }
295 
296             sb.append(rf);
297         }
298 
299         return sb.toString();
300     }
301 
302     public boolean isIncludePathSegments() {
303         return includePathSegments;
304     }
305 
306     public void setIncludePathSegments(boolean include) {
307         this.includePathSegments = include;
308     }
309 
310     public String getOrderBy() {
311         return this.orderBy;
312     }
313 
314     public void setOrderBy(String orderBy) {
315         this.orderBy = orderBy;
316     }
317 
318     public boolean isCacheEnabled() {
319         return cacheEnabled;
320     }
321 
322     public void setCacheEnabled(boolean cacheEnabled) {
323         this.cacheEnabled = cacheEnabled;
324     }
325 
326     public String getCacheKey() {
327         return cacheKey;
328     }
329 
330     /**
331      * Generates a new cache key from all parameters that are relevant for
332      * caching.
333      */
334     protected void generateCacheKey() {
335         if (!cacheEnabled) {
336             cacheKey = null;
337         }
338 
339         StringBuilder sb = new StringBuilder();
340 
341         sb.append(includeAcls ? "1" : "0");
342         sb.append(includeAllowableActions ? "1" : "0");
343         sb.append(includePolicies ? "1" : "0");
344         sb.append("|");
345         sb.append(filter == null ? "" : getFilterString());
346         sb.append("|");
347         sb.append(includeRelationships == null ? "" : includeRelationships.value());
348 
349         sb.append("|");
350         sb.append(renditionFilter == null ? "" : getRenditionFilterString());
351 
352         cacheKey = sb.toString();
353     }
354 
355     public int getMaxItemsPerPage() {
356         return this.maxItemsPerPage;
357     }
358 
359     public void setMaxItemsPerPage(int maxItemsPerPage) {
360         if (maxItemsPerPage < 1) {
361             throw new IllegalArgumentException("itemsPerPage must be > 0!");
362         }
363 
364         this.maxItemsPerPage = maxItemsPerPage;
365     }
366 }