NonInteractivePDFRenderService.java 6.49 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemfd.docmanager.Document
 *  com.adobe.forms.option.LCFormsOptions
 *  com.adobe.forms.service.AEMFormsHelperService
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 */
package com.adobe.fd.output.internal;

import com.adobe.aemfd.docmanager.Document;
import com.adobe.fd.forms.internal.utils.Utils;
import com.adobe.fd.output.api.BatchOptions;
import com.adobe.fd.output.api.BatchResult;
import com.adobe.fd.output.internal.XFABatchForPrintService;
import com.adobe.fd.output.internal.XFAPAExecuteService;
import com.adobe.fd.output.internal.utils.DebugDumper;
import com.adobe.forms.option.LCFormsOptions;
import com.adobe.forms.service.AEMFormsHelperService;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;

@Component(label="NonInteractive PDF Render Service", description="renders flat pdf from XDP")
@Service(value={NonInteractivePDFRenderService.class})
public class NonInteractivePDFRenderService {
    @Reference
    AEMFormsHelperService aemFormsHelperService;
    @Reference
    XFAPAExecuteService xfapaExecuteService;
    @Reference
    XFABatchForPrintService xfaNativeBatchService;

    public Document render(byte[] template, Document data, LCFormsOptions lcOptions, String imageResources, boolean signablePDF, boolean retainFormState) throws Exception {
        String[] renderRequestType = lcOptions.getRenderType().split("\\.");
        String renderType = renderRequestType[renderRequestType.length - 1];
        byte[] xciBytes = this.aemFormsHelperService.xciString(lcOptions, renderType).getBytes("utf-8");
        byte[] userXci = this.aemFormsHelperService.userXCI(lcOptions);
        DebugDumper dd = new DebugDumper(lcOptions.getDebugDir());
        dd.dump("template.xml", template);
        dd.dump("data.xml", data);
        dd.dump("xci.xml", xciBytes);
        dd.dump("user_xci.xml", userXci);
        dd.dump("resources.xml", imageResources.getBytes());
        List<byte[]> ret = this.xfapaExecuteService.paExecute(template, retainFormState ? template : Utils.getBytes(data), xciBytes, new byte[0], retainFormState, true, userXci, imageResources.getBytes(), signablePDF, false);
        dd.dump("procInfo.xml", ret.get(0));
        dd.dump("output." + renderType, ret.get(1));
        return new Document(ret.get(1));
    }

    public BatchResult renderBatch(Map<String, String> templates, Map<String, Document> data, LCFormsOptions lcOptions, BatchOptions options) throws Exception {
        LinkedHashMap<String, byte[]> resolveTemplateMap = new LinkedHashMap<String, byte[]>();
        String[] renderRequestType = lcOptions.getRenderType().split("\\.");
        String renderType = renderRequestType[renderRequestType.length - 1];
        LinkedHashMap<String, Document> dataMap = new LinkedHashMap<String, Document>();
        DebugDumper dd = new DebugDumper(lcOptions.getDebugDir());
        int dataCounter = 0;
        int templateCounter = 0;
        for (Map.Entry<String, String> entry : templates.entrySet()) {
            lcOptions.setTemplate(entry.getValue());
            byte[] resolvedTemplate = this.aemFormsHelperService.resolvedTemplate(lcOptions);
            resolveTemplateMap.put(entry.getKey(), resolvedTemplate);
            dd.dump("template_" + entry.getKey() + "_" + templateCounter + ".xml", resolvedTemplate);
            if (data.get(entry.getKey()) != null) {
                Document dataDoc = data.get(entry.getKey());
                dataMap.put(entry.getKey(), dataDoc);
                dd.dump("data_" + entry.getKey() + "_" + dataCounter + ".xml", dataDoc);
                ++dataCounter;
            }
            ++templateCounter;
        }
        byte[] xciBytes = this.aemFormsHelperService.xciString(lcOptions, renderType).replace("<record></record>", "<record>2</record>").getBytes("utf-8");
        byte[] userXci = this.aemFormsHelperService.userXCI(lcOptions);
        dd.dump("xci.xml", xciBytes);
        dd.dump("user_xci.xml", userXci);
        Map<String, Object> ret = this.xfaNativeBatchService.renderBatchForPrint(resolveTemplateMap, dataMap, xciBytes, options.getGenerateManyFiles(), renderType, userXci, new byte[0]);
        BatchResult br = this.toBatchResult(ret);
        if (br.getGeneratedDocs() != null && br.getGeneratedDocs().size() > 0) {
            for (int i = 0; i < br.getGeneratedDocs().size(); ++i) {
                Document genDoc = br.getGeneratedDocs().get(i);
                dd.dump("output_" + i + "." + renderType, genDoc);
            }
        }
        if (br.getMetaDataDoc() != null) {
            dd.dump("metadata.xml", br.getMetaDataDoc());
        }
        return br;
    }

    private BatchResult toBatchResult(Map map) {
        BatchResult br = new BatchResult();
        Object output_list = null;
        if (map.get("metadata") != null) {
            Document metadataDocument = (Document)map.get("metadata");
            br.setMetaDataDoc(metadataDocument);
        }
        if (map.get("batch_output") != null) {
            List outputDocumentList = (List)map.get("batch_output");
            br.setGeneratedDocs(outputDocumentList);
        }
        return br;
    }

    protected void bindAemFormsHelperService(AEMFormsHelperService aEMFormsHelperService) {
        this.aemFormsHelperService = aEMFormsHelperService;
    }

    protected void unbindAemFormsHelperService(AEMFormsHelperService aEMFormsHelperService) {
        if (this.aemFormsHelperService == aEMFormsHelperService) {
            this.aemFormsHelperService = null;
        }
    }

    protected void bindXfapaExecuteService(XFAPAExecuteService xFAPAExecuteService) {
        this.xfapaExecuteService = xFAPAExecuteService;
    }

    protected void unbindXfapaExecuteService(XFAPAExecuteService xFAPAExecuteService) {
        if (this.xfapaExecuteService == xFAPAExecuteService) {
            this.xfapaExecuteService = null;
        }
    }

    protected void bindXfaNativeBatchService(XFABatchForPrintService xFABatchForPrintService) {
        this.xfaNativeBatchService = xFABatchForPrintService;
    }

    protected void unbindXfaNativeBatchService(XFABatchForPrintService xFABatchForPrintService) {
        if (this.xfaNativeBatchService == xFABatchForPrintService) {
            this.xfaNativeBatchService = null;
        }
    }
}