Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.46 KB | None | 0 0
  1. **APEX-CLASS**
  2. public class TriggerFlodataWorkflowController
  3. {
  4. public string inputIds{get;set;}
  5. public List<String> selectedNames{get;set;}
  6. public List<String> selectedIds{get;set;}
  7. public List<selectOption> options{get;set;}
  8. public String selectedOption{get;set;}
  9. public Map<String, String> optionToTransID{get;set;}
  10. public List<String> listOfStrings {get; set;}
  11. public Map<String, Id> productNameToIdMap{get;set;}
  12. public Map<Id, String> productIdToNameMap{get;set;}
  13. public String selectedProduct{get;set;}
  14. public Map<String, String> objToTransIDMap{get;set;}
  15. Map<String,FloDataTransactionIds__c> transIDSetup{get;set;}
  16. Map<String, String> nameToLabelMap{get;set;}
  17. Map<string, FloDataTransactionIds__c> helpTextMap{get;set;}
  18. Map<String, Boolean> notsObjectMap {get;set;}
  19. public List<CPQMigrationJob__c> existingJobs{get;set;}
  20. public String runId{get;set;}
  21. public String currentJobId{get;set;}
  22. public String tabNumber{get;set;}
  23. public Map<Id, List<CPQMigrationJobDetails__c>> jobToDetailsMap{get;set;}
  24. public string message{get;set;}
  25. public List<string> logDetails{get;set;}
  26. public FloDataTransactionIds__c transactionRecord{get;set;}
  27. public List<Map<String, Object>> summaryMap{get;set;}
  28. public List<string> keys{get;set;}
  29.  
  30. public TriggerFlodataWorkflowController()
  31. {
  32. tabNumber = '1';
  33. transactionRecord = new FloDataTransactionIds__c();
  34. helpTextMap = new Map<string, FloDataTransactionIds__c>();
  35. selectedIds = new List<String>();
  36. productIdToNameMap = new Map<Id, String>();
  37. selectedNames = new List<String>();
  38. jobToDetailsMap = new Map<Id, List<CPQMigrationJobDetails__c>>();
  39. existingJobs = [
  40. SELECT Id,SubmittedDateTime__c, FloName__c, Status__c,FlodataRunId__c,owner.Name, SubmittedBy__c , EndDateTime__c,
  41. (SELECT Id,ComponentID__c,ComponentName__c,Status__c FROM CPQMigrationJobDetails__r)
  42. FROM CPQMigrationJob__c ORDER By SubmittedDateTime__c DESC
  43. ];
  44. currentJobId = existingJobs[0].Id;
  45. runId = existingJobs[0].FlodataRunId__c;
  46. for(CPQMigrationJob__c job : existingJobs)
  47. {
  48. if(job.getSobjects('CPQMigrationJobDetails__r') != null)
  49. {
  50. jobToDetailsMap.put(job.Id, job.getSobjects('CPQMigrationJobDetails__r'));
  51. }
  52. else
  53. {
  54. jobToDetailsMap.put(job.Id, new List<CPQMigrationJobDetails__c>());
  55. }
  56. }
  57. system.debug('jobToDetailsMap -- > '+jobToDetailsMap);
  58. objToTransIDMap = new Map<String, String>();
  59. nameToLabelMap = new Map<String, String>();
  60. notsObjectMap = new Map<String, Boolean>();
  61. options = new List<selectOption>();
  62. transIDSetup = FloDataTransactionIds__c.getAll();
  63. options.add(new SelectOption('','--None--'));
  64. selectedOption = '';
  65. for(FloDataTransactionIds__c data : transIDSetup.values())
  66. {
  67. objToTransIDMap.put(data.ObjectAPIName__c, data.TransactionID__c);
  68. SelectOption option = new SelectOption(data.ObjectAPIName__c,data.Name);
  69. option.setEscapeItem(false);
  70. options.add(option);
  71. notsObjectMap.put(data.ObjectAPIName__c, data.NotAnSObject__c);
  72. nameToLabelMap.put(data.ObjectAPIName__c, data.Name);
  73. helpTextMap.put(data.ObjectAPIName__c,data);
  74. }
  75. inputIds = '';
  76. productNameToIdMap = new Map<String, Id>();
  77. listOfStrings = new List<String>();
  78. }
  79. public void triggerFlowData()
  80. {
  81. HttpRequest req = new HttpRequest();
  82. HttpResponse res = new HttpResponse();
  83. Http http = new Http();
  84. system.debug('notsObjectMap'+notsObjectMap);
  85. if(notsObjectMap.get(selectedOption))
  86.  
  87. {
  88.  
  89. req.setEndpoint('callout:FlodataEndPoint/api/sendMessage?transId='+objToTransIDMap.get(selectedOption));
  90.  
  91. }
  92. else
  93. {
  94. req.setEndpoint('callout:FlodataEndPoint/api/sendMessage?transId='+objToTransIDMap.get(selectedOption)+'&RecordId='+inputIds);
  95. }
  96. req.setMethod('POST');
  97. req.setCompressed(true);
  98. try {
  99. res = http.send(req);
  100. system.debug('result '+res);
  101. if(res.getStatusCode() == 200)
  102. {
  103. ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.CONFIRM, 'Request submitted!'));
  104. String[] finalSelectedIds = inputIds.split(',');
  105. inputIds = '';
  106. selectedIds = new List<String>();
  107. Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
  108. system.debug('response body -- > '+results.get('runId'));
  109. selectedProduct = '';
  110. CPQMigrationJob__c job = new CPQMigrationJob__c();
  111. job.FloName__c = nameToLabelMap.get(selectedOption);
  112. job.SubmittedDateTime__c = System.Now();
  113. job.status__c = 'Submitted';
  114. job.FlodataRunId__c = String.valueOf(results.get('runId'));
  115. job.External_Id__c = string.valueOf(results.get('runName'));
  116. insert job;
  117. existingJobs.add(job);
  118. system.debug('job id '+job);
  119. List<Id> detailIds = new List<Id>();
  120. List<CPQMigrationJobDetails__c> details = new List<CPQMigrationJobDetails__c>();
  121. if(!notsObjectMap.get(selectedOption)){
  122. system.debug('not sobject');
  123. for(String selectedId : finalSelectedIds)
  124. {
  125. String idTrimmed = selectedId.remove(''');
  126. CPQMigrationJobDetails__c detail = new CPQMigrationJobDetails__c();
  127. detail.Job__c = job.Id;
  128. detail.ComponentID__c = selectedId;
  129. detail.ComponentName__c = productIdToNameMap.get(idTrimmed);
  130. detail.Status__c = 'Submitted';
  131. details.add(detail);
  132. }
  133. insert details;
  134.  
  135. jobToDetailsMap.put(job.Id, details);
  136. }
  137. // Modified to create new record for blanket migration i.e., not sobject - 8-5-19
  138. else
  139. {
  140. CPQMigrationJobDetails__c detail = new CPQMigrationJobDetails__c();
  141. detail.Job__c = job.Id;
  142. //detail.ComponentID__c = selectedId;
  143. detail.ComponentName__c = job.FloName__c;
  144. detail.Status__c = 'Submitted';
  145. //detail.External_Id__c = detail.name;
  146. insert detail;
  147.  
  148. system.debug('detail '+detail);
  149. }
  150.  
  151. //CPQMigrationJobDetails__c
  152. existingJobs = [
  153. SELECT Id, SubmittedDateTime__c, Status__c, FloName__c,Owner.Name,SubmittedBy__c,
  154. FlodataRunId__c, EndDateTime__c
  155. FROM CPQMigrationJob__c ORDER By SubmittedDateTime__c DESC
  156. ];
  157. }
  158. } catch(System.CalloutException e) {
  159. System.debug('Callout error: '+ e);
  160. System.debug(res.toString());
  161. }
  162. }
  163.  
  164.  
  165. public void fetchStatus()
  166. {
  167. HttpRequest req = new HttpRequest();
  168. HttpResponse res = new HttpResponse();
  169. Http http = new Http();
  170. req.setEndpoint('callout:FlodataEndPoint/api/getJobStatus?runId='+runId);
  171. req.setMethod('GET');
  172. req.setCompressed(true);
  173. try {
  174. res = http.send(req);
  175. if(res.getStatusCode() == 200)
  176. {
  177. inputIds = '';
  178. Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
  179. system.debug('response body -- > '+results);
  180. String newStatus = String.valueOf(results.get('jobStatus'));
  181. string s = string.valueOf(results.get('jobCompletedTime'));
  182. if(s != null)
  183. s = s.replace('/','-');
  184.  
  185. system.debug('s date '+s);
  186. //Datetime d = Datetime.valueOf(s);
  187. Id migrationJobId;
  188. List<CPQMigrationJobDetails__c> updateMigrationJobDetails = new List<CPQMigrationJobDetails__c>();
  189. for(CPQMigrationJob__c job : existingJobs)
  190. {
  191. if(job.FlodataRunId__c == runId)
  192. {
  193. job.Status__c = newStatus;
  194. if(s != null)
  195. job.EndDateTime__c = DateTime.Valueof(s);
  196. //system.debug('test date '+jobDetail.EndDateTime__c);
  197.  
  198. // Modified by sridevi to update Migration Job Detail Records - 8-5-19
  199. if(jobToDetailsMap.containsKey(job.id) && jobToDetailsMap.get(job.id) != null) {
  200. for(CPQMigrationJobDetails__c jobDetail : jobToDetailsMap.get(job.id)) {
  201. jobDetail.Status__c = newStatus;
  202. if(s != null) {
  203.  
  204. //jobDetail.EndDateTime__c = datetime.parse(string.valueOf(results.get('jobCompletedTime')));
  205.  
  206. }
  207. updateMigrationJobDetails.add(jobDetail);
  208. }
  209. jobToDetailsMap.put(job.id, updateMigrationJobDetails);
  210. }
  211. // End
  212.  
  213. }
  214. }
  215. update existingJobs;
  216. update updateMigrationJobDetails;
  217.  
  218. }
  219. } catch(System.CalloutException e) {
  220. System.debug('Callout error: '+ e);
  221. System.debug(res.toString());
  222. }
  223. }
  224.  
  225. public void addProdID()
  226. {
  227. system.debug('input ids '+inputIds);
  228. String selectedProdId = productNameToIdMap.get(selectedProduct);
  229. system.debug('selectedProdId -- > '+selectedProdId);
  230. system.debug('selected ids '+selectedIds);
  231. selectedNames.add(selectedProduct);
  232. if(!inputIds.contains(selectedProdId)){
  233. if(inputIds == '')
  234. {
  235. inputIds = '''+selectedProdId+''';
  236. }
  237. else
  238. {
  239. inputIds += ','+'''+selectedProdId+''';
  240. }
  241. selectedIds.add(selectedProdId);
  242. }
  243. system.debug('inputIds -- > '+inputIds);
  244. }
  245.  
  246. public void refreshCollections()
  247. {
  248. system.debug('refreshCollections -- > ');
  249. listOfStrings = new List<String>();
  250. productNameToIdMap = new Map<String, Id>();
  251. selectedProduct = '';
  252. inputIDs = '';
  253. system.debug('selected op '+selectedOption);
  254. system.debug('help text map '+helpTextMap);
  255. if(selectedOption != null && selectedOption != '' && selectedOption != 'NoObj' && selectedOption != 'PreReq')
  256. {
  257. String query = 'Select Id, Name from '+selectedOption;
  258. for(sObject obj : Database.query(query))
  259. {
  260. listOfStrings.add(String.valueOf(obj.get('name')));
  261. productIdToNameMap.put((Id)obj.get('id'), String.valueOf(obj.get('name')));
  262. productNameToIdMap.put(String.valueOf(obj.get('name')), (Id)obj.get('id'));
  263. }
  264.  
  265. }
  266. else
  267. {
  268. listOfStrings = new List<String>();
  269. productNameToIdMap = new Map<String, Id>();
  270. }
  271. // Below logic is to get the help text for the object
  272. if(helpTextMap.containsKey(selectedOption))
  273. transactionRecord = helpTextMap.get(SelectedOption);
  274.  
  275. system.debug('listOfStrings -- > '+listOfStrings);
  276.  
  277. }
  278.  
  279. public void switchTab()
  280. {
  281. tabNumber = 'Details';
  282. // Modified by sridevi to update the map when new job is created to display in the vf - 8-5-19
  283. if(currentJobId != null && !jobToDetailsMap.containsKey(currentJobId)) {
  284. List<CPQMigrationJobDetails__c> jobDetails = [select id,Status__c,Job__r.FlodataRunId__c,
  285. ComponentName__c from CPQMigrationJobDetails__c where Job__c =: currentJobId];
  286. jobToDetailsMap.put(currentJobId, jobDetails);
  287. }
  288. }
  289. public pageReference back() {
  290. pageReference pg = new PageReference('/apex/TriggerFlodataWorkflow');
  291. return pg;
  292. }
  293. // Below method is used to get the migration result
  294. public void getSummaryDetail() {
  295. summaryMap = new List<Map<String, Object>>();
  296. if(runId != null && runId != '') {
  297. // Below query is to get the summary from "Mail Summary" field from Migration Job object.
  298. CPQMigrationJob__c job = [select id, FlodataRunId__c, Mail_Summary__c from CPQMigrationJob__c where FlodataRunId__c =: runId];
  299. if(job.Mail_Summary__c != null) {
  300. Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(job.Mail_Summary__c);
  301. Object obj = results.get('MailSummary');
  302. List<Object> responseList = (List<Object>)obj;
  303. summaryMap = new List<Map<String, Object>>();
  304. for(Object objt : responseList) {
  305. Map<String, Object> singleMap = (Map<String, Object>)objt;
  306. if(singleMap.get('Total') == null)
  307. singleMap.put('Total',0);
  308. if(singleMap.get('Error') == null)
  309. singleMap.put('Error',0);
  310. if(singleMap.get('Success') == null)
  311. singleMap.put('Success',0);
  312. if(singleMap.get('Object') != null && singleMap.get('Object') != 'Object')
  313. summaryMap.add(singleMap);
  314. }
  315. keys = new List<string>{'Object','Operation','Success','Error','Total'};
  316. }
  317. }
  318. tabNumber = 'Summary';
  319. }
  320. // Below method is used to get log detail for the migration
  321. public void getLogs() {
  322. logDetails = null;
  323. HttpRequest req = new HttpRequest();
  324. HttpResponse res = new HttpResponse();
  325. Http http = new Http();
  326. // callout to get the log detail from flo data
  327. req.setEndpoint('callout:FlodataEndPoint/api/getLog?runId='+runId);
  328. req.setMethod('GET');
  329. req.setCompressed(true);
  330. string log = null;
  331. try {
  332. // sending the http request
  333. res = http.send(req);
  334. if(res.getStatusCode() == 200)
  335. {
  336. Map<string,object> resultMap = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
  337. if(resultMap.get('log') != null)
  338. log = string.valueOf(resultMap.get('log'));
  339. if(log != null)
  340. logDetails = log.split('n');
  341. }
  342. }catch(System.CalloutException e) {
  343. System.debug('Callout error: '+ e);
  344. }
  345.  
  346. tabNumber = 'Logs';
  347.  
  348. }
  349.  
  350. @isTest
  351. global class TriggerFlodataWorkflowControllerTest implements HttpCalloutMock
  352. {
  353. public string inputIds1{get;set;}
  354. public List<String> selectedNames1{get;set;}
  355. public List<String> selectedIds1{get;set;}
  356. public String selectedOption{get;set;}
  357.  
  358. public List<selectOption> options1{get;set;}
  359. public Map<String, String> optionToTransID1{get;set;}
  360. public List<String> listOfStrings1 {get; set;}
  361. public Map<String, Id> productNameToIdMap1{get;set;}
  362. public Map<Id, String> productIdToNameMap1{get;set;}
  363. public String selectedProduct1{get;set;}
  364. public Map<String, String> objToTransIDMap1{get;set;}
  365. public Map<String,FloDataTransactionIds__c> transIDSetup1{get;set;}
  366. Map<String, String> nameToLabelMap1{get;set;}
  367. Map<string, FloDataTransactionIds__c> helpTextMap1{get;set;}
  368. Map<String, Boolean> notsObjectMap1 {get;set;}
  369. public List<CPQMigrationJob__c> existingJobs1{get;set;}
  370. public String runId1{get;set;}
  371. public String currentJobId1{get;set;}
  372. public String tabNumber1{get;set;}
  373. public Map<Id, List<CPQMigrationJobDetails__c>> jobToDetailsMap1{get;set;}
  374. public string message1{get;set;}
  375. public List<string> logDetails1{get;set;}
  376. public FloDataTransactionIds__c transactionRecord1{get;set;}
  377. public List<Map<String, Object>> summaryMap1{get;set;}
  378. public List<string> keys1{get;set;}
  379.  
  380. static void setUpData(){
  381.  
  382. CPQMigrationJob__c CPQobj = new CPQMigrationJob__c();
  383. CPQobj.SubmittedDateTime__c = DateTime.parse('05/22/2012 11:46 AM');
  384. CPQobj.FloName__c = 'Blanket Migration';
  385. CPQobj.Status__c = 'Completed';
  386. CPQobj.FlodataRunId__c = '9428';
  387. insert CPQobj;
  388.  
  389. CPQMigrationJobDetails__c CPQMigobj = new CPQMigrationJobDetails__c();
  390. CPQMigobj.ComponentID__c = '01tK0000006ceK5IAI';
  391. CPQMigobj.Job__c = CPQobj.Id ;
  392. CPQMigobj.ComponentName__c = 'Blanket';
  393. CPQMigobj.Status__c = 'Blanket';
  394.  
  395. insert CPQMigobj;
  396.  
  397. FloDataTransactionIds__c FloObj =new FloDataTransactionIds__c ();
  398. FloObj.TransactionID__c='924';
  399. FloObj.ObjectAPIName__c ='Apttus_Config2__ProductAttributeRule__c';
  400. FloObj.Name='Attribute Rule';
  401. FloObj.NotAnSObject__c=True;
  402. insert FloObj;
  403. string inputIds = '';
  404.  
  405. Map<String, Boolean> notsObjectMap = new Map<String, Boolean>();
  406. notsObjectMap.put( FloObj.ObjectAPIName__c, FloObj.NotAnSObject__c);
  407.  
  408.  
  409.  
  410. }
  411.  
  412.  
  413. static testmethod void test1(){
  414.  
  415.  
  416. setUpData();
  417. Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
  418. Test.startTest();
  419. TriggerFlodataWorkflowController controllerObj = new TriggerFlodataWorkflowController();
  420. String selectedOption = 'Apttus_Config2__ProductAttributeRule__c' ;
  421. controllerObj.triggerFlowData();
  422. Test.stopTest();
  423.  
  424. }
  425.  
  426. global HTTPResponse respond(HTTPRequest request) {
  427. HttpResponse response = new HttpResponse();
  428. response.setHeader('Content-Type', 'application/xml');
  429. response.setBody('{"animal": {"id":1, "name":"Tiger"}}');
  430. response.setStatusCode(200);
  431. return response;
  432. }
  433.  
  434. @isTest
  435. static void testPostCallout() {
  436. Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
  437. test.startTest();
  438.  
  439. TriggerFlodataWorkflowController controllerObj = new TriggerFlodataWorkflowController();
  440. StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
  441. mock.setStaticResource('mockResponse');
  442. mock.setStatusCode(200);
  443. mock.setHeader('Content-Type', 'application/json');
  444. controllerObj.fetchStatus();
  445.  
  446. test.stoptest();
  447. }
  448.  
  449. static testmethod void test2(){
  450. setUpData();
  451. Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
  452. Test.startTest();
  453. TriggerFlodataWorkflowController controllerObj = new TriggerFlodataWorkflowController();
  454. controllerObj.fetchStatus();
  455. Test.stopTest();
  456. }
  457.  
  458. static testmethod void test3(){
  459. setUpData();
  460. Test.startTest();
  461. TriggerFlodataWorkflowController controllerObj = new TriggerFlodataWorkflowController();
  462. controllerObj.addProdID();
  463.  
  464. Test.stopTest();
  465. }
  466. static testmethod void test4(){
  467. setUpData();
  468. Test.startTest();
  469. TriggerFlodataWorkflowController controllerObj = new TriggerFlodataWorkflowController();
  470.  
  471. Test.stopTest();
  472. }
  473. static testmethod void test5(){
  474. setUpData();
  475. Test.startTest();
  476. TriggerFlodataWorkflowController controllerObj = new TriggerFlodataWorkflowController();
  477. controllerObj.switchTab();
  478. Test.stopTest();
  479. }
  480.  
  481. static testmethod void urltest() {
  482. setUpData();
  483. Test.startTest();
  484. TriggerFlodataWorkflowController controllerobj = new TriggerFlodataWorkflowController();
  485. controllerObj.back();
  486. Test.stopTest();
  487. pageReference pg = new PageReference('/apex/TriggerFlodataWorkflow');
  488. Test.setCurrentPage(pg);
  489. }
  490. static testmethod void test6(){
  491. setUpData();
  492. Test.startTest();
  493. TriggerFlodataWorkflowController controllerObj = new TriggerFlodataWorkflowController();
  494. controllerObj.getSummaryDetail();
  495. Test.stopTest();
  496. }
  497. static testmethod void test7(){
  498. setUpData();
  499. Test.startTest();
  500. Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
  501. TriggerFlodataWorkflowController controllerObj = new TriggerFlodataWorkflowController();
  502. controllerObj.getLogs();
  503. Test.stopTest();
  504. }
  505. static testmethod void test8(){
  506. setUpData();
  507. Test.startTest();
  508. TriggerFlodataWorkflowController controllerObj = new TriggerFlodataWorkflowController();
  509. controllerObj.refreshCollections();
  510. Test.stopTest();
  511. }
  512.  
  513. global class MockHttpResponseGenerator implements HttpCalloutMock {
  514.  
  515. global HTTPResponse respond(HTTPRequest req) {
  516.  
  517. // System.assertEquals('allout:FlodataEndPoint/api/getJobStatus?
  518. [![enter image description here][1]][1]runId=9428', req.getEndpoint());
  519. // System.assertEquals('GET', req.getMethod());
  520. HttpResponse res = new HttpResponse();
  521. res.setHeader('Content-Type', 'application/xml');
  522.  
  523.  
  524. return res;
  525. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement