KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > sco > SCOInverseUtil


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

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 JavaDoc;
18
19 /**
20  * Utility methods to help SCOs implement inverse mappings.
21  */

22 public class SCOInverseUtil {
23
24     /**
25      * Change the master reference on detailObject. This will remove the
26      * detail from its current master (if any and if different).
27      */

28     public static void addMasterOnDetail(Object JavaDoc 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     /**
39      * Clear the master reference on detailObject. This will <b>not</b> remove
40      * the detail from its current master. This can be done in bulk or more
41      * efficiently by the SCO.
42      */

43     public static void removeMasterOnDetail(Object JavaDoc 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     /**
54      * Add to the other half of a many-to-many relationship.
55      *
56      * @param inverse The instance on the other side of the relationship
57      * @param inverseFieldNo The collection field on inverse to add to
58      * @param toAdd The instance to add to the collection on inverse
59      */

60     public static void addToOtherSideOfManyToMany(Object JavaDoc 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 JavaDoc otherSide = pm.getObjectField(pcInverse, inverseFieldNo);
69         if (otherSide instanceof VersantManagedSCOCollection) {
70             ((VersantManagedSCOCollection) otherSide).manyToManyAdd(toAdd);
71         } else {
72             ((Collection JavaDoc) otherSide).add(toAdd);
73         }
74     }
75
76     /**
77      * Remove from the other half of a many-to-many relationship.
78      *
79      * @param inverse The instance on the other side of the relationship
80      * @param inverseFieldNo The collection field on inverse to remove from
81      * @param toRemove The instance to remve from the collection on inverse
82      */

83     public static void removeFromOtherSideOfManyToMany(Object JavaDoc inverse,
84                                                        int inverseFieldNo, PersistenceCapable toRemove) {
85         if (toRemove == null) return;
86         PMProxy pm = (PMProxy) toRemove.jdoGetPersistenceManager();
87
88         Object JavaDoc otherSide = pm.getObjectField((PersistenceCapable) inverse, inverseFieldNo);
89         if (otherSide instanceof VersantManagedSCOCollection) {
90             ((VersantManagedSCOCollection) otherSide).manyToManyRemove(toRemove);
91         } else {
92             ((Collection JavaDoc) otherSide).remove(toRemove);
93         }
94     }
95
96 }
97
Popular Tags