Guest User

Untitled

a guest
Apr 25th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.27 KB | None | 0 0
  1. public pagereference APIRequest(){
  2.  
  3. Type = ApexPages.currentPage().getParameters().get('Type');
  4. NoticeID = ApexPages.currentPage().getParameters().get('NoticeID');
  5.  
  6. system.debug('Test Debug');
  7.  
  8. Id EERT = Schema.SObjectType.Contracts_Finder_Submission__c.getRecordTypeInfosByName().get('Early Engagement').getRecordTypeId();
  9. Id FORT = Schema.SObjectType.Contracts_Finder_Submission__c.getRecordTypeInfosByName().get('Future Opportunity').getRecordTypeId();
  10. Id ORT = Schema.SObjectType.Contracts_Finder_Submission__c.getRecordTypeInfosByName().get('Opportunity').getRecordTypeId();
  11. Id OART = Schema.SObjectType.Contracts_Finder_Submission__c.getRecordTypeInfosByName().get('Opportunity - Awarded').getRecordTypeId();
  12.  
  13. //api endpoint from custom setting
  14. ProSpend_Config__c spConfig = ProSpend_Config__c.getInstance();
  15. String API = spConfig.Contracts_Finder_API__c;
  16. String Creds = spConfig.Contracts_Finder_API_Creds__c;
  17. boolean EmailAlerts = spConfig.Send_apex_email_alerts__c;
  18.  
  19. Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
  20. Map<String, Schema.SObjectField> fieldMap = schemaMap.get(utils.NameSpacePrefix()+'Contracts_Finder_Submission__c').getDescribe().fields.getMap();
  21. string CFSQuery = 'SELECT ';
  22. for(String fieldName : fieldMap.keyset()){
  23.  
  24. CFSQuery = CFSQuery + fieldName + ',';
  25. }
  26. CFSQuery = CFSQuery.substring(0,CFSQuery.length()-1);
  27. CFSQuery = CFSQuery + ' ,Contract__r.Supplier__c, Contract__r.Estimated_Value__c, Contact__r.Name FROM Contracts_Finder_Submission__c WHERE ID = : NoticeID';
  28. List<Contracts_Finder_Submission__c> SubList = database.query(CFSQuery);
  29.  
  30. IDsToUpdate = new List<ID>();
  31.  
  32. If(SubList.size()>0){
  33.  
  34. Contracts_Finder_Submission__c Submission = SubList[0];
  35.  
  36. String AuthToken = getToken(Creds,API,EmailAlerts) ;
  37.  
  38. system.debug('AuthToken------'+AuthToken);
  39.  
  40. String AwardResponse = '';
  41. String PublishResponse = '';
  42. String AttachResponse = '';
  43. List<ID> SuppliersToUpdate = new List<ID>();
  44. List<ID> FilesToUpdate = new List<ID>();
  45.  
  46. String ResponseString = '';
  47.  
  48. If(Type == 'Post'){
  49.  
  50. String GUID = DraftGUID(API, EmailAlerts, Submission, AuthToken);
  51. system.debug('GUID----'+GUID);
  52. If(GUID.left(3)=='---'){
  53.  
  54. Submission.HTTP_Response__c = GUID;
  55. update Submission;
  56. pagereference pr = new pagereference('/'+NoticeID);
  57. return pr;
  58. }
  59.  
  60.  
  61. system.debug('GUID----'+GUID);
  62. If(!String.isblank(GUID) && GUID <> null){
  63.  
  64. If(Submission.RecordTypeID == OART){
  65.  
  66. AwardReturn Award = AwardSuppliersToDraft(Submission, AuthToken, API, EmailAlerts, GUID);
  67. AwardResponse = Award.AwardResp;
  68. SuppliersToUpdate = Award.AwardIDs;
  69. }
  70.  
  71. AttachReturn Attach = AddAttachments (Submission, GUID, AuthToken, API, emailalerts,'Post');
  72. AttachResponse = Attach.AttachResp;
  73. FilesToUpdate = Attach.AttachIDs;
  74.  
  75. PublishResponse = PublishNotice(API, AuthToken, GUID);
  76.  
  77. ResponseString = GUID + 'n' + PublishResponse + 'n' + AttachResponse + 'n' + AwardResponse;
  78. }
  79. Else{
  80.  
  81. ResponseString = 'No GUID Returned';
  82. }
  83.  
  84. Submission.GUID__c = GUID;
  85. }
  86. Else If(Type == 'UPDATE'){
  87.  
  88. String UpdateResponse = UpdateNotice (API, EmailAlerts, Submission, AuthToken);
  89.  
  90. If(Submission.RecordTypeID == OART){
  91.  
  92. AwardReturn Award = AwardSuppliersToDraft(Submission, AuthToken, API, EmailAlerts, Submission.GUID__c);
  93. AwardResponse = Award.AwardResp;
  94. SuppliersToUpdate = Award.AwardIDs;
  95.  
  96. }
  97.  
  98. AttachReturn Attach = AddAttachments (Submission, Submission.GUID__c, AuthToken, API, emailalerts,'Update');
  99. AttachResponse = Attach.AttachResp;
  100. FilesToUpdate = Attach.AttachIDs;
  101.  
  102.  
  103. If(UpdateResponse == 'Notice Update request to contracts finder was SUCCESSFUL.'){
  104.  
  105. PublishResponse = PublishNotice(API, AuthToken, Submission.GUID__c);
  106.  
  107. ResponseString = Submission.GUID__c + 'n' + PublishResponse + 'n' + AttachResponse + 'n' + AwardResponse;
  108. }
  109. Else{
  110.  
  111. ResponseString = UpdateResponse;
  112. }
  113. }
  114. Else If(Type == 'WITHDRAW'){
  115.  
  116. String WithdrawResponse = WithdrawNotice (API, EmailAlerts, AuthToken, Submission.GUID__c);
  117.  
  118. ResponseString = WithdrawResponse;
  119. }
  120.  
  121.  
  122. Submission.HTTP_Response__c = ResponseString;
  123. update submission;
  124.  
  125. system.debug(Filestoupdate);
  126.  
  127. If(FilesToUpdate<>null){
  128. If(FilesToUpdate.size()>0){
  129.  
  130. List<ContentVersion> CVList = [SELECT ID, Published_to_contracts_finder__c FROM ContentVersion WHERE ContentDocumentID IN : FilesToUpdate];
  131. For(ContentVersion CV : CVList){
  132.  
  133. CV.Published_to_contracts_finder__c = true;
  134. }
  135. update CVList;
  136. }
  137. }
  138. If(SuppliersToUpdate<>null){
  139. If(SuppliersToUpdate.size()>0){
  140.  
  141. List<Supplier_Bidding__c> SuppList = [SELECT ID, Supplier_Award_on_Contracts_Finder__c FROM Supplier_Bidding__c WHERE ID IN : SuppliersToUpdate];
  142. For(Supplier_Bidding__c S : SuppList){
  143.  
  144. S.Supplier_Award_on_Contracts_Finder__c = true;
  145. }
  146. update SuppList;
  147. }
  148. }
  149. }
  150. pagereference pr = new pagereference('/'+NoticeID);
  151. return pr;
  152. }
  153.  
  154.  
  155. public String DraftGUID (String API, Boolean EmailAlerts, Contracts_Finder_Submission__c Submission, String Token){
  156.  
  157. //GET ALL NOTICE RECORDTYPES
  158. Id EERT = Schema.SObjectType.Contracts_Finder_Submission__c.getRecordTypeInfosByName().get('Early Engagement').getRecordTypeId();
  159. Id FORT = Schema.SObjectType.Contracts_Finder_Submission__c.getRecordTypeInfosByName().get('Future Opportunity').getRecordTypeId();
  160. Id ORT = Schema.SObjectType.Contracts_Finder_Submission__c.getRecordTypeInfosByName().get('Opportunity').getRecordTypeId();
  161. Id OART = Schema.SObjectType.Contracts_Finder_Submission__c.getRecordTypeInfosByName().get('Opportunity - Awarded').getRecordTypeId();
  162.  
  163. String DraftBody = NoticeJSONBody(Submission);
  164. String LenString = String.valueof(DraftBody.length());
  165.  
  166. //SUBMIT DRAFT NOTICE TO API
  167. HttpRequest UD = new HttpRequest();
  168. UD.setHeader('Authorization','bearer ' + Token);
  169. UD.setHeader('Content-Type','application/json');
  170. UD.setHeader('Content-Length',LenString);
  171. UD.setBody(DraftBody);
  172. UD.setmethod('POST');
  173. UD.setEndpoint(API+'/api/rest/2/create_draft_notice/JSON');
  174.  
  175. system.debug('DRAFT REQUEST --- ' + UD);
  176. system.debug('DRAFT BODY --- ' + UD.getbody());
  177. HTTP U = new HTTP();
  178. HTTPResponse UpdateResponse = new HttpResponse();
  179. try{
  180. If(!Test.isrunningtest()){
  181. UpdateResponse = U.send(UD);
  182. }
  183.  
  184. system.debug('draft response ----' + UpdateResponse.getbody());
  185. }
  186. catch(Exception ex){
  187.  
  188. If(EmailAlerts){
  189. es_Utils.sendMailToOrgAdmins('Contracts Finder Draft Issue', ex.getMessage() + ' ###RESPONSE### ' + UpdateResponse.getbody());
  190. }
  191. }
  192. If(EmailAlerts){
  193. es_Utils.sendMailToOrgAdmins('Contracts Finder Submission - Draft','A submission has been made to the contracts finder API, details here: update Response n' + UpdateResponse.getbody() ) ;
  194. }
  195. //GET GUID OF DRAFT NOTICE TO THEN USE IN PUBLISHING NOTICE
  196. String GUID = '';
  197. If(test.isRunningTest()){
  198. GUID = '1231231231213';
  199. return GUID;
  200. }
  201. If(UpdateResponse.getbody().left(1)=='"'){
  202. system.debug('CHARACTER CHECK');
  203. system.debug('draft response ----' + UpdateResponse.getbody());
  204.  
  205. GUID = UpdateResponse.getbody().removeend('"');
  206. GUID = GUID.right(GUID.length()-1);
  207.  
  208. return GUID;
  209. }
  210. Else{
  211. GUID = '---'+UpdateResponse.getbody()+'---';
  212. If(EmailAlerts){
  213. es_Utils.sendMailToOrgAdmins('Contracts Finder Post Draft Issue', GUID);
  214. }
  215. return GUID;
  216. }
  217. }
  218.  
  219.  
  220. public class AttachReturn {
  221.  
  222. String AttachResp {get;set;}
  223. List<ID> AttachIDs {get;set;}
  224. }
  225.  
  226. public AttachReturn AddAttachments (Contracts_Finder_Submission__c Submission, String GUID, String Token, String API, boolean emailalerts, String Type){
  227.  
  228. ID SubmissionID = Submission.ID;
  229.  
  230. //GET ALL FILES ATTACHED TO CONTRACTTS FINDER Notice
  231. List<ContentDocumentLink> FileID= [SELECT ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId =: SubmissionID];
  232. system.debug('FileID ---- ' + FileID);
  233. List<ContentDocument> CDList = new List<ContentDocument>();
  234.  
  235. List<ID> AttachIDsToUpdate = new List<ID>();
  236.  
  237. AttachReturn Response = new AttachReturn();
  238.  
  239. If(FileID.size()>0 || !String.isblank(Submission.Notes__c)){
  240.  
  241. List<ContentDistribution> AllFiles = new List<ContentDistribution>();
  242.  
  243.  
  244. Map<ID,ID> PreviouslyPublished = new Map<ID,ID>();
  245.  
  246. For(ContentDocumentLink C: FileID){
  247.  
  248. List<ContentDistribution> File = [SELECT ID, ContentDocumentID, DistributionPublicUrl FROM ContentDistribution WHERE ContentDocumentID =: C.ContentDocumentId];
  249. If(File.size()>0){
  250. AllFiles.add(File[0]);
  251. List<ContentDocument> CD = [SELECT ID, Title, Description FROM ContentDocument WHERE ID = : File[0].ContentDocumentID];
  252. system.debug('CD ---- ' + CD);
  253.  
  254. List<ContentVersion> ContentVersionList = [SELECT ID, Published_to_contracts_finder__c, ContentDocumentID FROM ContentVersion WHERE ContentDocumentID = :File[0].ContentDocumentID AND Published_to_contracts_finder__c=true];
  255. system.debug('ContentVersionList ---- ' + ContentVersionList);
  256. If(ContentVersionList.size()>0){
  257. PreviouslyPublished.PUT(ContentVersionList[0].ContentDocumentID,ContentVersionList[0].ContentDocumentID);
  258. }
  259.  
  260.  
  261. If(CD.size()>0){
  262. CDList.add(CD[0]);
  263. AttachIDsToUpdate.add(CD[0].ID);
  264. }
  265. }
  266. }
  267. system.debug('AllFiles ---- ' + AllFiles);
  268. system.debug('CDList ---- ' + CDList);
  269. system.debug('PreviouslyPublished ---- ' + PreviouslyPublished);
  270.  
  271. boolean submitattachmentrequest = false;
  272.  
  273.  
  274. system.debug('!String.isblank(Submission.Notes__c)----'+!String.isblank(Submission.Notes__c));
  275.  
  276. If(CDList.size()>0 || !String.isblank(Submission.Notes__c)){
  277.  
  278. String FileBody = '{'+
  279. '"noticeId": "'+GUID+'",'+
  280. '"additionalDetailsList": [';
  281.  
  282. system.debug('AllFiles----' + AllFiles);
  283. For(ContentDistribution CV : AllFiles){
  284.  
  285. If((CV.DistributionPublicUrl <> null || test.isRunningTest()) && PreviouslyPublished.get(CV.contentdocumentid)==null){
  286. submitattachmentrequest = true;
  287. //ContentToUpdate.add(CV.ContentDocumentID);
  288. String TitleDescription = '';
  289. For(ContentDocument CD : CDList){
  290.  
  291. If(CD.ID == CV.ContentDocumentID){
  292. If(!String.isblank(CD.Description)){
  293. TitleDescription = CD.Title + ' - ' + CD.Description;
  294. }
  295. Else{
  296. TitleDescription = CD.Title;
  297. }
  298. FileBody = FileBody + '{'+
  299. '"id": null,' +
  300. '"noticeId": "'+GUID+'",'+
  301. '"description": "'+TitleDescription+'",'+
  302. '"dataType": "Link",'+
  303. '"link": "'+CV.DistributionPublicUrl+'",'+
  304. '"mimeType": null,'+
  305. '"filename": null,'+
  306. '"fileContent": null,'+
  307. '"awardGuid": null' +
  308. '},';
  309.  
  310. TitleDescription = '';
  311. }
  312. }
  313. }
  314. }
  315.  
  316. If(!String.isblank(Submission.Notes__c)){
  317.  
  318. system.debug('Note Included with Request');
  319.  
  320. submitattachmentrequest = true;
  321.  
  322. FileBody = FileBody + '{'+
  323. '"id": null,' +
  324. '"noticeId": "'+GUID+'",'+
  325. '"dataType": "AdditionalText",'+
  326. '"mimeType": null,'+
  327. '"TextData": "'+Submission.Notes__c+'",'+
  328. '"filename": null,'+
  329. '"fileContent": null,'+
  330. '"awardGuid": null' +
  331. '},';
  332. }
  333.  
  334.  
  335.  
  336. FileBody = FileBody.removeend(',');
  337. FileBody = FileBody + ']}';
  338.  
  339. If(submitattachmentrequest){
  340.  
  341. String LenStringAttch = String.valueof(FileBody.length());
  342.  
  343. HttpRequest FileReq = new HttpRequest();
  344. FileReq.setHeader('Authorization','bearer ' + Token);
  345. //req.setHeader('Accept','text/json');
  346. FileReq.setHeader('Content-Type','application/json');
  347. FileReq.setHeader('Content-Length',LenStringAttch);
  348. FileReq.setBody(FileBody);
  349. FileReq.setmethod('POST');
  350. FileReq.setEndpoint(API+'/Draft/AdditionalDetail/Set');
  351. system.debug(FileReq);
  352. system.debug(FileReq.getbody());
  353. HTTP File = new HTTP();
  354. HTTPResponse FileResponse =new HttpResponse();
  355. try{
  356. If(!Test.isrunningtest()){
  357. FileResponse = File.send(FileReq);
  358. }
  359. Response.AttachResp = FileResponse.getbody();
  360. Response.AttachIDs = AttachIDsToUpdate;
  361. }
  362. catch(Exception ex){
  363. If(EmailAlerts){
  364. es_Utils.sendMailToOrgAdmins('Contracts Finder Attachment Issue', ex.getMessage() + ' ###RESPONSE### ' + FileResponse.getbody());
  365. }
  366. }
  367. If(EmailAlerts){
  368. es_Utils.sendMailToOrgAdmins('Contracts Finder Submission - Attachment','A submission has been made to the contracts finder API, details here: update Response n' + FileResponse.getbody() ) ;
  369. }
  370. system.debug('FileResponse.getbody() ---- ' + FileResponse.getbody());
  371. }
  372. If(Response.attachresp == null){
  373. Response.attachresp = 'No New Files Included With Notice.';
  374. }
  375. return Response;
  376. }
  377. Else{
  378.  
  379. Response.AttachResp = 'No New Files Included With Notice.';
  380. return Response;
  381. }
  382.  
  383. }
  384. Else{
  385. Response.AttachResp = 'No Files Included With Notice.';
  386.  
  387. }
  388.  
  389. return Response;
  390. }
Add Comment
Please, Sign In to add comment