001/*
002 * (C) Copyright 2019 Nuxeo (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 *     Thomas Roger
018 */
019package org.nuxeo.ecm.automation.core.operations.services;
020
021import java.io.IOException;
022
023import org.nuxeo.ecm.automation.AutomationService;
024import org.nuxeo.ecm.automation.OperationContext;
025import org.nuxeo.ecm.automation.OperationException;
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.automation.core.util.BlobList;
032import org.nuxeo.ecm.automation.core.util.DocumentHelper;
033import org.nuxeo.ecm.automation.core.util.Properties;
034import org.nuxeo.ecm.core.api.Blob;
035import org.nuxeo.ecm.core.api.CoreSession;
036import org.nuxeo.ecm.core.api.DocumentModel;
037import org.nuxeo.ecm.core.api.DocumentModelList;
038import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
039import org.nuxeo.ecm.platform.filemanager.api.FileImporterContext;
040import org.nuxeo.ecm.platform.filemanager.api.FileManager;
041
042/**
043 * Use {@link FileManager} to create documents from blobs and set multiple properties on them.
044 *
045 * @since 10.10
046 */
047@Operation(id = FileManagerImportWithProperties.ID, category = Constants.CAT_SERVICES, label = "Create Document from file", description = "Create Document(s) from Blob(s) using the FileManagerService and set multiple properties on them."
048        + "The destination container must be passed in a Context variable named currentDocument. "
049        + "<p>The properties are specified as <i>key=value</i> pairs separated by a new line. "
050        + "The key used for a property is the property xpath. "
051        + "To specify multi-line values you can use a \\ character followed by a new line. "
052        + "<p>Example:<pre>dc:title=The Document Title<br>dc:description=foo bar</pre>"
053        + "For updating a date, you will need to expose the value as ISO 8601 format, "
054        + "for instance : <p>Example:<pre>dc:title=The Document Title<br>dc:issued=@{org.nuxeo.ecm.core.schema.utils.DateParser.formatW3CDateTime(CurrentDate.date)}</pre><p>Returns back the updated document."
055        + "<p>To update a multi-valued field with multiple values:<pre>custom:multivalued=a,b,c,d</pre>")
056public class FileManagerImportWithProperties {
057
058    public static final String ID = "FileManager.ImportWithProperties";
059
060    @Context
061    protected CoreSession session;
062
063    @Context
064    protected FileManager fileManager;
065
066    @Context
067    protected AutomationService as;
068
069    @Context
070    protected OperationContext context;
071
072    @Param(name = "overwrite", required = false, description = "Whether to overwrite an existing file with the same title, defaults to false")
073    protected boolean overwrite = false;
074
075    @Param(name = "mimeTypeCheck", required = false, description = "Whether to check the blob's mime-type against the file name, defaults to true")
076    protected boolean mimeTypeCheck = true;
077
078    @Param(name = "properties")
079    protected Properties properties;
080
081    protected DocumentModel getCurrentDocument() throws OperationException {
082        String cdRef = (String) context.get("currentDocument");
083        return as.getAdaptedValue(context, cdRef, DocumentModel.class);
084    }
085
086    @OperationMethod
087    public DocumentModel run(Blob blob) throws OperationException, IOException {
088        DocumentModel currentDocument = getCurrentDocument();
089        String path = currentDocument.getPathAsString();
090        FileImporterContext fileCreationContext = FileImporterContext.builder(session, blob, path)
091                                                                     .overwrite(overwrite)
092                                                                     .mimeTypeCheck(mimeTypeCheck)
093                                                                     .persistDocument(false)
094                                                                     .build();
095        DocumentModel doc = fileManager.createOrUpdateDocument(fileCreationContext);
096        DocumentHelper.setProperties(session, doc, properties);
097
098        if (doc.isDirty()) {
099            doc = doc.getId() == null ? session.createDocument(doc) : session.saveDocument(doc);
100        }
101
102        return doc;
103    }
104
105    @OperationMethod
106    public DocumentModelList run(BlobList blobs) throws OperationException, IOException {
107        DocumentModelList result = new DocumentModelListImpl();
108        for (Blob blob : blobs) {
109            result.add(run(blob));
110        }
111        return result;
112    }
113
114}