Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2010
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.22 KB | None | 0 0
  1. // Campaign Hierarchy viewer - Apex controller by Jeff Trull (linmodemstudent@gmail.com)
  2. public class CampaignHierarchyController {
  3. class CampaignHierarchyLevel {
  4. // representing a level in the tree - an ID and a list of child nodes
  5. Campaign camp;
  6. List<CampaignHierarchyLevel> children;
  7. CampaignHierarchyLevel(Campaign c) {
  8. camp = c;
  9. children = new List<CampaignHierarchyLevel>();
  10. }
  11. }
  12. // for communicating with VF we have a sort of vestigial Campaign class
  13. public class CampaignLevelInfo {
  14. // three fields from Campaign, plus a flag to tell us if it's a leaf
  15. public ID parentId {get;set;}
  16. public ID id {get;set;}
  17. public String name {get;set;}
  18. public Boolean isLeaf {get;set;}
  19. }
  20.  
  21. List<CampaignHierarchyLevel> toplevel_campaigns; // the top of our data structure
  22. public CampaignHierarchyController() {
  23. // create a tree structure from the Campaign hierarchy and store it for VF reference
  24. toplevel_campaigns = new List<CampaignHierarchyLevel>();
  25. // if we fill the campaign tree by repeated SOQL queries (based on ParentId) we exceed the limit very quickly
  26. // instead let's do a single query for active campaigns, and process the results
  27. // question: would it be better to get all campaigns, and disable selecting the ones that are inactive?
  28. // this way we may end up missing active campaigns that are descendants of inactive ones
  29. Map<ID, List<Campaign>> child_campaigns = new Map<ID, List<Campaign>>();
  30. for (Campaign c : [select Id, Name, ParentId from Campaign where IsActive=true]) {
  31. if (c.ParentId == null) {
  32. toplevel_campaigns.add(new CampaignHierarchyLevel(c));
  33. }
  34. else {
  35. // add this campaign to its parent's list of child campaigns
  36. if (!child_campaigns.containsKey(c.ParentId)) {
  37. child_campaigns.put(c.ParentId, new List<Campaign>());
  38. }
  39. child_campaigns.get(c.ParentId).add(c);
  40. }
  41. }
  42. // now all of our campaigns are in one of two places:
  43. // 1) in toplevel_campaigns, if they had no parent, or
  44. // 2) listed as children of the appropriate parent (flat, by parent) in child_campaigns
  45. // now we turn this into a tree in a depth-first manner
  46. List<CampaignHierarchyLevel> camp_expand_stack = new List<CampaignHierarchyLevel>(toplevel_campaigns);
  47. while (!camp_expand_stack.isEmpty()) {
  48. // remove the last one from the end
  49. CampaignHierarchyLevel chl = camp_expand_stack.remove(camp_expand_stack.size() - 1);
  50. if (child_campaigns.containsKey(chl.camp.Id)) {
  51. // this campaign has children. Create hierarchy levels for them and push on stack
  52. for (Campaign cc : child_campaigns.get(chl.camp.Id)) {
  53. CampaignHierarchyLevel cchl = new CampaignHierarchyLevel(cc);
  54. chl.children.add(cchl);
  55. camp_expand_stack.add(cchl);
  56. }
  57. }
  58. }
  59. SelectedCampaign = new Campaign();
  60. }
  61.  
  62. public List<CampaignLevelInfo> getCampaignTree() {
  63. List<CampaignLevelInfo> ctree = new List<CampaignLevelInfo>();
  64. // DFS traversal will ensure parent campaigns appear before children in output list
  65. List<CampaignHierarchyLevel> camp_lvl_stack = new List<CampaignHierarchyLevel>(toplevel_campaigns);
  66. while (!camp_lvl_stack.isEmpty()) {
  67. CampaignHierarchyLevel chl = camp_lvl_stack.remove(camp_lvl_stack.size() - 1);
  68. CampaignLevelInfo cli = new CampaignLevelInfo();
  69. // copy Campaign fields and add "isleaf" flag based on child count from tree
  70. cli.id = chl.camp.id;
  71. cli.parentId = chl.camp.parentId;
  72. cli.name = chl.camp.name;
  73. cli.isLeaf = (chl.children.size() == 0);
  74. ctree.add(cli);
  75. // queue up children for future processing
  76. for (CampaignHierarchyLevel cchl : chl.children) {
  77. camp_lvl_stack.add(cchl);
  78. }
  79. }
  80. return ctree;
  81. }
  82.  
  83. public PageReference selectCampaign() {
  84. // do lookup and set SelectedCampaign based on ID chosen by user
  85. if (SelectedId != null) {
  86. SelectedCampaign = [select Id, Name, ParentId from Campaign where Id=:SelectedId limit 1];
  87. }
  88. return null;
  89. }
  90. public string SelectedId {get;set;}
  91. public Campaign SelectedCampaign {get;set;}
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement