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