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: MapMapping.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.FieldMetaData;
018: import com.triactive.jdo.model.MapMetaData;
019: import java.util.Map;
020: import javax.jdo.JDOUserException;
021: import javax.jdo.JDOFatalInternalException;
022:
023: public class MapMapping extends ComplexMapping {
024: protected final ClassBaseTable ownerTable;
025: protected final FieldMetaData fmd;
026: protected final SCOProcessor.MapProcessor scoProc;
027: protected final MapMetaData mapmd;
028: protected final MapTable mapTable;
029:
030: protected MapStore mapStore = null;
031:
032: public MapMapping(ClassBaseTable ownerTable, int relativeFieldNumber) {
033: super (ownerTable.getStoreManager().getDatabaseAdapter(),
034: ownerTable.getClassMetaData().getFieldRelative(
035: relativeFieldNumber).getClass());
036:
037: this .ownerTable = ownerTable;
038:
039: ClassMetaData cmd = ownerTable.getClassMetaData();
040: StoreManager storeMgr = ownerTable.getStoreManager();
041:
042: fmd = cmd.getFieldRelative(relativeFieldNumber);
043: scoProc = (SCOProcessor.MapProcessor) SCOProcessor
044: .forFieldType(fmd.getType());
045: mapmd = fmd.getMapMetaData();
046:
047: if (mapmd == null)
048: throw new JDOUserException("No map metadata found in "
049: + fmd);
050:
051: mapTable = mapmd.isInverseMap() ? null : storeMgr.newMapTable(
052: ownerTable, fmd);
053: }
054:
055: public synchronized MapStore getMapStore() {
056: if (mapStore == null) {
057: if (mapTable != null)
058: mapStore = new NormalMapStore(mapTable);
059: else
060: mapStore = new InverseMapStore(fmd, ownerTable
061: .getStoreManager());
062: }
063:
064: return mapStore;
065: }
066:
067: public void insertObject(StateManager sm, Object value) {
068: updateObject(sm, value);
069: }
070:
071: public Object fetchObject(StateManager sm) {
072: return scoProc.newSCOInstance(sm.getObject(), fmd.getName(),
073: getMapStore());
074: }
075:
076: public void updateObject(StateManager sm, Object value) {
077: if (value == null)
078: throw new JDOUserException("Map fields cannot be null");
079:
080: if (!(value instanceof SCO))
081: throw new JDOFatalInternalException(
082: "SCO field (Map) not wrapped by SCO object: "
083: + fmd.getName());
084:
085: ((SCO) value).applyUpdates();
086: }
087:
088: public void deleteObject(StateManager sm) {
089: if (mapmd.clearOnDelete())
090: getMapStore().clear(sm);
091: }
092:
093: public boolean equals(Object obj) {
094: if (obj == this )
095: return true;
096:
097: if (!obj.getClass().equals(getClass()))
098: return false;
099:
100: MapMapping mm = (MapMapping) obj;
101:
102: return ownerTable.equals(mm.ownerTable)
103: && fmd.getName().equals(mm.fmd.getName());
104: }
105:
106: public int hashCode() {
107: return ownerTable.hashCode() ^ fmd.getName().hashCode();
108: }
109:
110: public SQLExpression newSQLLiteral(QueryStatement qs, Object value) {
111: // TODO: do this right
112: throw new JDOFatalInternalException(
113: "Maps not yet supported in query expressions");
114: }
115:
116: public SQLExpression newSQLExpression(QueryStatement qs,
117: QueryStatement.QueryColumn qsc, String fieldName) {
118: throw new JDOFatalInternalException(
119: "Cannot select column designating a map, map columns are predetermined");
120: }
121:
122: public SQLExpression newSQLExpression(QueryStatement qs,
123: TableExpression te, String fieldName) {
124: // TODO: do this right
125: throw new JDOFatalInternalException(
126: "Maps not yet supported in query expressions");
127: }
128: }
|