KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > type > AbstractStatefulType


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Rodrigo Westrupp
28  */

29
30 package com.caucho.amber.type;
31
32 import com.caucho.amber.field.AmberField;
33 import com.caucho.amber.field.AmberFieldCompare;
34 import com.caucho.amber.manager.AmberPersistenceUnit;
35 import com.caucho.amber.table.Column;
36 import com.caucho.amber.table.Table;
37 import com.caucho.bytecode.JClass;
38 import com.caucho.bytecode.JClassDependency;
39 import com.caucho.bytecode.JField;
40 import com.caucho.bytecode.JMethod;
41 import com.caucho.config.ConfigException;
42 import com.caucho.java.JavaWriter;
43 import com.caucho.make.ClassDependency;
44 import com.caucho.util.CharBuffer;
45 import com.caucho.util.L10N;
46 import com.caucho.vfs.PersistentDependency;
47
48 import java.io.IOException JavaDoc;
49 import java.util.ArrayList JavaDoc;
50 import java.util.Collections JavaDoc;
51 import java.util.HashMap JavaDoc;
52 import java.util.logging.Logger JavaDoc;
53
54 /**
55  * Represents a stateful type:
56  * embeddable, entity or mapped-superclass.
57  */

58 abstract public class AbstractStatefulType extends AbstractEnhancedType {
59   private static final Logger JavaDoc log = Logger.getLogger(AbstractStatefulType.class.getName());
60   private static final L10N L = new L10N(AbstractStatefulType.class);
61
62   private boolean _isFieldAccess;
63
64   private ArrayList JavaDoc<AmberField> _fields = new ArrayList JavaDoc<AmberField>();
65
66   private volatile boolean _isConfigured;
67
68   private ArrayList JavaDoc<PersistentDependency> _dependencies
69     = new ArrayList JavaDoc<PersistentDependency>();
70
71   private HashMap JavaDoc<String JavaDoc,String JavaDoc> _completionFields
72     = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
73
74   private Column _discriminator;
75
76   public AbstractStatefulType(AmberPersistenceUnit amberPersistenceUnit)
77   {
78     super(amberPersistenceUnit);
79   }
80
81   /**
82    * Set true for field-access.
83    */

84   public void setFieldAccess(boolean isFieldAccess)
85   {
86     _isFieldAccess = isFieldAccess;
87   }
88
89   /**
90    * Set true for field-access.
91    */

92   public boolean isFieldAccess()
93   {
94     return _isFieldAccess;
95   }
96
97   /**
98    * Returns the discriminator.
99    */

100   public Column getDiscriminator()
101   {
102     return _discriminator;
103   }
104
105   /**
106    * Sets the discriminator.
107    */

108   public void setDiscriminator(Column discriminator)
109   {
110     _discriminator = discriminator;
111   }
112
113   /**
114    * Returns the java type.
115    */

116   public String JavaDoc getJavaTypeName()
117   {
118     return getInstanceClassName();
119   }
120
121   /**
122    * Adds a new field.
123    */

124   public void addField(AmberField field)
125   {
126     _fields.add(field);
127     Collections.sort(_fields, new AmberFieldCompare());
128   }
129
130   /**
131    * Returns the fields.
132    */

133   public ArrayList JavaDoc<AmberField> getFields()
134   {
135     return _fields;
136   }
137
138   /**
139    * Returns the field with a given name.
140    */

141   public AmberField getField(String JavaDoc name)
142   {
143     for (int i = 0; i < _fields.size(); i++) {
144       AmberField field = _fields.get(i);
145
146       if (field.getName().equals(name))
147         return field;
148     }
149
150     return null;
151   }
152
153   /**
154    * Sets the bean class.
155    */

156   public void setBeanClass(JClass beanClass)
157   {
158     super.setBeanClass(beanClass);
159
160     addDependency(_beanClass);
161   }
162
163   /**
164    * Adds a dependency.
165    */

166   public void addDependency(Class JavaDoc cl)
167   {
168     addDependency(new ClassDependency(cl));
169   }
170
171   /**
172    * Adds a dependency.
173    */

174   public void addDependency(JClass cl)
175   {
176     addDependency(new JClassDependency(cl));
177   }
178
179   /**
180    * Adds a dependency.
181    */

182   public void addDependency(PersistentDependency depend)
183   {
184     if (! _dependencies.contains(depend))
185       _dependencies.add(depend);
186   }
187
188   /**
189    * Gets the dependency.
190    */

191   public ArrayList JavaDoc<PersistentDependency> getDependencies()
192   {
193     return _dependencies;
194   }
195
196   /**
197    * Adds a new completion field.
198    */

199   public void addCompletionField(String JavaDoc name)
200   {
201     _completionFields.put(name, name);
202   }
203
204   /**
205    * Returns true if and only if it has the completion field.
206    */

207   public boolean containsCompletionField(String JavaDoc completionField)
208   {
209     return _completionFields.containsKey(completionField);
210   }
211
212   /**
213    * Remove all completion fields.
214    */

215   public void removeAllCompletionFields()
216   {
217     _completionFields.clear();
218   }
219
220   /**
221    * Set true if configured.
222    */

223   public boolean startConfigure()
224   {
225     synchronized (this) {
226       if (_isConfigured)
227         return false;
228
229       _isConfigured = true;
230
231       return true;
232     }
233   }
234
235   /**
236    * Initialize the type.
237    */

238   public void init()
239     throws ConfigException
240   {
241   }
242
243   /**
244    * Converts the value.
245    */

246   public String JavaDoc generateCastFromObject(String JavaDoc value)
247   {
248     return "((" + getInstanceClassName() + ") " + value + ")";
249   }
250
251   /**
252    * Generates a string to load the field.
253    */

254   public int generateLoad(JavaWriter out, String JavaDoc rs,
255                           String JavaDoc indexVar, int index, int loadGroupIndex)
256     throws IOException JavaDoc
257   {
258     if (loadGroupIndex == 0 && _discriminator != null)
259       index++;
260
261     ArrayList JavaDoc<AmberField> fields = getFields();
262     for (int i = 0; i < fields.size(); i++) {
263       AmberField field = fields.get(i);
264
265       if (field.getLoadGroupIndex() == loadGroupIndex)
266         index = field.generateLoad(out, rs, indexVar, index);
267     }
268
269     return index;
270   }
271
272   /**
273    * Generates the select clause for a load.
274    */

275   abstract public String JavaDoc generateLoadSelect(Table table, String JavaDoc id);
276
277   /**
278    * Generates the select clause for a load.
279    */

280   public String JavaDoc generateLoadSelect(Table table,
281                                    String JavaDoc id,
282                                    int loadGroup)
283   {
284     return generateLoadSelect(table, id, loadGroup, false);
285   }
286
287   /**
288    * Generates the select clause for a load.
289    */

290   public String JavaDoc generateLoadSelect(Table table,
291                                    String JavaDoc id,
292                                    int loadGroup,
293                                    boolean hasSelect)
294   {
295     CharBuffer cb = CharBuffer.allocate();
296
297     ArrayList JavaDoc<AmberField> fields = getFields();
298
299     for (int i = 0; i < fields.size(); i++) {
300       AmberField field = fields.get(i);
301
302       if (field.getLoadGroupIndex() != loadGroup)
303         continue;
304
305       String JavaDoc propSelect = field.generateLoadSelect(table, id);
306
307       if (propSelect == null)
308         continue;
309
310       if (hasSelect)
311         cb.append(", ");
312       hasSelect = true;
313
314       cb.append(propSelect);
315     }
316
317     if (cb.length() == 0)
318       return null;
319     else
320       return cb.close();
321   }
322
323   /**
324    * Generates the foreign delete
325    */

326   public void generateInvalidateForeign(JavaWriter out)
327     throws IOException JavaDoc
328   {
329     for (AmberField field : getFields()) {
330       field.generateInvalidateForeign(out);
331     }
332   }
333
334   /**
335    * Generates any expiration code.
336    */

337   public void generateExpire(JavaWriter out)
338     throws IOException JavaDoc
339   {
340     for (AmberField field : getFields()) {
341       field.generateExpire(out);
342     }
343   }
344
345   /**
346    * Gets a matching getter.
347    */

348   public JMethod getGetter(String JavaDoc name)
349   {
350     return getGetter(_beanClass, name);
351   }
352
353   /**
354    * Gets a matching getter.
355    */

356   public static JMethod getGetter(JClass cl, String JavaDoc name)
357   {
358     JMethod []methods = cl.getMethods();
359
360     for (int i = 0; i < methods.length; i++) {
361       JClass []param = methods[i].getParameterTypes();
362       String JavaDoc methodName = methods[i].getName();
363
364       if (name.equals(methodName) && param.length == 0)
365         return methods[i];
366     }
367
368     cl = cl.getSuperClass();
369
370     if (cl != null)
371       return getGetter(cl, name);
372     else
373       return null;
374   }
375
376   /**
377    * Gets a matching getter.
378    */

379   public static JField getField(JClass cl, String JavaDoc name)
380   {
381     JField []fields = cl.getDeclaredFields();
382
383     for (int i = 0; i < fields.length; i++) {
384       if (name.equals(fields[i].getName()))
385         return fields[i];
386     }
387
388     cl = cl.getSuperClass();
389
390     if (cl != null)
391       return getField(cl, name);
392     else
393       return null;
394   }
395
396   /**
397    * Gets a matching getter.
398    */

399   public static JMethod getSetter(JClass cl, String JavaDoc name)
400   {
401     JMethod []methods = cl.getMethods();
402
403     for (int i = 0; i < methods.length; i++) {
404       JClass []param = methods[i].getParameterTypes();
405       String JavaDoc methodName = methods[i].getName();
406
407       if (name.equals(methodName) && param.length == 1)
408         return methods[i];
409     }
410
411     cl = cl.getSuperClass();
412
413     if (cl != null)
414       return getSetter(cl, name);
415     else
416       return null;
417   }
418
419   /**
420    * Returns the load mask generated on create.
421    */

422   public long getCreateLoadMask(int group)
423   {
424     long mask = 0;
425
426     for (int i = 0; i < _fields.size(); i++) {
427       mask |= _fields.get(i).getCreateLoadMask(group);
428     }
429
430     return mask;
431   }
432
433   /**
434    * Printable version of the entity.
435    */

436   public String JavaDoc toString()
437   {
438     return "AbstractStatefulType[" + _beanClass.getName() + "]";
439   }
440 }
441
Popular Tags