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: SetMapping.java,v 1.8 2004/02/01 18:22:42 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.store;
012:
013: import com.triactive.jdo.SCO;
014: import com.triactive.jdo.StateManager;
015: import com.triactive.jdo.sco.SCOProcessor;
016: import com.triactive.jdo.model.ClassMetaData;
017: import com.triactive.jdo.model.CollectionMetaData;
018: import com.triactive.jdo.model.FieldMetaData;
019: import java.util.Set;
020: import javax.jdo.JDOUserException;
021: import javax.jdo.JDOFatalInternalException;
022:
023: public class SetMapping extends ComplexMapping {
024: protected final ClassBaseTable ownerTable;
025: protected final FieldMetaData fmd;
026: protected final SCOProcessor.SetProcessor scoProc;
027: protected final CollectionMetaData colmd;
028: protected final SetTable setTable;
029:
030: protected SetStore setStore = null;
031:
032: public SetMapping(DatabaseAdapter dba, Class type) {
033: super (dba, type);
034:
035: ownerTable = null;
036: fmd = null;
037: scoProc = null;
038: colmd = null;
039: setTable = null;
040: }
041:
042: public SetMapping(ClassBaseTable ownerTable, int relativeFieldNumber) {
043: super (ownerTable.getStoreManager().getDatabaseAdapter(),
044: ownerTable.getClassMetaData().getFieldRelative(
045: relativeFieldNumber).getClass());
046:
047: this .ownerTable = ownerTable;
048:
049: ClassMetaData cmd = ownerTable.getClassMetaData();
050: StoreManager storeMgr = ownerTable.getStoreManager();
051:
052: fmd = cmd.getFieldRelative(relativeFieldNumber);
053: scoProc = (SCOProcessor.SetProcessor) SCOProcessor
054: .forFieldType(fmd.getType());
055: colmd = fmd.getCollectionMetaData();
056:
057: if (colmd == null)
058: throw new JDOUserException(
059: "No collection metadata found in " + fmd);
060:
061: setTable = colmd.isInverseCollection() ? null : storeMgr
062: .newSetTable(ownerTable, fmd);
063: }
064:
065: public synchronized SetStore getSetStore() {
066: if (setStore == null) {
067: if (setTable != null)
068: setStore = new NormalSetStore(setTable);
069: else
070: setStore = new InverseSetStore(fmd, ownerTable
071: .getStoreManager());
072: }
073:
074: return setStore;
075: }
076:
077: public void insertObject(StateManager sm, Object value) {
078: updateObject(sm, value);
079: }
080:
081: public Object fetchObject(StateManager sm) {
082: return scoProc.newSCOInstance(sm.getObject(), fmd.getName(),
083: getSetStore());
084: }
085:
086: public void updateObject(StateManager sm, Object value) {
087: if (value == null)
088: throw new JDOUserException(
089: "Collection fields cannot be null");
090:
091: if (!(value instanceof SCO))
092: throw new JDOFatalInternalException(
093: "SCO field not assigned an SCO wrapper object: "
094: + fmd.getName());
095:
096: ((SCO) value).applyUpdates();
097: }
098:
099: public void deleteObject(StateManager sm) {
100: if (colmd.clearOnDelete())
101: getSetStore().clear(sm);
102: }
103:
104: public boolean equals(Object obj) {
105: if (obj == this )
106: return true;
107:
108: if (!obj.getClass().equals(getClass()))
109: return false;
110:
111: SetMapping sm = (SetMapping) obj;
112:
113: return (ownerTable == null ? (sm.ownerTable == null)
114: : ownerTable.equals(sm.ownerTable))
115: && (fmd == null ? (sm.fmd == null) : fmd.getName()
116: .equals(sm.fmd.getName()));
117: }
118:
119: public int hashCode() {
120: return (ownerTable == null ? 0 : ownerTable.hashCode())
121: ^ (fmd == null ? 0 : fmd.getName().hashCode());
122: }
123:
124: public SQLExpression newSQLLiteral(QueryStatement qs, Object value) {
125: if (value instanceof Queryable) {
126: Queryable q = (Queryable) value;
127: return new SubquerySetExpression(qs, q.newQueryStatement(q
128: .getCandidateClass()));
129: } else
130: return new SetLiteral(qs, (Set) value);
131: }
132:
133: public SQLExpression newSQLExpression(QueryStatement qs,
134: QueryStatement.QueryColumn qsc, String fieldName) {
135: throw new JDOFatalInternalException(
136: "Cannot select column designating a set, set columns are predetermined");
137: }
138:
139: public SQLExpression newSQLExpression(QueryStatement qs,
140: TableExpression te, String fieldName) {
141: return new CandidateSetExpression(qs, qs.getColumn(te,
142: ownerTable.getIDMapping().getColumn()), getSetStore(),
143: fieldName);
144: }
145: }
|