KickJava   Java API By Example, From Geeks To Geeks.

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


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: MapMapping.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.FieldMetaData;
18 import com.triactive.jdo.model.MapMetaData;
19 import java.util.Map JavaDoc;
20 import javax.jdo.JDOUserException;
21 import javax.jdo.JDOFatalInternalException;
22
23
24 public class MapMapping extends ComplexMapping
25 {
26     protected final ClassBaseTable ownerTable;
27     protected final FieldMetaData fmd;
28     protected final SCOProcessor.MapProcessor scoProc;
29     protected final MapMetaData mapmd;
30     protected final MapTable mapTable;
31
32     protected MapStore mapStore = null;
33
34
35     public MapMapping(ClassBaseTable ownerTable, int relativeFieldNumber)
36     {
37         super(ownerTable.getStoreManager().getDatabaseAdapter(),
38               ownerTable.getClassMetaData().getFieldRelative(relativeFieldNumber).getClass());
39
40         this.ownerTable = ownerTable;
41
42         ClassMetaData cmd = ownerTable.getClassMetaData();
43         StoreManager storeMgr = ownerTable.getStoreManager();
44
45         fmd = cmd.getFieldRelative(relativeFieldNumber);
46         scoProc = (SCOProcessor.MapProcessor)SCOProcessor.forFieldType(fmd.getType());
47         mapmd = fmd.getMapMetaData();
48
49         if (mapmd == null)
50             throw new JDOUserException("No map metadata found in " + fmd);
51
52         mapTable = mapmd.isInverseMap() ? null : storeMgr.newMapTable(ownerTable, fmd);
53     }
54
55     public synchronized MapStore getMapStore()
56     {
57         if (mapStore == null)
58         {
59             if (mapTable != null)
60                 mapStore = new NormalMapStore(mapTable);
61             else
62                 mapStore = new InverseMapStore(fmd, ownerTable.getStoreManager());
63         }
64
65         return mapStore;
66     }
67
68     public void insertObject(StateManager sm, Object JavaDoc value)
69     {
70         updateObject(sm, value);
71     }
72
73     public Object JavaDoc fetchObject(StateManager sm)
74     {
75         return scoProc.newSCOInstance(sm.getObject(), fmd.getName(), getMapStore());
76     }
77
78     public void updateObject(StateManager sm, Object JavaDoc value)
79     {
80         if (value == null)
81             throw new JDOUserException("Map fields cannot be null");
82
83         if (!(value instanceof SCO))
84             throw new JDOFatalInternalException("SCO field (Map) not wrapped by SCO object: " + fmd.getName());
85
86         ((SCO)value).applyUpdates();
87     }
88
89     public void deleteObject(StateManager sm)
90     {
91         if (mapmd.clearOnDelete())
92             getMapStore().clear(sm);
93     }
94
95     public boolean equals(Object JavaDoc obj)
96     {
97         if (obj == this)
98             return true;
99
100         if (!obj.getClass().equals(getClass()))
101             return false;
102
103         MapMapping mm = (MapMapping)obj;
104
105         return ownerTable.equals(mm.ownerTable) && fmd.getName().equals(mm.fmd.getName());
106     }
107
108     public int hashCode()
109     {
110         return ownerTable.hashCode() ^ fmd.getName().hashCode();
111     }
112
113     public SQLExpression newSQLLiteral(QueryStatement qs, Object JavaDoc value)
114     {
115         // TODO: do this right
116
throw new JDOFatalInternalException("Maps not yet supported in query expressions");
117     }
118
119     public SQLExpression newSQLExpression(QueryStatement qs, QueryStatement.QueryColumn qsc, String JavaDoc fieldName)
120     {
121         throw new JDOFatalInternalException("Cannot select column designating a map, map columns are predetermined");
122     }
123
124     public SQLExpression newSQLExpression(QueryStatement qs, TableExpression te, String JavaDoc fieldName)
125     {
126         // TODO: do this right
127
throw new JDOFatalInternalException("Maps not yet supported in query expressions");
128     }
129 }
130
Popular Tags