This project has retired. For details please refer to its Attic page.
QueryResultImpl 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.ArrayList;
23  import java.util.LinkedHashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.chemistry.opencmis.client.api.CmisObject;
28  import org.apache.chemistry.opencmis.client.api.ObjectFactory;
29  import org.apache.chemistry.opencmis.client.api.QueryResult;
30  import org.apache.chemistry.opencmis.client.api.Relationship;
31  import org.apache.chemistry.opencmis.client.api.Rendition;
32  import org.apache.chemistry.opencmis.client.api.Session;
33  import org.apache.chemistry.opencmis.commons.data.AllowableActions;
34  import org.apache.chemistry.opencmis.commons.data.ObjectData;
35  import org.apache.chemistry.opencmis.commons.data.PropertyData;
36  import org.apache.chemistry.opencmis.commons.data.RenditionData;
37  
38  /**
39   * Implementation of <code>QueryResult</code>.
40   */
41  public class QueryResultImpl implements QueryResult, Serializable {
42  
43      private static final long serialVersionUID = 1L;
44  
45      private Map<String, PropertyData<?>> propertiesById;
46      private Map<String, PropertyData<?>> propertiesByQueryName;
47      private AllowableActions allowableActions;
48      private List<Relationship> relationships;
49      private List<Rendition> renditions;
50  
51      /**
52       * Constructor.
53       */
54      public QueryResultImpl(Session session, ObjectData objectData) {
55          if (objectData != null) {
56  
57              ObjectFactory of = session.getObjectFactory();
58  
59              // handle properties
60              if (objectData.getProperties() != null) {
61                  propertiesById = new LinkedHashMap<String, PropertyData<?>>();
62                  propertiesByQueryName = new LinkedHashMap<String, PropertyData<?>>();
63  
64                  List<PropertyData<?>> queryProperties = of.convertQueryProperties(objectData.getProperties());
65  
66                  for (PropertyData<?> property : queryProperties) {
67                      propertiesById.put(property.getId(), property);
68                      propertiesByQueryName.put(property.getQueryName(), property);
69                  }
70              }
71  
72              // handle allowable actions
73              if (objectData.getAllowableActions() != null) {
74                  this.allowableActions = objectData.getAllowableActions();
75              }
76  
77              // handle relationships
78              if (objectData.getRelationships() != null) {
79                  relationships = new ArrayList<Relationship>();
80                  for (ObjectData rod : objectData.getRelationships()) {
81                      CmisObject relationship = of.convertObject(rod, session.getDefaultContext());
82                      if (relationship instanceof Relationship) {
83                          relationships.add((Relationship) relationship);
84                      }
85                  }
86              }
87  
88              // handle renditions
89              if (objectData.getRenditions() != null) {
90                  this.renditions = new ArrayList<Rendition>();
91                  for (RenditionData rd : objectData.getRenditions()) {
92                      this.renditions.add(of.convertRendition(null, rd));
93                  }
94              }
95          }
96      }
97  
98      /*
99       * (non-Javadoc)
100      * 
101      * @see org.apache.opencmis.client.api.QueryResult#getProperties()
102      */
103     public List<PropertyData<?>> getProperties() {
104         return new ArrayList<PropertyData<?>>(propertiesByQueryName.values());
105     }
106 
107     /*
108      * (non-Javadoc)
109      * 
110      * @see
111      * org.apache.opencmis.client.api.QueryResult#getPropertyById(java.lang.
112      * String)
113      */
114     @SuppressWarnings("unchecked")
115     public <T> PropertyData<T> getPropertyById(String id) {
116         return (PropertyData<T>) propertiesById.get(id);
117     }
118 
119     /*
120      * (non-Javadoc)
121      * 
122      * @see
123      * org.apache.opencmis.client.api.QueryResult#getPropertyByQueryName(java
124      * .lang.String)
125      */
126     @SuppressWarnings("unchecked")
127     public <T> PropertyData<T> getPropertyByQueryName(String queryName) {
128         return (PropertyData<T>) propertiesByQueryName.get(queryName);
129     }
130 
131     /*
132      * (non-Javadoc)
133      * 
134      * @see
135      * org.apache.opencmis.client.api.QueryResult#getPropertyValueById(java.
136      * lang.String)
137      */
138     public <T> T getPropertyValueById(String id) {
139         PropertyData<T> property = getPropertyById(id);
140         if (property == null) {
141             return null;
142         }
143 
144         return property.getFirstValue();
145     }
146 
147     /*
148      * (non-Javadoc)
149      * 
150      * @see
151      * org.apache.opencmis.client.api.QueryResult#getPropertyValueByQueryName
152      * (java.lang.String)
153      */
154     public <T> T getPropertyValueByQueryName(String queryName) {
155         PropertyData<T> property = getPropertyByQueryName(queryName);
156         if (property == null) {
157             return null;
158         }
159 
160         return property.getFirstValue();
161     }
162 
163     /*
164      * (non-Javadoc)
165      * 
166      * @see
167      * org.apache.opencmis.client.api.QueryResult#getPropertyMultivalueById(
168      * java.lang.String)
169      */
170     public <T> List<T> getPropertyMultivalueById(String id) {
171         PropertyData<T> property = getPropertyById(id);
172         if (property == null) {
173             return null;
174         }
175 
176         return property.getValues();
177     }
178 
179     /*
180      * (non-Javadoc)
181      * 
182      * @see
183      * org.apache.opencmis.client.api.QueryResult#getPropertyMultivalueByQueryName
184      * (java.lang.String)
185      */
186     public <T> List<T> getPropertyMultivalueByQueryName(String queryName) {
187         PropertyData<T> property = getPropertyByQueryName(queryName);
188         if (property == null) {
189             return null;
190         }
191 
192         return property.getValues();
193     }
194 
195     /*
196      * (non-Javadoc)
197      * 
198      * @see org.apache.opencmis.client.api.QueryResult#getAllowableActions()
199      */
200     public AllowableActions getAllowableActions() {
201         return allowableActions;
202     }
203 
204     /*
205      * (non-Javadoc)
206      * 
207      * @see org.apache.opencmis.client.api.QueryResult#getRelationships()
208      */
209     public List<Relationship> getRelationships() {
210         return relationships;
211     }
212 
213     /*
214      * (non-Javadoc)
215      * 
216      * @see org.apache.opencmis.client.api.QueryResult#getRenditions()
217      */
218     public List<Rendition> getRenditions() {
219         return renditions;
220     }
221 }