001/*
002 * (C) Copyright 2006-2007 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;
040
041/**
042 * Use {@link FileManager} to create documents from blobs
043 *
044 * @author Tiry (tdelprat@nuxeo.com)
045 */
046@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.")
047public class FileManagerImport {
048
049    public static final String ID = "FileManager.Import";
050
051    @Context
052    protected CoreSession session;
053
054    @Context
055    protected FileManager fileManager;
056
057    @Context
058    protected AutomationService as;
059
060    @Context
061    protected OperationContext context;
062
063    @Param(name = "overwite", required = false)
064    protected Boolean overwite = false;
065
066    @Param(name = "noMimeTypeCheck", required = false)
067    protected Boolean noMimeTypeCheck = false;
068
069    protected DocumentModel getCurrentDocument() throws OperationException {
070        String cdRef = (String) context.get("currentDocument");
071        return as.getAdaptedValue(context, cdRef, DocumentModel.class);
072    }
073
074    @OperationMethod
075    public DocumentModel run(Blob blob) throws OperationException, IOException {
076        DocumentModel currentDocument = getCurrentDocument();
077        String path = currentDocument.getPathAsString();
078        FileImporterContext fileCreationContext = FileImporterContext.builder(session, blob, path)
079                                                                     .overwrite(overwite)
080                                                                     .mimeTypeCheck(!noMimeTypeCheck)
081                                                                     .build();
082        return fileManager.createOrUpdateDocument(fileCreationContext);
083    }
084
085    @OperationMethod
086    public DocumentModelList run(BlobList blobs) throws OperationException, IOException {
087        DocumentModelList result = new DocumentModelListImpl();
088        for (Blob blob : blobs) {
089            result.add(run(blob));
090        }
091        return result;
092    }
093
094}