1 2 12 package com.versant.core.common; 13 14 import com.versant.core.jdo.VersantPersistenceManagerImp; 15 import com.versant.core.jdo.sco.CollectionDiffUtil; 16 import com.versant.core.jdo.sco.VersantSCOCollection; 17 import com.versant.core.jdo.sco.VersantSCOMap; 18 import com.versant.core.jdo.sco.MapDiffUtil; 19 import com.versant.core.jdo.PCStateMan; 20 import com.versant.core.jdo.PMProxy; 21 import com.versant.core.jdo.VersantStateManager; 22 import com.versant.core.metadata.FieldMetaData; 23 import com.versant.core.metadata.MDStatics; 24 25 import javax.jdo.spi.PersistenceCapable; 26 import javax.jdo.PersistenceManager; 27 import java.util.*; 28 29 32 public final class StateUtil { 33 34 public static Date getPValueForSCO(Date date) { 35 return date; 36 } 37 38 41 public static CollectionDiff getPValueForSCO(Collection col, 42 PersistenceContext pm, 43 VersantStateManager sm, FieldMetaData fmd) { 44 if (fmd.isReadOnly) return null; 45 CollectionDiff diff = null; 46 if (col instanceof VersantSCOCollection) { 47 diff = ((VersantSCOCollection)col).getCollectionDiff(pm); 48 } else { 49 diff = CollectionDiffUtil.getNonSCOCollectionDiff(col, pm, fmd); 50 } 51 FieldMetaData inverseFieldMetaData = fmd.inverseFieldMetaData; 52 if (fmd.category == MDStatics.CATEGORY_COLLECTION && inverseFieldMetaData != null && fmd.inverseFieldMetaData.fake) { 53 Object [] values = diff.insertedValues; 54 fixFakeFieldsOnChildren(values, pm, inverseFieldMetaData, sm, 55 false); 56 values = ((UnorderedCollectionDiff)diff).deletedValues; 57 fixFakeFieldsOnChildren(values, pm, inverseFieldMetaData, sm, true); 58 } 59 return diff; 60 } 61 62 private static void fixFakeFieldsOnChildren(Object [] values, 63 PersistenceContext pm, 64 FieldMetaData inverseFieldMetaData, VersantStateManager sm, boolean remove) { 65 if (values != null) { 66 for (int i = 0; i < values.length; i++) { 67 Object oid = values[i]; 68 if (oid == null) { 69 throw BindingSupportImpl.getInstance().invalidOperation( 70 "One-to-Many collections may not contain null values."); 71 } 72 PersistenceCapable pc = (PersistenceCapable)pm.getObjectById( 73 oid, false); 74 pm.getInternalSM(pc).setFakeMaster(inverseFieldMetaData.stateFieldNo, sm, remove); 75 } 76 } 77 } 78 79 public static CollectionDiff getPValueForSCO(Map map, 80 PersistenceContext pm, FieldMetaData fmd) { 81 if (map instanceof VersantSCOMap) { 82 return ((VersantSCOMap)map).getMapDiff(pm); 83 } else { 84 MapDiffUtil mapDiffUtil = new MapDiffUtil(fmd); 85 CollectionDiff collectionDiff = mapDiffUtil.getDiff(map, null, pm); 86 collectionDiff.status = CollectionDiff.STATUS_NEW; 87 return collectionDiff; 88 } 89 } 90 91 public static Object getPValueForRef(PersistenceCapable pc, 92 PersistenceContext pm) { 93 return pm.getInternalOID(pc); 94 } 95 96 public static void doReachable(Collection col, 97 VersantPersistenceManagerImp sm) { 98 for (Iterator iterator = col.iterator(); iterator.hasNext();) { 99 Object o = iterator.next(); 100 if (o != null) sm.makeReachablePersistent(o); 101 } 102 } 103 104 public static void doReachable(Map map, VersantPersistenceManagerImp sm, 105 FieldMetaData fmd) { 106 boolean keyIsRef = fmd.isMapKeyRef(); 107 boolean valueIsRef = fmd.isMapValueRef(); 108 final Set entrySet = map.entrySet(); 109 for (Iterator iterator = entrySet.iterator(); iterator.hasNext();) { 110 Map.Entry entry = (Map.Entry)iterator.next(); 111 if (keyIsRef) sm.makeReachablePersistent(entry.getKey()); 112 if (valueIsRef) sm.makeReachablePersistent(entry.getValue()); 113 } 114 } 115 116 public static void doReachable(Object [] array, 117 VersantPersistenceManagerImp sm) { 118 for (int i = 0; i < array.length; i++) { 119 if (array[i] != null) sm.makeReachablePersistent(array[i]); 120 } 121 } 122 123 public static void doReachable(PersistenceCapable pc, 124 VersantPersistenceManagerImp sm) { 125 sm.makeReachablePersistent(pc); 126 } 127 128 public static void doReachableEmbeddedReference(PersistenceCapable pc, 129 VersantPersistenceManagerImp pm, PCStateMan sm, FieldMetaData fmd) { 130 PersistenceManager otherPm = pc.jdoGetPersistenceManager(); 131 if (otherPm != null) { 132 otherPm = ((PMProxy)otherPm).getRealPM(); 133 } 134 if (otherPm != null && otherPm != pm) { 135 throw BindingSupportImpl.getInstance().unsupported( 136 "Sharing of embedded instances is not supported"); 137 } 138 PCStateMan tmpSM = pm.getSMIfManaged(pc); 139 if (tmpSM != null) { 140 if (tmpSM == sm) { 141 } else { 143 throw BindingSupportImpl.getInstance().unsupported( 144 "Sharing of embedded instances is not supported"); 145 } 146 } else { 147 sm.createEmbeddedSM(pc, fmd); 148 } 149 } 150 151 public static void retrieve(Map map, VersantPersistenceManagerImp sm, 152 FieldMetaData fmd) { 153 boolean keyIsRef = fmd.isMapKeyRef(); 154 boolean valueIsRef = fmd.isMapValueRef(); 155 final Set entrySet = map.entrySet(); 156 for (Iterator iterator = entrySet.iterator(); iterator.hasNext();) { 157 Map.Entry entry = (Map.Entry)iterator.next(); 158 if (keyIsRef) sm.retrieveImp(entry.getKey()); 159 if (valueIsRef) sm.retrieveImp(entry.getValue()); 160 } 161 } 162 163 } 164 165 | Popular Tags |