Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RallyIntegrationServices {
- public static String urlParams='&fetch=FormattedID,Name,State,Description,ScheduleState,Release,Owner,Project,Priority,c_Priority,c_AccountManager,c_Customer,BlockedReason,ObjectID,Notes,Attachments';
- public String rallyUserName;
- public String rallyPassword;
- public String rallyUrl;
- public Case caseObject{get; set;}
- public String workspace;
- public String baseUrl;
- public String cookie = '';
- public RallyObj__c rallyObj= null;
- public RallyIntegrationServices (){
- try{
- rallyObj = [SELECT Password__c, RallyURL__c, RallyUsername_c__c, Username__c, Workspace_Name__c FROM RallyObj__c order by createddate asc limit 1];
- }catch(Exception e){
- System.debug('e.exception' + e.getCause());
- }
- setWorkspaceURL();
- }
- public RallyIntegrationServices (RallyObj__c obj){
- rallyObj = obj;
- setWorkspaceURL();
- }
- public void setWorkspaceURL()
- {
- if(rallyObj!=null){
- rallyUserName= rallyObj.RallyUsername_c__c;
- rallyPassword= rallyObj.Password__c ;
- workspace = rallyObj.Workspace_Name__c;
- rallyUrl = rallyObj.RallyURL__c;
- }
- System.debug('workspace :: ' + workspace);
- if(workspace==null){
- baseUrl = 'https://rally1.rallydev.com/slm/webservice/v2.0/workspace/16636255277';
- }
- else if(workspace.equals('SANDBOX')){
- baseUrl = 'https://rally1.rallydev.com/slm/webservice/v2.0/workspace/16636255277';
- }
- else if(workspace.equals('PEOPLENET')){
- baseUrl = 'https://rally1.rallydev.com/slm/webservice/v2.0/workspace/16238506248';
- }
- }
- public String getRallyResponse(String url){
- Blob headerValue = Blob.valueOf(rallyUserName +':'+ rallyPassword );
- String auth = 'Basic ' + EncodingUtil.base64Encode(headerValue);
- String response = null;
- HTTP http = new HTTP();
- HTTPResponse authresp = new HTTPResponse();
- HTTPRequest request = new HTTPRequest();
- System.debug('url : ' + url);
- try {
- request.setHeader('Content-Type', 'application/json');
- request.setHeader('Authorization',auth);
- request.setMethod('GET');
- request.setTimeout(120000);
- request.setEndpoint(url);
- authresp = http.send(request);
- response = authresp.getBody();
- cookie = authresp.getHeader('Set-Cookie');
- System.debug('cookie : ' + cookie);
- }catch (exception e) {
- System.debug('Request Error : ' + e.getMessage() + ' : ' + e.getStackTraceString());
- response='';
- }
- return response;
- }
- public Map<String,Object> getResultsList(String respBody, String artifactType){
- try{
- Map<String,Object> queryResults = new Map<String,Object>();
- if(respBody != null){
- Map<String,Object> baseResponse = (Map<String,Object>) JSON.deserializeUntyped(respBody);
- queryResults = (artifactType=='DE') ? (Map<String,Object>) baseResponse.get('Defect') : (Map<String,Object>) baseResponse.get('HierarchicalRequirement');
- }
- return queryResults;
- }
- catch(Exception e)
- {
- System.debug('Query ' + e.getMessage() + ' : ' +e.getLineNumber());
- return null;
- }
- }
- string USorDE = null;
- public List<SearchResults> searchUpdateQuery(String query, String caseNumber, String Notes){
- List<SearchResults> data=new List<SearchResults>();
- String requestURL;
- try {
- integer lastIndexOfSlash = query.lastIndexOf('/');
- integer lastIndexOfDot = query.lastIndexOf('.');
- String objectId = query.substring(lastIndexOfSlash+1, lastIndexOfDot);
- String artifactType = '';
- if(query!=null){
- if(query.contains('defect')){
- System.debug('test line ...4');
- requestURL = 'https://rally1.rallydev.com/slm/webservice/v2.0/defect/' + objectId;
- artifactType = 'DE';
- }
- else if(query.contains('hierarchicalrequirement')){
- System.debug('test line ...5');
- requestURL = 'https://rally1.rallydev.com/slm/webservice/v2.0/hierarchicalrequirement/' + objectId;
- artifactType = 'US';
- }
- }
- Map<String,Object> artifactResult = new Map<String,Object>();
- try{
- System.debug('artifactType : ' + artifactType);
- artifactResult = getResultsList(getRallyResponse(requestURL),artifactType);
- }catch(Exception e){
- System.debug('Error: ' + e.getMessage());
- }
- if(Test.isRunningTest())
- {
- 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}}';
- artifactResult = getResultsList(respBody, 'DE');
- }
- if(artifactResult!=null && !artifactResult.isEmpty()){
- system.debug(caseNumber);
- try{
- Case caseAssoc = [SELECT Rally_ID__c, CaseNumber, RALLYSM__Development_Status__c FROM Case WHERE CaseNumber=:caseNumber LIMIT 1];
- system.debug('Compare Data : ' + (String)caseAssoc.Rally_ID__c+' '+(String)artifactResult.get('FormattedID'));
- data.add(new SearchResults((String)artifactResult.get('FormattedID'),(String)artifactResult.get('ScheduleState'),(String)artifactResult.get('Notes')));
- }
- catch(exception e){
- system.debug('In Failure Block' + e.getMessage() + ' line number : ' + e.getLineNumber());
- }
- }
- }
- catch (exception e) {
- system.debug('In ' + e.getMessage() + ' line number : ' + e.getLineNumber() );
- data = null;
- system.debug(e);
- }
- return data;
- }
- public void updateCaseInformation()
- {
- CalloutOffset__c callout = null;
- // get offset and page size of the sync
- List<CalloutOffset__c> Callouts = [select key__c, pagelimit__c, pageoffset__c from CalloutOffset__c where key__c='RallySync'];
- if(Callouts==null || callouts.isEmpty())
- {
- callout = new CalloutOffset__c();
- callout.key__c = 'RallySync';
- callout.pagelimit__c = 100;
- callout.pageoffset__c = 0;
- }
- else
- callout = callouts.get(0);
- List<Case> caseList = new List<Case>();
- if(NewCaseComm==null)
- NewCaseComm = new list<CaseComment>();
- list<Case> rallyCases = new list<Case>();
- if(workspace=='SANDBOX')
- 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];
- else if (workspace=='PEOPLENET')
- 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];
- if(Test.isRunningTest())
- {
- rallyCases = new list<Case>();
- Case ca = [select Id, casenumber,RALLYSM__Rally_Artifact_Ref__c from case where subject='Case Test'];
- rallyCases.add(ca);
- }
- Integer record = 1;
- Integer pl = rallyCases.size()> (callout.pageoffset__c + callout.pagelimit__c) ? Integer.valueOf(callout.pagelimit__c) : Integer.valueOf(rallyCases.size() - callout.pageoffset__c);
- for(Case rallyCase : rallyCases )
- {
- if(record>=callout.pageoffset__c && record < (callout.pageoffset__c + callout.pagelimit__c)){
- List<SearchResults> searchlist = searchUpdateQuery(rallyCase.RALLYSM__Rally_Artifact_Ref__c, rallyCase.CaseNumber, 'Notes');
- if(searchlist!=null && !searchlist.isEmpty())
- {
- SearchResults sr = searchlist.get(0);
- rallyCase = modifyCaseInformation(sr, rallyCase);
- caseList.add(rallyCase);
- }
- }
- record = record + 1;
- }
- try{
- System.debug('Before update........');
- try{
- callout.pageoffset__c = callout.pageoffset__c + callout.pagelimit__c;
- if(callout.pageoffset__c> rallyCases.size())
- callout.pageoffset__c = 0;
- if(Callouts==null || callouts.isEmpty())
- insert callout;
- else
- update callout;
- if(NewCaseComm!=null && !NewCaseComm.isEmpty())
- insert NewCaseComm;
- }catch(Exception nc){
- System.debug('Error in comments >>>>>>>>>>: ' + nc.getMessage() + ' : ' +nc.getCause() + ' : ' + nc.getLineNumber());
- }
- if(!caseList.isEmpty())
- {
- Integer index =0;
- try{
- update caseList;
- System.debug('Schedule update size : ' + caseList.size());
- }
- catch(Exception ex)
- {
- System.debug('inner Error >>>>>>>>>>: ' + ex.getMessage() + ' : ' +ex.getCause() + ' : ' + ex.getLineNumber());
- }
- /*List<Case> newlist = new List<Case>();
- for(Case cc : caseList)
- {
- newList.add(cc);
- if(index==20){
- try{
- update newlist;
- System.debug('Test.... Updated new list.>>>>>>>>>>>...............');
- System.debug('PL : ' + pl + ' :callout ' + callout.Id + ' :next ' + (callout.pageoffset__c + callout.pagelimit__c) + ' : cr ' + callout.pageoffset__c);
- }
- catch(Exception ex)
- {
- System.debug('inner Error >>>>>>>>>>: ' + ex.getMessage() + ' : ' +ex.getCause() + ' : ' + ex.getLineNumber());
- }
- index = 0;
- newlist = new List<Case>();
- }
- index = index+1;
- }*/
- }
- System.debug('After update........');
- }
- catch(Exception ex)
- {
- System.debug('Error >>>>>>>>>>: ' + ex.getMessage() + ' : ' +ex.getCause() + ' : ' + ex.getLineNumber());
- }
- }
- public List<CaseComment> NewCaseComm{get; set;}
- public Case modifyCaseInformation(SearchResults sResult, Case newCase)
- {
- try {
- System.debug('workspace : ' + workspace);
- System.debug('sResult.scheduleState: ' + sResult.scheduleState);
- String url = newCase.RALLYSM__Rally_Artifact_Ref__c;
- if(Test.isRunningTest())
- url = 'defect';
- // update case
- newcase.RALLYSM__Development_Status__c = sResult.scheduleState;
- newcase.Rally_ID__c = sResult.rallyID;
- if((sResult.scheduleState == 'Accepted' && url.contains('defect') && workspace.equals('SANDBOX'))
- || (sResult.scheduleState == 'Accepted' && url.contains('hierarchicalrequirement') && workspace.equals('SANDBOX'))
- || (sResult.scheduleState == 'Finalized' && url.contains('defect') && workspace.equals('PEOPLENET'))
- || (sResult.scheduleState == 'Finalized' && url.contains('hierarchicalrequirement') && workspace.equals('PEOPLENET'))){
- newcase.Status = 'Resolution Provided';
- newCase.OwnerId = '00GE0000000MPX8';
- String body = sResult.Notes.length()>4000 ? sResult.Notes.substring(0, 3999) : sResult.Notes;
- List<CaseComment> comms = [select Id, CommentBody from CaseComment where CommentBody=:body and ParentId =:newCase.Id];
- if(comms==null || comms.isEmpty()){
- CaseComment comm = new CaseComment();
- comm.ParentId = newCase.Id;
- comm.CommentBody =body;
- NewCaseComm.add(comm);
- }
- }
- System.debug('test line ...4.11111....');
- return newCase;
- }
- catch(Exception e)
- {
- system.debug('exception in update : ' +e.getMessage());
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement