001/*
002 * (C) Copyright 2016 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 *     Thibaud Arguillere
018 *     Miguel Nixo
019 */
020package org.nuxeo.ecm.platform.pdf.operations;
021
022import java.io.IOException;
023import org.apache.pdfbox.exceptions.COSVisitorException;
024import org.nuxeo.ecm.automation.OperationContext;
025import org.nuxeo.ecm.automation.core.Constants;
026import org.nuxeo.ecm.automation.core.annotations.Context;
027import org.nuxeo.ecm.automation.core.annotations.Operation;
028import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
029import org.nuxeo.ecm.automation.core.annotations.Param;
030import org.nuxeo.ecm.automation.core.util.BlobList;
031import org.nuxeo.ecm.core.api.Blob;
032import org.nuxeo.ecm.core.api.NuxeoException;
033import org.nuxeo.ecm.core.api.CoreSession;
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.ecm.core.api.DocumentModelList;
036import org.nuxeo.ecm.platform.pdf.PDFMerge;
037
038/**
039 * The input document(s) always is(are) the first PDF(s), and each pdf is read in the <code>xpath</code> field. If
040 * <code>xpath</xpath> is not set, it is set to the default value <code>file:content</code>.
041 * <p>
042 * The operation appends:
043 * <ul>
044 * <li>First, The blob referenced in the <code>toAppendVarName</code> Context variable.</li>
045 * <li>Then, it appends all the blobs stored in the <code>toAppendListVarName</code> Context variable.</li>
046 * <li>And last, it appends the blobs stored in the docs whose IDs are passed in <code>toAppendDocIDsVarName</code>,
047 * using the <code>xpath</code> parameter.</li>
048 * </ul>
049 * <p>
050 * All variable names are optional: You can pass only <code>toAppendVarName</code>, or <code>toAppendVarName</code> and
051 * <code>toAppendDocIDsVarName</code>, or ...
052 * <p>
053 * Returns the final PDF.
054 *
055 * @since 8.10
056 */
057@Operation(id = PDFMergeDocumentsOperation.ID, category = Constants.CAT_CONVERSION,
058    label = "PDF: Merge with Document(s)", description = "The input document(s) always is(are) the first PDFs, and " +
059    "their PDF is read in the <code>xpath</code> field (but it is ok for the input doc to have no blob). The " +
060    "operation appends the blob referenced in the <code>toAppendVarName</code> Context variable. It then appends all " +
061    "the blobs stored in the <code>toAppendListVarName</code> Context variable. It then append the blobs stored in " +
062    "the docs whose IDs are passed in <code>toAppendDocIDsVarName</code> (the same <code>xpath</code> is used). " +
063    "Returns the final PDF.")
064public class PDFMergeDocumentsOperation {
065
066    public static final String ID = "PDF.MergeWithDocs";
067
068    @Context
069    protected CoreSession session;
070
071    @Context
072    protected OperationContext ctx;
073
074    @Param(name = "xpath", required = false, values = { "file:content" })
075    protected String xpath = "file:content";
076
077    @Param(name = "toAppendVarName", required = false)
078    protected String toAppendVarName = "";
079
080    @Param(name = "toAppendListVarName", required = false)
081    protected String toAppendListVarName = "";
082
083    @Param(name = "toAppendDocIDsVarName", required = false)
084    protected String toAppendDocIDsVarName = "";
085
086    @Param(name = "fileName", required = false)
087    protected String fileName = "";
088
089    @Param(name = "pdfTitle", required = false)
090    protected String pdfTitle = "";
091
092    @Param(name = "pdfSubject", required = false)
093    protected String pdfSubject = "";
094
095    @Param(name = "pdfAuthor", required = false)
096    protected String pdfAuthor = "";
097
098    @OperationMethod
099    public Blob run(DocumentModel inDoc) throws NuxeoException {
100        PDFMerge pdfm = new PDFMerge(inDoc, xpath);
101        return doMerge(pdfm);
102    }
103
104    @OperationMethod
105    public Blob run(DocumentModelList inDocs) throws NuxeoException {
106        PDFMerge pdfm = new PDFMerge(inDocs, xpath);
107        return doMerge(pdfm);
108    }
109
110    protected Blob doMerge(PDFMerge inMergeTool) throws NuxeoException {
111        // Append the single blob
112        if (toAppendVarName != null && !toAppendVarName.isEmpty()) {
113            inMergeTool.addBlob((Blob) ctx.get(toAppendVarName));
114        }
115        // Append the blob list
116        if (toAppendListVarName != null && !toAppendListVarName.isEmpty()) {
117            if (ctx.get(toAppendListVarName) instanceof BlobList) {
118                inMergeTool.addBlobs((BlobList) ctx.get(toAppendListVarName));
119            } else {
120                throw new NuxeoException(ctx.get(toAppendListVarName).getClass() + " is not a Collection");
121            }
122        }
123        // Append a list of Documents via their IDs
124        if (toAppendDocIDsVarName != null && !toAppendDocIDsVarName.isEmpty()) {
125            if (ctx.get(toAppendDocIDsVarName) instanceof String[]) {
126                inMergeTool.addBlobs((String[]) ctx.get(toAppendDocIDsVarName), xpath, session);
127            } else {
128                throw new NuxeoException(ctx.get(toAppendDocIDsVarName).getClass() + " is not a String[]");
129            }
130        }
131        // Merge
132        try {
133            return inMergeTool.merge(fileName, pdfTitle, pdfSubject, pdfAuthor);
134        } catch (COSVisitorException | IOException e) {
135            throw new NuxeoException(e);
136        }
137    }
138
139}