Advertisement
Guest User

Untitled

a guest
Sep 13th, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.37 KB | None | 0 0
  1. public class RallyIntegrationServices {
  2. public static String urlParams='&fetch=FormattedID,Name,State,Description,ScheduleState,Release,Owner,Project,Priority,c_Priority,c_AccountManager,c_Customer,BlockedReason,ObjectID,Notes,Attachments';
  3. public String rallyUserName;
  4. public String rallyPassword;
  5. public String rallyUrl;
  6. public Case caseObject{get; set;}
  7. public String workspace;
  8. public String baseUrl;
  9. public String cookie = '';
  10. public RallyObj__c rallyObj= null;
  11.  
  12. public RallyIntegrationServices (){
  13. try{
  14. rallyObj = [SELECT Password__c, RallyURL__c, RallyUsername_c__c, Username__c, Workspace_Name__c FROM RallyObj__c order by createddate asc limit 1];
  15. }catch(Exception e){
  16. System.debug('e.exception' + e.getCause());
  17. }
  18. setWorkspaceURL();
  19. }
  20.  
  21. public RallyIntegrationServices (RallyObj__c obj){
  22. rallyObj = obj;
  23. setWorkspaceURL();
  24. }
  25.  
  26. public void setWorkspaceURL()
  27. {
  28. if(rallyObj!=null){
  29. rallyUserName= rallyObj.RallyUsername_c__c;
  30. rallyPassword= rallyObj.Password__c ;
  31. workspace = rallyObj.Workspace_Name__c;
  32. rallyUrl = rallyObj.RallyURL__c;
  33. }
  34.  
  35. System.debug('workspace :: ' + workspace);
  36.  
  37. if(workspace==null){
  38. baseUrl = 'https://rally1.rallydev.com/slm/webservice/v2.0/workspace/16636255277';
  39. }
  40. else if(workspace.equals('SANDBOX')){
  41. baseUrl = 'https://rally1.rallydev.com/slm/webservice/v2.0/workspace/16636255277';
  42. }
  43. else if(workspace.equals('PEOPLENET')){
  44. baseUrl = 'https://rally1.rallydev.com/slm/webservice/v2.0/workspace/16238506248';
  45. }
  46. }
  47.  
  48. public String getRallyResponse(String url){
  49. Blob headerValue = Blob.valueOf(rallyUserName +':'+ rallyPassword );
  50. String auth = 'Basic ' + EncodingUtil.base64Encode(headerValue);
  51. String response = null;
  52. HTTP http = new HTTP();
  53.  
  54. HTTPResponse authresp = new HTTPResponse();
  55. HTTPRequest request = new HTTPRequest();
  56.  
  57. System.debug('url : ' + url);
  58. try {
  59. request.setHeader('Content-Type', 'application/json');
  60. request.setHeader('Authorization',auth);
  61. request.setMethod('GET');
  62. request.setTimeout(120000);
  63. request.setEndpoint(url);
  64. authresp = http.send(request);
  65. response = authresp.getBody();
  66. cookie = authresp.getHeader('Set-Cookie');
  67.  
  68. System.debug('cookie : ' + cookie);
  69. }catch (exception e) {
  70. System.debug('Request Error : ' + e.getMessage() + ' : ' + e.getStackTraceString());
  71. response='';
  72. }
  73. return response;
  74. }
  75.  
  76. public Map<String,Object> getResultsList(String respBody, String artifactType){
  77. try{
  78. Map<String,Object> queryResults = new Map<String,Object>();
  79. if(respBody != null){
  80. Map<String,Object> baseResponse = (Map<String,Object>) JSON.deserializeUntyped(respBody);
  81. queryResults = (artifactType=='DE') ? (Map<String,Object>) baseResponse.get('Defect') : (Map<String,Object>) baseResponse.get('HierarchicalRequirement');
  82. }
  83. return queryResults;
  84. }
  85. catch(Exception e)
  86. {
  87. System.debug('Query ' + e.getMessage() + ' : ' +e.getLineNumber());
  88. return null;
  89. }
  90. }
  91.  
  92. string USorDE = null;
  93. public List<SearchResults> searchUpdateQuery(String query, String caseNumber, String Notes){
  94. List<SearchResults> data=new List<SearchResults>();
  95. String requestURL;
  96. try {
  97. integer lastIndexOfSlash = query.lastIndexOf('/');
  98. integer lastIndexOfDot = query.lastIndexOf('.');
  99. String objectId = query.substring(lastIndexOfSlash+1, lastIndexOfDot);
  100. String artifactType = '';
  101. if(query!=null){
  102. if(query.contains('defect')){
  103. System.debug('test line ...4');
  104. requestURL = 'https://rally1.rallydev.com/slm/webservice/v2.0/defect/' + objectId;
  105. artifactType = 'DE';
  106. }
  107. else if(query.contains('hierarchicalrequirement')){
  108. System.debug('test line ...5');
  109. requestURL = 'https://rally1.rallydev.com/slm/webservice/v2.0/hierarchicalrequirement/' + objectId;
  110. artifactType = 'US';
  111. }
  112. }
  113.  
  114. Map<String,Object> artifactResult = new Map<String,Object>();
  115. try{
  116. System.debug('artifactType : ' + artifactType);
  117. artifactResult = getResultsList(getRallyResponse(requestURL),artifactType);
  118. }catch(Exception e){
  119. System.debug('Error: ' + e.getMessage());
  120. }
  121.  
  122. if(Test.isRunningTest())
  123. {
  124. string respBody = '{"Defect":{"_rallyAPIMajor":"1","_rallyAPIMinor":"43","_ref":"https://rally1.rallydev.com/slm/webservice/1.43/defect/52080409832.js","_objectVersion":"5","_refObjectName":"Re: Test again - test to create case","CreationDate":"2016-03-01T08:59:53.525Z","_CreatedAt":"today at 3:59 am","ObjectID":52080409832,"Subscription":{"_rallyAPIMajor":"1","_rallyAPIMinor":"43","_ref":"https://rally1.rallydev.com/slm/webservice/1.43/subscription/15955256619.js","_refObjectName":"PeopleNet","_type":"Subscription"},"Workspace":{"_rallyAPIMajor":"1","_rallyAPIMinor":"43","_ref":"https://rally1.rallydev.com/slm/webservice/1.43/workspace/16636255277.js","_refObjectName":"SANDBOX","_type":"Workspace"},"Changesets":[],"Description":"Tesystem.","Discussion":[],"DisplayColor":null,"FormattedID":"DE92","LastUpdateDate":"2016-03-01T09:03:03.255Z","Name":"Re: Test again - test to create case","Notes":"Created with Rally Support Manager by Sridhar Venugopal.","Owner":{"_rallyAPIMajor":"1","_rallyAPIMinor":"43","_ref":"https://rally1.rallydev.com/slm/webservice/1.43/user/16285019298.js","_refObjectName":"Dale Humphries","_type":"User"},"Project":{"_rallyAPIMajor":"1","_rallyAPIMinor":"43","_ref":"https://rally1.rallydev.com/slm/webservice/1.43/project/26982392463.js","_refObjectName":"Account Management","_type":"Project"},"Ready":false,"RevisionHistory":{"_rallyAPIMajor":"1","_rallyAPIMinor":"43","_ref":"https://rally1.rallydev.com/slm/webservice/1.43/revisionhistory/52080409833.js","_type":"RevisionHistory"},"Tags":[],"ScheduleState":"Accepted","AcceptedDate":"2016-03-01T09:03:03.247Z","AffectsDoc":false,"Attachments":[],"Blocked":false,"BlockedReason":null,"Blocker":null,"ClosedDate":null,"Duplicates":[],"Environment":"Production","FixedInBuild":null,"FoundInBuild":null,"InProgressDate":"2016-03-01T09:02:56.482Z","Iteration":null,"OpenedDate":null,"Package":null,"PlanEstimate":null,"Priority":"Medium","Rank":3,"Recycled":false,"Release":null,"ReleaseNote":false,"Requirement":null,"Resolution":"None","SalesforceCaseID":null,"SalesforceCaseNumber":null,"Severity":"None","State":"Submitted","SubmittedBy":{"_rallyAPIMajor":"1","_rallyAPIMinor":"43","_ref":"https://rally1.rallydev.com/slm/webservice/1.43/user/32674398804.js","_refObjectName":"SridharV","_type":"User"},"TargetBuild":null,"TargetDate":null,"TaskActualTotal":0,"TaskEstimateTotal":0,"TaskRemainingTotal":0,"TaskStatus":"NONE","Tasks":[],"TestCase":null,"TestCaseResult":null,"TestCaseStatus":"NONE","TestCases":[],"VerifiedInBuild":null,"AccountManager":null,"AMdeprecated":null,"Customer":null,"Customerdeprecated":null,"GroupBranch":null,"TfsId":null}}';
  125. artifactResult = getResultsList(respBody, 'DE');
  126. }
  127.  
  128. if(artifactResult!=null && !artifactResult.isEmpty()){
  129. system.debug(caseNumber);
  130. try{
  131. Case caseAssoc = [SELECT Rally_ID__c, CaseNumber, RALLYSM__Development_Status__c FROM Case WHERE CaseNumber=:caseNumber LIMIT 1];
  132. system.debug('Compare Data : ' + (String)caseAssoc.Rally_ID__c+' '+(String)artifactResult.get('FormattedID'));
  133. data.add(new SearchResults((String)artifactResult.get('FormattedID'),(String)artifactResult.get('ScheduleState'),(String)artifactResult.get('Notes')));
  134. }
  135. catch(exception e){
  136. system.debug('In Failure Block' + e.getMessage() + ' line number : ' + e.getLineNumber());
  137. }
  138. }
  139. }
  140. catch (exception e) {
  141. system.debug('In ' + e.getMessage() + ' line number : ' + e.getLineNumber() );
  142. data = null;
  143. system.debug(e);
  144. }
  145. return data;
  146. }
  147.  
  148. public void updateCaseInformation()
  149. {
  150. CalloutOffset__c callout = null;
  151. // get offset and page size of the sync
  152. List<CalloutOffset__c> Callouts = [select key__c, pagelimit__c, pageoffset__c from CalloutOffset__c where key__c='RallySync'];
  153. if(Callouts==null || callouts.isEmpty())
  154. {
  155. callout = new CalloutOffset__c();
  156. callout.key__c = 'RallySync';
  157. callout.pagelimit__c = 100;
  158. callout.pageoffset__c = 0;
  159. }
  160. else
  161. callout = callouts.get(0);
  162.  
  163.  
  164. List<Case> caseList = new List<Case>();
  165. if(NewCaseComm==null)
  166. NewCaseComm = new list<CaseComment>();
  167.  
  168. list<Case> rallyCases = new list<Case>();
  169. if(workspace=='SANDBOX')
  170. rallyCases =[select Id,RALLYSM__Rally_Artifact_Ref__c,casenumber,RALLYSM__Development_Status__c from case where RALLYSM__Rally_Artifact_Ref__c != null and (RALLYSM__Development_Status__c !='Accepted' or RALLYSM__Development_Status__c != 'Finalized') and (Status != 'Resolution Provided' and Status != 'Closed') limit 49000];
  171. else if (workspace=='PEOPLENET')
  172. rallyCases =[select Id,RALLYSM__Rally_Artifact_Ref__c,casenumber,RALLYSM__Development_Status__c from case where RALLYSM__Rally_Artifact_Ref__c != null and RALLYSM__Development_Status__c != 'Finalized' and (Status != 'Resolution Provided' and Status != 'Closed') limit 49000];
  173.  
  174. if(Test.isRunningTest())
  175. {
  176. rallyCases = new list<Case>();
  177. Case ca = [select Id, casenumber,RALLYSM__Rally_Artifact_Ref__c from case where subject='Case Test'];
  178. rallyCases.add(ca);
  179. }
  180.  
  181. Integer record = 1;
  182. Integer pl = rallyCases.size()> (callout.pageoffset__c + callout.pagelimit__c) ? Integer.valueOf(callout.pagelimit__c) : Integer.valueOf(rallyCases.size() - callout.pageoffset__c);
  183.  
  184. for(Case rallyCase : rallyCases )
  185. {
  186. if(record>=callout.pageoffset__c && record < (callout.pageoffset__c + callout.pagelimit__c)){
  187. List<SearchResults> searchlist = searchUpdateQuery(rallyCase.RALLYSM__Rally_Artifact_Ref__c, rallyCase.CaseNumber, 'Notes');
  188. if(searchlist!=null && !searchlist.isEmpty())
  189. {
  190. SearchResults sr = searchlist.get(0);
  191. rallyCase = modifyCaseInformation(sr, rallyCase);
  192. caseList.add(rallyCase);
  193. }
  194. }
  195. record = record + 1;
  196. }
  197.  
  198. try{
  199. System.debug('Before update........');
  200. try{
  201. callout.pageoffset__c = callout.pageoffset__c + callout.pagelimit__c;
  202. if(callout.pageoffset__c> rallyCases.size())
  203. callout.pageoffset__c = 0;
  204.  
  205. if(Callouts==null || callouts.isEmpty())
  206. insert callout;
  207. else
  208. update callout;
  209.  
  210. if(NewCaseComm!=null && !NewCaseComm.isEmpty())
  211. insert NewCaseComm;
  212.  
  213. }catch(Exception nc){
  214. System.debug('Error in comments >>>>>>>>>>: ' + nc.getMessage() + ' : ' +nc.getCause() + ' : ' + nc.getLineNumber());
  215. }
  216.  
  217. if(!caseList.isEmpty())
  218. {
  219. Integer index =0;
  220. try{
  221. update caseList;
  222. System.debug('Schedule update size : ' + caseList.size());
  223. }
  224. catch(Exception ex)
  225. {
  226. System.debug('inner Error >>>>>>>>>>: ' + ex.getMessage() + ' : ' +ex.getCause() + ' : ' + ex.getLineNumber());
  227. }
  228. /*List<Case> newlist = new List<Case>();
  229. for(Case cc : caseList)
  230. {
  231. newList.add(cc);
  232. if(index==20){
  233. try{
  234. update newlist;
  235. System.debug('Test.... Updated new list.>>>>>>>>>>>...............');
  236. System.debug('PL : ' + pl + ' :callout ' + callout.Id + ' :next ' + (callout.pageoffset__c + callout.pagelimit__c) + ' : cr ' + callout.pageoffset__c);
  237. }
  238. catch(Exception ex)
  239. {
  240. System.debug('inner Error >>>>>>>>>>: ' + ex.getMessage() + ' : ' +ex.getCause() + ' : ' + ex.getLineNumber());
  241. }
  242. index = 0;
  243. newlist = new List<Case>();
  244. }
  245. index = index+1;
  246. }*/
  247. }
  248. System.debug('After update........');
  249. }
  250. catch(Exception ex)
  251. {
  252. System.debug('Error >>>>>>>>>>: ' + ex.getMessage() + ' : ' +ex.getCause() + ' : ' + ex.getLineNumber());
  253. }
  254. }
  255.  
  256. public List<CaseComment> NewCaseComm{get; set;}
  257.  
  258. public Case modifyCaseInformation(SearchResults sResult, Case newCase)
  259. {
  260. try {
  261. System.debug('workspace : ' + workspace);
  262. System.debug('sResult.scheduleState: ' + sResult.scheduleState);
  263. String url = newCase.RALLYSM__Rally_Artifact_Ref__c;
  264.  
  265. if(Test.isRunningTest())
  266. url = 'defect';
  267.  
  268. // update case
  269. newcase.RALLYSM__Development_Status__c = sResult.scheduleState;
  270. newcase.Rally_ID__c = sResult.rallyID;
  271. if((sResult.scheduleState == 'Accepted' && url.contains('defect') && workspace.equals('SANDBOX'))
  272. || (sResult.scheduleState == 'Accepted' && url.contains('hierarchicalrequirement') && workspace.equals('SANDBOX'))
  273. || (sResult.scheduleState == 'Finalized' && url.contains('defect') && workspace.equals('PEOPLENET'))
  274. || (sResult.scheduleState == 'Finalized' && url.contains('hierarchicalrequirement') && workspace.equals('PEOPLENET'))){
  275. newcase.Status = 'Resolution Provided';
  276. newCase.OwnerId = '00GE0000000MPX8';
  277.  
  278. String body = sResult.Notes.length()>4000 ? sResult.Notes.substring(0, 3999) : sResult.Notes;
  279.  
  280. List<CaseComment> comms = [select Id, CommentBody from CaseComment where CommentBody=:body and ParentId =:newCase.Id];
  281.  
  282. if(comms==null || comms.isEmpty()){
  283. CaseComment comm = new CaseComment();
  284. comm.ParentId = newCase.Id;
  285. comm.CommentBody =body;
  286. NewCaseComm.add(comm);
  287. }
  288. }
  289. System.debug('test line ...4.11111....');
  290. return newCase;
  291. }
  292. catch(Exception e)
  293. {
  294. system.debug('exception in update : ' +e.getMessage());
  295. return null;
  296. }
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement