1 56 package org.objectstyle.cayenne.service; 57 58 import org.apache.commons.beanutils.PropertyUtils; 59 import org.objectstyle.cayenne.CayenneRuntimeException; 60 import org.objectstyle.cayenne.ObjectId; 61 import org.objectstyle.cayenne.Persistent; 62 import org.objectstyle.cayenne.distribution.GlobalID; 63 import org.objectstyle.cayenne.graph.GraphChangeHandler; 64 65 72 class ClientToServerDiffConverter implements GraphChangeHandler { 73 74 ObjectDataContext context; 75 76 ClientToServerDiffConverter(ObjectDataContext context) { 77 this.context = context; 78 } 79 80 public void nodeIdChanged(Object nodeId, Object newId) { 81 throw new CayenneRuntimeException( 82 "Not supported - client is not allowed to change Global ID of server objects."); 83 } 84 85 public void nodeCreated(Object nodeId) { 86 ObjectId id = toObjectId(nodeId); 87 context.createAndRegisterNewObject(id); 88 } 89 90 public void nodeRemoved(Object nodeId) { 91 Persistent object = findObject(nodeId); 92 context.deleteObject(object); 93 } 94 95 public void nodePropertyChanged( 96 Object nodeId, 97 String property, 98 Object oldValue, 99 Object newValue) { 100 101 Persistent object = findObject(nodeId); 102 try { 103 PropertyUtils.setSimpleProperty(object, property, newValue); 104 } 105 catch (Exception e) { 106 throw new CayenneRuntimeException("Error setting property: " + property, e); 107 } 108 } 109 110 public void arcCreated(Object nodeId, Object targetNodeId, Object arcId) { 111 throw new CayenneRuntimeException( 112 "TODO: implement relationship change updates..."); 113 } 114 115 public void arcDeleted(Object nodeId, Object targetNodeId, Object arcId) { 116 throw new CayenneRuntimeException( 117 "TODO: implement relationship change updates..."); 118 } 119 120 Persistent findObject(Object nodeId) { 121 ObjectId id = toObjectId(nodeId); 122 return context.getObjectStore().getObject(id); 123 } 124 125 ObjectId toObjectId(Object nodeId) { 126 if (nodeId instanceof GlobalID) { 127 return context.getEntityResolver().convertToObjectID((GlobalID) nodeId); 128 } 129 else if (nodeId instanceof ObjectId) { 130 return (ObjectId) nodeId; 131 } 132 else if (nodeId == null) { 133 throw new NullPointerException ("Null GlobalID"); 134 } 135 else { 136 throw new CayenneRuntimeException( 137 "Client node id is expected to be GlobalID, got: " + nodeId); 138 } 139 } 140 } 141 | Popular Tags |