1 2 12 package com.versant.core.jdo.sco; 13 14 import com.versant.core.jdo.PMProxy; 15 16 import javax.jdo.spi.PersistenceCapable; 17 import java.util.Collection ; 18 19 22 public class SCOInverseUtil { 23 24 28 public static void addMasterOnDetail(Object detailObject, 29 PersistenceCapable owner, int detailFieldNo) { 30 if (owner == null) return; 31 PersistenceCapable detail = (PersistenceCapable) detailObject; 32 if (!detail.jdoIsPersistent()) { 33 owner.jdoGetPersistenceManager().makePersistent(detail); 34 } 35 ((PMProxy)owner.jdoGetPersistenceManager()).setMasterOnDetail(detail, detailFieldNo, owner, true); 36 } 37 38 43 public static void removeMasterOnDetail(Object detailObject, 44 PersistenceCapable owner, int detailFieldNo) { 45 if (owner == null) return; 46 PersistenceCapable detail = (PersistenceCapable) detailObject; 47 if (!detail.jdoIsPersistent()) { 48 owner.jdoGetPersistenceManager().makePersistent(detail); 49 } 50 ((PMProxy)owner.jdoGetPersistenceManager()).setMasterOnDetail(detail, detailFieldNo, null, false); 51 } 52 53 60 public static void addToOtherSideOfManyToMany(Object inverse, int inverseFieldNo, 61 PersistenceCapable toAdd) { 62 if (toAdd == null) return; 63 final PMProxy pm = (PMProxy) toAdd.jdoGetPersistenceManager(); 64 65 PersistenceCapable pcInverse = (PersistenceCapable) inverse; 66 if (!pcInverse.jdoIsPersistent()) pm.makePersistent(pcInverse); 67 68 Object otherSide = pm.getObjectField(pcInverse, inverseFieldNo); 69 if (otherSide instanceof VersantManagedSCOCollection) { 70 ((VersantManagedSCOCollection) otherSide).manyToManyAdd(toAdd); 71 } else { 72 ((Collection ) otherSide).add(toAdd); 73 } 74 } 75 76 83 public static void removeFromOtherSideOfManyToMany(Object inverse, 84 int inverseFieldNo, PersistenceCapable toRemove) { 85 if (toRemove == null) return; 86 PMProxy pm = (PMProxy) toRemove.jdoGetPersistenceManager(); 87 88 Object otherSide = pm.getObjectField((PersistenceCapable) inverse, inverseFieldNo); 89 if (otherSide instanceof VersantManagedSCOCollection) { 90 ((VersantManagedSCOCollection) otherSide).manyToManyRemove(toRemove); 91 } else { 92 ((Collection ) otherSide).remove(toRemove); 93 } 94 } 95 96 } 97 | Popular Tags |