|
| 1 | +package com.docusign.controller.eSignature.examples; |
| 2 | + |
| 3 | +import com.docusign.DSConfiguration; |
| 4 | +import com.docusign.common.WorkArguments; |
| 5 | +import com.docusign.controller.eSignature.services.DeleteRestoreEnvelopeService; |
| 6 | +import com.docusign.core.model.DoneExample; |
| 7 | +import com.docusign.core.model.Session; |
| 8 | +import com.docusign.core.model.User; |
| 9 | +import com.docusign.esign.client.ApiClient; |
| 10 | +import com.docusign.esign.model.Folder; |
| 11 | +import com.docusign.esign.model.FoldersResponse; |
| 12 | +import org.springframework.http.MediaType; |
| 13 | +import org.springframework.stereotype.Controller; |
| 14 | +import org.springframework.ui.ModelMap; |
| 15 | +import org.springframework.web.bind.annotation.GetMapping; |
| 16 | +import org.springframework.web.bind.annotation.PostMapping; |
| 17 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 18 | + |
| 19 | +import javax.servlet.http.HttpServletResponse; |
| 20 | +import java.text.MessageFormat; |
| 21 | + |
| 22 | +/** |
| 23 | + * Used to delete the envelope and restore it back. |
| 24 | + */ |
| 25 | +@Controller |
| 26 | +@RequestMapping("/eg045") |
| 27 | +public class EG045ControllerDeleteRestoreEnvelope extends AbstractEsignatureController { |
| 28 | + public static final String RECYCLE_BIN_FOLDER_ID = "recyclebin"; |
| 29 | + |
| 30 | + public static final String SENT_ITEMS_FOLDER_NAME = "Sent items"; |
| 31 | + |
| 32 | + public static final String EXAMPLE_NUMBER = "/eg045"; |
| 33 | + |
| 34 | + public static final String RESTORE_ENVELOPE = "/restoreEnvelope"; |
| 35 | + |
| 36 | + public static final String RESTORE_ENVELOPE_PAGE = "pages/esignature/examples/eg045RestoreEnvelope"; |
| 37 | + |
| 38 | + public EG045ControllerDeleteRestoreEnvelope(DSConfiguration config, Session session, User user) { |
| 39 | + super(config, "eg045", session, user); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + protected void onInitModel(WorkArguments args, ModelMap model) throws Exception { |
| 44 | + super.onInitModel(args, model); |
| 45 | + model.addAttribute("envelopeId", session.getEnvelopeId()); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + protected Object doWork(WorkArguments args, ModelMap model, |
| 50 | + HttpServletResponse response) throws Exception { |
| 51 | + String envelopeId = args.getEnvelopeId(); |
| 52 | + session.setEnvelopeId(envelopeId); |
| 53 | + //ds-snippet-start:eSign45Step2 |
| 54 | + ApiClient apiClient = createApiClient(session.getBasePath(), user.getAccessToken()); |
| 55 | + //ds-snippet-end:eSign45Step2 |
| 56 | + DeleteRestoreEnvelopeService.deleteEnvelope( |
| 57 | + apiClient, |
| 58 | + session.getAccountId(), |
| 59 | + envelopeId); |
| 60 | + |
| 61 | + DoneExample.createDefault(getTextForCodeExampleByApiType().ExampleName) |
| 62 | + .withMessage( |
| 63 | + MessageFormat.format( |
| 64 | + getTextForCodeExampleByApiType().AdditionalPage |
| 65 | + .get(0).ResultsPageText, |
| 66 | + envelopeId)) |
| 67 | + .withRedirect(EXAMPLE_NUMBER + RESTORE_ENVELOPE) |
| 68 | + .addToModel(model, config); |
| 69 | + |
| 70 | + return DONE_EXAMPLE_PAGE; |
| 71 | + } |
| 72 | + |
| 73 | + @GetMapping(value = RESTORE_ENVELOPE, produces = MediaType.APPLICATION_JSON_VALUE) |
| 74 | + public String getRestoreEnvelope(WorkArguments args, ModelMap model) throws Exception { |
| 75 | + super.onInitModel(args, model); |
| 76 | + |
| 77 | + String envelopeId = session.getEnvelopeId(); |
| 78 | + String restoreEnvelopeText = MessageFormat.format( |
| 79 | + config.getCodeExamplesText().SupportingTexts.HelpingTexts.EnvelopeWillBeRestored, |
| 80 | + envelopeId); |
| 81 | + |
| 82 | + model.addAttribute("restoreText", restoreEnvelopeText); |
| 83 | + model.addAttribute("envelopeId", envelopeId); |
| 84 | + |
| 85 | + return RESTORE_ENVELOPE_PAGE; |
| 86 | + } |
| 87 | + |
| 88 | + @PostMapping(value = RESTORE_ENVELOPE, produces = MediaType.APPLICATION_JSON_VALUE) |
| 89 | + public String postRestoreEnvelope(WorkArguments args, ModelMap model) throws Exception { |
| 90 | + String envelopeId = session.getEnvelopeId(); |
| 91 | + String accountId = session.getAccountId(); |
| 92 | + String folderName = args.getFolderName() != null ? args.getFolderName() : SENT_ITEMS_FOLDER_NAME; |
| 93 | + |
| 94 | + ApiClient apiClient = createApiClient(session.getBasePath(), user.getAccessToken()); |
| 95 | + |
| 96 | + FoldersResponse availableFolders = DeleteRestoreEnvelopeService.getFolders(apiClient, accountId); |
| 97 | + Folder folder = DeleteRestoreEnvelopeService.getFolderIdByName(availableFolders.getFolders(), |
| 98 | + folderName); |
| 99 | + |
| 100 | + if (folder == null) { |
| 101 | + DoneExample.createDefault(getTextForCodeExampleByApiType().ExampleName) |
| 102 | + .withMessage( |
| 103 | + MessageFormat.format( |
| 104 | + getTextForCodeExampleByApiType().AdditionalPage |
| 105 | + .get(1).ResultsPageText, |
| 106 | + folderName)) |
| 107 | + .withRedirect(EXAMPLE_NUMBER + RESTORE_ENVELOPE) |
| 108 | + .addToModel(model, config); |
| 109 | + |
| 110 | + return DONE_EXAMPLE_PAGE; |
| 111 | + } |
| 112 | + |
| 113 | + DeleteRestoreEnvelopeService.moveEnvelopeToFolder( |
| 114 | + apiClient, |
| 115 | + accountId, |
| 116 | + envelopeId, |
| 117 | + folder.getFolderId(), |
| 118 | + RECYCLE_BIN_FOLDER_ID); |
| 119 | + |
| 120 | + DoneExample.createDefault(getTextForCodeExampleByApiType().ExampleName) |
| 121 | + .withMessage( |
| 122 | + MessageFormat.format( |
| 123 | + getTextForCodeExampleByApiType().ResultsPageText, |
| 124 | + envelopeId, |
| 125 | + folder.getType(), |
| 126 | + folderName)) |
| 127 | + .addToModel(model, config); |
| 128 | + |
| 129 | + return DONE_EXAMPLE_PAGE; |
| 130 | + } |
| 131 | +} |
0 commit comments