This project has retired. For details please refer to its Attic page.
DocumentImpl 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.math.BigInteger;
22  import java.util.ArrayList;
23  import java.util.HashSet;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import org.apache.chemistry.opencmis.client.api.CmisObject;
29  import org.apache.chemistry.opencmis.client.api.Document;
30  import org.apache.chemistry.opencmis.client.api.ObjectFactory;
31  import org.apache.chemistry.opencmis.client.api.ObjectId;
32  import org.apache.chemistry.opencmis.client.api.ObjectType;
33  import org.apache.chemistry.opencmis.client.api.OperationContext;
34  import org.apache.chemistry.opencmis.client.api.Policy;
35  import org.apache.chemistry.opencmis.client.api.TransientCmisObject;
36  import org.apache.chemistry.opencmis.client.api.TransientDocument;
37  import org.apache.chemistry.opencmis.commons.PropertyIds;
38  import org.apache.chemistry.opencmis.commons.data.Ace;
39  import org.apache.chemistry.opencmis.commons.data.ContentStream;
40  import org.apache.chemistry.opencmis.commons.data.ObjectData;
41  import org.apache.chemistry.opencmis.commons.enums.Updatability;
42  import org.apache.chemistry.opencmis.commons.enums.VersioningState;
43  import org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException;
44  import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
45  import org.apache.chemistry.opencmis.commons.spi.Holder;
46  
47  public class DocumentImpl extends AbstractFilableCmisObject implements Document {
48  
49      private static final long serialVersionUID = 1L;
50  
51      /**
52       * Constructor.
53       */
54      public DocumentImpl(SessionImpl session, ObjectType objectType, ObjectData objectData, OperationContext context) {
55          initialize(session, objectType, objectData, context);
56      }
57  
58      @Override
59      protected TransientCmisObject createTransientCmisObject() {
60          TransientDocumentImpl td = new TransientDocumentImpl();
61          td.initialize(getSession(), this);
62  
63          return td;
64      }
65  
66      public TransientDocument getTransientDocument() {
67          return (TransientDocument) getTransientObject();
68      }
69  
70      // properties
71  
72      public String getCheckinComment() {
73          return getPropertyValue(PropertyIds.CHECKIN_COMMENT);
74      }
75  
76      public String getVersionLabel() {
77          return getPropertyValue(PropertyIds.VERSION_LABEL);
78      }
79  
80      public String getVersionSeriesId() {
81          return getPropertyValue(PropertyIds.VERSION_SERIES_ID);
82      }
83  
84      public String getVersionSeriesCheckedOutId() {
85          return getPropertyValue(PropertyIds.VERSION_SERIES_CHECKED_OUT_ID);
86      }
87  
88      public String getVersionSeriesCheckedOutBy() {
89          return getPropertyValue(PropertyIds.VERSION_SERIES_CHECKED_OUT_BY);
90      }
91  
92      public Boolean isImmutable() {
93          return getPropertyValue(PropertyIds.IS_IMMUTABLE);
94      }
95  
96      public Boolean isLatestMajorVersion() {
97          return getPropertyValue(PropertyIds.IS_LATEST_MAJOR_VERSION);
98      }
99  
100     public Boolean isLatestVersion() {
101         return getPropertyValue(PropertyIds.IS_LATEST_VERSION);
102     }
103 
104     public Boolean isMajorVersion() {
105         return getPropertyValue(PropertyIds.IS_MAJOR_VERSION);
106     }
107 
108     public Boolean isVersionSeriesCheckedOut() {
109         return getPropertyValue(PropertyIds.IS_VERSION_SERIES_CHECKED_OUT);
110     }
111 
112     public long getContentStreamLength() {
113         BigInteger bigInt = getPropertyValue(PropertyIds.CONTENT_STREAM_LENGTH);
114         return (bigInt == null) ? (long) -1 : bigInt.longValue();
115     }
116 
117     public String getContentStreamMimeType() {
118         return getPropertyValue(PropertyIds.CONTENT_STREAM_MIME_TYPE);
119     }
120 
121     public String getContentStreamFileName() {
122         return getPropertyValue(PropertyIds.CONTENT_STREAM_FILE_NAME);
123     }
124 
125     public String getContentStreamId() {
126         return getPropertyValue(PropertyIds.CONTENT_STREAM_ID);
127     }
128 
129     // operations
130 
131     public Document copy(ObjectId targetFolderId, Map<String, ?> properties, VersioningState versioningState,
132             List<Policy> policies, List<Ace> addAces, List<Ace> removeAces, OperationContext context) {
133 
134         ObjectId newId = getSession().createDocumentFromSource(this, properties, targetFolderId, versioningState,
135                 policies, addAces, removeAces);
136 
137         // if no context is provided the object will not be fetched
138         if (context == null || newId == null) {
139             return null;
140         }
141         // get the new object
142         CmisObject object = getSession().getObject(newId, context);
143         if (!(object instanceof Document)) {
144             throw new CmisRuntimeException("Newly created object is not a document! New id: " + newId);
145         }
146 
147         return (Document) object;
148     }
149 
150     public Document copy(ObjectId targetFolderId) {
151         return copy(targetFolderId, null, null, null, null, null, getSession().getDefaultContext());
152     }
153 
154     public void deleteAllVersions() {
155         delete(true);
156     }
157 
158     // versioning
159 
160     public ObjectId checkOut() {
161         String newObjectId = null;
162 
163         readLock();
164         try {
165             String objectId = getObjectId();
166             Holder<String> objectIdHolder = new Holder<String>(objectId);
167 
168             getBinding().getVersioningService().checkOut(getRepositoryId(), objectIdHolder, null, null);
169             newObjectId = objectIdHolder.getValue();
170         } finally {
171             readUnlock();
172         }
173 
174         if (newObjectId == null) {
175             return null;
176         }
177 
178         return getSession().createObjectId(newObjectId);
179     }
180 
181     public void cancelCheckOut() {
182         String objectId = getObjectId();
183 
184         getBinding().getVersioningService().cancelCheckOut(getRepositoryId(), objectId, null);
185     }
186 
187     public ObjectId checkIn(boolean major, Map<String, ?> properties, ContentStream contentStream,
188             String checkinComment, List<Policy> policies, List<Ace> addAces, List<Ace> removeAces) {
189         String newObjectId = null;
190 
191         readLock();
192         try {
193             Holder<String> objectIdHolder = new Holder<String>(getObjectId());
194 
195             ObjectFactory of = getObjectFactory();
196 
197             Set<Updatability> updatebility = new HashSet<Updatability>();
198             updatebility.add(Updatability.READWRITE);
199             updatebility.add(Updatability.WHENCHECKEDOUT);
200 
201             getBinding().getVersioningService().checkIn(getRepositoryId(), objectIdHolder, major,
202                     of.convertProperties(properties, getType(), updatebility), of.convertContentStream(contentStream),
203                     checkinComment, of.convertPolicies(policies), of.convertAces(addAces), of.convertAces(removeAces),
204                     null);
205 
206             newObjectId = objectIdHolder.getValue();
207         } finally {
208             readUnlock();
209         }
210 
211         if (newObjectId == null) {
212             return null;
213         }
214 
215         return getSession().createObjectId(newObjectId);
216 
217     }
218 
219     public List<Document> getAllVersions() {
220         return getAllVersions(getSession().getDefaultContext());
221     }
222 
223     public List<Document> getAllVersions(OperationContext context) {
224         String objectId;
225         String versionSeriesId;
226 
227         readLock();
228         try {
229             objectId = getObjectId();
230             versionSeriesId = getVersionSeriesId();
231         } finally {
232             readUnlock();
233         }
234 
235         List<ObjectData> versions = getBinding().getVersioningService().getAllVersions(getRepositoryId(), objectId,
236                 versionSeriesId, context.getFilterString(), context.isIncludeAllowableActions(), null);
237 
238         ObjectFactory objectFactory = getSession().getObjectFactory();
239 
240         List<Document> result = new ArrayList<Document>();
241         if (versions != null) {
242             for (ObjectData objectData : versions) {
243                 CmisObject doc = objectFactory.convertObject(objectData, context);
244                 if (!(doc instanceof Document)) {
245                     // should not happen...
246                     continue;
247                 }
248 
249                 result.add((Document) doc);
250             }
251         }
252 
253         return result;
254 
255     }
256 
257     public Document getObjectOfLatestVersion(boolean major) {
258         return getObjectOfLatestVersion(major, getSession().getDefaultContext());
259     }
260 
261     public Document getObjectOfLatestVersion(boolean major, OperationContext context) {
262         String objectId;
263         String versionSeriesId;
264 
265         readLock();
266         try {
267             objectId = getObjectId();
268             versionSeriesId = getVersionSeriesId();
269         } finally {
270             readUnlock();
271         }
272 
273         if (versionSeriesId == null) {
274             throw new CmisRuntimeException("Version series id is unknown!");
275         }
276 
277         ObjectData objectData = getBinding().getVersioningService().getObjectOfLatestVersion(getRepositoryId(),
278                 objectId, versionSeriesId, major, context.getFilterString(), context.isIncludeAllowableActions(),
279                 context.getIncludeRelationships(), context.getRenditionFilterString(), context.isIncludePolicies(),
280                 context.isIncludeAcls(), null);
281 
282         ObjectFactory objectFactory = getSession().getObjectFactory();
283 
284         CmisObject result = objectFactory.convertObject(objectData, context);
285         if (!(result instanceof Document)) {
286             throw new CmisRuntimeException("Latest version is not a document!");
287         }
288 
289         return (Document) result;
290     }
291 
292     // content operations
293 
294     public ContentStream getContentStream() {
295         return getContentStream(null);
296     }
297 
298     public ContentStream getContentStream(String streamId) {
299         String objectId = getObjectId();
300 
301         // get the stream
302         ContentStream contentStream;
303         try {
304             contentStream = getBinding().getObjectService().getContentStream(getRepositoryId(), objectId, streamId,
305                     null, null, null);
306         } catch (CmisConstraintException e) {
307             // no content stream
308             return null;
309         }
310 
311         // handle incompliant repositories
312         if (contentStream == null) {
313             return null;
314         }
315 
316         // the AtomPub binding doesn't return a file name
317         // -> get the file name from properties, if present
318         String filename = contentStream.getFileName();
319         if (filename == null) {
320             filename = getContentStreamFileName();
321         }
322 
323         long length = (contentStream.getBigLength() == null ? -1 : contentStream.getBigLength().longValue());
324 
325         // convert and return stream object
326         return getSession().getObjectFactory().createContentStream(filename, length, contentStream.getMimeType(),
327                 contentStream.getStream());
328     }
329 
330     public Document setContentStream(ContentStream contentStream, boolean overwrite) {
331         ObjectId objectId = setContentStream(contentStream, overwrite, true);
332         if (objectId == null) {
333             return null;
334         }
335 
336         if (!getObjectId().equals(objectId.getId())) {
337             return (Document) getSession().getObject(objectId, getCreationContext());
338         }
339 
340         return this;
341     }
342 
343     public ObjectId setContentStream(ContentStream contentStream, boolean overwrite, boolean refresh) {
344         String newObjectId = null;
345 
346         readLock();
347         try {
348             Holder<String> objectIdHolder = new Holder<String>(getObjectId());
349             Holder<String> changeTokenHolder = new Holder<String>((String) getPropertyValue(PropertyIds.CHANGE_TOKEN));
350 
351             getBinding().getObjectService().setContentStream(getRepositoryId(), objectIdHolder, overwrite,
352                     changeTokenHolder, getObjectFactory().convertContentStream(contentStream), null);
353 
354             newObjectId = objectIdHolder.getValue();
355         } finally {
356             readUnlock();
357         }
358 
359         if (refresh) {
360             refresh();
361         }
362 
363         if (newObjectId == null) {
364             return null;
365         }
366 
367         return getSession().createObjectId(newObjectId);
368     }
369 
370     public Document deleteContentStream() {
371         ObjectId objectId = deleteContentStream(true);
372         if (objectId == null) {
373             return null;
374         }
375 
376         if (!getObjectId().equals(objectId.getId())) {
377             return (Document) getSession().getObject(objectId, getCreationContext());
378         }
379 
380         return this;
381     }
382 
383     public ObjectId deleteContentStream(boolean refresh) {
384         String newObjectId = null;
385 
386         readLock();
387         try {
388             Holder<String> objectIdHolder = new Holder<String>(getObjectId());
389             Holder<String> changeTokenHolder = new Holder<String>((String) getPropertyValue(PropertyIds.CHANGE_TOKEN));
390 
391             getBinding().getObjectService().deleteContentStream(getRepositoryId(), objectIdHolder, changeTokenHolder,
392                     null);
393 
394             newObjectId = objectIdHolder.getValue();
395         } finally {
396             readUnlock();
397         }
398 
399         if (refresh) {
400             refresh();
401         }
402 
403         if (newObjectId == null) {
404             return null;
405         }
406 
407         return getSession().createObjectId(newObjectId);
408     }
409 
410     public ObjectId checkIn(boolean major, Map<String, ?> properties, ContentStream contentStream, String checkinComment) {
411         return this.checkIn(major, properties, contentStream, checkinComment, null, null, null);
412     }
413 }