001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.opencmis.impl.client;
020
021import java.io.OutputStream;
022import java.math.BigInteger;
023import java.util.ArrayList;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.chemistry.opencmis.client.api.Document;
028import org.apache.chemistry.opencmis.client.api.ObjectId;
029import org.apache.chemistry.opencmis.client.api.ObjectType;
030import org.apache.chemistry.opencmis.client.api.OperationContext;
031import org.apache.chemistry.opencmis.client.api.Policy;
032import org.apache.chemistry.opencmis.client.api.SecondaryType;
033import org.apache.chemistry.opencmis.client.runtime.util.AppendOutputStream;
034import org.apache.chemistry.opencmis.commons.PropertyIds;
035import org.apache.chemistry.opencmis.commons.data.Ace;
036import org.apache.chemistry.opencmis.commons.data.ContentStream;
037import org.apache.chemistry.opencmis.commons.data.ContentStreamHash;
038import org.apache.chemistry.opencmis.commons.enums.CmisVersion;
039import org.apache.chemistry.opencmis.commons.enums.VersioningState;
040import org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException;
041import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
042import org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException;
043import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamHashImpl;
044import org.apache.chemistry.opencmis.commons.spi.Holder;
045import org.nuxeo.ecm.core.opencmis.impl.server.NuxeoObjectData;
046
047/**
048 * Live local CMIS Document, which is backed by a Nuxeo document.
049 */
050public class NuxeoDocument extends NuxeoFileableObject implements Document {
051
052    public NuxeoDocument(NuxeoSession session, NuxeoObjectData data, ObjectType type,
053            List<SecondaryType> secondaryTypes) {
054        super(session, data, type, secondaryTypes);
055    }
056
057    @Override
058    public void cancelCheckOut() {
059        service.cancelCheckOut(getRepositoryId(), getId(), null);
060    }
061
062    @Override
063    public ObjectId checkIn(boolean major, Map<String, ?> properties, ContentStream contentStream, String checkinComment) {
064        return checkIn(major, properties, contentStream, checkinComment, null, null, null);
065    }
066
067    @Override
068    public ObjectId checkIn(boolean major, Map<String, ?> properties, ContentStream contentStream,
069            String checkinComment, List<Policy> policies, List<Ace> addAces, List<Ace> removeAces) {
070        Holder<String> idHolder = new Holder<>(getId());
071        service.checkIn(getRepositoryId(), idHolder, Boolean.valueOf(major),
072                objectFactory.convertProperties(properties, type, null, UPDATABILITY_READWRITE),
073                objectFactory.convertContentStream(contentStream), checkinComment,
074                objectFactory.convertPolicies(policies), objectFactory.convertAces(addAces),
075                objectFactory.convertAces(removeAces), null);
076        return session.createObjectId(idHolder.getValue());
077    }
078
079    @Override
080    public ObjectId checkOut() {
081        Holder<String> idHolder = new Holder<>(getId());
082        service.checkOut(getRepositoryId(), idHolder, null, null);
083        return session.createObjectId(idHolder.getValue());
084    }
085
086    @Override
087    public NuxeoDocument copy(ObjectId target) {
088        return copy(target, null, null, null, null, null, session.getDefaultContext());
089    }
090
091    @Override
092    public NuxeoDocument copy(ObjectId target, Map<String, ?> properties, VersioningState versioningState,
093            List<Policy> policies, List<Ace> addACEs, List<Ace> removeACEs, OperationContext context) {
094        if (target == null || target.getId() == null) {
095            throw new CmisInvalidArgumentException("Invalid target: " + target);
096        }
097        if (context == null) {
098            context = session.getDefaultContext();
099        }
100        NuxeoObjectData newData = nuxeoCmisService.copy(getId(), target.getId(), properties, type, versioningState,
101                policies, addACEs, removeACEs, context);
102        return (NuxeoDocument) session.getObjectFactory().convertObject(newData, context);
103    }
104
105    @Override
106    public void deleteAllVersions() {
107        // TODO Auto-generated method stub
108        throw new UnsupportedOperationException();
109    }
110
111    @Override
112    public NuxeoDocument deleteContentStream() {
113        ObjectId objectId = deleteContentStream(true);
114        return (NuxeoDocument) session.getObject(objectId);
115    }
116
117    @Override
118    public ObjectId deleteContentStream(boolean refresh) {
119        Holder<String> objectIdHolder = new Holder<String>(getId());
120        String changeToken = getPropertyValue(PropertyIds.CHANGE_TOKEN);
121        Holder<String> changeTokenHolder = new Holder<String>(changeToken);
122
123        service.deleteContentStream(getRepositoryId(), objectIdHolder, changeTokenHolder, null);
124
125        String objectId = objectIdHolder.getValue(); // never null
126        return session.createObjectId(objectId);
127    }
128
129    @Override
130    public List<Document> getAllVersions() {
131        // TODO Auto-generated method stub
132        throw new UnsupportedOperationException();
133    }
134
135    @Override
136    public List<Document> getAllVersions(OperationContext context) {
137        // TODO Auto-generated method stub
138        throw new UnsupportedOperationException();
139    }
140
141    @Override
142    public String getCheckinComment() {
143        return getPropertyValue(PropertyIds.CHECKIN_COMMENT);
144    }
145
146    @Override
147    public ContentStream getContentStream() {
148        return getContentStream(null, null, null);
149    }
150
151    @Override
152    public ContentStream getContentStream(String streamId) {
153        return getContentStream(streamId, null, null);
154    }
155
156    @Override
157    public ContentStream getContentStream(BigInteger offset, BigInteger length) {
158        return getContentStream(null, offset, length);
159    }
160
161    @Override
162    public ContentStream getContentStream(String streamId, BigInteger offset, BigInteger length) {
163        try {
164            return service.getContentStream(getRepositoryId(), getId(), streamId, offset, length, null);
165        } catch (CmisConstraintException e) {
166            return null;
167        }
168    }
169
170    @Override
171    public String getContentStreamFileName() {
172        return getPropertyValue(PropertyIds.CONTENT_STREAM_FILE_NAME);
173    }
174
175    @Override
176    public String getContentStreamId() {
177        return getPropertyValue(PropertyIds.CONTENT_STREAM_ID);
178    }
179
180    @Override
181    public long getContentStreamLength() {
182        Long length = getPropertyValue(PropertyIds.CONTENT_STREAM_LENGTH);
183        return length == null ? -1 : length.longValue();
184    }
185
186    @Override
187    public String getContentStreamMimeType() {
188        return getPropertyValue(PropertyIds.CONTENT_STREAM_MIME_TYPE);
189    }
190
191    @Override
192    public Document getObjectOfLatestVersion(boolean major) {
193        // TODO Auto-generated method stub
194        throw new UnsupportedOperationException();
195    }
196
197    @Override
198    public Document getObjectOfLatestVersion(boolean major, OperationContext context) {
199        // TODO Auto-generated method stub
200        throw new UnsupportedOperationException();
201    }
202
203    @Override
204    public String getVersionLabel() {
205        return getPropertyValue(PropertyIds.VERSION_LABEL);
206    }
207
208    @Override
209    public String getVersionSeriesCheckedOutBy() {
210        return getPropertyValue(PropertyIds.VERSION_SERIES_CHECKED_OUT_BY);
211    }
212
213    @Override
214    public String getVersionSeriesCheckedOutId() {
215        return getPropertyValue(PropertyIds.VERSION_SERIES_CHECKED_OUT_ID);
216    }
217
218    @Override
219    public String getVersionSeriesId() {
220        return getPropertyValue(PropertyIds.VERSION_SERIES_ID);
221    }
222
223    @Override
224    public Boolean isImmutable() {
225        return getPropertyValue(PropertyIds.IS_IMMUTABLE);
226    }
227
228    @Override
229    public Boolean isLatestMajorVersion() {
230        return getPropertyValue(PropertyIds.IS_LATEST_MAJOR_VERSION);
231    }
232
233    @Override
234    public Boolean isLatestVersion() {
235        return getPropertyValue(PropertyIds.IS_LATEST_VERSION);
236    }
237
238    @Override
239    public Boolean isMajorVersion() {
240        return getPropertyValue(PropertyIds.IS_MAJOR_VERSION);
241    }
242
243    @Override
244    public Boolean isVersionSeriesCheckedOut() {
245        return getPropertyValue(PropertyIds.IS_VERSION_SERIES_CHECKED_OUT);
246    }
247
248    @Override
249    public Boolean isPrivateWorkingCopy() {
250        return getPropertyValue(PropertyIds.IS_PRIVATE_WORKING_COPY);
251    }
252
253    @Override
254    public String getLatestAccessibleStateId() {
255        return getPropertyValue(PropertyIds.LATEST_ACCESSIBLE_STATE_ID);
256    }
257
258    @Override
259    public Document setContentStream(ContentStream contentStream, boolean overwrite) {
260        ObjectId objectId = setContentStream(contentStream, overwrite, true);
261        return (Document) session.getObject(objectId);
262    }
263
264    @Override
265    public ObjectId setContentStream(ContentStream contentStream, boolean overwrite, boolean refresh) {
266        Holder<String> objectIdHolder = new Holder<String>(getId());
267        String changeToken = getPropertyValue(PropertyIds.CHANGE_TOKEN);
268        Holder<String> changeTokenHolder = new Holder<String>(changeToken);
269
270        service.setContentStream(getRepositoryId(), objectIdHolder, Boolean.valueOf(overwrite), changeTokenHolder,
271                contentStream, null);
272
273        String objectId = objectIdHolder.getValue(); // never null
274        return session.createObjectId(objectId);
275    }
276
277    @Override
278    public Document appendContentStream(ContentStream contentStream, boolean isLastChunk) {
279        ObjectId objectId = appendContentStream(contentStream, isLastChunk, true);
280        return (Document) session.getObject(objectId);
281    }
282
283    @Override
284    public ObjectId appendContentStream(ContentStream contentStream, boolean isLastChunk, boolean refresh) {
285        throw new CmisNotSupportedException();
286    }
287
288    @Override
289    public List<ContentStreamHash> getContentStreamHashes() {
290        List<String> hashes = getPropertyValue(PropertyIds.CONTENT_STREAM_HASH);
291        if (hashes == null || hashes.size() == 0) {
292            return null;
293        }
294
295        List<ContentStreamHash> result = new ArrayList<ContentStreamHash>(hashes.size());
296        for (String hash : hashes) {
297            result.add(new ContentStreamHashImpl(hash));
298        }
299
300        return result;
301    }
302
303    @Override
304    public String getContentUrl() {
305        throw new UnsupportedOperationException();
306    }
307
308    @Override
309    public String getContentUrl(String streamId) {
310        throw new UnsupportedOperationException();
311    }
312
313    @Override
314    public OutputStream createOverwriteOutputStream(String filename, String mimeType) {
315        if (session.getRepositoryInfo().getCmisVersion() == CmisVersion.CMIS_1_0) {
316            throw new CmisNotSupportedException("This method is not supported for CMIS 1.0 repositories");
317        }
318        return new AppendOutputStream(session, this, true, filename, mimeType);
319    }
320
321    @Override
322    public OutputStream createOverwriteOutputStream(String filename, String mimeType, int bufferSize) {
323        if (session.getRepositoryInfo().getCmisVersion() == CmisVersion.CMIS_1_0) {
324            throw new CmisNotSupportedException("This method is not supported for CMIS 1.0 repositories");
325        }
326        return new AppendOutputStream(session, this, true, filename, mimeType, bufferSize);
327    }
328
329    @Override
330    public OutputStream createAppendOutputStream() {
331        if (session.getRepositoryInfo().getCmisVersion() == CmisVersion.CMIS_1_0) {
332            throw new CmisNotSupportedException("This method is not supported for CMIS 1.0 repositories");
333        }
334        return new AppendOutputStream(session, this, false, null, null);
335    }
336
337    @Override
338    public OutputStream createAppendOutputStream(int bufferSize) {
339        if (session.getRepositoryInfo().getCmisVersion() == CmisVersion.CMIS_1_0) {
340            throw new CmisNotSupportedException("This method is not supported for CMIS 1.0 repositories");
341        }
342        return new AppendOutputStream(session, this, false, null, null, bufferSize);
343    }
344
345}