Created
July 6, 2011 18:55
-
-
Save recalde/1068037 to your computer and use it in GitHub Desktop.
CPP_Builder_Rob_Controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
global with sharing class CPPBuilderRob { | |
public CPPBuilderRob() { | |
// Remember: @RemoteActions are static, hence no controller has no state | |
} | |
@RemoteAction | |
global static CPP_Report_Poller GetReportGroups(string opportunityId) { | |
// Query from CPP_Report_Request__c | |
CPP_Report_Request__c[] reportRequest = | |
[select Id, Report_Name__c, Sort_Order__c, Status__c, IsStandard__c, IsManual__c, Source_System__c, ContentId__c, Proposal_Id__c, OpportunityId__c | |
from CPP_Report_Request__c | |
where OpportunityId__c = :opportunityId | |
order by Sort_Order__c]; | |
// Group by Proposal ID | |
List<CPP_Report_Group> reportGroups = new List<CPP_Report_Group>(); | |
integer groupNumber = 1; | |
for (CPP_Report_Request__c request:reportRequest) { | |
CPP_Report_Group reportGroup = FindGroup(reportGroups, request); | |
// If group doesn't already exist then create one | |
if (reportGroup == null) { | |
reportGroup = new CPP_Report_Group(); | |
reportGroup.ProposalID = request.Proposal_Id__c; | |
reportGroup.GroupName = 'Proposal ' + request.Proposal_Id__c; | |
reportGroup.GroupNumber = groupNumber; | |
groupNumber++; | |
reportGroups.add(reportGroup); | |
} | |
reportGroup.reports.add(request); | |
} | |
CPP_Report_Poller poller = new CPP_Report_Poller(); | |
poller.LastModifiedDate = GetLastModifiedDate(opportunityId); | |
poller.Groups = reportGroups; | |
return poller; | |
} | |
@RemoteAction | |
global static void RemoveReport(string reportId) { | |
CPP_Report_Request__c report = [select Id from CPP_Report_Request__c where Id = :reportId]; | |
Delete report; | |
} | |
@RemoteAction | |
global static DateTime GetLastModifiedDate(string opportunityId) { | |
// Query from CPP_Report_Request__c | |
CPP_Report_Request__c[] reportRequest = | |
[select LastModifiedDate from CPP_Report_Request__c | |
where OpportunityId__c = :opportunityId | |
order by LastModifiedDate DESC limit 1]; | |
if (reportRequest.size() > 0) | |
return reportRequest[0].LastModifiedDate; | |
else | |
return DateTime.now(); | |
} | |
// Lookup a group based on its Proposal Id | |
global static CPP_Report_Group FindGroup(List<CPP_Report_Group> reportGroups, CPP_Report_Request__c reportRequest) { | |
for (CPP_Report_Group reportGroup:reportGroups) { | |
if (reportGroup.ProposalID == reportRequest.Proposal_Id__c) | |
return reportGroup; | |
} | |
return null; | |
} | |
// This is the page-level view-model | |
global class CPP_Report_Poller { | |
public DateTime LastModifiedDate { get; set; } | |
public List<CPP_Report_Group> Groups { get; set;} | |
public CPP_Report_Poller() { | |
} | |
} | |
// Groups are rendered as a tab in the visual force page. | |
global class CPP_Report_Group { | |
public string ProposalID { get; set; } | |
public string GroupName { get; set; } | |
public integer GroupNumber { get; set; } | |
public List<CPP_Report_Request__c> Reports { get; set;} | |
public CPP_Report_Group() { | |
Reports = new List<CPP_Report_Request__c>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment