- 1 public without sharing class TreeUtil {
- 2 3 // map to hold roles with Id as the key
- 4 private static Map treeMap;
- 5 6 // map to hold child roles with parentRoleId as the key
- 7 private static Map > parentNodeToChildNodeMap;
- 8 9 10 private static List nodes {
- get;
- set;
- }
- 11 12 private static JSONGenerator gen {
- get;
- set;
- }
- 13 14 private static Tree__c rootNode {
- get;
- set;
- }
- 15 16 static {
- 17 initTreeDatas();
- 18
- }
- 19 20 public static void initTreeDatas() {
- 21 treeMap = new Map([SELECT IsLeafNode__c, ParentNode__c, Id, Name FROM Tree__c order by ParentNode__c]);
- 22 parentNodeToChildNodeMap = new Map > ();
- 23
- for (Tree__c tree: treeMap.values()) {
- 24 List tempList;
- 25
- if (parentNodeToChildNodeMap.containsKey(tree.ParentNode__c)) {
- 26 tempList = parentNodeToChildNodeMap.get(tree.ParentNode__c);
- 27 tempList.add(tree);
- 28 parentNodeToChildNodeMap.put(tree.ParentNode__c, tempList);
- 29
- } else {
- 30 tempList = new List();
- 31 tempList.add(tree);
- 32
- if (tree.ParentNode__c != null) {
- 33 parentNodeToChildNodeMap.put(tree.ParentNode__c, tempList);
- 34
- } else {
- 35 rootNode = tree;
- 36
- }
- 37
- }
- 38
- }
- 39
- }
- 40 41 42 private static void convertNodeToJSON(NodeWrapper nw) {
- 43 gen.writeStartObject();
- 44
- if (!nw.isLeafNode) {
- 45 gen.writeStringField('title', nw.nodeName);
- 46 gen.writeStringField('key', nw.nodeId);
- 47 gen.writeBooleanField('unselectable', false);
- 48 gen.writeBooleanField('expand', true);
- 49 gen.writeBooleanField('isFolder', true);
- 50
- if (nw.hasChildNodes) {
- 51 gen.writeFieldName('children');
- 52 gen.writeStartArray();
- 53
- for (NodeWrapper r: nw.childNodes) {
- 54 convertNodeToJSON(r);
- 55
- }
- 56 gen.writeEndArray();
- 57
- }
- 58
- } else {
- 59 gen.writeStringField('title', nw.nodeName);
- 60 gen.writeStringField('key', nw.nodeId);
- 61
- }
- 62 gen.writeEndObject();
- 63
- }
- 64 65 public static NodeWrapper createNode(Tree__c tree) {
- 66 NodeWrapper n = new NodeWrapper();
- 67 n.nodeName = tree.Name;
- 68 n.nodeId = tree.Id;
- 69 n.parentNodeId = tree.ParentNode__c;
- 70 71
- if (tree.IsLeafNode__c) {
- 72 n.isLeafNode = true;
- 73 n.hasChildNodes = false;
- 74
- } else {
- 75 List nwList = new List();
- 76
- if (parentNodeToChildNodeMap.get(tree.Id) != null) {
- 77 n.hasChildNodes = true;
- 78 n.isLeafNode = false;
- 79
- for (Tree__c tempTree: parentNodeToChildNodeMap.get(tree.Id)) {
- 80 nwList.add(createNode(tempTree));
- 81
- }
- 82 n.childNodes = nwList;
- 83
- }
- 84 85
- }
- 86
- return n;
- 87
- }
- 88 89 90 public static String getTreeAndSubTrees() {
- 91 gen = JSON.createGenerator(true);
- 92 NodeWrapper node = createNode(rootNode);
- 93 gen.writeStartArray();
- 94 convertNodeToJSON(node);
- 95 gen.writeEndArray();
- 96
- return gen.getAsString();
- 97
- }
- 98 99 100 public class NodeWrapper {
- 101 102 //current node name
- 103 public String nodeName {
- get;
- set;
- }
- 104 105 //current node id
- 106 public String nodeId {
- get;
- set;
- }
- 107 108 //if current node isn't root,set it's parent parentNodeId
- 109 public String parentNodeId {
- get;
- set;
- }
- 110 111 //if this node set as a parent node,does it has child node
- 112 public Boolean hasChildNodes {
- get;
- set;
- }
- 113 114 //if current node is leaf node,set to true
- 115 public Boolean isLeafNode {
- get;
- set;
- }
- 116 117 118 //all of child nodes of current node
- 119 public List childNodes {
- get;
- set;
- }
- 120 121 public NodeWrapper() {
- 122 hasChildNodes = false;
- 123
- }
- 124
- }
- 125 126
- }
来源: http://www.cnblogs.com/zero-zyq/p/6691448.html