KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > cfg > CmpField


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 Scott Ferguson
28  */

29
30 package com.caucho.ejb.cfg;
31
32 import com.caucho.amber.field.IdField;
33 import com.caucho.amber.field.KeyPropertyField;
34 import com.caucho.amber.manager.AmberPersistenceUnit;
35 import com.caucho.amber.table.Column;
36 import com.caucho.amber.type.EntityType;
37 import com.caucho.amber.type.Type;
38 import com.caucho.bytecode.JClass;
39 import com.caucho.bytecode.JMethod;
40 import com.caucho.config.ConfigException;
41 import com.caucho.jdbc.JdbcMetaData;
42 import com.caucho.util.CharBuffer;
43 import com.caucho.util.L10N;
44
45 import javax.annotation.PostConstruct;
46 import javax.ejb.EJBLocalObject JavaDoc;
47 import javax.ejb.EJBObject JavaDoc;
48
49 /**
50  * Configuraton for a cmp-field.
51  */

52 public class CmpField extends CmpProperty {
53   private static final L10N L = new L10N(CmpProperty.class);
54
55   private String JavaDoc _sqlColumn;
56   private String JavaDoc _abstractSQLType;
57   private String JavaDoc _sqlType;
58   private boolean _isAutoGenerate = true;
59
60   private JClass _javaType = JClass.STRING;
61
62   /**
63    * Creates a new cmp-field
64    *
65    * @param entity the owning entity bean
66    */

67   public CmpField(EjbEntityBean entity)
68   {
69     super(entity);
70   }
71
72   /**
73    * Returns the SQL column name.
74    */

75   public String JavaDoc getSQLColumn()
76   {
77     return _sqlColumn;
78   }
79
80   /**
81    * Sets the SQL column name.
82    */

83   public void setSQLColumn(String JavaDoc sqlColumn)
84   {
85     _sqlColumn = sqlColumn;
86   }
87
88   /**
89    * Returns the SQL type name.
90    */

91   public String JavaDoc getSQLType()
92   {
93     return _sqlType;
94   }
95
96   /**
97    * Sets the SQL type name.
98    */

99   public void setSQLType(String JavaDoc sqlType)
100   {
101     _sqlType = sqlType;
102   }
103
104   /**
105    * Returns the abstract SQL type name.
106    */

107   public String JavaDoc getAbstractSQLType()
108   {
109     return _abstractSQLType;
110   }
111
112   /**
113    * Sets the abstract SQL type name.
114    */

115   public void setAbstractSQLType(String JavaDoc sqlType)
116   {
117     _abstractSQLType = sqlType;
118   }
119
120   /**
121    * Sets the Java type.
122    */

123   public void setJavaType(JClass javaType)
124   {
125     if (javaType.getName().equals("java.util.Map"))
126       Thread.dumpStack();
127     
128     //_javaType = new JClassWrapper(javaType);
129
_javaType = javaType;
130   }
131
132   /**
133    * Returns the Java type.
134    */

135   public JClass getJavaType()
136   {
137     return _javaType;
138   }
139
140   /**
141    * Set true for auto-generation.
142    */

143   public void setAutoGenerate(boolean isAutoGenerate)
144   {
145     _isAutoGenerate = isAutoGenerate;
146   }
147
148   /**
149    * true for auto-generation.
150    */

151   public boolean isAutoGenerate()
152   {
153     return _isAutoGenerate;
154   }
155
156   /**
157    * Initialize the field.
158    */

159   @PostConstruct
160   public void init()
161     throws ConfigException
162   {
163     if (getEntity().isCMP1())
164       return;
165     
166     String JavaDoc name = getName();
167
168     String JavaDoc getterName = ("get" +
169              Character.toUpperCase(name.charAt(0)) +
170              name.substring(1));
171     
172     JMethod getter = getEntity().getMethod(getEntity().getEJBClassWrapper(),
173                       getterName,
174                        new JClass[0]);
175
176     if (getter == null)
177       throw new ConfigException(L.l("{0}: '{1}' is an unknown cmp-field. cmp-fields must have matching getter methods.",
178                     getEntity().getEJBClass().getName(),
179                     name));
180     else if (! getter.isPublic()) {
181       throw new ConfigException(L.l("{0}: '{1}' must be public. cmp-fields getters must be public.",
182                     getEntity().getEJBClass().getName(),
183                     getter.getFullName()));
184     }
185     else if (! getter.isAbstract() && ! getEntity().isAllowPOJO()) {
186       throw new ConfigException(L.l("{0}: '{1}' must be abstract. cmp-fields getters must be abstract.",
187                     getEntity().getEJBClass().getName(),
188                     getter.getFullName()));
189     }
190     else if (getter.getExceptionTypes().length != 0) {
191       throw new ConfigException(L.l("{0}: '{1}' must not throw {2}. Container managed fields and relations must not throw exceptions.",
192                     getEntity().getEJBClass().getName(),
193                     getter.getFullName(),
194                     getter.getExceptionTypes()[0].getName()));
195     }
196
197     _javaType = getter.getReturnType();
198
199     if ("void".equals(_javaType.getName())) {
200       throw new ConfigException(L.l("{0}: '{1}' must not return void. CMP fields must not return void.",
201                     getEntity().getEJBClass().getName(),
202                     getName()));
203     }
204     else if (_javaType.isAssignableTo(EJBLocalObject JavaDoc.class)) {
205       throw new ConfigException(L.l("{0}: '{1}' must not return an EJB interface. CMP fields must return concrete values.",
206                     getEntity().getEJBClass().getName(),
207                     getName()));
208     }
209     else if (_javaType.isAssignableTo(EJBObject JavaDoc.class)) {
210       throw new ConfigException(L.l("{0}: '{1}' must not return an EJB interface. CMP fields must return concrete values.",
211                     getEntity().getEJBClass().getName(),
212                     getName()));
213     }
214     
215     String JavaDoc setterName = ("set" +
216              Character.toUpperCase(name.charAt(0)) +
217              name.substring(1));
218     
219     JMethod setter = getEntity().getMethod(getEntity().getEJBClassWrapper(),
220                        setterName,
221                        new JClass[] { getter.getReturnType() });
222
223     if (setter == null) {
224     }
225     else if (! setter.isPublic()) {
226       throw new ConfigException(L.l("{0}: '{1}' must be public. cmp-fields setters must be public.",
227                     getEntity().getEJBClass().getName(),
228                     setter.getFullName()));
229     }
230     else if (! "void".equals(setter.getReturnType().getName())) {
231       throw new ConfigException(L.l("{0}: '{1}' must return void. cmp-fields setters must return void.",
232                     getEntity().getEJBClass().getName(),
233                     setter.getFullName()));
234     }
235     else if (! setter.isAbstract() && ! getEntity().isAllowPOJO()) {
236       throw new ConfigException(L.l("{0}: '{1}' must be abstract. cmp-fields setters must be abstract.",
237                     getEntity().getEJBClass().getName(),
238                     setter.getFullName()));
239     }
240     else if (setter.getExceptionTypes().length != 0) {
241       throw new ConfigException(L.l("{0}: '{1}' must not throw {2}. Container managed fields and relations must not throw exceptions.",
242                     getEntity().getEJBClass().getName(),
243                     setter.getFullName(),
244                     setter.getExceptionTypes()[0].getName()));
245     }
246
247     if (_sqlColumn == null)
248       _sqlColumn = toSqlName(getName());
249   }
250
251   /**
252    * Amber creating the id field.
253    */

254   public IdField createId(AmberPersistenceUnit amberPersistenceUnit, EntityType type)
255     throws ConfigException
256   {
257     String JavaDoc fieldName = getName();
258     String JavaDoc sqlName = getSQLColumn();
259
260     if (sqlName == null)
261       sqlName = toSqlName(fieldName);
262       
263     JClass dataType = getJavaType();
264
265     if (dataType == null)
266       throw new NullPointerException JavaDoc(L.l("'{0}' is an unknown field",
267                      fieldName));
268
269     Type amberType = amberPersistenceUnit.createType(dataType);
270     Column column = type.getTable().createColumn(sqlName, amberType);
271
272     KeyPropertyField idField = new KeyPropertyField(type, fieldName, column);
273
274     if (! isAutoGenerate()) {
275     }
276     else if ("int".equals(dataType.getName()) ||
277     "long".equals(dataType.getName()) ||
278     "java.lang.Integer".equals(dataType.getName()) ||
279     "java.lang.Long".equals(dataType.getName())) {
280       JdbcMetaData metaData = amberPersistenceUnit.getMetaData();
281
282       if (metaData.supportsIdentity()) {
283     idField.setGenerator("identity");
284     column.setGeneratorType("identity");
285       }
286       else if (metaData.supportsSequences()) {
287     idField.setGenerator("sequence");
288     column.setGeneratorType("sequence");
289
290     String JavaDoc name = type.getTable().getName() + "_cseq";
291
292     type.setGenerator(idField.getName(), amberPersistenceUnit.createSequenceGenerator(name, 10));
293       }
294       else {
295     // XXX: should try table
296
}
297     }
298
299     return idField;
300   }
301
302   static String JavaDoc toSqlName(String JavaDoc name)
303   {
304     CharBuffer cb = new CharBuffer();
305
306     for (int i = 0; i < name.length(); i++) {
307       char ch = name.charAt(i);
308
309       if (! Character.isUpperCase(ch))
310     cb.append(ch);
311       else if (i > 0 && ! Character.isUpperCase(name.charAt(i - 1))) {
312     cb.append("_");
313     cb.append(Character.toLowerCase(ch));
314       }
315       else if (i + 1 < name.length() &&
316            ! Character.isUpperCase(name.charAt(i + 1))) {
317     cb.append("_");
318     cb.append(Character.toLowerCase(ch));
319       }
320       else
321     cb.append(Character.toLowerCase(ch));
322     }
323
324     return cb.toString();
325   }
326
327   public String JavaDoc toString()
328   {
329     return "CmpField[" + getName() + "]";
330   }
331 }
332
Popular Tags