This project has retired. For details please refer to its Attic page.
DocumentVersionImpl 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.inmemory.storedobj.impl;
20  
21  import java.io.IOException;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.chemistry.opencmis.commons.PropertyIds;
27  import org.apache.chemistry.opencmis.commons.data.ContentStream;
28  import org.apache.chemistry.opencmis.commons.data.PropertyData;
29  import org.apache.chemistry.opencmis.commons.enums.VersioningState;
30  import org.apache.chemistry.opencmis.commons.spi.BindingsObjectFactory;
31  import org.apache.chemistry.opencmis.inmemory.ConfigConstants;
32  import org.apache.chemistry.opencmis.inmemory.ConfigurationSettings;
33  import org.apache.chemistry.opencmis.inmemory.FilterParser;
34  import org.apache.chemistry.opencmis.inmemory.storedobj.api.DocumentVersion;
35  import org.apache.chemistry.opencmis.inmemory.storedobj.api.Folder;
36  import org.apache.chemistry.opencmis.inmemory.storedobj.api.VersionedDocument;
37  
38  /**
39   * A class representing a single version of a document
40   *
41   * @author Jens
42   *
43   */
44  public class DocumentVersionImpl extends StoredObjectImpl implements DocumentVersion {
45  
46      private final Long MAX_CONTENT_SIZE_KB = ConfigurationSettings.getConfigurationValueAsLong(ConfigConstants.MAX_CONTENT_SIZE_KB);
47  
48      private ContentStreamDataImpl fContent;
49      private final VersionedDocument fContainer; // the document this version belongs
50      // to
51      private String fComment; // checkin comment
52      boolean fIsMajor;
53      boolean fIsPwc; // true if this is the PWC
54  
55      public DocumentVersionImpl(String repositoryId, VersionedDocument container, ContentStream content,
56              VersioningState verState, ObjectStoreImpl objStore) {
57          super(objStore);
58          setRepositoryId(repositoryId);
59          fContainer = container;
60          setContent(content, false);
61          fIsMajor = verState == VersioningState.MAJOR;
62          fIsPwc = verState == VersioningState.CHECKEDOUT;
63          fProperties = new HashMap<String, PropertyData<?>>(); // ensure that we
64          // have a map
65      }
66  
67      public void setContent(ContentStream content, boolean mustPersist) {
68          if (null == content) {
69              fContent = null;
70          } else {
71              fContent = new ContentStreamDataImpl(MAX_CONTENT_SIZE_KB == null ? 0 : MAX_CONTENT_SIZE_KB);
72              fContent.setFileName(content.getFileName());
73              fContent.setMimeType(content.getMimeType());
74              try {
75                  fContent.setContent(content.getStream());
76              } catch (IOException e) {
77                  e.printStackTrace();
78                  throw new RuntimeException("Failed to get content from InputStream", e);
79              }
80          }
81      }
82  
83      public void setCheckinComment(String comment) {
84          fComment = comment;
85      }
86  
87      public String getCheckinComment() {
88          return fComment;
89      }
90  
91      public String getVersionLabel() {
92          int majorNo = 0;
93          int minorNo = 0;
94          List<DocumentVersion> allVersions = fContainer.getAllVersions();
95          for (DocumentVersion ver : allVersions) {
96              if (ver.isMajor()) {
97                  ++majorNo;
98                  minorNo = 0;
99              } else {
100                 ++minorNo;
101             }
102             if (ver == this) {
103                 break;
104             }
105         }
106         String label = "V " + majorNo + "." + minorNo;
107         return label;
108     }
109 
110     public boolean isMajor() {
111         return fIsMajor && !isPwc();
112     }
113 
114     public boolean isPwc() {
115         return fIsPwc;
116     }
117 
118     public void commit(boolean isMajor) {
119         fIsPwc = false; // unset working copy flag
120         fIsMajor = isMajor;
121     }
122 
123     public ContentStream getContent(long offset, long length) {
124         if (offset <= 0 && length < 0) {
125             return fContent;
126         } else {
127             return fContent.getCloneWithLimits(offset, length);
128         }
129     }
130 
131     public VersionedDocument getParentDocument() {
132         return fContainer;
133     }
134 
135     private boolean isLatestVersion() {
136         List<DocumentVersion> allVers = fContainer.getAllVersions();
137         boolean isLatestVersion;
138         if (isPwc()) {
139             isLatestVersion = allVers.size() > 1 && allVers.get(allVers.size() - 2).equals(this);
140         } else {
141             isLatestVersion = allVers.get(allVers.size() - 1).equals(this);
142         }
143         return isLatestVersion;
144     }
145 
146     private boolean isLatestMajorVersion() {
147         if (!fIsMajor) {
148             return false;
149         }
150 
151         List<DocumentVersion> allVersions = fContainer.getAllVersions();
152         DocumentVersion latestMajor = null;
153 
154         for (DocumentVersion ver : allVersions) {
155             if (ver.isMajor() && !ver.isPwc()) {
156                 latestMajor = ver;
157             }
158         }
159 
160         boolean isLatestMajorVersion = latestMajor == this;
161         return isLatestMajorVersion;
162     }
163 
164     // public void persist() {
165     // if (null==fId)
166     // fId = UUID.randomUUID().toString();
167     // }
168 
169     @Override
170     public void fillProperties(Map<String, PropertyData<?>> properties, BindingsObjectFactory objFactory,
171             List<String> requestedIds) {
172 
173         DocumentVersion pwc = fContainer.getPwc();
174 
175         // First get the properties of the container (like custom type
176         // properties, etc)
177         fContainer.fillProperties(properties, objFactory, requestedIds);
178 
179         // overwrite the version specific properties (like modification date,
180         // user, etc.)
181         // and set some properties specific to the version
182         super.fillProperties(properties, objFactory, requestedIds);
183 
184 
185         if (FilterParser.isContainedInFilter(PropertyIds.IS_IMMUTABLE, requestedIds)) {
186             properties.put(PropertyIds.IS_IMMUTABLE, objFactory.createPropertyBooleanData(PropertyIds.IS_IMMUTABLE,
187                     false));
188         }
189 
190         // fill the version related properties
191         if (FilterParser.isContainedInFilter(PropertyIds.IS_LATEST_VERSION, requestedIds)) {
192             properties.put(PropertyIds.IS_LATEST_VERSION, objFactory.createPropertyBooleanData(
193                     PropertyIds.IS_LATEST_VERSION, isLatestVersion()));
194         }
195         if (FilterParser.isContainedInFilter(PropertyIds.IS_MAJOR_VERSION, requestedIds)) {
196             properties.put(PropertyIds.IS_MAJOR_VERSION, objFactory.createPropertyBooleanData(
197                     PropertyIds.IS_MAJOR_VERSION, fIsMajor));
198         }
199         if (FilterParser.isContainedInFilter(PropertyIds.IS_LATEST_MAJOR_VERSION, requestedIds)) {
200             properties.put(PropertyIds.IS_LATEST_MAJOR_VERSION, objFactory.createPropertyBooleanData(
201                     PropertyIds.IS_LATEST_MAJOR_VERSION, isLatestMajorVersion()));
202         }
203         if (FilterParser.isContainedInFilter(PropertyIds.VERSION_SERIES_ID, requestedIds)) {
204             properties.put(PropertyIds.VERSION_SERIES_ID, objFactory.createPropertyIdData(
205                     PropertyIds.VERSION_SERIES_ID, fContainer.getId()));
206         }
207         if (FilterParser.isContainedInFilter(PropertyIds.IS_VERSION_SERIES_CHECKED_OUT, requestedIds)) {
208             properties.put(PropertyIds.IS_VERSION_SERIES_CHECKED_OUT, objFactory.createPropertyBooleanData(
209                     PropertyIds.IS_VERSION_SERIES_CHECKED_OUT, fContainer.isCheckedOut()));
210         }
211         if (FilterParser.isContainedInFilter(PropertyIds.VERSION_SERIES_CHECKED_OUT_BY, requestedIds)) {
212             properties.put(PropertyIds.VERSION_SERIES_CHECKED_OUT_BY, objFactory.createPropertyStringData(
213                     PropertyIds.VERSION_SERIES_CHECKED_OUT_BY, fContainer.getCheckedOutBy()));
214         }
215         if (FilterParser.isContainedInFilter(PropertyIds.VERSION_SERIES_CHECKED_OUT_ID, requestedIds)) {
216             properties.put(PropertyIds.VERSION_SERIES_CHECKED_OUT_ID, objFactory.createPropertyIdData(
217                     PropertyIds.VERSION_SERIES_CHECKED_OUT_ID, pwc == null ? null : pwc.getId()));
218         }
219         if (FilterParser.isContainedInFilter(PropertyIds.CHECKIN_COMMENT, requestedIds)) {
220             properties.put(PropertyIds.CHECKIN_COMMENT, objFactory.createPropertyStringData(
221                     PropertyIds.CHECKIN_COMMENT, fComment));
222         }
223         if (FilterParser.isContainedInFilter(PropertyIds.VERSION_LABEL, requestedIds)) {
224             properties.put(PropertyIds.VERSION_LABEL, objFactory.createPropertyStringData(PropertyIds.VERSION_LABEL,
225                     getVersionLabel()));
226         }
227 
228         // Set the content related properties
229         if (null != fContent) {
230             if (FilterParser.isContainedInFilter(PropertyIds.CONTENT_STREAM_FILE_NAME, requestedIds)) {
231                 properties.put(PropertyIds.CONTENT_STREAM_FILE_NAME, objFactory.createPropertyStringData(
232                         PropertyIds.CONTENT_STREAM_FILE_NAME, fContent.getFileName()));
233             }
234             if (FilterParser.isContainedInFilter(PropertyIds.CONTENT_STREAM_ID, requestedIds)) {
235                 properties.put(PropertyIds.CONTENT_STREAM_ID, objFactory.createPropertyStringData(
236                         PropertyIds.CONTENT_STREAM_ID, (String) null));
237             }
238             if (FilterParser.isContainedInFilter(PropertyIds.CONTENT_STREAM_LENGTH, requestedIds)) {
239                 properties.put(PropertyIds.CONTENT_STREAM_LENGTH, objFactory.createPropertyIntegerData(
240                         PropertyIds.CONTENT_STREAM_LENGTH, fContent.getBigLength()));
241             }
242             if (FilterParser.isContainedInFilter(PropertyIds.CONTENT_STREAM_MIME_TYPE, requestedIds)) {
243                 properties.put(PropertyIds.CONTENT_STREAM_MIME_TYPE, objFactory.createPropertyStringData(
244                         PropertyIds.CONTENT_STREAM_MIME_TYPE, fContent.getMimeType()));
245             }
246         }
247     }
248 
249     @Override
250     public int getAclId() {
251         return ((StoredObjectImpl)fContainer).getAclId();
252     }    
253 
254     @Override
255     public void setAclId(int id) {
256         ((StoredObjectImpl)fContainer).setAclId(id);
257     }    
258     
259     public List<Folder> getParents(String user) {
260         return fContainer.getParents(user);
261     }
262 
263     public String getPathSegment() {
264         return fContainer.getPathSegment();
265     }
266 
267     public void move(Folder oldParent, Folder newParent) {
268         fContainer.move(oldParent, newParent);
269     }
270 
271     public void addParent(Folder parent) {
272         fContainer.addParent(parent);
273     }
274 
275     public void removeParent(Folder parent) {
276         fContainer.removeParent(parent);
277     }
278 
279     public boolean hasContent() {
280         return null != fContent;
281     }
282 
283     public boolean hasParent() {
284       return true;
285     }
286 
287 
288 }