|
| 1 | +package com.docusign.controller.connectedFields.services; |
| 2 | + |
| 3 | +import com.docusign.controller.eSignature.examples.EnvelopeHelpers; |
| 4 | +import com.docusign.esign.api.EnvelopesApi; |
| 5 | +import com.docusign.esign.client.ApiException; |
| 6 | +import com.docusign.esign.model.ConnectionInstance; |
| 7 | +import com.docusign.esign.model.Document; |
| 8 | +import com.docusign.esign.model.EnvelopeDefinition; |
| 9 | +import com.docusign.esign.model.EnvelopeSummary; |
| 10 | +import com.docusign.esign.model.ExtensionData; |
| 11 | +import com.docusign.esign.model.Recipients; |
| 12 | +import com.docusign.esign.model.SignHere; |
| 13 | +import com.docusign.esign.model.Signer; |
| 14 | +import com.docusign.esign.model.Tabs; |
| 15 | +import com.docusign.esign.model.Text; |
| 16 | +import com.fasterxml.jackson.databind.JsonNode; |
| 17 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 18 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | +import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.net.URI; |
| 23 | +import java.net.http.HttpClient; |
| 24 | +import java.net.http.HttpRequest; |
| 25 | +import java.net.http.HttpResponse; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +public class SetConnectedFieldsService { |
| 33 | + private static final String ACTION_CONTRACT = "actionContract"; |
| 34 | + |
| 35 | + private static final String TAB_LABEL = "tabLabel"; |
| 36 | + |
| 37 | + private static final String APPLICATION_NAME = "applicationName"; |
| 38 | + |
| 39 | + private static final String EXTENSION_DATA = "extensionData"; |
| 40 | + |
| 41 | + private static final String TABS = "tabs"; |
| 42 | + |
| 43 | + private static final String APP_ID = "appId"; |
| 44 | + |
| 45 | + private static final String EXPECTED_A_JSON_ARRAY = "Expected a JSON array"; |
| 46 | + |
| 47 | + private static final HttpClient client = HttpClient.newHttpClient(); |
| 48 | + |
| 49 | + private static final ObjectMapper objectMapper = new ObjectMapper(); |
| 50 | + |
| 51 | + private static final String PDF_DOCUMENT_FILE_NAME = "World_Wide_Corp_lorem.pdf"; |
| 52 | + |
| 53 | + private static final String PDF_DOCUMENT_NAME = "Lorem Ipsum"; |
| 54 | + |
| 55 | + public static EnvelopeSummary signingViaEmail( |
| 56 | + EnvelopesApi envelopesApi, |
| 57 | + String accountId, |
| 58 | + EnvelopeDefinition envelope) throws ApiException { |
| 59 | + return envelopesApi.createEnvelope(accountId, envelope); |
| 60 | + } |
| 61 | + |
| 62 | + public static String getConnectedFieldsTabGroups(String accountId, String accessToken) throws Exception { |
| 63 | + String url = String.format( |
| 64 | + "https://api-d.docusign.com/v1/accounts/%s/connected-fields/tab-groups", |
| 65 | + accountId); |
| 66 | + |
| 67 | + HttpRequest request = HttpRequest.newBuilder() |
| 68 | + .uri(URI.create(url)) |
| 69 | + .GET() |
| 70 | + .header("Authorization", "Bearer " + accessToken) |
| 71 | + .header("Accept", "application/json") |
| 72 | + .build(); |
| 73 | + |
| 74 | + try { |
| 75 | + HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 76 | + |
| 77 | + if (response.statusCode() >= 200 && response.statusCode() < 300) { |
| 78 | + return response.body(); |
| 79 | + } else { |
| 80 | + throw new IOException("Unexpected response code: " + response.statusCode()); |
| 81 | + } |
| 82 | + } catch (IOException | InterruptedException e) { |
| 83 | + throw new Exception("DocuSign API Request failed: " + e.getMessage(), e); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public static EnvelopeDefinition makeEnvelope(String signerEmail, String signerName, JsonNode selectedApp) |
| 88 | + throws Exception { |
| 89 | + String appId = selectedApp.has(APP_ID) ? selectedApp.get(APP_ID).asText() : ""; |
| 90 | + JsonNode tabLabels = selectedApp.get(TABS); |
| 91 | + |
| 92 | + EnvelopeDefinition envelopeDefinition = new EnvelopeDefinition(); |
| 93 | + envelopeDefinition.setEmailSubject("Please sign this document set"); |
| 94 | + envelopeDefinition.setStatus("sent"); |
| 95 | + |
| 96 | + Document document = EnvelopeHelpers.createDocumentFromFile( |
| 97 | + PDF_DOCUMENT_FILE_NAME, |
| 98 | + PDF_DOCUMENT_NAME, |
| 99 | + "1"); |
| 100 | + |
| 101 | + envelopeDefinition.setDocuments(Collections.singletonList(document)); |
| 102 | + |
| 103 | + Signer signer = new Signer(); |
| 104 | + signer.setEmail(signerEmail); |
| 105 | + signer.setName(signerName); |
| 106 | + signer.setRecipientId("1"); |
| 107 | + signer.setRoutingOrder("1"); |
| 108 | + |
| 109 | + SignHere signHere = new SignHere(); |
| 110 | + signHere.setAnchorString("/sn1/"); |
| 111 | + signHere.setAnchorUnits("pixels"); |
| 112 | + signHere.setAnchorYOffset("10"); |
| 113 | + signHere.setAnchorXOffset("20"); |
| 114 | + |
| 115 | + List<Text> textTabs = new ArrayList<Text>(); |
| 116 | + |
| 117 | + if (tabLabels != null && tabLabels.isArray()) { |
| 118 | + for (JsonNode tab : tabLabels) { |
| 119 | + JsonNode extensionData = tab.get(EXTENSION_DATA); |
| 120 | + |
| 121 | + String connectionKey = ""; |
| 122 | + String connectionValue = ""; |
| 123 | + |
| 124 | + JsonNode connectionInstances = extensionData != null ? extensionData.get("connectionInstances") : null; |
| 125 | + if (connectionInstances != null && connectionInstances.isArray() && connectionInstances.size() > 0) { |
| 126 | + JsonNode firstInstance = connectionInstances.get(0); |
| 127 | + connectionKey = getText(firstInstance, "connectionKey"); |
| 128 | + connectionValue = getText(firstInstance, "connectionValue"); |
| 129 | + } |
| 130 | + |
| 131 | + String extensionGroupId = getText(extensionData, "extensionGroupId"); |
| 132 | + String publisherName = getText(extensionData, "publisherName"); |
| 133 | + String applicationName = getText(extensionData, APPLICATION_NAME); |
| 134 | + String actionName = getText(extensionData, "actionName"); |
| 135 | + String actionInputKey = getText(extensionData, "actionInputKey"); |
| 136 | + String actionContract = getText(extensionData, ACTION_CONTRACT); |
| 137 | + String extensionName = getText(extensionData, "extensionName"); |
| 138 | + String extensionContract = getText(extensionData, "extensionContract"); |
| 139 | + String requiredForExtension = getText(extensionData, "requiredForExtension"); |
| 140 | + String tabLabel = getText(tab, TAB_LABEL); |
| 141 | + |
| 142 | + Text textTab = new Text(); |
| 143 | + textTab.setRequireInitialOnSharedChange("false"); |
| 144 | + textTab.setRequireAll("false"); |
| 145 | + textTab.setName(applicationName); |
| 146 | + textTab.setRequired("false"); |
| 147 | + textTab.setLocked("false"); |
| 148 | + textTab.setDisableAutoSize("false"); |
| 149 | + textTab.setMaxLength("4000"); |
| 150 | + textTab.setTabLabel(tabLabel); |
| 151 | + textTab.setFont("lucidaconsole"); |
| 152 | + textTab.setFontColor("black"); |
| 153 | + textTab.setFontSize("size9"); |
| 154 | + textTab.setDocumentId("1"); |
| 155 | + textTab.setRecipientId("1"); |
| 156 | + textTab.setPageNumber("1"); |
| 157 | + textTab.setXPosition("273"); |
| 158 | + textTab.setYPosition("191"); |
| 159 | + textTab.setWidth("84"); |
| 160 | + textTab.setHeight("22"); |
| 161 | + textTab.setTemplateRequired("false"); |
| 162 | + textTab.setTabType("text"); |
| 163 | + |
| 164 | + ExtensionData extension = new ExtensionData(); |
| 165 | + extension.setExtensionGroupId(extensionGroupId); |
| 166 | + extension.setPublisherName(publisherName); |
| 167 | + extension.setApplicationId(appId); |
| 168 | + extension.setApplicationName(applicationName); |
| 169 | + extension.setActionName(actionName); |
| 170 | + extension.setActionContract(actionContract); |
| 171 | + extension.setExtensionName(extensionName); |
| 172 | + extension.setExtensionContract(extensionContract); |
| 173 | + extension.setRequiredForExtension(requiredForExtension); |
| 174 | + extension.setActionInputKey(actionInputKey); |
| 175 | + extension.setExtensionPolicy("None"); |
| 176 | + |
| 177 | + ConnectionInstance connectionInstance = new ConnectionInstance(); |
| 178 | + connectionInstance.setConnectionKey(connectionKey); |
| 179 | + connectionInstance.setConnectionValue(connectionValue); |
| 180 | + extension.setConnectionInstances(Collections.singletonList(connectionInstance)); |
| 181 | + |
| 182 | + textTab.setExtensionData(extension); |
| 183 | + textTabs.add(textTab); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + Tabs tabs = new Tabs(); |
| 188 | + tabs.setSignHereTabs(Collections.singletonList(signHere)); |
| 189 | + tabs.setTextTabs(textTabs); |
| 190 | + signer.setTabs(tabs); |
| 191 | + |
| 192 | + Recipients recipients = new Recipients(); |
| 193 | + recipients.setSigners(Collections.singletonList(signer)); |
| 194 | + envelopeDefinition.setRecipients(recipients); |
| 195 | + |
| 196 | + return envelopeDefinition; |
| 197 | + } |
| 198 | + |
| 199 | + public static List<Map<String, String>> convertJsonToList(String jsonString) throws Exception { |
| 200 | + JsonNode root = objectMapper.readTree(jsonString); |
| 201 | + List<Map<String, String>> result = new ArrayList<>(); |
| 202 | + |
| 203 | + if (root.isArray()) { |
| 204 | + for (JsonNode app : root) { |
| 205 | + String appId = app.has(APP_ID) ? app.get(APP_ID).asText() : null; |
| 206 | + String applicationName = ""; |
| 207 | + |
| 208 | + JsonNode tabs = app.get(TABS); |
| 209 | + if (tabs != null && tabs.isArray() && tabs.size() > 0) { |
| 210 | + JsonNode extensionData = tabs.get(0).get(EXTENSION_DATA); |
| 211 | + if (extensionData != null && extensionData.has(APPLICATION_NAME)) { |
| 212 | + applicationName = extensionData.get(APPLICATION_NAME).asText(); |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + if (appId != null && applicationName != null) { |
| 217 | + Map<String, String> map = new HashMap<>(); |
| 218 | + map.put(APP_ID, appId); |
| 219 | + map.put(APPLICATION_NAME, applicationName); |
| 220 | + result.add(map); |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + return result; |
| 226 | + } |
| 227 | + |
| 228 | + private static String getText(JsonNode node, String fieldName) { |
| 229 | + return node != null && node.has(fieldName) ? node.get(fieldName).asText() : ""; |
| 230 | + } |
| 231 | + |
| 232 | + public static String filterData(String jsonData) throws Exception { |
| 233 | + JsonNode rootNode = objectMapper.readTree(jsonData); |
| 234 | + |
| 235 | + if (!rootNode.isArray()) { |
| 236 | + throw new IllegalArgumentException(EXPECTED_A_JSON_ARRAY); |
| 237 | + } |
| 238 | + |
| 239 | + ArrayNode filteredArray = JsonNodeFactory.instance.arrayNode(); |
| 240 | + |
| 241 | + for (JsonNode item : rootNode) { |
| 242 | + JsonNode tabs = item.get(TABS); |
| 243 | + |
| 244 | + if (tabs != null && tabs.isArray()) { |
| 245 | + for (JsonNode tab : tabs) { |
| 246 | + JsonNode extensionData = tab.get(EXTENSION_DATA); |
| 247 | + String tabLabel = tab.has(TAB_LABEL) ? tab.get(TAB_LABEL).asText() : null; |
| 248 | + |
| 249 | + boolean hasVerify = extensionData != null && |
| 250 | + extensionData.has(ACTION_CONTRACT) && |
| 251 | + extensionData.get(ACTION_CONTRACT).asText().contains("Verify"); |
| 252 | + |
| 253 | + boolean hasConnectedData = tabLabel != null && tabLabel.contains("connecteddata"); |
| 254 | + |
| 255 | + if (hasVerify || hasConnectedData) { |
| 256 | + filteredArray.add(item); |
| 257 | + break; |
| 258 | + } |
| 259 | + } |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + return objectMapper.writeValueAsString(filteredArray); |
| 264 | + } |
| 265 | + |
| 266 | + public static JsonNode findAppById(String extensionAppsJson, String appId) throws Exception { |
| 267 | + JsonNode root = objectMapper.readTree(extensionAppsJson); |
| 268 | + if (!root.isArray()) { |
| 269 | + throw new IllegalArgumentException(EXPECTED_A_JSON_ARRAY); |
| 270 | + } |
| 271 | + |
| 272 | + for (JsonNode app : root) { |
| 273 | + JsonNode appIdNode = app.get(APP_ID); |
| 274 | + if (appIdNode != null && appId.equals(appIdNode.asText())) { |
| 275 | + return app; |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + return null; |
| 280 | + } |
| 281 | +} |
0 commit comments