001/*
002 * (C) Copyright 2015 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 *     Antoine Taillefer <ataillefer@nuxeo.com>
018 */
019package org.nuxeo.drive.operations;
020
021import org.nuxeo.ecm.automation.core.Constants;
022import org.nuxeo.ecm.automation.core.annotations.Context;
023import org.nuxeo.ecm.automation.core.annotations.Operation;
024import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
025import org.nuxeo.ecm.automation.core.annotations.Param;
026import org.nuxeo.ecm.core.api.Blob;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.NuxeoException;
030import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
031
032/**
033 * Updates the given {@link DocumentModel} with the given blob.
034 *
035 * @author Antoine Taillefer
036 * @since 7.4
037 */
038@Operation(id = NuxeoDriveAttachBlob.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Attach blob")
039public class NuxeoDriveAttachBlob {
040
041    public static final String ID = "NuxeoDrive.AttachBlob";
042
043    @Context
044    protected CoreSession session;
045
046    @Param(name = "document")
047    protected DocumentModel doc;
048
049    /**
050     * @deprecated since 9.1 versioning policy is now handled at versioning service level, as versioning is removed at
051     * drive level, this parameter is not used anymore
052     */
053    @Deprecated
054    @Param(name = "applyVersioningPolicy", required = false, values = "false")
055    protected boolean applyVersioningPolicy = false;
056
057    /**
058     * @deprecated since 9.1 versioning policy is now handled at versioning service level, as versioning is removed at
059     * drive level, this parameter is not used anymore
060     */
061    @Deprecated
062    @Param(name = "factoryName", required = false, values = "defaultFileSystemItemFactory")
063    protected String factoryName = "defaultFileSystemItemFactory";
064
065    @OperationMethod
066    public Blob run(Blob blob) {
067        BlobHolder bh = doc.getAdapter(BlobHolder.class);
068        if (bh == null) {
069            throw new NuxeoException(String.format("Document %s is not a BlobHolder, no blob can be attached to it.",
070                    doc.getId()));
071        }
072        bh.setBlob(blob);
073        session.saveDocument(doc);
074        return blob;
075    }
076
077}