1 19 20 package org.apache.cayenne.reflect.valueholder; 21 22 import org.apache.cayenne.Persistent; 23 import org.apache.cayenne.ValueHolder; 24 import org.apache.cayenne.reflect.Accessor; 25 import org.apache.cayenne.reflect.BaseToOneProperty; 26 import org.apache.cayenne.reflect.ClassDescriptor; 27 import org.apache.cayenne.reflect.PropertyException; 28 import org.apache.cayenne.util.PersistentObjectHolder; 29 30 38 class ValueHolderProperty extends BaseToOneProperty { 39 40 ValueHolderProperty(ClassDescriptor owner, ClassDescriptor targetDescriptor, 41 Accessor accessor, String reverseName) { 42 super(owner, targetDescriptor, accessor, reverseName); 43 } 44 45 48 public boolean isFault(Object object) { 49 ValueHolder holder = (ValueHolder) accessor.getValue(object); 50 return holder == null || holder.isFault(); 51 } 52 53 public void invalidate(Object object) { 54 ValueHolder holder = (ValueHolder) accessor.getValue(object); 55 if (holder != null && !holder.isFault()) { 56 holder.invalidate(); 57 } 58 } 59 60 public Object readPropertyDirectly(Object object) throws PropertyException { 61 ValueHolder holder = (ValueHolder) accessor.getValue(object); 62 63 return (holder != null) ? holder.getValueDirectly() : null; 66 } 67 68 public Object readProperty(Object object) throws PropertyException { 69 return ensureValueHolderSet(object).getValue(); 70 } 71 72 public void writePropertyDirectly(Object object, Object oldValue, Object newValue) 73 throws PropertyException { 74 75 ValueHolder holder = (ValueHolder) accessor.getValue(object); 76 if (holder == null) { 77 holder = createValueHolder(object); 78 accessor.setValue(object, holder); 79 } 80 81 holder.setValueDirectly(newValue); 82 } 83 84 public void writeProperty(Object object, Object oldValue, Object newValue) 85 throws PropertyException { 86 ensureValueHolderSet(object).setValueDirectly(newValue); 87 } 88 89 92 public void injectValueHolder(Object object) throws PropertyException { 93 ensureValueHolderSet(object); 94 } 95 96 100 protected ValueHolder ensureValueHolderSet(Object object) throws PropertyException { 101 102 ValueHolder holder = (ValueHolder) accessor.getValue(object); 103 if (holder == null) { 104 holder = createValueHolder(object); 105 accessor.setValue(object, holder); 106 } 107 108 return holder; 109 } 110 111 115 protected ValueHolder createValueHolder(Object object) throws PropertyException { 116 if (!(object instanceof Persistent)) { 117 118 throw new PropertyException( 119 "ValueHolders for non-persistent objects are not supported.", 120 this, 121 object); 122 } 123 124 return new PersistentObjectHolder((Persistent) object, getName()); 125 } 126 } 127 | Popular Tags |