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.drive.adapter.impl.FileSystemItemHelper;
022import org.nuxeo.drive.service.FileSystemItemAdapterService;
023import org.nuxeo.drive.service.FileSystemItemFactory;
024import org.nuxeo.drive.service.VersioningFileSystemItemFactory;
025import org.nuxeo.drive.service.impl.FileSystemItemAdapterServiceImpl;
026import org.nuxeo.ecm.automation.core.Constants;
027import org.nuxeo.ecm.automation.core.annotations.Context;
028import org.nuxeo.ecm.automation.core.annotations.Operation;
029import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
030import org.nuxeo.ecm.automation.core.annotations.Param;
031import org.nuxeo.ecm.core.api.Blob;
032import org.nuxeo.ecm.core.api.CoreSession;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.api.NuxeoException;
035import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * Updates the given {@link DocumentModel} with the given blob using the versioning policy of the given
040 * {@link VersioningFileSystemItemFactory}.
041 *
042 * @author Antoine Taillefer
043 * @since 7.4
044 */
045@Operation(id = NuxeoDriveAttachBlob.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Attach blob")
046public class NuxeoDriveAttachBlob {
047
048    public static final String ID = "NuxeoDrive.AttachBlob";
049
050    @Context
051    protected CoreSession session;
052
053    @Param(name = "document")
054    protected DocumentModel doc;
055
056    @Param(name = "applyVersioningPolicy", required = false, values = "false")
057    protected boolean applyVersioningPolicy = false;
058
059    @Param(name = "factoryName", required = false, values = "defaultFileSystemItemFactory")
060    protected String factoryName = "defaultFileSystemItemFactory";
061
062    @OperationMethod
063    public Blob run(Blob blob) {
064        if (applyVersioningPolicy) {
065            FileSystemItemFactory factory = ((FileSystemItemAdapterServiceImpl) Framework.getService(FileSystemItemAdapterService.class)).getFileSystemItemFactory(factoryName);
066            if (!(factory instanceof VersioningFileSystemItemFactory)) {
067                throw new NuxeoException(String.format("Factory %s must implement VersioningFileSystemItemFactory.",
068                        factoryName));
069            }
070            VersioningFileSystemItemFactory versioningFactory = (VersioningFileSystemItemFactory) factory;
071            FileSystemItemHelper.versionIfNeeded(versioningFactory, doc, session);
072        }
073
074        BlobHolder bh = doc.getAdapter(BlobHolder.class);
075        if (bh == null) {
076            throw new NuxeoException(String.format("Document %s is not a BlobHolder, no blob can be attached to it.",
077                    doc.getId()));
078        }
079        bh.setBlob(blob);
080        session.saveDocument(doc);
081        return blob;
082    }
083
084}