AgreementCreationUtils.java
12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* 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);
}
}