001/*
002 * (C) Copyright 2006-2020 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 *     Nuxeo - initial API and implementation
018 *
019 */
020package org.nuxeo.ecm.automation.core.operations.services;
021
022import java.io.IOException;
023
024import org.nuxeo.ecm.automation.AutomationService;
025import org.nuxeo.ecm.automation.OperationContext;
026import org.nuxeo.ecm.automation.OperationException;
027import org.nuxeo.ecm.automation.core.Constants;
028import org.nuxeo.ecm.automation.core.annotations.Context;
029import org.nuxeo.ecm.automation.core.annotations.Operation;
030import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
031import org.nuxeo.ecm.automation.core.annotations.Param;
032import org.nuxeo.ecm.automation.core.util.BlobList;
033import org.nuxeo.ecm.core.api.Blob;
034import org.nuxeo.ecm.core.api.CoreSession;
035import org.nuxeo.ecm.core.api.DocumentModel;
036import org.nuxeo.ecm.core.api.DocumentModelList;
037import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
038import org.nuxeo.ecm.platform.filemanager.api.FileImporterContext;
039import org.nuxeo.ecm.platform.filemanager.api.FileManager;
040import org.nuxeo.runtime.transaction.TransactionHelper;
041
042/**
043 * Use {@link FileManager} to create documents from blobs
044 *
045 * @author Tiry (tdelprat@nuxeo.com)
046 */
047@Operation(id = FileManagerImport.ID, category = Constants.CAT_SERVICES, label = "Create Document from file", description = "Create Document(s) from Blob(s) using the FileManagerService. The destination container must be passed in a Context variable named currentDocument.")
048public class FileManagerImport {
049
050    public static final String ID = "FileManager.Import";
051
052    protected static final int IMPORT_TX_TIMEOUT_SEC = 86_400; // 1 day
053
054    @Context
055    protected CoreSession session;
056
057    @Context
058    protected FileManager fileManager;
059
060    @Context
061    protected AutomationService as;
062
063    @Context
064    protected OperationContext context;
065
066    /** @deprecated since 11.2, use overwrite instead. No more used. */
067    @Param(name = "overwite", required = false)
068    @Deprecated(since = "11.2")
069    protected Boolean overwite = false;
070
071    /** @since 11.2 */
072    @Param(name = "overwrite", alias = "overwite", required = false)
073    protected Boolean overwrite = false;
074
075    @Param(name = "noMimeTypeCheck", required = false)
076    protected Boolean noMimeTypeCheck = false;
077
078    protected DocumentModel getCurrentDocument() throws OperationException {
079        String cdRef = (String) context.get("currentDocument");
080        return as.getAdaptedValue(context, cdRef, DocumentModel.class);
081    }
082
083    @OperationMethod
084    public DocumentModel run(Blob blob) throws OperationException, IOException {
085        // do the import in a long-running transaction to avoid timeouts
086        TransactionHelper.commitOrRollbackTransaction();
087        TransactionHelper.startTransaction(IMPORT_TX_TIMEOUT_SEC);
088        DocumentModel currentDocument = getCurrentDocument();
089        String path = currentDocument.getPathAsString();
090        FileImporterContext fileCreationContext = FileImporterContext.builder(session, blob, path)
091                                                                     .overwrite(overwrite)
092                                                                     .mimeTypeCheck(!noMimeTypeCheck)
093                                                                     .build();
094        DocumentModel doc = fileManager.createOrUpdateDocument(fileCreationContext);
095        // go back to default transaction timeout
096        TransactionHelper.commitOrRollbackTransaction();
097        TransactionHelper.startTransaction();
098        return doc;
099    }
100
101    @OperationMethod
102    public DocumentModelList run(BlobList blobs) throws OperationException, IOException {
103        DocumentModelList result = new DocumentModelListImpl();
104        for (Blob blob : blobs) {
105            result.add(run(blob));
106        }
107        return result;
108    }
109
110}