PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/lmig/icm/thailand/endorsement/service/PubServiceImpl.java

https://gitlab.com/trungkien963/be-java
Java | 215 lines | 158 code | 57 blank | 0 comment | 2 complexity | 318e7ed39992690ce426e7c60d40b08d MD5 | raw file
  1. package com.lmig.icm.thailand.endorsement.service;
  2. import com.lmig.icm.thailand.endorsement.payload.request.PubLoginRequest;
  3. import com.lmig.icm.thailand.endorsement.payload.response.*;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.http.*;
  6. import org.springframework.stereotype.Service;
  7. import org.springframework.util.LinkedMultiValueMap;
  8. import org.springframework.util.MultiValueMap;
  9. import org.springframework.web.client.RestTemplate;
  10. import java.util.ArrayList;
  11. import java.util.Arrays;
  12. import java.util.List;
  13. @Service
  14. public class PubServiceImpl implements PubService {
  15. private static final String clientId = "HJ4FwzG2cCmUmqLjF9AnaO8EFV4REfoL";
  16. private static final String clientSecret = "k2zn4aarQDKZ520D";
  17. @Autowired
  18. private RestTemplate restTemplate;
  19. @Override
  20. public PubLoginResponse login(PubLoginRequest pubLoginRequest) {
  21. HttpHeaders headers = new HttpHeaders();
  22. headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
  23. MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
  24. map.add("client_id", pubLoginRequest.getClient_id());
  25. map.add("client_secret", pubLoginRequest.getClient_secret());
  26. HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(map, headers);
  27. WsPubLoginResponse wsPubLoginResponse = restTemplate.postForObject("https://test-apis.ap-southeast-1.lmig.com/oauth/client_credential/accesstoken?grant_type=client_credentials", entity, WsPubLoginResponse.class);
  28. PubLoginResponse pubLoginResponse = new PubLoginResponse();
  29. pubLoginResponse.setRefreshTokenExpiresIn(wsPubLoginResponse.getRefresh_token_expires_in());
  30. pubLoginResponse.setApiProductList(wsPubLoginResponse.getApi_product_list());
  31. pubLoginResponse.setApiProductListJson(wsPubLoginResponse.getApi_product_list_json());
  32. pubLoginResponse.setOrganizationName(wsPubLoginResponse.getOrganization_name());
  33. pubLoginResponse.setDeveloperEmail(wsPubLoginResponse.getDeveloper_email());
  34. pubLoginResponse.setTokenType(wsPubLoginResponse.getToken_type());
  35. pubLoginResponse.setIssuedAt(wsPubLoginResponse.getIssued_at());
  36. pubLoginResponse.setClientId(wsPubLoginResponse.getClient_id());
  37. pubLoginResponse.setAccessToken(wsPubLoginResponse.getAccess_token());
  38. pubLoginResponse.setApplicationName(wsPubLoginResponse.getApplication_name());
  39. pubLoginResponse.setScope(wsPubLoginResponse.getScope());
  40. pubLoginResponse.setExpiresIn(wsPubLoginResponse.getExpires_in());
  41. pubLoginResponse.setRefreshCount(wsPubLoginResponse.getRefresh_count());
  42. pubLoginResponse.setStatus(wsPubLoginResponse.getStatus());
  43. return pubLoginResponse;
  44. }
  45. @Override
  46. public PubForms getForms() {
  47. PubLoginRequest pubLoginRequest = new PubLoginRequest();
  48. pubLoginRequest.setClient_id(clientId);
  49. pubLoginRequest.setClient_secret(clientSecret);
  50. PubLoginResponse pubLoginResponse = login(pubLoginRequest);
  51. String token = "Bearer " + pubLoginResponse.getAccessToken();
  52. HttpHeaders headers = new HttpHeaders();
  53. headers.setContentType(MediaType.APPLICATION_JSON);
  54. headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
  55. headers.set("Authorization", token);
  56. HttpEntity entity = new HttpEntity(headers);
  57. ResponseEntity<WsPubFormList> response = restTemplate.exchange("https://test-apis.ap-southeast-1.lmig.com/v2/pubapi/forms?clientId=amcmregional", HttpMethod.GET, entity, WsPubFormList.class);
  58. WsPubFormList wsPubFormList = response.getBody();
  59. List<WsPubForm> wsPubForms = wsPubFormList.getFormList();
  60. PubForms pubForms = new PubForms();
  61. List<PubForm> forms = new ArrayList<>();
  62. for(WsPubForm wsPubForm : wsPubForms) {
  63. PubForm form = new PubForm();
  64. form.setId(wsPubForm.getFormId());
  65. form.setMetaURL(wsPubForm.getFormMetaURL());
  66. forms.add(form);
  67. }
  68. pubForms.setForms(forms);
  69. return pubForms;
  70. }
  71. @Override
  72. public PubFormDetails getFormById(String formId) {
  73. PubLoginRequest pubLoginRequest = new PubLoginRequest();
  74. pubLoginRequest.setClient_id(clientId);
  75. pubLoginRequest.setClient_secret(clientSecret);
  76. PubLoginResponse pubLoginResponse = login(pubLoginRequest);
  77. String token = "Bearer " + pubLoginResponse.getAccessToken();
  78. HttpHeaders headers = new HttpHeaders();
  79. headers.setContentType(MediaType.APPLICATION_JSON);
  80. headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
  81. headers.set("Authorization", token);
  82. HttpEntity entity = new HttpEntity(headers);
  83. ResponseEntity<WsPubFormDetails> response = restTemplate.exchange("https://test-apis.ap-southeast-1.lmig.com/v2/pubapi/sampleFormsRequest?clientId=amcmregional&formId=" + formId, HttpMethod.GET, entity, WsPubFormDetails.class);
  84. WsPubFormDetails wsPubFormDetails = response.getBody();
  85. PubFormDetails pubFormDetails = new PubFormDetails();
  86. pubFormDetails.setCallBackUrl(wsPubFormDetails.getCallBackUrl());
  87. pubFormDetails.setValidateOnly(wsPubFormDetails.getValidateOnly());
  88. PubFormClient pubFormClient = new PubFormClient();
  89. pubFormClient.setId(wsPubFormDetails.getClient().getId());
  90. pubFormClient.setRequestId(wsPubFormDetails.getClient().getRequestId());
  91. pubFormClient.setClientRequestId(wsPubFormDetails.getClient().getClientRequestId());
  92. pubFormClient.setApiKey(wsPubFormDetails.getClient().getApiKey());
  93. pubFormDetails.setClient(pubFormClient);
  94. List<PubFormPackage> pubFormPackages = new ArrayList<>();
  95. List<WsPubFormPackage> wsPubFormPackages = wsPubFormDetails.getFormPackages();
  96. for(WsPubFormPackage wsPubFormPackage : wsPubFormPackages) {
  97. PubFormPackage pubFormPackage = new PubFormPackage();
  98. pubFormPackage.setFormIds(wsPubFormPackage.getFormIds());
  99. pubFormPackage.setPackageId(wsPubFormPackage.getPackageId());
  100. PubFormFields pubFormFields = new PubFormFields();
  101. pubFormFields.setCompanyAddress(wsPubFormPackage.getFormFields().getThailand_Endorsement_Sample_CompanyAddress());
  102. pubFormFields.setRecipientName(wsPubFormPackage.getFormFields().getThailand_Endorsement_Sample_RecipientName());
  103. pubFormFields.setSurname(wsPubFormPackage.getFormFields().getThailand_Endorsement_Sample_Surname());
  104. pubFormFields.setCompanyAddress(wsPubFormPackage.getFormFields().getThailand_Endorsement_Sample_CompanyAddress());
  105. pubFormFields.setRecipientAddress(wsPubFormPackage.getFormFields().getThailand_Endorsement_Sample_RecipientAddress());
  106. pubFormFields.setRecipientCity(wsPubFormPackage.getFormFields().getThailand_Endorsement_Sample_RecipientCity());
  107. pubFormPackage.setFormFields(pubFormFields);
  108. PubFormDistributions pubFormDistributions = new PubFormDistributions();
  109. pubFormDistributions.setEmail(wsPubFormPackage.getDistributions().getEmail());
  110. pubFormDistributions.setArchive(wsPubFormPackage.getDistributions().getArchive());
  111. pubFormDistributions.setEsign(wsPubFormPackage.getDistributions().getEsign());
  112. pubFormDistributions.setPrint(wsPubFormPackage.getDistributions().getPrint());
  113. pubFormDistributions.setNebulaArchive(wsPubFormPackage.getDistributions().getNebulaArchive());
  114. pubFormPackage.setDistributions(pubFormDistributions);
  115. pubFormPackages.add(pubFormPackage);
  116. }
  117. pubFormDetails.setFormPackages(pubFormPackages);
  118. return pubFormDetails;
  119. }
  120. @Override
  121. public WsPubFormStatus upload(WsPubFormDetails wsPubFormDetails) {
  122. PubLoginRequest pubLoginRequest = new PubLoginRequest();
  123. pubLoginRequest.setClient_id(clientId);
  124. pubLoginRequest.setClient_secret(clientSecret);
  125. PubLoginResponse pubLoginResponse = login(pubLoginRequest);
  126. String token = "Bearer " + pubLoginResponse.getAccessToken();
  127. HttpHeaders headers = new HttpHeaders();
  128. headers.setContentType(MediaType.APPLICATION_JSON);
  129. headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
  130. headers.set("Authorization", token);
  131. HttpEntity<WsPubFormDetails> entity = new HttpEntity<>(wsPubFormDetails, headers);
  132. WsPubFormUpload wsPubFormUpload = restTemplate.postForObject("https://test-apis.ap-southeast-1.lmig.com/v2/pubapi/formsRequest?clientId=amcmregional", entity, WsPubFormUpload.class);
  133. HttpHeaders headers2 = new HttpHeaders();
  134. headers2.setContentType(MediaType.APPLICATION_JSON);
  135. headers2.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
  136. headers2.set("Authorization", token);
  137. HttpEntity entity2 = new HttpEntity(headers2);
  138. ResponseEntity<WsPubFormStatus> response = restTemplate.exchange("https://test-apis.ap-southeast-1.lmig.com/v2/pubapi/formsRequestStatus?requestId=" + wsPubFormUpload.getRequestId() + "&clientId=amcmregional", HttpMethod.GET, entity2, WsPubFormStatus.class);
  139. WsPubFormStatus wsPubFormStatus = response.getBody();
  140. return wsPubFormStatus;
  141. }
  142. @Override
  143. public WsPubFormStatus download(String requestId) {
  144. PubLoginRequest pubLoginRequest = new PubLoginRequest();
  145. pubLoginRequest.setClient_id(clientId);
  146. pubLoginRequest.setClient_secret(clientSecret);
  147. PubLoginResponse pubLoginResponse = login(pubLoginRequest);
  148. String token = "Bearer " + pubLoginResponse.getAccessToken();
  149. HttpHeaders headers = new HttpHeaders();
  150. headers.setContentType(MediaType.APPLICATION_JSON);
  151. headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
  152. headers.set("Authorization", token);
  153. HttpEntity entity = new HttpEntity(headers);
  154. ResponseEntity<WsPubFormStatus> response = restTemplate.exchange("https://test-apis.ap-southeast-1.lmig.com/v2/pubapi/formsRequestStatus?requestId=" + requestId + "&clientId=amcmregional", HttpMethod.GET, entity, WsPubFormStatus.class);
  155. WsPubFormStatus wsPubFormStatus = response.getBody();
  156. return wsPubFormStatus;
  157. }
  158. }