001: /*
002: * Copyright 2004 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: SCOProcessor.java,v 1.3 2004/02/07 22:42:50 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.sco;
012:
013: import com.triactive.jdo.PersistenceManager;
014: import com.triactive.jdo.SCO;
015: import com.triactive.jdo.store.Mapping;
016: import com.triactive.jdo.store.MapMapping;
017: import com.triactive.jdo.store.MapStore;
018: import com.triactive.jdo.store.SetMapping;
019: import com.triactive.jdo.store.SetStore;
020: import javax.jdo.JDOHelper;
021: import javax.jdo.JDOUserException;
022:
023: /**
024: * An object that helps manage SCO fields of a particular declared type.
025: * Currently, an SCO processor's only function is to serve as a factory for SCO
026: * insatnces.
027: *
028: * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
029: * @version $Revision: 1.3 $
030: */
031:
032: public abstract class SCOProcessor {
033: private static final java.util.HashMap processors = new java.util.HashMap();
034:
035: static {
036: SCOProcessor hashMapProcessor = new HashMapProcessor();
037: SCOProcessor hashSetProcessor = new HashSetProcessor();
038:
039: processors.put(java.util.Collection.class, hashSetProcessor);
040: processors.put(java.util.Date.class, new DateProcessor());
041: processors.put(java.util.HashMap.class, hashMapProcessor);
042: processors.put(java.util.HashSet.class, hashSetProcessor);
043: processors.put(java.util.Hashtable.class,
044: new HashtableProcessor());
045: processors.put(java.util.Map.class, hashMapProcessor);
046: processors.put(java.util.Set.class, hashSetProcessor);
047: processors.put(java.sql.Date.class, new SqlDateProcessor());
048: processors.put(java.sql.Timestamp.class,
049: new SqlTimestampProcessor());
050: }
051:
052: /**
053: * Tests whether a given type represents a supported second-class mutable
054: * type.
055: *
056: * @param c
057: * The class to be tested.
058: * @return
059: * <code>true</code> if fields declared as type <var>c</var> are
060: * supported as second-class mutable objects, <code>false</code>
061: * otherwise.
062: */
063:
064: public static boolean isSecondClassMutableType(Class c) {
065: return processors.containsKey(c);
066: }
067:
068: /**
069: * Returns the SCO processor for fields of the given type.
070: *
071: * @param c
072: * The type of field.
073: * @return
074: * The corresponding SCO processor.
075: */
076:
077: public static SCOProcessor forFieldType(Class c) {
078: return (SCOProcessor) processors.get(c);
079: }
080:
081: /**
082: * Returns a new SCO instance.
083: * The returned instance is owned by the specified owner but has
084: * <em>not</em> been assigned to the corresponding field.
085: *
086: * @param owner
087: * The first-class object that will own the new second-class object.
088: * @param fieldName
089: * The name of the field in the owning object.
090: * @param value
091: * The initial value of the new object.
092: *
093: * @return
094: * A new SCO instance.
095: */
096:
097: public abstract SCO newSCOInstance(Object owner, String fieldName,
098: Object value);
099:
100: private static Mapping getMapping(Object owner, String fieldName) {
101: return ((PersistenceManager) JDOHelper
102: .getPersistenceManager(owner)).getStoreManager()
103: .getClassBaseTable(owner.getClass()).getFieldMapping(
104: fieldName);
105: }
106:
107: /**
108: * An SCO processor for Map fields.
109: */
110:
111: public static abstract class MapProcessor extends SCOProcessor {
112: /**
113: * Returns the backing store for the specified Map field.
114: */
115: protected MapStore getMapStore(Object owner, String fieldName) {
116: return ((MapMapping) getMapping(owner, fieldName))
117: .getMapStore();
118: }
119:
120: /**
121: * Returns a new SCO Map instance that is unloaded.
122: * The returned instance is owned by the specified owner but has
123: * <em>not</em> been assigned to the corresponding field.
124: * <p>
125: * Initially, the contents of the map are not necessarily loaded in
126: * memory. Its contents are partially or fully loaded from the backing
127: * store in response to application method calls on the map.
128: *
129: * @param owner
130: * The first-class object that will own the new second-class object.
131: * @param fieldName
132: * The name of the field in the owning object.
133: * @param mapStore
134: * The backing store for the map.
135: *
136: * @return
137: * A new SCO instance.
138: */
139: public abstract SCO newSCOInstance(Object owner,
140: String fieldName, MapStore mapStore);
141: }
142:
143: /**
144: * An SCO processor for Set fields.
145: */
146:
147: public static abstract class SetProcessor extends SCOProcessor {
148: /**
149: * Returns the backing store for the specified Collection or Set field.
150: */
151: protected SetStore getSetStore(Object owner, String fieldName) {
152: return ((SetMapping) getMapping(owner, fieldName))
153: .getSetStore();
154: }
155:
156: /**
157: * Returns a new SCO Set instance that is unloaded.
158: * The returned instance is owned by the specified owner but has
159: * <em>not</em> been assigned to the corresponding field.
160: * <p>
161: * Initially, the contents of the set are not necessarily loaded in
162: * memory. Its contents are partially or fully loaded from the backing
163: * store in response to application method calls on the set.
164: *
165: * @param owner
166: * The first-class object that will own the new second-class object.
167: * @param fieldName
168: * The name of the field in the owning object.
169: * @param setStore
170: * The backing store for the set.
171: *
172: * @return
173: * A new SCO instance.
174: */
175: public abstract SCO newSCOInstance(Object owner,
176: String fieldName, SetStore setStore);
177: }
178:
179: /**
180: * The SCO processor for java.util.Date fields.
181: */
182: public static class DateProcessor extends SCOProcessor {
183: public SCO newSCOInstance(Object owner, String fieldName,
184: Object value) {
185: return new com.triactive.jdo.sco.Date(owner, fieldName,
186: (java.util.Date) value);
187: }
188: }
189:
190: /**
191: * The SCO processor for java.util.HashMap fields.
192: */
193: public static class HashMapProcessor extends MapProcessor {
194: public SCO newSCOInstance(Object owner, String fieldName,
195: MapStore mapStore) {
196: return new com.triactive.jdo.sco.HashMap(owner, fieldName,
197: mapStore);
198: }
199:
200: public SCO newSCOInstance(Object owner, String fieldName,
201: Object value) {
202: return new com.triactive.jdo.sco.HashMap(owner, fieldName,
203: getMapStore(owner, fieldName),
204: (java.util.Map) value);
205: }
206: }
207:
208: /**
209: * The SCO processor for java.util.HashSet fields.
210: */
211: public static class HashSetProcessor extends SetProcessor {
212: public SCO newSCOInstance(Object owner, String fieldName,
213: SetStore setStore) {
214: return new com.triactive.jdo.sco.HashSet(owner, fieldName,
215: setStore);
216: }
217:
218: public SCO newSCOInstance(Object owner, String fieldName,
219: Object value) {
220: return new com.triactive.jdo.sco.HashSet(owner, fieldName,
221: getSetStore(owner, fieldName),
222: (java.util.Collection) value);
223: }
224: }
225:
226: /**
227: * The SCO processor for java.util.Hashtable fields.
228: */
229: public static class HashtableProcessor extends MapProcessor {
230: public SCO newSCOInstance(Object owner, String fieldName,
231: MapStore mapStore) {
232: return new com.triactive.jdo.sco.Hashtable(owner,
233: fieldName, mapStore);
234: }
235:
236: public SCO newSCOInstance(Object owner, String fieldName,
237: Object value) {
238: return new com.triactive.jdo.sco.Hashtable(owner,
239: fieldName, getMapStore(owner, fieldName),
240: (java.util.Map) value);
241: }
242: }
243:
244: /**
245: * The SCO processor for java.sql.Date fields.
246: */
247: public static class SqlDateProcessor extends SCOProcessor {
248: public SCO newSCOInstance(Object owner, String fieldName,
249: Object value) {
250: return new com.triactive.jdo.sco.SqlDate(owner, fieldName,
251: (java.sql.Date) value);
252: }
253: }
254:
255: /**
256: * The SCO processor for java.sql.Timestamp fields.
257: */
258: public static class SqlTimestampProcessor extends SCOProcessor {
259: public SCO newSCOInstance(Object owner, String fieldName,
260: Object value) {
261: return new com.triactive.jdo.sco.SqlTimestamp(owner,
262: fieldName, (java.sql.Timestamp) value);
263: }
264: }
265: }
|