1 19 20 package org.apache.cayenne.access; 21 22 import java.util.List ; 23 24 import org.apache.cayenne.CayenneRuntimeException; 25 import org.apache.cayenne.ObjectContext; 26 import org.apache.cayenne.ObjectId; 27 import org.apache.cayenne.Persistent; 28 import org.apache.cayenne.QueryResponse; 29 import org.apache.cayenne.graph.GraphChangeHandler; 30 import org.apache.cayenne.map.ObjEntity; 31 import org.apache.cayenne.query.ObjectIdQuery; 32 import org.apache.cayenne.query.Query; 33 import org.apache.cayenne.reflect.AttributeProperty; 34 import org.apache.cayenne.reflect.ClassDescriptor; 35 import org.apache.cayenne.reflect.Property; 36 import org.apache.cayenne.reflect.PropertyVisitor; 37 import org.apache.cayenne.reflect.ToManyProperty; 38 import org.apache.cayenne.reflect.ToOneProperty; 39 40 47 class ChildDiffLoader implements GraphChangeHandler { 48 49 ObjectContext context; 50 51 ChildDiffLoader(ObjectContext context) { 52 this.context = context; 53 } 54 55 public void nodeIdChanged(Object nodeId, Object newId) { 56 throw new CayenneRuntimeException("Not supported"); 57 } 58 59 public void nodeCreated(Object nodeId) { 60 ObjectId id = (ObjectId) nodeId; 61 if (id.getEntityName() == null) { 62 throw new NullPointerException ("Null entity name in id " + id); 63 } 64 65 ObjEntity entity = context.getEntityResolver().getObjEntity(id.getEntityName()); 66 if (entity == null) { 67 throw new IllegalArgumentException ("Entity not mapped with Cayenne: " + id); 68 } 69 70 Persistent dataObject = null; 71 try { 72 dataObject = (Persistent) entity.getJavaClass().newInstance(); 73 } 74 catch (Exception ex) { 75 throw new CayenneRuntimeException("Error instantiating object.", ex); 76 } 77 78 dataObject.setObjectId(id); 79 context.registerNewObject(dataObject); 80 } 81 82 public void nodeRemoved(Object nodeId) { 83 context.deleteObject(findObject(nodeId)); 84 } 85 86 public void nodePropertyChanged( 87 Object nodeId, 88 String property, 89 Object oldValue, 90 Object newValue) { 91 92 Persistent object = findObject(nodeId); 95 ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor( 96 ((ObjectId) nodeId).getEntityName()); 97 98 try { 99 descriptor.getProperty(property).writeProperty(object, null, newValue); 100 } 101 catch (Exception e) { 102 throw new CayenneRuntimeException("Error setting property: " + property, e); 103 } 104 } 105 106 public void arcCreated(Object nodeId, Object targetNodeId, Object arcId) { 107 108 final Persistent source = findObject(nodeId); 109 final Persistent target = findObject(targetNodeId); 110 111 ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor( 112 ((ObjectId) nodeId).getEntityName()); 113 Property property = descriptor.getProperty(arcId.toString()); 114 115 property.visit(new PropertyVisitor() { 116 117 public boolean visitAttribute(AttributeProperty property) { 118 return false; 119 } 120 121 public boolean visitToMany(ToManyProperty property) { 122 property.addTarget(source, target, false); 123 return false; 124 } 125 126 public boolean visitToOne(ToOneProperty property) { 127 property.setTarget(source, target, false); 128 return false; 129 } 130 }); 131 } 132 133 public void arcDeleted(Object nodeId, final Object targetNodeId, Object arcId) { 134 final Persistent source = findObject(nodeId); 135 136 ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor( 137 ((ObjectId) nodeId).getEntityName()); 138 Property property = descriptor.getProperty(arcId.toString()); 139 property.visit(new PropertyVisitor() { 140 141 public boolean visitAttribute(AttributeProperty property) { 142 return false; 143 } 144 145 public boolean visitToMany(ToManyProperty property) { 146 Persistent target = findObject(targetNodeId); 147 property.removeTarget(source, target, false); 148 return false; 149 } 150 151 public boolean visitToOne(ToOneProperty property) { 152 property.setTarget(source, null, false); 153 return false; 154 } 155 }); 156 } 157 158 Persistent findObject(Object nodeId) { 159 162 Persistent object = (Persistent) context.getGraphManager().getNode(nodeId); 163 if (object != null) { 164 return object; 165 } 166 167 Query query = new ObjectIdQuery((ObjectId) nodeId); 169 QueryResponse response = context.getChannel().onQuery(context, query); 170 List objects = response.firstList(); 171 172 if (objects.size() == 0) { 173 throw new CayenneRuntimeException("No object for ID exists: " + nodeId); 174 } 175 else if (objects.size() > 1) { 176 throw new CayenneRuntimeException( 177 "Expected zero or one object, instead query matched: " 178 + objects.size()); 179 } 180 181 return (Persistent) objects.get(0); 182 } 183 } 184 | Popular Tags |