001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Antoine Taillefer <ataillefer@nuxeo.com>
016 */
017package org.nuxeo.drive.operations;
018
019import org.nuxeo.drive.adapter.impl.FileSystemItemHelper;
020import org.nuxeo.drive.service.FileSystemItemAdapterService;
021import org.nuxeo.drive.service.FileSystemItemFactory;
022import org.nuxeo.drive.service.VersioningFileSystemItemFactory;
023import org.nuxeo.drive.service.impl.FileSystemItemAdapterServiceImpl;
024import org.nuxeo.ecm.automation.core.Constants;
025import org.nuxeo.ecm.automation.core.annotations.Context;
026import org.nuxeo.ecm.automation.core.annotations.Operation;
027import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
028import org.nuxeo.ecm.automation.core.annotations.Param;
029import org.nuxeo.ecm.core.api.Blob;
030import org.nuxeo.ecm.core.api.CoreSession;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.NuxeoException;
033import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * Updates the given {@link DocumentModel} with the given blob using the versioning policy of the given
038 * {@link VersioningFileSystemItemFactory}.
039 *
040 * @author Antoine Taillefer
041 * @since 7.4
042 */
043@Operation(id = NuxeoDriveAttachBlob.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Attach blob")
044public class NuxeoDriveAttachBlob {
045
046    public static final String ID = "NuxeoDrive.AttachBlob";
047
048    @Context
049    protected CoreSession session;
050
051    @Param(name = "document")
052    protected DocumentModel doc;
053
054    @Param(name = "applyVersioningPolicy", required = false, values = "false")
055    protected boolean applyVersioningPolicy = false;
056
057    @Param(name = "factoryName", required = false, values = "defaultFileSystemItemFactory")
058    protected String factoryName = "defaultFileSystemItemFactory";
059
060    @OperationMethod
061    public Blob run(Blob blob) {
062        if (applyVersioningPolicy) {
063            FileSystemItemFactory factory = ((FileSystemItemAdapterServiceImpl) Framework.getService(FileSystemItemAdapterService.class)).getFileSystemItemFactory(factoryName);
064            if (!(factory instanceof VersioningFileSystemItemFactory)) {
065                throw new NuxeoException(String.format("Factory %s must implement VersioningFileSystemItemFactory.",
066                        factoryName));
067            }
068            VersioningFileSystemItemFactory versioningFactory = (VersioningFileSystemItemFactory) factory;
069            FileSystemItemHelper.versionIfNeeded(versioningFactory, doc, session);
070        }
071
072        BlobHolder bh = doc.getAdapter(BlobHolder.class);
073        if (bh == null) {
074            throw new NuxeoException(String.format("Document %s is not a BlobHolder, no blob can be attached to it.",
075                    doc.getId()));
076        }
077        bh.setBlob(blob);
078        session.saveDocument(doc);
079        return blob;
080    }
081
082}