This project has retired. For details please refer to its Attic page.
VersionedDocumentImpl 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.util.ArrayList;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.chemistry.opencmis.commons.PropertyIds;
26  import org.apache.chemistry.opencmis.commons.data.ContentStream;
27  import org.apache.chemistry.opencmis.commons.data.Properties;
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.exceptions.CmisConstraintException;
31  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
32  import org.apache.chemistry.opencmis.commons.spi.BindingsObjectFactory;
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.VersionedDocument;
36  
37  public class VersionedDocumentImpl extends AbstractMultiFilingImpl implements VersionedDocument {
38  
39      private boolean fIsCheckedOut;
40      private String fCheckedOutUser;
41      private final List<DocumentVersion> fVersions;
42  
43      public VersionedDocumentImpl(ObjectStoreImpl objStore) {
44          super(objStore);
45          fVersions = new ArrayList<DocumentVersion>();
46          fIsCheckedOut = false;
47      }
48  
49      public DocumentVersion addVersion(ContentStream content, VersioningState verState, String user) {
50  
51          if (isCheckedOut()) {
52              throw new CmisConstraintException("Cannot add a version to document, document is checked out.");
53          }
54  
55          DocumentVersionImpl ver = new DocumentVersionImpl(fRepositoryId, this, content, verState, fObjStore);
56          ver.setSystemBasePropertiesWhenCreatedDirect(getName(), getTypeId(), user); // copy
57          // name and type id from version series.
58          ver.persist();
59          fVersions.add(ver);
60          if (verState == VersioningState.CHECKEDOUT) {
61              fCheckedOutUser = user;
62              fIsCheckedOut = true;
63          }
64  
65          return ver;
66      }
67  
68      public boolean deleteVersion(DocumentVersion version) {
69          if (fIsCheckedOut) {
70              // Note: Do not throw an exception here if the document is checked-out. In AtomPub binding cancelCheckout
71              // mapped to a deleteVersion() call!
72              DocumentVersion pwc = getPwc();
73              if (pwc == version) {
74                  cancelCheckOut(false); // note object is already deleted from map in ObjectStore 
75                  return !fVersions.isEmpty();
76              }
77          }
78          boolean found = fVersions.remove(version);
79          if (!found) {
80              throw new CmisInvalidArgumentException("Version is not contained in the document:" + version.getId());
81          }
82  
83          return !fVersions.isEmpty();
84      }
85  
86      public void cancelCheckOut(String user) {
87          cancelCheckOut(true);
88      }
89  
90      public void checkIn(boolean isMajor, Properties properties, ContentStream content, String checkinComment, String user) {
91          if (fIsCheckedOut) {
92              if (fCheckedOutUser.equals(user)) {
93                  fIsCheckedOut = false;
94                  fCheckedOutUser = null;
95              } else {
96                  throw new CmisConstraintException("Error: Can't checkin. Document " + getId() + " user " + user
97                          + " has not checked out the document");
98              }
99          } else {
100             throw new CmisConstraintException("Error: Can't cancel checkout, Document " + getId()
101                     + " is not checked out.");
102         }
103 
104         DocumentVersion pwc = getPwc();
105         
106         if (null != content)
107             pwc.setContent(content, false);
108 
109         if (null != properties && null != properties.getProperties())
110             ((DocumentVersionImpl)pwc).setCustomProperties(properties.getProperties());
111 
112         pwc.setCheckinComment(checkinComment);
113         pwc.commit(isMajor);
114     }
115 
116     public DocumentVersion checkOut(ContentStream content, String user) {
117         if (fIsCheckedOut) {
118             throw new CmisConstraintException("Error: Can't checkout, Document " + getId() + " is already checked out.");
119         }
120 
121         // create PWC
122         DocumentVersion pwc = addVersion(content, VersioningState.CHECKEDOUT, user); // will
123         // set check-out flag
124         return pwc;
125     }
126 
127     public List<DocumentVersion> getAllVersions() {
128         return fVersions;
129     }
130 
131     public DocumentVersion getLatestVersion(boolean major) {
132 
133         DocumentVersion latest = null;
134         if (major) {
135             for (DocumentVersion ver : fVersions) {
136                 if (ver.isMajor()) {
137                     latest = ver;
138                 }
139             }
140         } else {
141             latest = fVersions.get(fVersions.size() - 1);
142         }
143         return latest;
144     }
145 
146     public boolean isCheckedOut() {
147         return fIsCheckedOut;
148     }
149 
150     public String getCheckedOutBy() {
151         return fCheckedOutUser;
152     }
153 
154     public DocumentVersion getPwc() {
155         for (DocumentVersion ver : fVersions) {
156             if (ver.isPwc()) {
157                 return ver;
158             }
159         }
160         return null;
161     }
162 
163     @Override
164     public void fillProperties(Map<String, PropertyData<?>> properties, BindingsObjectFactory objFactory,
165             List<String> requestedIds) {
166 
167         DocumentVersion pwc = getPwc();
168 
169         super.fillProperties(properties, objFactory, requestedIds);
170 
171 
172         if (FilterParser.isContainedInFilter(PropertyIds.IS_IMMUTABLE, requestedIds)) {
173             properties.put(PropertyIds.IS_IMMUTABLE, objFactory.createPropertyBooleanData(PropertyIds.IS_IMMUTABLE,
174                     false));
175         }
176 
177         // overwrite the version related properties
178         if (FilterParser.isContainedInFilter(PropertyIds.VERSION_SERIES_ID, requestedIds)) {
179             properties.put(PropertyIds.VERSION_SERIES_ID, objFactory.createPropertyIdData(
180                     PropertyIds.VERSION_SERIES_ID, getId()));
181         }
182         if (FilterParser.isContainedInFilter(PropertyIds.IS_VERSION_SERIES_CHECKED_OUT, requestedIds)) {
183             properties.put(PropertyIds.IS_VERSION_SERIES_CHECKED_OUT, objFactory.createPropertyBooleanData(
184                     PropertyIds.IS_VERSION_SERIES_CHECKED_OUT, isCheckedOut()));
185         }
186         if (FilterParser.isContainedInFilter(PropertyIds.VERSION_SERIES_CHECKED_OUT_BY, requestedIds)) {
187             properties.put(PropertyIds.VERSION_SERIES_CHECKED_OUT_BY, objFactory.createPropertyStringData(
188                     PropertyIds.VERSION_SERIES_CHECKED_OUT_BY, getCheckedOutBy()));
189         }
190         if (FilterParser.isContainedInFilter(PropertyIds.VERSION_SERIES_CHECKED_OUT_ID, requestedIds)) {
191             properties.put(PropertyIds.VERSION_SERIES_CHECKED_OUT_ID, objFactory.createPropertyIdData(
192                     PropertyIds.VERSION_SERIES_CHECKED_OUT_ID, pwc == null ? null : pwc.getId()));
193         }
194 
195     }
196     
197     private void cancelCheckOut(boolean deleteInObjectStore) {
198         
199         DocumentVersion pwc = getPwc();
200         fIsCheckedOut = false;
201         fCheckedOutUser = null;
202         fVersions.remove(pwc);
203         if (deleteInObjectStore)
204             fObjStore.removeVersion(pwc);
205     }
206 
207 }