1 19 20 package org.apache.cayenne.reflect; 21 22 import java.util.Collection ; 23 24 import org.apache.cayenne.Fault; 25 import org.apache.cayenne.ValueHolder; 26 27 33 public abstract class BaseToManyProperty extends BaseArcProperty implements 34 ToManyProperty { 35 36 public BaseToManyProperty(ClassDescriptor owner, ClassDescriptor targetDescriptor, 37 Accessor accessor, String reverseName) { 38 super(owner, targetDescriptor, accessor, reverseName); 39 } 40 41 public Object readProperty(Object object) throws PropertyException { 42 return ensureCollectionValueHolderSet(object); 43 } 44 45 48 public void writePropertyDirectly(Object object, Object oldValue, Object newValue) 49 throws PropertyException { 50 51 if (newValue instanceof Fault) { 52 super.writePropertyDirectly(object, null, newValue); 53 } 54 else { 55 ValueHolder holder = (ValueHolder) readProperty(object); 57 holder.setValueDirectly(newValue); 58 } 59 } 60 61 public void addTarget(Object source, Object target, boolean setReverse) { 62 if (target == null) { 63 throw new NullPointerException ("Attempt to add null object."); 64 } 65 66 72 Collection collection = (Collection ) readProperty(source); 75 collection.add(target); 76 77 if (target != null && setReverse) { 78 setReverse(source, null, target); 79 } 80 } 81 82 public void removeTarget(Object source, Object target, boolean setReverse) { 83 84 88 Collection collection = (Collection ) readProperty(source); 91 collection.remove(target); 92 93 if (target != null && setReverse) { 94 setReverse(source, target, null); 95 } 96 } 97 98 public boolean visit(PropertyVisitor visitor) { 99 return visitor.visitToMany(this); 100 } 101 102 105 public void injectValueHolder(Object object) throws PropertyException { 106 ensureCollectionValueHolderSet(object); 107 } 108 109 113 protected ValueHolder ensureCollectionValueHolderSet(Object object) 114 throws PropertyException { 115 116 Object value = accessor.getValue(object); 117 118 if (value == null || value instanceof Fault) { 119 value = createCollectionValueHolder(object); 120 accessor.setValue(object, value); 121 } 122 123 return (ValueHolder) value; 124 } 125 126 129 protected abstract ValueHolder createCollectionValueHolder(Object object) 130 throws PropertyException; 131 } 132 | Popular Tags |