1 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 ; 47 import javax.ejb.EJBObject ; 48 49 52 public class CmpField extends CmpProperty { 53 private static final L10N L = new L10N(CmpProperty.class); 54 55 private String _sqlColumn; 56 private String _abstractSQLType; 57 private String _sqlType; 58 private boolean _isAutoGenerate = true; 59 60 private JClass _javaType = JClass.STRING; 61 62 67 public CmpField(EjbEntityBean entity) 68 { 69 super(entity); 70 } 71 72 75 public String getSQLColumn() 76 { 77 return _sqlColumn; 78 } 79 80 83 public void setSQLColumn(String sqlColumn) 84 { 85 _sqlColumn = sqlColumn; 86 } 87 88 91 public String getSQLType() 92 { 93 return _sqlType; 94 } 95 96 99 public void setSQLType(String sqlType) 100 { 101 _sqlType = sqlType; 102 } 103 104 107 public String getAbstractSQLType() 108 { 109 return _abstractSQLType; 110 } 111 112 115 public void setAbstractSQLType(String sqlType) 116 { 117 _abstractSQLType = sqlType; 118 } 119 120 123 public void setJavaType(JClass javaType) 124 { 125 if (javaType.getName().equals("java.util.Map")) 126 Thread.dumpStack(); 127 128 _javaType = javaType; 130 } 131 132 135 public JClass getJavaType() 136 { 137 return _javaType; 138 } 139 140 143 public void setAutoGenerate(boolean isAutoGenerate) 144 { 145 _isAutoGenerate = isAutoGenerate; 146 } 147 148 151 public boolean isAutoGenerate() 152 { 153 return _isAutoGenerate; 154 } 155 156 159 @PostConstruct 160 public void init() 161 throws ConfigException 162 { 163 if (getEntity().isCMP1()) 164 return; 165 166 String name = getName(); 167 168 String 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 .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 .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 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 254 public IdField createId(AmberPersistenceUnit amberPersistenceUnit, EntityType type) 255 throws ConfigException 256 { 257 String fieldName = getName(); 258 String 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 (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 name = type.getTable().getName() + "_cseq"; 291 292 type.setGenerator(idField.getName(), amberPersistenceUnit.createSequenceGenerator(name, 10)); 293 } 294 else { 295 } 297 } 298 299 return idField; 300 } 301 302 static String toSqlName(String 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 toString() 328 { 329 return "CmpField[" + getName() + "]"; 330 } 331 } 332 | Popular Tags |