1 56 package org.objectstyle.cayenne.graph; 57 58 import java.util.Collection ; 59 60 import org.apache.commons.beanutils.PropertyUtils; 61 import org.objectstyle.cayenne.CayenneRuntimeException; 62 63 71 public class BeanMergeHandler implements GraphChangeHandler { 72 73 protected GraphMap graphMap; 74 75 public BeanMergeHandler(GraphMap graphMap) { 76 this.graphMap = graphMap; 77 } 78 79 82 public GraphMap getGraphMap() { 83 return graphMap; 84 } 85 86 public void nodeIdChanged(Object nodeId, Object newId) { 87 Object node = graphMap.unregisterNode(nodeId); 88 89 if (node != null) { 90 graphMap.registerNode(newId, node); 91 } 92 } 93 94 97 public void nodeCreated(Object nodeId) { 98 } 100 101 104 public void nodeRemoved(Object nodeId) { 105 } 107 108 public void nodePropertyChanged( 109 Object nodeId, 110 String property, 111 Object oldValue, 112 Object newValue) { 113 114 Object node = graphMap.getNode(nodeId); 115 if (node != null) { 116 try { 117 PropertyUtils.setSimpleProperty(node, property, newValue); 118 } 119 catch (Exception e) { 120 throw new CayenneRuntimeException("Error setting property " + property, e); 121 } 122 } 123 } 124 125 public void arcCreated(Object nodeId, Object targetNodeId, Object arcId) { 126 Object node = graphMap.getNode(nodeId); 127 if (node != null) { 128 129 Object nodeTarget = graphMap.getNode(targetNodeId); 130 131 try { 134 135 Object property = PropertyUtils.getSimpleProperty(node, arcId.toString()); 136 if (property instanceof Collection ) { 137 138 Collection collection = (Collection ) property; 139 if (nodeTarget != null && !collection.contains(nodeTarget)) { 140 collection.add(nodeTarget); 141 } 142 } 143 else { 144 PropertyUtils.setSimpleProperty(node, arcId.toString(), nodeTarget); 145 } 146 } 147 catch (Exception e) { 148 throw new CayenneRuntimeException("Error creating arc " + arcId, e); 149 } 150 } 151 } 152 153 public void arcDeleted(Object nodeId, Object targetNodeId, Object arcId) { 154 Object node = graphMap.getNode(nodeId); 155 if (node != null) { 156 157 Object nodeTarget = graphMap.getNode(targetNodeId); 158 159 try { 162 163 Object property = PropertyUtils.getSimpleProperty(node, arcId.toString()); 164 if (property instanceof Collection ) { 165 166 Collection collection = (Collection ) property; 167 if (nodeTarget != null) { 168 collection.remove(nodeTarget); 169 } 170 } 171 else { 172 PropertyUtils.setSimpleProperty(node, arcId.toString(), null); 173 } 174 } 175 catch (Exception e) { 176 throw new CayenneRuntimeException("Error removing arc " + arcId, e); 177 } 178 } 179 } 180 181 } 182 | Popular Tags |