KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > ClassView


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: ClassView.java,v 1.5 2003/05/24 03:33:13 jackknifebarber Exp $
9  */

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 JavaDoc;
18 import java.sql.SQLException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.StringTokenizer JavaDoc;
21 import java.util.List JavaDoc;
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 JavaDoc 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 JavaDoc viewImpStr = cmd.getViewImports();
61         String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc getSQLCreateStatements()
164     {
165         assertIsInitialized();
166
167         String JavaDoc 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 JavaDoc stmts = new ArrayList JavaDoc();
182         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(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