This project has retired. For details please refer to its Attic page.
TransientDocumentImpl 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.List;
24  import java.util.Map;
25  
26  import org.apache.chemistry.opencmis.client.api.CmisObject;
27  import org.apache.chemistry.opencmis.client.api.Document;
28  import org.apache.chemistry.opencmis.client.api.ObjectId;
29  import org.apache.chemistry.opencmis.client.api.OperationContext;
30  import org.apache.chemistry.opencmis.client.api.Policy;
31  import org.apache.chemistry.opencmis.client.api.Session;
32  import org.apache.chemistry.opencmis.client.api.TransientDocument;
33  import org.apache.chemistry.opencmis.commons.PropertyIds;
34  import org.apache.chemistry.opencmis.commons.data.Ace;
35  import org.apache.chemistry.opencmis.commons.data.ContentStream;
36  import org.apache.chemistry.opencmis.commons.data.Properties;
37  import org.apache.chemistry.opencmis.commons.enums.AclPropagation;
38  import org.apache.chemistry.opencmis.commons.enums.VersioningState;
39  import org.apache.chemistry.opencmis.commons.spi.Holder;
40  
41  public class TransientDocumentImpl extends AbstractTransientFileableCmisObject implements TransientDocument {
42  
43      private ContentStream contentStream;
44      private boolean contentOverwrite;
45      private boolean deleteContent;
46  
47      @Override
48      protected void initialize(Session session, CmisObject object) {
49          super.initialize(session, object);
50  
51          contentStream = null;
52          contentOverwrite = false;
53          deleteContent = false;
54      }
55  
56      public void deleteAllVersions() {
57          delete(true);
58      }
59  
60      public ContentStream getContentStream() {
61          return ((Document) getCmisObject()).getContentStream();
62      }
63  
64      public ContentStream getContentStream(String streamId) {
65          return ((Document) getCmisObject()).getContentStream(streamId);
66      }
67  
68      public void setContentStream(ContentStream contentStream, boolean overwrite) {
69          this.contentStream = contentStream;
70          this.contentOverwrite = overwrite;
71  
72          deleteContent = false;
73          isModified = true;
74      }
75  
76      public void deleteContentStream() {
77          deleteContent = true;
78  
79          contentStream = null;
80          isModified = true;
81      }
82  
83      public Document getObjectOfLatestVersion(boolean major) {
84          return ((Document) getCmisObject()).getObjectOfLatestVersion(major);
85      }
86  
87      public Document getObjectOfLatestVersion(boolean major, OperationContext context) {
88          return ((Document) getCmisObject()).getObjectOfLatestVersion(major, context);
89      }
90  
91      public List<Document> getAllVersions() {
92          return ((Document) getCmisObject()).getAllVersions();
93      }
94  
95      public List<Document> getAllVersions(OperationContext context) {
96          return ((Document) getCmisObject()).getAllVersions(context);
97      }
98  
99      public Document copy(ObjectId targetFolderId) {
100         return ((Document) getCmisObject()).copy(targetFolderId);
101     }
102 
103     public Document copy(ObjectId targetFolderId, Map<String, ?> properties, VersioningState versioningState,
104             List<Policy> policies, List<Ace> addACEs, List<Ace> removeACEs, OperationContext context) {
105         return ((Document) getCmisObject()).copy(targetFolderId, properties, versioningState, policies, addACEs,
106                 removeACEs, context);
107     }
108 
109     // properties
110 
111     public String getCheckinComment() {
112         return getPropertyValue(PropertyIds.CHECKIN_COMMENT);
113     }
114 
115     public String getVersionLabel() {
116         return getPropertyValue(PropertyIds.VERSION_LABEL);
117     }
118 
119     public String getVersionSeriesId() {
120         return getPropertyValue(PropertyIds.VERSION_SERIES_ID);
121     }
122 
123     public String getVersionSeriesCheckedOutId() {
124         return getPropertyValue(PropertyIds.VERSION_SERIES_CHECKED_OUT_ID);
125     }
126 
127     public String getVersionSeriesCheckedOutBy() {
128         return getPropertyValue(PropertyIds.VERSION_SERIES_CHECKED_OUT_BY);
129     }
130 
131     public Boolean isImmutable() {
132         return getPropertyValue(PropertyIds.IS_IMMUTABLE);
133     }
134 
135     public Boolean isLatestMajorVersion() {
136         return getPropertyValue(PropertyIds.IS_LATEST_MAJOR_VERSION);
137     }
138 
139     public Boolean isLatestVersion() {
140         return getPropertyValue(PropertyIds.IS_LATEST_VERSION);
141     }
142 
143     public Boolean isMajorVersion() {
144         return getPropertyValue(PropertyIds.IS_MAJOR_VERSION);
145     }
146 
147     public Boolean isVersionSeriesCheckedOut() {
148         return getPropertyValue(PropertyIds.IS_VERSION_SERIES_CHECKED_OUT);
149     }
150 
151     public long getContentStreamLength() {
152         BigInteger bigInt = getPropertyValue(PropertyIds.CONTENT_STREAM_LENGTH);
153         return (bigInt == null) ? (long) -1 : bigInt.longValue();
154     }
155 
156     public String getContentStreamMimeType() {
157         return getPropertyValue(PropertyIds.CONTENT_STREAM_MIME_TYPE);
158     }
159 
160     public String getContentStreamFileName() {
161         return getPropertyValue(PropertyIds.CONTENT_STREAM_FILE_NAME);
162     }
163 
164     public String getContentStreamId() {
165         return getPropertyValue(PropertyIds.CONTENT_STREAM_ID);
166     }
167 
168     // override save()
169 
170     @Override
171     public ObjectId save() {
172         if (!isModified()) {
173             // nothing has change, so there is nothing to do
174             return getObjectId();
175         }
176 
177         String objectId = getId();
178 
179         if (saveDelete(objectId)) {
180             // object has been deleted, there is nothing else to do
181             // ... and there is no object id anymore
182             return null;
183         }
184 
185         String newObjectId = objectId;
186         String newChangeToken = getChangeToken();
187 
188         newObjectId = saveProperties(getId(), newChangeToken);
189 
190         if (isPropertyUpdateRequired && ((contentStream != null) || deleteContent)) {
191             // we only need a new change token if the properties have changed
192             // AND the content should be modified
193             newChangeToken = getLatestChangeToken(newObjectId);
194         }
195 
196         newObjectId = saveContent(newObjectId, newChangeToken);
197 
198         saveACL(newObjectId);
199         savePolicies(newObjectId);
200 
201         return getSession().createObjectId(newObjectId);
202     }
203 
204     protected String saveContent(String objectId, String changeToken) {
205         Holder<String> objectIdHolder = new Holder<String>(objectId);
206         Holder<String> changeTokenHolder = new Holder<String>(changeToken);
207 
208         if (contentStream != null) {
209             getBinding().getObjectService().setContentStream(getRepositoryId(), objectIdHolder, contentOverwrite,
210                     changeTokenHolder, contentStream, null);
211         } else if (deleteContent) {
212             getBinding().getObjectService().deleteContentStream(getRepositoryId(), objectIdHolder, changeTokenHolder,
213                     null);
214         }
215 
216         if (objectIdHolder.getValue() != null) {
217             return objectIdHolder.getValue();
218         }
219 
220         return objectId;
221     }
222 
223     public ObjectId checkIn(boolean major, String checkinComment) {
224         Holder<String> objectIdHolder = new Holder<String>(getId());
225 
226         // convert properties
227         Properties checkinProperties = prepareProperties();
228 
229         // prepare policies
230         List<String> checkinPolicies = null;
231         if ((addPolicies != null) && (!addPolicies.isEmpty())) {
232             checkinPolicies = new ArrayList<String>(addPolicies);
233         }
234 
235         // prepare ACLs
236         List<AceChangeHolder> checkinAddAces = new ArrayList<AceChangeHolder>();
237         List<AceChangeHolder> checkinRemoveAces = new ArrayList<AceChangeHolder>();
238         for (AclPropagation ap : AclPropagation.values()) {
239             if (addAces.containsKey(ap)) {
240                 checkinAddAces.addAll(addAces.get(ap));
241             }
242             if (removeAces.containsKey(ap)) {
243                 checkinAddAces.addAll(removeAces.get(ap));
244             }
245         }
246         if (addAces.containsKey(null)) {
247             checkinAddAces.addAll(addAces.get(null));
248         }
249         if (removeAces.containsKey(null)) {
250             checkinAddAces.addAll(removeAces.get(null));
251         }
252 
253         // check in
254         getBinding().getVersioningService().checkIn(getRepositoryId(), objectIdHolder, major, checkinProperties,
255                 contentStream, checkinComment, checkinPolicies, prepareAcl(checkinAddAces),
256                 prepareAcl(checkinRemoveAces), null);
257 
258         if (objectIdHolder.getValue() != null) {
259             return getSession().createObjectId(objectIdHolder.getValue());
260         }
261 
262         return getObjectId();
263     }
264 }