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 input 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", description = "Update the given document with the input blob." //
039        + " Return the input blob.")
040public class NuxeoDriveAttachBlob {
041
042    public static final String ID = "NuxeoDrive.AttachBlob";
043
044    @Context
045    protected CoreSession session;
046
047    @Param(name = "document", description = "The document to update.")
048    protected DocumentModel doc;
049
050    /**
051     * @deprecated since 9.1 versioning policy is now handled at versioning service level, as versioning is removed at
052     *             drive level, this parameter is not used anymore
053     */
054    @Deprecated
055    @Param(name = "applyVersioningPolicy", required = false, values = "false")
056    protected boolean applyVersioningPolicy = false;
057
058    /**
059     * @deprecated since 9.1 versioning policy is now handled at versioning service level, as versioning is removed at
060     *             drive level, this parameter is not used anymore
061     */
062    @Deprecated
063    @Param(name = "factoryName", required = false, values = "defaultFileSystemItemFactory")
064    protected String factoryName = "defaultFileSystemItemFactory";
065
066    @OperationMethod
067    public Blob run(Blob blob) {
068        BlobHolder bh = doc.getAdapter(BlobHolder.class);
069        if (bh == null) {
070            throw new NuxeoException(
071                    String.format("Document %s is not a BlobHolder, no blob can be attached to it.", doc.getId()));
072        }
073        bh.setBlob(blob);
074        session.saveDocument(doc);
075        return blob;
076    }
077
078}