AgreementCreationUtils.java 12.6 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aem.adobesign.recipient.RecipientAuthenticationMethod
 *  com.adobe.aem.adobesign.recipient.RecipientInfo
 *  com.adobe.aem.adobesign.recipient.RecipientPhoneInfo
 *  com.adobe.aem.adobesign.recipient.RecipientSecurityOption
 *  com.adobe.aem.adobesign.recipient.RecipientSetInfo
 *  com.adobe.aem.adobesign.service.AgreementCreationOptions
 *  com.adobe.aem.adobesign.service.AgreementCreationOptions$ReminderFrequency
 *  com.adobe.aem.adobesign.service.AgreementCreationOptions$SignatureFlow
 *  com.adobe.aemfd.docmanager.Document
 *  com.adobe.granite.workflow.WorkflowException
 *  com.adobe.granite.workflow.WorkflowSession
 *  com.adobe.granite.workflow.exec.WorkItem
 *  com.adobe.granite.workflow.exec.WorkflowData
 *  com.adobe.granite.workflow.metadata.MetaDataMap
 *  com.adobe.granite.workflow.model.WorkflowNode
 *  javax.jcr.Session
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.scripting.SlingBindings
 *  org.apache.sling.api.scripting.SlingScript
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.fd.workflow.adobesign.internal.utils;

import com.adobe.aem.adobesign.recipient.RecipientAuthenticationMethod;
import com.adobe.aem.adobesign.recipient.RecipientInfo;
import com.adobe.aem.adobesign.recipient.RecipientPhoneInfo;
import com.adobe.aem.adobesign.recipient.RecipientSecurityOption;
import com.adobe.aem.adobesign.recipient.RecipientSetInfo;
import com.adobe.aem.adobesign.service.AgreementCreationOptions;
import com.adobe.aemfd.docmanager.Document;
import com.adobe.fd.workflow.adobesign.api.RecipientInfoSpecifier;
import com.adobe.fd.workflow.adobesign.impl.AgreementCreation;
import com.adobe.fd.workflow.utils.DocumentUtils;
import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.adobe.granite.workflow.model.WorkflowNode;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import javax.jcr.Session;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.scripting.SlingBindings;
import org.apache.sling.api.scripting.SlingScript;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AgreementCreationUtils {
    private static final Logger log = LoggerFactory.getLogger(AgreementCreation.class);

    public static AgreementCreationOptions getAgreementCreationOptions(WorkItem item, WorkflowSession session, MetaDataMap args, List<RecipientInfoSpecifier> signersInfoSpecifer) throws WorkflowException {
        try {
            AgreementCreationOptions agreementCreationOptions = new AgreementCreationOptions();
            String resource = (String)args.get("signersListScript", String.class);
            List<RecipientSetInfo> recipientSetInfos = null;
            if (((String)args.get("signerDetails", String.class)).equals("Dynamic")) {
                recipientSetInfos = AgreementCreationUtils.getDynamicSignersList(resource, item.getNode(), item, session, signersInfoSpecifer);
            } else if (((String)args.get("signerDetails", String.class)).equals("Static")) {
                recipientSetInfos = AgreementCreationUtils.getStaticSignersList(args);
            }
            if (args.get("name", String.class) == null) {
                throw new WorkflowException("Agreement name cannot be null");
            }
            agreementCreationOptions.setName((String)args.get("name", String.class));
            if (args.get("locale", String.class) != null) {
                agreementCreationOptions.setLocale((String)args.get("locale", String.class));
            }
            if (args.get("daysUntilSigningDeadline", Integer.class) != null) {
                agreementCreationOptions.setDaysUntilSigningDeadline(((Integer)args.get("daysUntilSigningDeadline", Integer.class)).intValue());
            }
            if (args.get("message", String.class) != null) {
                agreementCreationOptions.setMessage((String)args.get("message", String.class));
            }
            if (args.get("reminderFrequency", String.class) != null) {
                agreementCreationOptions.setReminderFrequency(AgreementCreationOptions.ReminderFrequency.valueOf((String)((String)args.get("reminderFrequency", String.class))));
            }
            if (args.get("signatureFlow", String.class) != null) {
                agreementCreationOptions.setSignatureFlow(AgreementCreationOptions.SignatureFlow.valueOf((String)((String)args.get("signatureFlow", String.class))));
            }
            if (args.get("postSigningUrl", String.class) != null) {
                agreementCreationOptions.setPostSigningUrl((String)args.get("postSigningUrl", String.class));
            }
            if (args.get("redirectDelay", Integer.class) != null) {
                agreementCreationOptions.setRedirectDelay(((Integer)args.get("redirectDelay", Integer.class)).intValue());
            }
            agreementCreationOptions.setRecipientSetInfos(recipientSetInfos);
            return agreementCreationOptions;
        }
        catch (Exception e) {
            throw new WorkflowException((Throwable)e);
        }
    }

    private static List<RecipientSetInfo> getStaticSignersList(MetaDataMap args) throws WorkflowException {
        ArrayList<RecipientSetInfo> recipientSetInfos = new ArrayList<RecipientSetInfo>();
        String[] staticSigners = (String[])args.get("staticSigners", String[].class);
        if (staticSigners == null || staticSigners.length == 0) {
            return null;
        }
        for (String signers : staticSigners) {
            try {
                JSONObject obj = new JSONObject(signers);
                RecipientSetInfo recipientSetInfo = new RecipientSetInfo();
                ArrayList<RecipientInfo> recipientInfoList = new ArrayList<RecipientInfo>();
                RecipientInfo recipientInfo = new RecipientInfo();
                RecipientSecurityOption securityOptions = null;
                RecipientAuthenticationMethod recipientAuthenticationMethod = RecipientAuthenticationMethod.NONE;
                String email = (String)obj.get("email");
                String authenticationType = (String)obj.get("authenticationType");
                if (authenticationType.equals("phoneVerification")) {
                    recipientAuthenticationMethod = RecipientAuthenticationMethod.PHONE;
                    String phoneNumber = (String)obj.get("phoneNumber");
                    String countryCode = (String)obj.get("countryCode");
                    RecipientPhoneInfo[] recipientPhoneInfo = new RecipientPhoneInfo[]{new RecipientPhoneInfo(phoneNumber, countryCode)};
                    securityOptions = new RecipientSecurityOption(recipientAuthenticationMethod, recipientPhoneInfo, null);
                } else if (authenticationType.equals("socialLogin")) {
                    recipientAuthenticationMethod = RecipientAuthenticationMethod.WEB_IDENTITY;
                    securityOptions = new RecipientSecurityOption(recipientAuthenticationMethod, null, null);
                } else if (authenticationType.equals("kba")) {
                    recipientAuthenticationMethod = RecipientAuthenticationMethod.KBA;
                    securityOptions = new RecipientSecurityOption(recipientAuthenticationMethod, null, null);
                }
                recipientInfo.setEmail(email);
                recipientInfo.setSecurityOptions(securityOptions);
                recipientInfoList.add(recipientInfo);
                recipientSetInfo.setMemberInfos(recipientInfoList);
                recipientSetInfos.add(recipientSetInfo);
                continue;
            }
            catch (JSONException e1) {
                log.error(e1.getMessage());
                throw new WorkflowException((Throwable)e1);
            }
        }
        return recipientSetInfos;
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    private static List<RecipientSetInfo> getDynamicSignersList(String resource, WorkflowNode node, WorkItem item, WorkflowSession session, List<RecipientInfoSpecifier> signersInfoSpecifer) throws WorkflowException {
        ArrayList recipientSetInfos = new ArrayList<RecipientSetInfo>();
        if (resource == null) {
            throw new WorkflowException("Process resource is null");
        }
        if (resource.equals("")) {
            throw new WorkflowException("Process resource is not defined");
        }
        MetaDataMap metaData = node.getMetaDataMap();
        if (resource.startsWith("/")) {
            Resource scriptResource = null;
            ResourceResolver resolver = (ResourceResolver)session.adaptTo(ResourceResolver.class);
            if (resolver == null) throw new WorkflowException("Resource Resolver cannot be null");
            scriptResource = resolver.getResource(resource);
            if (scriptResource == null) throw new WorkflowException("Could not find script resource: " + resource);
            if (scriptResource.getResourceType().equals("sling:nonexisting")) throw new WorkflowException("Could not find script resource: " + resource);
            SlingScript script = (SlingScript)scriptResource.adaptTo(SlingScript.class);
            if (script == null) return recipientSetInfos;
            SlingBindings props = new SlingBindings();
            props.put((Object)"workItem", (Object)item);
            props.put((Object)"workflowSession", (Object)session);
            props.put((Object)"workflowData", (Object)item.getWorkflowData());
            props.put((Object)"metaData", (Object)node.getMetaDataMap());
            props.put((Object)"jcrSession", resolver.adaptTo(Session.class));
            try {
                System.out.println("Calling script " + (Object)script);
                Object result = script.call(props, "getAdobeSignRecipients", new Object[]{""});
                System.out.println("Result from script " + (Object)script + " is " + result);
                if (result == null) return recipientSetInfos;
                if (!(result instanceof ArrayList)) return recipientSetInfos;
                return (ArrayList)result;
            }
            catch (Exception e) {
                throw new WorkflowException("Script execution resulted in an error: " + e.getMessage());
            }
        }
        Object toBeExecuted = AgreementCreationUtils.evaluateExecutable(resource, signersInfoSpecifer);
        if (toBeExecuted == null) throw new WorkflowException("Signer Specifier implementation not found: " + resource);
        try {
            if (!(toBeExecuted instanceof RecipientInfoSpecifier)) return recipientSetInfos;
            RecipientInfoSpecifier chooser = (RecipientInfoSpecifier)toBeExecuted;
            return chooser.getAdobeSignRecipients(item, session, metaData);
        }
        catch (Exception e) {
            throw new WorkflowException("Signer Specifier execution resulted in an error: " + e.getMessage());
        }
    }

    private static Object evaluateExecutable(String resource, List<RecipientInfoSpecifier> signersInfoSpecifer) {
        for (RecipientInfoSpecifier chooser : signersInfoSpecifer) {
            if (!resource.equals(chooser.getClass().getName())) continue;
            return chooser;
        }
        return null;
    }

    public static Document getInputDoc(WorkItem workItem, MetaDataMap args) throws WorkflowException {
        return DocumentUtils.getInputDocument(workItem, "inputpath");
    }

    public static String getAdobeSignCloudService(MetaDataMap args) throws WorkflowException {
        if (args.get("signConfig", String.class) == null) {
            throw new WorkflowException("Adobe Sign Cloud Configuration is not defined");
        }
        return (String)args.get("signConfig", String.class);
    }

    public static void writeVariabletoMetadata(WorkItem workItem, String key, String value) throws WorkflowException {
        if (key == null) {
            throw new WorkflowException("Key cannot be null");
        }
        String workItemKey = (String)workItem.getNode().getMetaDataMap().get(key, String.class);
        if (workItemKey == null) {
            throw new WorkflowException(key + " is not found at workitem");
        }
        MetaDataMap metaDataMap = workItem.getMetaDataMap();
        metaDataMap.put((Object)workItemKey, (Object)value);
    }
}