001: /*
002: * Copyright 2002 (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: ClassView.java,v 1.5 2003/05/24 03:33:13 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.store;
012:
013: import com.triactive.jdo.PersistenceManager;
014: import com.triactive.jdo.model.ClassMetaData;
015: import com.triactive.jdo.model.FieldMetaData;
016: import com.triactive.jdo.util.MacroString;
017: import java.sql.Connection;
018: import java.sql.SQLException;
019: import java.util.ArrayList;
020: import java.util.StringTokenizer;
021: import java.util.List;
022: import javax.jdo.Extent;
023: import javax.jdo.JDOFatalInternalException;
024: import javax.jdo.JDOUnsupportedOptionException;
025: import javax.jdo.JDOUserException;
026:
027: public class ClassView extends JDOView implements ClassTable {
028: private final ClassMetaData cmd;
029: private final Class clazz;
030: private final MacroString viewDef;
031:
032: private Mapping[] fieldMappings;
033:
034: ClassView(TableMetadata tmd, ClassMetaData cmd,
035: StoreManager storeMgr) {
036: super (tmd, storeMgr);
037:
038: this .cmd = cmd;
039:
040: clazz = cmd.getPCClass();
041:
042: switch (cmd.getIdentityType()) {
043: case ClassMetaData.NO_IDENTITY:
044: break;
045:
046: case ClassMetaData.APPLICATION_IDENTITY:
047: throw new JDOUnsupportedOptionException(
048: "Application identity not supported: "
049: + clazz.getName());
050:
051: case ClassMetaData.DATASTORE_IDENTITY:
052: throw new JDOUnsupportedOptionException(
053: "Datastore identity not supported: "
054: + clazz.getName());
055:
056: default:
057: throw new JDOFatalInternalException(
058: "Invalid identity type on class " + clazz.getName());
059: }
060:
061: String viewImpStr = cmd.getViewImports();
062: String viewDefStr = cmd.getViewDefinition(dba.getVendorID());
063:
064: if (viewDefStr == null)
065: throw new ViewDefinitionException(clazz, viewDefStr);
066:
067: viewDef = new MacroString(clazz, viewImpStr, viewDefStr);
068: }
069:
070: public void initialize() {
071: assertIsUninitialized();
072:
073: if (cmd.getPCSuperclass() != null)
074: throw new PersistentSuperclassNotAllowedException(clazz);
075:
076: int fieldCount = cmd.getFieldCount();
077: fieldMappings = new Mapping[fieldCount];
078:
079: for (int fieldNumber = 0; fieldNumber < fieldCount; ++fieldNumber) {
080: FieldMetaData fmd = cmd.getFieldRelative(fieldNumber);
081:
082: switch (fmd.getPersistenceModifier()) {
083: case FieldMetaData.PERSISTENCE_MODIFIER_NONE:
084: default:
085: throw new JDOFatalInternalException(
086: "Invalid persistence modifier on field "
087: + fmd.getName());
088:
089: case FieldMetaData.PERSISTENCE_MODIFIER_TRANSACTIONAL:
090: break;
091:
092: case FieldMetaData.PERSISTENCE_MODIFIER_PERSISTENT:
093: fieldMappings[fieldNumber] = dba
094: .getMapping(newColumn(fmd));
095: }
096:
097: }
098:
099: state = TABLE_STATE_INITIALIZED;
100: }
101:
102: public Class getType() {
103: return clazz;
104: }
105:
106: public ClassMetaData getClassMetaData() {
107: return cmd;
108: }
109:
110: protected Column newColumn(FieldMetaData fmd) {
111: Class type = fmd.getType();
112: Column col = newColumn(type, fmd.getName());
113:
114: col.setOptions(fmd);
115:
116: if (fmd.getNullValueHandling() != FieldMetaData.NULL_VALUE_EXCEPTION
117: && !type.isPrimitive())
118: col.setNullable();
119:
120: return col;
121: }
122:
123: public boolean isFieldPersistent(int fieldNumber) {
124: assertIsInitialized();
125:
126: return fieldMappings[fieldNumber] != null;
127: }
128:
129: public Mapping getFieldMapping(int fieldNumber) {
130: assertIsInitialized();
131:
132: Mapping m = fieldMappings[fieldNumber];
133:
134: if (m == null)
135: throw new NoSuchPersistentFieldException(clazz, fieldNumber);
136:
137: return m;
138: }
139:
140: public Mapping getFieldMapping(String fieldName) {
141: assertIsInitialized();
142:
143: int rfn = cmd.getRelativeFieldNumber(fieldName);
144:
145: if (rfn < 0)
146: throw new NoSuchPersistentFieldException(clazz, fieldName);
147:
148: return getFieldMapping(rfn);
149: }
150:
151: protected List getSQLCreateStatements() {
152: assertIsInitialized();
153:
154: String stmt = viewDef
155: .substituteMacros(new MacroString.MacroHandler() {
156: public void onIdentifierMacro(
157: MacroString.IdentifierMacro im) {
158: storeMgr.resolveIdentifierMacro(im);
159: }
160:
161: public void onParameterMacro(
162: MacroString.ParameterMacro pm) {
163: throw new JDOUserException(
164: "Parameter macros not allowed in view definitions: "
165: + pm);
166: }
167: });
168:
169: ArrayList stmts = new ArrayList();
170: StringTokenizer tokens = new StringTokenizer(stmt, ";");
171:
172: while (tokens.hasMoreTokens())
173: stmts.add(tokens.nextToken());
174:
175: return stmts;
176: }
177:
178: public Extent newExtent(PersistenceManager pm, boolean subclasses) {
179: assertIsValidated();
180:
181: return new ClassViewExtent(pm, this, subclasses);
182: }
183: }
|