KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > SetMapping


1 /*
2  * Copyright 2004 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: SetMapping.java,v 1.8 2004/02/01 18:22:42 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import com.triactive.jdo.SCO;
14 import com.triactive.jdo.StateManager;
15 import com.triactive.jdo.sco.SCOProcessor;
16 import com.triactive.jdo.model.ClassMetaData;
17 import com.triactive.jdo.model.CollectionMetaData;
18 import com.triactive.jdo.model.FieldMetaData;
19 import java.util.Set JavaDoc;
20 import javax.jdo.JDOUserException;
21 import javax.jdo.JDOFatalInternalException;
22
23
24 public class SetMapping extends ComplexMapping
25 {
26     protected final ClassBaseTable ownerTable;
27     protected final FieldMetaData fmd;
28     protected final SCOProcessor.SetProcessor scoProc;
29     protected final CollectionMetaData colmd;
30     protected final SetTable setTable;
31
32     protected SetStore setStore = null;
33
34
35     public SetMapping(DatabaseAdapter dba, Class JavaDoc type)
36     {
37         super(dba, type);
38
39         ownerTable = null;
40         fmd = null;
41         scoProc = null;
42         colmd = null;
43         setTable = null;
44     }
45
46     public SetMapping(ClassBaseTable ownerTable, int relativeFieldNumber)
47     {
48         super(ownerTable.getStoreManager().getDatabaseAdapter(),
49               ownerTable.getClassMetaData().getFieldRelative(relativeFieldNumber).getClass());
50
51         this.ownerTable = ownerTable;
52
53         ClassMetaData cmd = ownerTable.getClassMetaData();
54         StoreManager storeMgr = ownerTable.getStoreManager();
55
56         fmd = cmd.getFieldRelative(relativeFieldNumber);
57         scoProc = (SCOProcessor.SetProcessor)SCOProcessor.forFieldType(fmd.getType());
58         colmd = fmd.getCollectionMetaData();
59
60         if (colmd == null)
61             throw new JDOUserException("No collection metadata found in " + fmd);
62
63         setTable = colmd.isInverseCollection() ? null : storeMgr.newSetTable(ownerTable, fmd);
64     }
65
66     public synchronized SetStore getSetStore()
67     {
68         if (setStore == null)
69         {
70             if (setTable != null)
71                 setStore = new NormalSetStore(setTable);
72             else
73                 setStore = new InverseSetStore(fmd, ownerTable.getStoreManager());
74         }
75
76         return setStore;
77     }
78
79     public void insertObject(StateManager sm, Object JavaDoc value)
80     {
81         updateObject(sm, value);
82     }
83
84     public Object JavaDoc fetchObject(StateManager sm)
85     {
86         return scoProc.newSCOInstance(sm.getObject(), fmd.getName(), getSetStore());
87     }
88
89     public void updateObject(StateManager sm, Object JavaDoc value)
90     {
91         if (value == null)
92             throw new JDOUserException("Collection fields cannot be null");
93
94         if (!(value instanceof SCO))
95             throw new JDOFatalInternalException("SCO field not assigned an SCO wrapper object: " + fmd.getName());
96
97         ((SCO)value).applyUpdates();
98     }
99
100     public void deleteObject(StateManager sm)
101     {
102         if (colmd.clearOnDelete())
103             getSetStore().clear(sm);
104     }
105
106     public boolean equals(Object JavaDoc obj)
107     {
108         if (obj == this)
109             return true;
110
111         if (!obj.getClass().equals(getClass()))
112             return false;
113
114         SetMapping sm = (SetMapping)obj;
115
116         return (ownerTable == null ? (sm.ownerTable == null) : ownerTable.equals(sm.ownerTable))
117             && (fmd == null ? (sm.fmd == null) : fmd.getName().equals(sm.fmd.getName()));
118     }
119
120     public int hashCode()
121     {
122         return (ownerTable == null ? 0 : ownerTable.hashCode())
123              ^ (fmd == null ? 0 : fmd.getName().hashCode());
124     }
125
126     public SQLExpression newSQLLiteral(QueryStatement qs, Object JavaDoc value)
127     {
128         if (value instanceof Queryable)
129         {
130             Queryable q = (Queryable)value;
131             return new SubquerySetExpression(qs, q.newQueryStatement(q.getCandidateClass()));
132         }
133         else
134             return new SetLiteral(qs, (Set JavaDoc)value);
135     }
136
137     public SQLExpression newSQLExpression(QueryStatement qs, QueryStatement.QueryColumn qsc, String JavaDoc fieldName)
138     {
139         throw new JDOFatalInternalException("Cannot select column designating a set, set columns are predetermined");
140     }
141
142     public SQLExpression newSQLExpression(QueryStatement qs, TableExpression te, String JavaDoc fieldName)
143     {
144         return new CandidateSetExpression(qs,
145                                           qs.getColumn(te, ownerTable.getIDMapping().getColumn()),
146                                           getSetStore(),
147                                           fieldName);
148     }
149 }
150
Popular Tags