1 package org.jboss.cache.pojo.test.propagation.impl; 2 3 import org.jboss.cache.pojo.test.propagation.Node; 4 import org.jboss.cache.pojo.test.propagation.PropagationManager; 5 import org.jboss.cache.pojo.test.propagation.PropagationRule; 6 import org.jboss.cache.pojo.test.propagation.StateItem; 7 8 import java.util.HashMap ; 9 import java.util.List ; 10 import java.util.Map ; 11 12 public class PropagationManagerImpl implements PropagationManager 13 { 14 private Node rootNode_; 15 16 private Map nodeMap_ = new HashMap (); 17 18 private transient PropagationRule orRule_; 19 20 public PropagationManagerImpl() 21 { 22 orRule_ = PropagationRuleFactory.getPropagationRule(); 23 } 24 25 public void setRootNode(String rdn) 26 { 27 this.rootNode_ = new NodeImpl(); 28 rootNode_.setNodeFDN(rdn); 29 rootNode_.setNodeRDN(rdn); 30 rootNode_.setPropagationRule(orRule_); 31 32 StateItem summary = new StateItemImpl(0); 33 summary.setState(PropagationRule.STATE_CLEAR); 34 rootNode_.setSummaryStateItem(summary); 35 36 registMap(rootNode_); 37 } 38 39 public void addNode(String parentFdn, String rdn) 40 { 41 Node parent = findNode(parentFdn); 42 if (parent != null) 43 { 44 Node node = new NodeImpl(); 45 node.setNodeFDN(parentFdn + "." + rdn); 46 node.setNodeRDN(rdn); 47 node.setPropagationRule(orRule_); 48 49 node.setParentNode(parent); 50 parent.addChildNode(node); 51 52 StateItem summary = new StateItemImpl(0); 53 summary.setState(PropagationRule.STATE_CLEAR); 54 node.setSummaryStateItem(summary); 55 56 registMap(node); 57 58 PropagationRule rule = node.getPropagationRule(); 59 rule.summaryUpperPropagate(node); 60 } 61 } 62 63 public void addStateItem(String parentFdn, long itemId, long defaultState) 64 { 65 Node node = findNode(parentFdn); 66 if (node != null) 67 { 68 StateItem item = new StateItemImpl(itemId); 69 item.setState(defaultState); 70 71 node.addStateItem(item); 72 73 PropagationRule rule = node.getPropagationRule(); 74 rule.summaryUpperPropagate(node); 75 } 76 } 77 78 public void stateChange(String parentFdn, long itemId, long newState) 79 { 80 Node node = findNode(parentFdn); 81 if (node != null) 82 { 83 PropagationRule rule = node.getPropagationRule(); 84 rule.changeState(node, itemId, newState); 85 } 86 } 87 88 public Node findNode(String fdn) 89 { 90 return (Node) nodeMap_.get(fdn); 91 } 92 93 private void registMap(Node node) 94 { 95 this.nodeMap_.put(node.getNodeFDN(), node); 96 } 97 98 public void printNodes() 99 { 100 printNode(rootNode_, ""); 101 } 102 103 private void printNode(Node node, String prefix) 104 { 105 System.out.println(prefix + node.getNodeRDN() + " (Summary : " 106 + node.getSummaryStateItem().getState() + ")"); 107 108 String itemPrefix = prefix + " | "; 109 List items = node.getStateItems(); 110 int size = items.size(); 111 for (int idx = 0; idx < size; idx++) 112 { 113 StateItem item = (StateItem) items.get(idx); 114 printStateItem(item, itemPrefix); 115 } 116 117 String childPrefix = prefix + " + "; 118 List children = node.getChildren(); 119 size = children.size(); 120 for (int idx = 0; idx < size; idx++) 121 { 122 Node child = (Node) children.get(idx); 123 printNode(child, childPrefix); 124 } 125 } 126 127 private void printStateItem(StateItem item, String prefix) 128 { 129 System.out.println(prefix + "(" + item.getItemId() + " : " 130 + item.getState() + ")"); 131 } 132 133 private String toNodeString(Node node, String prefix) 134 { 135 StringBuffer buf = new StringBuffer (); 136 137 buf.append(prefix + node.getNodeRDN() + " (Summary : " 138 + node.getSummaryStateItem().getState() + ")"); 139 140 String itemPrefix = prefix + " | "; 141 List items = node.getStateItems(); 142 int size = items.size(); 143 for (int idx = 0; idx < size; idx++) 144 { 145 StateItem item = (StateItem) items.get(idx); 146 buf.append(toStateItemString(item, itemPrefix)); 147 } 148 149 String childPrefix = prefix + " + "; 150 List children = node.getChildren(); 151 size = children.size(); 152 for (int idx = 0; idx < size; idx++) 153 { 154 Node child = (Node) children.get(idx); 155 buf.append(toNodeString(child, childPrefix)); 156 } 157 158 return buf.toString(); 159 } 160 161 private String toStateItemString(StateItem item, String prefix) 162 { 163 return (prefix + "(" + item.getItemId() + " : " 164 + item.getState() + ")"); 165 } 166 167 public String toString() 168 { 169 return toNodeString(rootNode_, "+"); 170 } 171 } 172 | Popular Tags |