1 10 11 package com.triactive.jdo.store; 12 13 import com.triactive.jdo.PersistenceManager; 14 import com.triactive.jdo.model.ClassMetaData; 15 import com.triactive.jdo.model.FieldMetaData; 16 import com.triactive.jdo.util.MacroString; 17 import java.sql.Connection ; 18 import java.sql.SQLException ; 19 import java.util.ArrayList ; 20 import java.util.StringTokenizer ; 21 import java.util.List ; 22 import javax.jdo.Extent; 23 import javax.jdo.JDOFatalInternalException; 24 import javax.jdo.JDOUnsupportedOptionException; 25 import javax.jdo.JDOUserException; 26 27 28 public class ClassView extends JDOView implements ClassTable 29 { 30 private final ClassMetaData cmd; 31 private final Class clazz; 32 private final MacroString viewDef; 33 34 private Mapping[] fieldMappings; 35 36 37 ClassView(TableMetadata tmd, ClassMetaData cmd, StoreManager storeMgr) 38 { 39 super(tmd, storeMgr); 40 41 this.cmd = cmd; 42 43 clazz = cmd.getPCClass(); 44 45 switch (cmd.getIdentityType()) 46 { 47 case ClassMetaData.NO_IDENTITY: 48 break; 49 50 case ClassMetaData.APPLICATION_IDENTITY: 51 throw new JDOUnsupportedOptionException("Application identity not supported: " + clazz.getName()); 52 53 case ClassMetaData.DATASTORE_IDENTITY: 54 throw new JDOUnsupportedOptionException("Datastore identity not supported: " + clazz.getName()); 55 56 default: 57 throw new JDOFatalInternalException("Invalid identity type on class " + clazz.getName()); 58 } 59 60 String viewImpStr = cmd.getViewImports(); 61 String viewDefStr = cmd.getViewDefinition(dba.getVendorID()); 62 63 if (viewDefStr == null) 64 throw new ViewDefinitionException(clazz, viewDefStr); 65 66 viewDef = new MacroString(clazz, viewImpStr, viewDefStr); 67 } 68 69 70 public void initialize() 71 { 72 assertIsUninitialized(); 73 74 if (cmd.getPCSuperclass() != null) 75 throw new PersistentSuperclassNotAllowedException(clazz); 76 77 int fieldCount = cmd.getFieldCount(); 78 fieldMappings = new Mapping[fieldCount]; 79 80 for (int fieldNumber = 0; fieldNumber < fieldCount; ++fieldNumber) 81 { 82 FieldMetaData fmd = cmd.getFieldRelative(fieldNumber); 83 84 switch (fmd.getPersistenceModifier()) 85 { 86 case FieldMetaData.PERSISTENCE_MODIFIER_NONE: 87 default: 88 throw new JDOFatalInternalException("Invalid persistence modifier on field " + fmd.getName()); 89 90 case FieldMetaData.PERSISTENCE_MODIFIER_TRANSACTIONAL: 91 break; 92 93 case FieldMetaData.PERSISTENCE_MODIFIER_PERSISTENT: 94 fieldMappings[fieldNumber] = dba.getMapping(newColumn(fmd)); 95 } 96 97 } 98 99 state = TABLE_STATE_INITIALIZED; 100 } 101 102 103 public Class getType() 104 { 105 return clazz; 106 } 107 108 109 public ClassMetaData getClassMetaData() 110 { 111 return cmd; 112 } 113 114 115 protected Column newColumn(FieldMetaData fmd) 116 { 117 Class type = fmd.getType(); 118 Column col = newColumn(type, fmd.getName()); 119 120 col.setOptions(fmd); 121 122 if (fmd.getNullValueHandling() != FieldMetaData.NULL_VALUE_EXCEPTION && !type.isPrimitive()) 123 col.setNullable(); 124 125 return col; 126 } 127 128 129 public boolean isFieldPersistent(int fieldNumber) 130 { 131 assertIsInitialized(); 132 133 return fieldMappings[fieldNumber] != null; 134 } 135 136 137 public Mapping getFieldMapping(int fieldNumber) 138 { 139 assertIsInitialized(); 140 141 Mapping m = fieldMappings[fieldNumber]; 142 143 if (m == null) 144 throw new NoSuchPersistentFieldException(clazz, fieldNumber); 145 146 return m; 147 } 148 149 150 public Mapping getFieldMapping(String fieldName) 151 { 152 assertIsInitialized(); 153 154 int rfn = cmd.getRelativeFieldNumber(fieldName); 155 156 if (rfn < 0) 157 throw new NoSuchPersistentFieldException(clazz, fieldName); 158 159 return getFieldMapping(rfn); 160 } 161 162 163 protected List getSQLCreateStatements() 164 { 165 assertIsInitialized(); 166 167 String stmt = viewDef.substituteMacros(new MacroString.MacroHandler() 168 { 169 public void onIdentifierMacro(MacroString.IdentifierMacro im) 170 { 171 storeMgr.resolveIdentifierMacro(im); 172 } 173 174 public void onParameterMacro(MacroString.ParameterMacro pm) 175 { 176 throw new JDOUserException("Parameter macros not allowed in view definitions: " + pm); 177 } 178 } 179 ); 180 181 ArrayList stmts = new ArrayList (); 182 StringTokenizer tokens = new StringTokenizer (stmt, ";"); 183 184 while (tokens.hasMoreTokens()) 185 stmts.add(tokens.nextToken()); 186 187 return stmts; 188 } 189 190 191 public Extent newExtent(PersistenceManager pm, boolean subclasses) 192 { 193 assertIsValidated(); 194 195 return new ClassViewExtent(pm, this, subclasses); 196 } 197 } 198 | Popular Tags |