CelestialGraph


SUBMITTED BY: Guest

DATE: Jan. 22, 2014, 12:48 p.m.

FORMAT: Text only

SIZE: 5.2 kB

HITS: 4948

  1. public with sharing class Celestial {
  2. public string jsonString {get;set;}
  3. public List<User__c> userList {get;set;}
  4. public List<mainWrapper> jsonList = new List<mainWrapper>();
  5. public Celestial() {
  6. userList = [select id, Name, Reports_To__c, Position__c from User__c ];
  7. mainWrapper mw = new mainWrapper();
  8. mw.name = 'Chris C';
  9. mw.position = 'Charmain ';
  10. mw.adjacencies = new List<childWrapper>();
  11. jsonList.add(mw);
  12. List<User__c> users = childPeopleByPosition('CIO');
  13. for(User__c u1 : users )
  14. {
  15. childWrapper cObj1 = new childWrapper();
  16. cObj1.nodeTo = u1.Id;
  17. mw.adjacencies.add(cObj1);
  18. mainWrapper mw1 = new mainWrapper ();
  19. mw1.id = u1.Id;
  20. mw1.name = u1.Name;
  21. mw1.position = u1.Position__c;
  22. mw1.adjacencies = new List<childWrapper>();
  23. jsonList.add(mw1);
  24. List<User__c> users1 = new List<User__c>();
  25. users1 = childPeopleById(mw1.Id);
  26. for(User__c u2 : users1 )
  27. {
  28. childWrapper cObj2 = new childWrapper();
  29. cObj2.nodeTo = u2.Id;
  30. mw1.adjacencies.add(cObj2);
  31. mainWrapper mw2 = new mainWrapper();
  32. mw2.id = u2.Id;
  33. mw2.name = u2.Name;
  34. mw2.position = u2.Position__c;
  35. mw2.adjacencies = new List<childWrapper>();
  36. jsonList.add(mw2);
  37. List<User__c> users2 = new List<User__c>();
  38. users2 = childPeopleById(mw2.Id);
  39. for(User__c u3 : users2 )
  40. {
  41. childWrapper cObj3 = new childWrapper();
  42. cObj3.nodeTo = u3.Id;
  43. mw2.adjacencies.add(cObj3);
  44. mainWrapper mw3 = new mainWrapper();
  45. mw3.id = u3.Id;
  46. mw3.name = u3.Name;
  47. mw3.position = u3.Position__c;
  48. mw3.adjacencies = new List<childWrapper>();
  49. jsonList.add(mw3);
  50. List<User__c> users3 = new List<User__c>();
  51. users3 = childPeopleById(mw3.Id);
  52. for(User__c u4 : users3 )
  53. {
  54. childWrapper cObj4 = new childWrapper();
  55. cObj4.nodeTo = u4.Id;
  56. mw3.adjacencies.add(cObj4);
  57. mainWrapper mw4 = new mainWrapper();
  58. mw4.id = u4.Id;
  59. mw4.name = u4.Name;
  60. mw4.position = u4.Position__c;
  61. mw4.adjacencies = new List<childWrapper>();
  62. jsonList.add(mw4);
  63. List<User__c> users4 = new List<User__c>();
  64. users4 = childPeopleById(mw4.Id);
  65. for(User__c u5: users4 )
  66. {
  67. childWrapper cObj5 = new childWrapper ();
  68. cObj5.nodeTo = u5.Id;
  69. mw4.adjacencies.add(cObj5);
  70. mainWrapper mw5 = new mainWrapper();
  71. mw5.id = u5.Id;
  72. mw5.name = u5.Name;
  73. mw5.position = u5.Position__c;
  74. jsonList.add(mw5);
  75. }
  76. }
  77. }
  78. }
  79. }
  80. System.debug(jsonList);
  81. jsonString = JSON.serialize(jsonList);
  82. }
  83. public List<User__c> childPeopleByPosition (String position)
  84. {
  85. List<User__c> childList = new List<User__c>();
  86. for(User__c p: userList )
  87. {
  88. if (p.Position__c == position )
  89. {
  90. childList.add(p);
  91. }
  92. }
  93. return childList;
  94. }
  95. public List<User__c> childPeopleById (Id parentId)
  96. {
  97. List<User__c> childList = new List<User__c>();
  98. for(User__c p: userList )
  99. {
  100. if (p.Reports_To__c == parentId )
  101. {
  102. childList.add(p);
  103. }
  104. }
  105. return childList;
  106. }
  107. public class mainWrapper
  108. {
  109. public String name;
  110. public String id;
  111. public String position;
  112. public List<childWrapper> adjacencies;
  113. }
  114. public class childWrapper
  115. {
  116. public string nodeTo;
  117. }
  118. }

comments powered by Disqus