Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.97 KB | None | 0 0
  1. public class timeAllocation_handler {
  2. Report currentReport;
  3. Reports.ReportMetadata reportMeta;
  4. Reports.ReportResults reportResult;
  5.  
  6. public timeAllocation_handler()
  7. {
  8. currentReport = [SELECT Id FROM Report WHERE DeveloperName = 'Time_Allocation_for_Account'];
  9. Account a = [SELECT Id FROM Account WHERE Name = 'Account Name'];
  10.  
  11. Reports.ReportDescribeResult reportDescribe = Reports.ReportManager.describeReport(currentReport.Id);
  12. reportMeta = reportDescribe.getReportMetadata();
  13.  
  14. Reports.ReportFilter[] reportFilters = new Reports.ReportFilter[]{};
  15.  
  16. Reports.ReportFilter rf = new Reports.ReportFilter('FK_ACC_ID','equals',a.Id+'');
  17.  
  18. rf = new Reports.ReportFilter('FK_ACC_ID','equals',a.Id);
  19. reportFilters.add(rf);
  20.  
  21. //this will be set to equals in the live version
  22. rf = new Reports.ReportFilter('for__Timesheet2__c.for__ActivityDate__c','lessThan','LAST_MONTH');
  23. reportFilters.add(rf);
  24.  
  25. rf = new Reports.ReportFilter('for__Timesheet2__c.for__ActivityDate__c','equals','THIS_YEAR');
  26. reportFilters.add(rf);
  27.  
  28. reportMeta.setReportFilters(reportFilters);
  29. }
  30.  
  31. public Reports.ReportResults getReport()
  32. {
  33. if(reportResult == null)
  34. {
  35. reportResult = Reports.ReportManager.runReport(currentReport.Id, reportMeta);
  36. }
  37.  
  38. return reportResult;
  39. }
  40.  
  41. Map<String,reportItem> reportItemMap = new Map<String,ReportItem>();
  42. Map<String,Reports.GroupingValue> downMap = new Map<String,Reports.GroupingValue>();
  43. Map<String,Reports.GroupingValue> acrossMap = new Map<String,Reports.GroupingValue>();
  44.  
  45. public void build(Reports.Dimension rD, Reports.GroupingValue rG)
  46. {
  47. //level 1
  48. for(Reports.GroupingValue rG1:rD.getGroupings())
  49. {
  50. setValue(rG1.getKey()+'!'+rG.getKey(),rG1);
  51. //level 2
  52. for(Reports.GroupingValue rG2:rG1.getgroupings())
  53. {
  54. setValue(rG2.getKey()+rG.getKey(),rG2);
  55. //level 3
  56. for(Reports.GroupingValue rG3:rG2.getgroupings())
  57. {
  58. setValue(rG3.getKey()+'!'+rG.getKey(),rG3);
  59. }
  60. }
  61. }
  62. }
  63.  
  64. public void setValue(string s,Reports.GroupingValue rG)
  65. {
  66. if(reportItemMap.containsKey(s))
  67. {
  68. //reportItemMap.get(s).cellValue = rG.getLabel();
  69. }
  70. }
  71.  
  72.  
  73. public reportItem[] getFilteredList()
  74. {
  75.  
  76. cell v0 = new cell(null,null);
  77. cell v1 = new cell(null,null);
  78. cell v2 = new cell(null,null);
  79. cell v3 = new cell(null,null);
  80.  
  81. return getFilteredList(v0,v1,v2,v3);
  82. }
  83.  
  84. private reportItem[] getFilteredList(cell v0,cell v1,cell v2,cell v3)
  85. {
  86. reportItem[] newList = new reportItem[]{};
  87. for(reportItem rItem:getReportList())
  88. {
  89. if(v0.cellKey == rItem.cellValue0.cellKey && v0.cellKey != null)
  90. {
  91.  
  92. }
  93. }
  94.  
  95. return newList;
  96. }
  97.  
  98. public integer getFactSize()
  99. {
  100. return getreport().getFactMap().size();
  101. }
  102.  
  103.  
  104. reportItem[] rList = new reportItem[]{};
  105.  
  106. public reportItem[] getReportList()
  107. {
  108. if(rList.size() == 0)
  109. {
  110. getReport();
  111.  
  112. for(Reports.GroupingValue rG:reportResult.getgroupingsDown().getGroupings())
  113. {
  114. reportItem xItem = new reportItem();
  115. xItem.xKey = rG.getKey();
  116. xItem.cellValue0 = new cell(rG.getLabel(),rG.getKey());
  117.  
  118. rList.add(xItem);
  119.  
  120. //sub totals
  121. yList = new reportItem[]{};
  122. for(reportItem yItem:getyList())
  123. {
  124. yItem.cellValue3 = yItem.cellValue1;
  125. yItem.cellValue2 = yItem.cellValue0;
  126. yItem.cellValue1 = null;
  127. yItem.cellValue0 = xItem.cellValue0;
  128. yItem.xKey = xItem.xKey;
  129. rList.add(yItem);
  130. }
  131.  
  132. for(Reports.GroupingValue rG1:rG.getgroupings())
  133. {
  134. reportItem xItem1 = new reportItem();
  135. xItem1.cellValue0 = xItem.cellValue0;
  136. xItem1.cellValue1 = new cell(rG1.getLabel(),rG1.getKey().replace(rG.getKey()+'_',''));
  137. xItem1.xKey = rG1.getKey();
  138. rList.add(xItem1);
  139.  
  140. yList = new reportItem[]{};
  141. for(reportItem yItem:getyList())
  142. {
  143. yItem.cellValue3 = yItem.cellValue1;
  144. yItem.cellValue2 = yItem.cellValue0;
  145.  
  146. yItem.cellValue1 = xItem1.cellValue1;
  147. yItem.cellValue0 = xItem.cellValue0;
  148.  
  149. yItem.xKey = xItem1.xKey;
  150.  
  151. rList.add(yItem);
  152. }
  153.  
  154.  
  155. for(Reports.GroupingValue rG2:rG1.getgroupings())
  156. {
  157. reportItem xItem2 = new reportItem();
  158. xItem2.cellValue0 = xItem.cellValue0;
  159. xItem2.cellValue1 = xItem1.cellValue1;
  160. xItem2.cellValue2 = new cell(rG2.getLabel(),rG2.getKey());
  161. xItem2.xKey = rG2.getKey();
  162. rList.add(xItem2);
  163.  
  164. yList = new reportItem[]{};
  165. for(reportItem yItem:getyList())
  166. {
  167. yItem.cellValue3 = yItem.cellValue1;
  168. yItem.cellValue2 = yItem.cellValue0;
  169.  
  170. yItem.cellValue1 = xItem1.cellValue1;
  171. yItem.cellValue0 = xItem.cellValue0;
  172.  
  173. yItem.xKey = xItem2.xKey;
  174.  
  175. rList.add(yItem);
  176. }
  177. }
  178.  
  179. }
  180. }
  181.  
  182.  
  183. yList = new reportItem[]{};
  184. for(reportItem yItem:getyList())
  185. {
  186. yItem.cellValue3 = yItem.cellValue1;
  187. yItem.cellValue2 = yItem.cellValue0;
  188. yItem.cellValue1 = null;
  189. yItem.cellValue0 = null;
  190. rList.add(yItem);
  191. }
  192.  
  193.  
  194.  
  195. reportItem xItem = new reportItem();
  196. xItem.cellValue0 = new cell('Grand Total','T!T');
  197. rList.add(xItem);
  198.  
  199. for(reportItem rItem:rList)
  200. {
  201. if(rItem.xKey == null)
  202. {
  203. rItem.xKey = 'T';
  204. }
  205.  
  206. if(rItem.yKey == null)
  207. {
  208. rItem.yKey = 'T';
  209. }
  210.  
  211. //set the label
  212. if(getreport().getFactMap().containsKey(rItem.getKey()))
  213. {
  214. rItem.reportFact = getreport().getFactMap().get(rItem.getKey());
  215. }
  216.  
  217. if(rItem.cellValue0 == null)
  218. {
  219. rItem.cellValue0 = new cell(null,'T');
  220. }
  221.  
  222. if(rItem.cellValue2 == null)
  223. {
  224. rItem.cellValue2 = new cell(null,'T');
  225. }
  226. }
  227.  
  228.  
  229. }
  230.  
  231. return rList;
  232. }
  233. reportItem[] yList = new reportItem[]{};
  234. public reportItem[] getyList()
  235. {
  236.  
  237. if(yList.size() == 0)
  238. {
  239. Reports.GroupingValue[] rGs = reportResult.getgroupingsAcross().getGroupings();
  240.  
  241. for(Reports.GroupingValue rG:rGs)
  242. {
  243. reportItem yItem = new reportItem();
  244. yItem.cellValue0 = new cell(rG.getLabel(),rG.getKey());
  245. yItem.yKey = rG.getKey();
  246. yList.add(yItem);
  247.  
  248. Reports.GroupingValue[] rG1s = rG.getgroupings();
  249.  
  250. for(Reports.GroupingValue rG1:rG1s)
  251. {
  252. reportItem yItem1 = new reportItem();
  253. yItem1.cellValue0 = yItem.cellValue0;
  254. yItem1.cellValue1 = new cell(rG1.getLabel(),rG1.getKey().replace(rG.getKey()+'_',''));
  255. yItem1.yKey = rG1.getKey();
  256. yList.add(yItem1);
  257.  
  258. Reports.GroupingValue[] rG2s = rG1.getgroupings();
  259.  
  260. for(Reports.GroupingValue rG2:rG2s)
  261. {
  262. reportItem yItem2 = new reportItem();
  263. yItem2.cellValue0 = yItem.cellValue0;
  264. yItem2.cellValue1 = yItem1.cellValue1;
  265. yItem2.cellValue2 = new cell(rG2.getLabel(),rG2.getKey());
  266. yItem.yKey = rG2.getKey();
  267. yList.add(yItem2);
  268. }
  269. }
  270. }
  271. }
  272.  
  273. return yList;
  274. }
  275.  
  276. public class reportItem
  277. {
  278. public cell cellValue0 {get;set;}
  279. public cell cellValue1 {get;set;}
  280. public cell cellValue2 {get;set;}
  281. public cell cellValue3 {get;set;}
  282. public string xKey {get;set;}
  283. public string yKey {get;set;}
  284. public Reports.ReportFact reportFact {get;set;}
  285.  
  286. public string getKey()
  287. {
  288. return xKey + '!' + yKey;
  289. }
  290. }
  291.  
  292. public class cell
  293. {
  294. public string cellValue {get;set;}
  295. public string cellKey {get;set;}
  296.  
  297. public cell(string v,string k)
  298. {
  299. cellValue = v;
  300. cellKey = k;
  301. }
  302. }
  303.  
  304. public class filteredResult
  305. {
  306. public reportItem[] reportItems {get;set;}
  307. public totalRow[] totals {get;set;}
  308.  
  309. public filteredResult()
  310. {
  311. reportItems = new reportItem[]{};
  312. totals = new totalRow[]{};
  313. }
  314. }
  315.  
  316. public class totalRow
  317. {
  318. public string label {get;set;}
  319. public double value {get;set;}
  320.  
  321. public totalRow(string l,string v)
  322. {
  323. label = l;
  324. value = double.valueOf(v);
  325. }
  326. }
  327.  
  328. }
  329.  
  330. <apex:page controller="timeAllocation_handler" sidebar="false" >
  331. <apex:pageBlock >
  332. <apex:pageBlockSection columns="1">
  333. <apex:pageBlockTable value="{!ReportList}" var="r" >
  334. <apex:column headerValue="0" value="{!r.cellValue0.cellKey}"/>
  335. <apex:column headerValue="0" value="{!r.cellValue0.cellValue}"/>
  336.  
  337. <apex:column headerValue="1" value="{!r.cellValue1.cellKey}"/>
  338. <apex:column headerValue="1" value="{!r.cellValue1.cellValue}"/>
  339.  
  340. <apex:column headerValue="2" value="{!r.cellValue2.cellKey}"/>
  341. <apex:column headerValue="2" value="{!r.cellValue2.cellValue}"/>
  342.  
  343. <apex:column headerValue="3" value="{!r.cellValue3.cellKey}"/>
  344. <apex:column headerValue="3" value="{!r.cellValue3.cellValue}"/>
  345.  
  346. <apex:column value="{!r.key}"/>
  347. <apex:column value="{!reportList.size}"/>
  348. <apex:column value="{!Factsize}"/>
  349. <apex:repeat value="{!r.reportFact.aggregates}" var="agg">
  350. <apex:column headerValue="asf" value="{!agg.label}"/>
  351. </apex:repeat>
  352. </apex:pageBlockTable>
  353. </apex:pageBlockSection>
  354. </apex:pageBlock>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement