AdobeSignStatusHandlerImpl.java 12.6 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aem.adobesign.handler.AdobeSignHandlerObject
 *  com.adobe.aem.adobesign.handler.AdobeSignStatusHandler
 *  com.adobe.aem.adobesign.recipient.ParticipantInfo
 *  com.adobe.aem.adobesign.recipient.ParticipantSetInfo
 *  com.adobe.aem.adobesign.service.AdobeSignServiceException
 *  com.adobe.aem.adobesign.service.AgreementStatusOptions
 *  com.adobe.aem.adobesign.service.AgreementStatusResponse
 *  com.adobe.aem.adobesign.service.AgreementStatusResponse$AgreementStatus
 *  com.adobe.aemfd.docmanager.Document
 *  com.adobe.granite.resourceresolverhelper.ResourceResolverHelper
 *  com.adobe.granite.workflow.WorkflowException
 *  com.adobe.granite.workflow.WorkflowSession
 *  com.adobe.granite.workflow.collection.util.ResultSet
 *  com.adobe.granite.workflow.exec.Route
 *  com.adobe.granite.workflow.exec.WorkItem
 *  com.adobe.granite.workflow.exec.filter.WorkItemFilter
 *  com.adobe.granite.workflow.metadata.MetaDataMap
 *  com.adobe.granite.workflow.model.WorkflowNode
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.LoginException
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.fd.workflow.adobesign.ItemStatus;

import com.adobe.aem.adobesign.handler.AdobeSignHandlerObject;
import com.adobe.aem.adobesign.handler.AdobeSignStatusHandler;
import com.adobe.aem.adobesign.recipient.ParticipantInfo;
import com.adobe.aem.adobesign.recipient.ParticipantSetInfo;
import com.adobe.aem.adobesign.service.AdobeSignServiceException;
import com.adobe.aem.adobesign.service.AgreementStatusOptions;
import com.adobe.aem.adobesign.service.AgreementStatusResponse;
import com.adobe.aemfd.docmanager.Document;
import com.adobe.fd.workflow.adobesign.ItemStatus.AdobeSignWorkItemFilter;
import com.adobe.fd.workflow.adobesign.internal.utils.AgreementCreationUtils;
import com.adobe.fd.workflow.utils.CommonUtils;
import com.adobe.fd.workflow.utils.DocumentUtils;
import com.adobe.granite.resourceresolverhelper.ResourceResolverHelper;
import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.collection.util.ResultSet;
import com.adobe.granite.workflow.exec.Route;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.filter.WorkItemFilter;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.adobe.granite.workflow.model.WorkflowNode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(immediate=1)
@Service(value={AdobeSignStatusHandler.class})
public class AdobeSignStatusHandlerImpl
implements AdobeSignStatusHandler {
    private final Logger logger = LoggerFactory.getLogger(AdobeSignStatusHandlerImpl.class);
    @Reference
    private SlingRepository slingRepository;
    @Reference
    private ResourceResolverFactory resourceResolverFactory;
    @Reference
    private ResourceResolverHelper resourceResolverHelper;

    public Map<String, AdobeSignHandlerObject> getPendingAgreements() throws AdobeSignServiceException {
        ResourceResolver resourceResolver = null;
        try {
            resourceResolver = CommonUtils.getFnDServiceUserResourceResolver(this.resourceResolverFactory);
        }
        catch (LoginException e1) {
            this.logger.error("Exception while login via fd-user");
            throw new AdobeSignServiceException((Throwable)e1);
        }
        WorkflowSession graniteWorkflowSession = (WorkflowSession)resourceResolver.adaptTo(WorkflowSession.class);
        AdobeSignWorkItemFilter filter = new AdobeSignWorkItemFilter();
        ResultSet workItems = null;
        try {
            workItems = graniteWorkflowSession.getActiveWorkItems(0, -1, (WorkItemFilter)filter);
        }
        catch (WorkflowException e) {
            throw new AdobeSignServiceException((Throwable)e);
        }
        finally {
            if (resourceResolver != null) {
                resourceResolver.close();
            }
        }
        WorkItem[] allWorkflowsItems = null;
        if (workItems != null) {
            allWorkflowsItems = (WorkItem[])workItems.getItems();
        }
        HashMap<String, AdobeSignHandlerObject> agreements = new HashMap<String, AdobeSignHandlerObject>();
        for (WorkItem pendingItems : allWorkflowsItems) {
            AdobeSignHandlerObject handlerObj = new AdobeSignHandlerObject();
            handlerObj.getStatusOptions().setFetchSignedDocument(true);
            handlerObj.getStatusOptions().setFetchAttachments(false);
            handlerObj.getStatusOptions().setFetchFormData(true);
            String signConfig = null;
            if (pendingItems.getNode().getMetaDataMap().get("signConfig", String.class) != null) {
                signConfig = (String)pendingItems.getNode().getMetaDataMap().get("signConfig", String.class);
            }
            handlerObj.setSignConfig(signConfig);
            handlerObj.setSystemId(pendingItems.getId());
            agreements.put((String)pendingItems.getMetaDataMap().get("agreementId", String.class), handlerObj);
        }
        return agreements;
    }

    public Map<String, Object> updateAgreementStatus(String systemId, AgreementStatusResponse asr) throws AdobeSignServiceException {
        block26 : {
            Session _session;
            ResourceResolver resourceResolver = null;
            try {
                resourceResolver = CommonUtils.getFnDServiceUserResourceResolver(this.resourceResolverFactory);
            }
            catch (LoginException e1) {
                this.logger.error("Exception while login via fd-user");
                throw new AdobeSignServiceException((Throwable)e1);
            }
            WorkflowSession graniteWorkflowSession = (WorkflowSession)resourceResolver.adaptTo(WorkflowSession.class);
            try {
                _session = CommonUtils.getFDServiceUserSession(this.slingRepository);
            }
            catch (RepositoryException e1) {
                this.logger.error("Exception while getting session from sling repository");
                throw new AdobeSignServiceException((Throwable)e1);
            }
            try {
                if (StringUtils.isEmpty((String)systemId)) {
                    throw new AdobeSignServiceException("System Id must be specified for updating e sign properties.");
                }
                WorkItem currentWorkItem = graniteWorkflowSession.getWorkItem(systemId);
                if (asr == null) {
                    this.logger.info("Respone return from scheduler is null");
                    Map<String, Object> map = null;
                    return map;
                }
                String inputId = asr.getAgreementId();
                String storedId = null;
                storedId = (String)currentWorkItem.getMetaDataMap().get("agreementId", String.class);
                if (!storedId.equals(inputId)) {
                    this.logger.error("Cannot update sign status properties with differing agreement Id.");
                    throw new AdobeSignServiceException("Cannot update sign status properties with differing agreement Id.");
                }
                if (asr.getAgreementStatus() != AgreementStatusResponse.AgreementStatus.OUT_FOR_SIGNATURE) {
                    if (asr.getAgreementStatus() == AgreementStatusResponse.AgreementStatus.SIGNED) {
                        String payload = CommonUtils.getPayloadPath(currentWorkItem);
                        MetaDataMap metaDataMap = currentWorkItem.getNode().getMetaDataMap();
                        String locUnderPayload = (String)metaDataMap.get("output", String.class);
                        DocumentUtils.createOrUpdateDocument(_session, new Document(asr.getSignedDocument()), payload, locUnderPayload);
                        AgreementCreationUtils.writeVariabletoMetadata(currentWorkItem, "variableForStatus", asr.getAgreementStatus().toString());
                        List routes = graniteWorkflowSession.getRoutes(currentWorkItem, false);
                        Route route = null;
                        if (routes.size() > 0) {
                            for (Route r : routes) {
                                if (!r.hasDefault()) continue;
                                route = r;
                            }
                        }
                        if (route == null) {
                            route = (Route)routes.get(0);
                        }
                        graniteWorkflowSession.complete(currentWorkItem, route);
                    } else {
                        AgreementCreationUtils.writeVariabletoMetadata(currentWorkItem, "variableForStatus", asr.getAgreementStatus().toString());
                        List routes = graniteWorkflowSession.getRoutes(currentWorkItem, false);
                        Route route = null;
                        if (routes.size() > 0) {
                            for (Route r : routes) {
                                if (!r.hasDefault()) continue;
                                route = r;
                            }
                        }
                        if (route == null) {
                            route = (Route)routes.get(0);
                        }
                        graniteWorkflowSession.complete(currentWorkItem, route);
                    }
                    _session.save();
                    break block26;
                }
                ArrayList<String> nextEmails = new ArrayList<String>();
                List nextParticipants = asr.getNextParticipantSets();
                for (ParticipantSetInfo particiants : nextParticipants) {
                    List participantInfo = particiants.getMembers();
                    for (ParticipantInfo eachParticipantInfo : participantInfo) {
                        String email = eachParticipantInfo.getEmail();
                        nextEmails.add(email);
                    }
                }
                String[] emails = nextEmails.toArray(new String[0]);
                Node _node = _session.getNode(currentWorkItem.getId() + "/metaData");
                _node.setProperty("nextParticipants", emails);
                _session.save();
            }
            catch (AdobeSignServiceException e) {
                throw e;
            }
            catch (Exception e) {
                throw new AdobeSignServiceException((Throwable)e);
            }
            finally {
                if (resourceResolver != null) {
                    resourceResolver.close();
                }
            }
        }
        return null;
    }

    protected void bindSlingRepository(SlingRepository slingRepository) {
        this.slingRepository = slingRepository;
    }

    protected void unbindSlingRepository(SlingRepository slingRepository) {
        if (this.slingRepository == slingRepository) {
            this.slingRepository = null;
        }
    }

    protected void bindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        this.resourceResolverFactory = resourceResolverFactory;
    }

    protected void unbindResourceResolverFactory(ResourceResolverFactory resourceResolverFactory) {
        if (this.resourceResolverFactory == resourceResolverFactory) {
            this.resourceResolverFactory = null;
        }
    }

    protected void bindResourceResolverHelper(ResourceResolverHelper resourceResolverHelper) {
        this.resourceResolverHelper = resourceResolverHelper;
    }

    protected void unbindResourceResolverHelper(ResourceResolverHelper resourceResolverHelper) {
        if (this.resourceResolverHelper == resourceResolverHelper) {
            this.resourceResolverHelper = null;
        }
    }
}