001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Florent Guillaume
011 */
012package org.nuxeo.ecm.core.opencmis.impl.client;
013
014import java.math.BigInteger;
015import java.util.ArrayList;
016import java.util.List;
017import java.util.Map;
018
019import org.apache.chemistry.opencmis.client.api.Document;
020import org.apache.chemistry.opencmis.client.api.ObjectId;
021import org.apache.chemistry.opencmis.client.api.ObjectType;
022import org.apache.chemistry.opencmis.client.api.OperationContext;
023import org.apache.chemistry.opencmis.client.api.Policy;
024import org.apache.chemistry.opencmis.commons.PropertyIds;
025import org.apache.chemistry.opencmis.commons.data.Ace;
026import org.apache.chemistry.opencmis.commons.data.ContentStream;
027import org.apache.chemistry.opencmis.commons.data.ContentStreamHash;
028import org.apache.chemistry.opencmis.commons.enums.VersioningState;
029import org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException;
030import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
031import org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException;
032import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamHashImpl;
033import org.apache.chemistry.opencmis.commons.spi.Holder;
034import org.nuxeo.ecm.core.opencmis.impl.server.NuxeoObjectData;
035
036/**
037 * Live local CMIS Document, which is backed by a Nuxeo document.
038 */
039public class NuxeoDocument extends NuxeoFileableObject implements Document {
040
041    public NuxeoDocument(NuxeoSession session, NuxeoObjectData data, ObjectType type) {
042        super(session, data, type);
043    }
044
045    @Override
046    public void cancelCheckOut() {
047        service.cancelCheckOut(getRepositoryId(), getId(), null);
048    }
049
050    @Override
051    public ObjectId checkIn(boolean major, Map<String, ?> properties, ContentStream contentStream, String checkinComment) {
052        return checkIn(major, properties, contentStream, checkinComment, null, null, null);
053    }
054
055    @Override
056    public ObjectId checkIn(boolean major, Map<String, ?> properties, ContentStream contentStream,
057            String checkinComment, List<Policy> policies, List<Ace> addAces, List<Ace> removeAces) {
058        Holder<String> idHolder = new Holder<>(getId());
059        service.checkIn(getRepositoryId(), idHolder, Boolean.valueOf(major),
060                objectFactory.convertProperties(properties, type, null, UPDATABILITY_READWRITE),
061                objectFactory.convertContentStream(contentStream), checkinComment,
062                objectFactory.convertPolicies(policies), objectFactory.convertAces(addAces),
063                objectFactory.convertAces(removeAces), null);
064        return session.createObjectId(idHolder.getValue());
065    }
066
067    @Override
068    public ObjectId checkOut() {
069        Holder<String> idHolder = new Holder<>(getId());
070        service.checkOut(getRepositoryId(), idHolder, null, null);
071        return session.createObjectId(idHolder.getValue());
072    }
073
074    @Override
075    public NuxeoDocument copy(ObjectId target) {
076        return copy(target, null, null, null, null, null, session.getDefaultContext());
077    }
078
079    @Override
080    public NuxeoDocument copy(ObjectId target, Map<String, ?> properties, VersioningState versioningState,
081            List<Policy> policies, List<Ace> addACEs, List<Ace> removeACEs, OperationContext context) {
082        if (target == null || target.getId() == null) {
083            throw new CmisInvalidArgumentException("Invalid target: " + target);
084        }
085        if (context == null) {
086            context = session.getDefaultContext();
087        }
088        NuxeoObjectData newData = nuxeoCmisService.copy(getId(), target.getId(), properties, type, versioningState,
089                policies, addACEs, removeACEs, context);
090        return (NuxeoDocument) session.getObjectFactory().convertObject(newData, context);
091    }
092
093    @Override
094    public void deleteAllVersions() {
095        // TODO Auto-generated method stub
096        throw new UnsupportedOperationException();
097    }
098
099    @Override
100    public NuxeoDocument deleteContentStream() {
101        ObjectId objectId = deleteContentStream(true);
102        return (NuxeoDocument) session.getObject(objectId);
103    }
104
105    @Override
106    public ObjectId deleteContentStream(boolean refresh) {
107        Holder<String> objectIdHolder = new Holder<String>(getId());
108        String changeToken = getPropertyValue(PropertyIds.CHANGE_TOKEN);
109        Holder<String> changeTokenHolder = new Holder<String>(changeToken);
110
111        service.deleteContentStream(getRepositoryId(), objectIdHolder, changeTokenHolder, null);
112
113        String objectId = objectIdHolder.getValue(); // never null
114        return session.createObjectId(objectId);
115    }
116
117    @Override
118    public List<Document> getAllVersions() {
119        // TODO Auto-generated method stub
120        throw new UnsupportedOperationException();
121    }
122
123    @Override
124    public List<Document> getAllVersions(OperationContext context) {
125        // TODO Auto-generated method stub
126        throw new UnsupportedOperationException();
127    }
128
129    @Override
130    public String getCheckinComment() {
131        return getPropertyValue(PropertyIds.CHECKIN_COMMENT);
132    }
133
134    @Override
135    public ContentStream getContentStream() {
136        return getContentStream(null, null, null);
137    }
138
139    @Override
140    public ContentStream getContentStream(String streamId) {
141        return getContentStream(streamId, null, null);
142    }
143
144    @Override
145    public ContentStream getContentStream(BigInteger offset, BigInteger length) {
146        return getContentStream(null, offset, length);
147    }
148
149    @Override
150    public ContentStream getContentStream(String streamId, BigInteger offset, BigInteger length) {
151        try {
152            return service.getContentStream(getRepositoryId(), getId(), streamId, offset, length, null);
153        } catch (CmisConstraintException e) {
154            return null;
155        }
156    }
157
158    @Override
159    public String getContentStreamFileName() {
160        return getPropertyValue(PropertyIds.CONTENT_STREAM_FILE_NAME);
161    }
162
163    @Override
164    public String getContentStreamId() {
165        return getPropertyValue(PropertyIds.CONTENT_STREAM_ID);
166    }
167
168    @Override
169    public long getContentStreamLength() {
170        Long length = getPropertyValue(PropertyIds.CONTENT_STREAM_LENGTH);
171        return length == null ? -1 : length.longValue();
172    }
173
174    @Override
175    public String getContentStreamMimeType() {
176        return getPropertyValue(PropertyIds.CONTENT_STREAM_MIME_TYPE);
177    }
178
179    @Override
180    public Document getObjectOfLatestVersion(boolean major) {
181        // TODO Auto-generated method stub
182        throw new UnsupportedOperationException();
183    }
184
185    @Override
186    public Document getObjectOfLatestVersion(boolean major, OperationContext context) {
187        // TODO Auto-generated method stub
188        throw new UnsupportedOperationException();
189    }
190
191    @Override
192    public String getVersionLabel() {
193        return getPropertyValue(PropertyIds.VERSION_LABEL);
194    }
195
196    @Override
197    public String getVersionSeriesCheckedOutBy() {
198        return getPropertyValue(PropertyIds.VERSION_SERIES_CHECKED_OUT_BY);
199    }
200
201    @Override
202    public String getVersionSeriesCheckedOutId() {
203        return getPropertyValue(PropertyIds.VERSION_SERIES_CHECKED_OUT_ID);
204    }
205
206    @Override
207    public String getVersionSeriesId() {
208        return getPropertyValue(PropertyIds.VERSION_SERIES_ID);
209    }
210
211    @Override
212    public Boolean isImmutable() {
213        return getPropertyValue(PropertyIds.IS_IMMUTABLE);
214    }
215
216    @Override
217    public Boolean isLatestMajorVersion() {
218        return getPropertyValue(PropertyIds.IS_LATEST_MAJOR_VERSION);
219    }
220
221    @Override
222    public Boolean isLatestVersion() {
223        return getPropertyValue(PropertyIds.IS_LATEST_VERSION);
224    }
225
226    @Override
227    public Boolean isMajorVersion() {
228        return getPropertyValue(PropertyIds.IS_MAJOR_VERSION);
229    }
230
231    @Override
232    public Boolean isVersionSeriesCheckedOut() {
233        return getPropertyValue(PropertyIds.IS_VERSION_SERIES_CHECKED_OUT);
234    }
235
236    @Override
237    public Boolean isPrivateWorkingCopy() {
238        return getPropertyValue(PropertyIds.IS_PRIVATE_WORKING_COPY);
239    }
240
241    @Override
242    public Document setContentStream(ContentStream contentStream, boolean overwrite) {
243        ObjectId objectId = setContentStream(contentStream, overwrite, true);
244        return (Document) session.getObject(objectId);
245    }
246
247    @Override
248    public ObjectId setContentStream(ContentStream contentStream, boolean overwrite, boolean refresh) {
249        Holder<String> objectIdHolder = new Holder<String>(getId());
250        String changeToken = getPropertyValue(PropertyIds.CHANGE_TOKEN);
251        Holder<String> changeTokenHolder = new Holder<String>(changeToken);
252
253        service.setContentStream(getRepositoryId(), objectIdHolder, Boolean.valueOf(overwrite), changeTokenHolder,
254                contentStream, null);
255
256        String objectId = objectIdHolder.getValue(); // never null
257        return session.createObjectId(objectId);
258    }
259
260    @Override
261    public Document appendContentStream(ContentStream contentStream, boolean isLastChunk) {
262        ObjectId objectId = appendContentStream(contentStream, isLastChunk, true);
263        return (Document) session.getObject(objectId);
264    }
265
266    @Override
267    public ObjectId appendContentStream(ContentStream contentStream, boolean isLastChunk, boolean refresh) {
268        throw new CmisNotSupportedException();
269    }
270
271    @Override
272    public List<ContentStreamHash> getContentStreamHashes() {
273        List<String> hashes = getPropertyValue(PropertyIds.CONTENT_STREAM_HASH);
274        if (hashes == null || hashes.size() == 0) {
275            return null;
276        }
277
278        List<ContentStreamHash> result = new ArrayList<ContentStreamHash>(hashes.size());
279        for (String hash : hashes) {
280            result.add(new ContentStreamHashImpl(hash));
281        }
282
283        return result;
284    }
285
286}