1 28 29 package com.caucho.amber.field; 30 31 import com.caucho.amber.expr.AmberExpr; 32 import com.caucho.amber.expr.KeyColumnExpr; 33 import com.caucho.amber.expr.PathExpr; 34 import com.caucho.amber.query.QueryParser; 35 import com.caucho.amber.table.Column; 36 import com.caucho.amber.type.RelatedType; 37 import com.caucho.amber.type.Type; 38 import com.caucho.bytecode.JClassWrapper; 39 import com.caucho.config.ConfigException; 40 import com.caucho.java.JavaWriter; 41 import com.caucho.log.Log; 42 import com.caucho.util.L10N; 43 44 import java.io.IOException ; 45 import java.lang.reflect.Method ; 46 import java.util.ArrayList ; 47 import java.util.HashSet ; 48 import java.util.logging.Logger ; 49 50 53 public class KeyPropertyField extends PropertyField implements IdField { 54 private static final L10N L = new L10N(KeyPropertyField.class); 55 protected static final Logger log = Log.open(KeyPropertyField.class); 56 57 private Column _column; 58 private boolean _isKeyField; 59 private String _generator; 60 61 public KeyPropertyField(RelatedType tableType) 62 { 63 super(tableType); 64 } 65 66 public KeyPropertyField(RelatedType tableType, 67 String name) 68 throws ConfigException 69 { 70 super(tableType, name); 71 } 72 73 public KeyPropertyField(RelatedType entityType, 74 String name, 75 Column column) 76 throws ConfigException 77 { 78 super(entityType, name); 79 80 _column = column; 81 _column.setPrimaryKey(true); 82 } 83 84 87 public boolean isKeyField() 88 { 89 return _isKeyField; 90 } 91 92 95 public void setKeyField(boolean isKeyField) 96 { 97 _isKeyField = isKeyField; 98 } 99 100 103 public void setGenerator(String generator) 104 { 105 _generator = generator; 106 107 if (_column != null) 108 _column.setGeneratorType(generator); 109 } 110 111 114 public String getGenerator() 115 { 116 return _generator; 117 } 118 119 122 public boolean isAutoGenerate() 123 { 124 return _generator != null; 125 } 126 127 130 public void setColumn(Column column) 131 { 132 _column = column; 133 _column.setPrimaryKey(true); 134 } 135 136 139 public Column getColumn() 140 { 141 return _column; 142 } 143 144 147 public ArrayList <Column> getColumns() 148 { 149 ArrayList <Column> columns = new ArrayList <Column>(); 150 151 columns.add(_column); 152 153 return columns; 154 } 155 156 159 public int getComponentCount() 160 { 161 return 1; 162 } 163 164 167 public Type getType() 168 { 169 return _column.getType(); 170 } 171 172 175 public String getForeignTypeName() 176 { 177 return _column.getType().getForeignTypeName(); 178 } 179 180 183 public void generatePrologue(JavaWriter out, HashSet <Object > completedSet) 184 throws IOException 185 { 186 super.generatePrologue(out, completedSet); 187 188 if (isAbstract()) { 189 out.println(); 190 191 out.println(); 192 out.println("public " + getJavaTypeName() + " " + getGetterName() + "()"); 193 out.println("{"); 194 out.println(" return " + getFieldName() + ";"); 195 out.println("}"); 196 197 out.println(); 198 out.println("public void " + getSetterName() + "(" + getJavaTypeName() + " v)"); 199 out.println("{"); 200 out.println(" " + getFieldName() + " = v;"); 201 out.println("}"); 202 } 203 } 204 205 208 public String generateSuperGetter() 209 { 210 if (isAbstract() || getGetterMethod() == null) 211 return getFieldName(); 212 else 213 return getGetterMethod().getName() + "()"; 214 } 215 216 219 public String generateSuperSetter(String value) 220 { 221 if (isAbstract() || getGetterMethod() == null || getSetterMethod() == null) 222 return(getFieldName() + " = " + value + ";"); 223 else 224 return getSetterMethod().getName() + "(" + value + ")"; 225 } 226 227 230 public String generateSelect(String id) 231 { 232 return _column.generateSelect(id); 233 } 234 235 238 public String generateJavaSelect(String id) 239 { 240 String select = getName(); 241 242 if (id != null) 243 select = id + "." + select; 244 245 return select; 246 } 247 248 251 public String generateMatchArgWhere(String id) 252 { 253 return _column.generateMatchArgWhere(id); 254 } 255 256 259 public String generateRawWhere(String id) 260 { 261 return id + "." + getName() + "=?"; 262 } 263 264 267 public int generateLoadForeign(JavaWriter out, String rs, 268 String indexVar, int index) 269 throws IOException 270 { 271 return generateLoadForeign(out, rs, indexVar, index, 272 getForeignTypeName().replace('.', '_')); 273 } 274 275 278 public void generateLoadFromObject(JavaWriter out, String obj) 279 throws IOException 280 { 281 out.println(generateSuperSetter(generateGet(obj)) + ";"); 282 } 283 284 287 public String generateSetNull(String obj) 288 throws IOException 289 { 290 return generateSet(obj, getColumn().getType().generateNull()); 291 } 292 293 296 public String generateIsNull(String value) 297 { 298 return "(" + getType().generateIsNull(generateSuperGetter()) + ")"; 299 } 300 301 304 public int generateLoadForeign(JavaWriter out, String rs, 305 String indexVar, int index, 306 String name) 307 throws IOException 308 { 309 return _column.getType().generateLoadForeign(out, rs, indexVar, index); 311 } 312 313 316 public void generateSet(JavaWriter out, String pstmt, 317 String index, String value) 318 throws IOException 319 { 320 if (value == null) 321 _column.getType().generateSetNull(out, pstmt, index); 322 else 323 _column.getType().generateSet(out, pstmt, index, 324 generateGet(value)); 325 } 326 327 330 public void generateMatch(JavaWriter out, String key) 331 throws IOException 332 { 333 out.println("return " + generateEquals("super", key) + ";"); 334 } 335 336 339 public String generateEquals(String left, String right) 340 { 341 return _column.getType().generateEquals(left, right); 342 } 343 344 347 public void generateSetInsert(JavaWriter out, String pstmt, String index) 348 throws IOException 349 { 350 String value = generateSuperGetter(); 351 352 if (isAutoGenerate()) { 353 out.println("if (" + getType().generateIsNull(value) + ") {"); 354 out.pushDepth(); 355 356 getType().generateSetNull(out, pstmt, index); 357 358 out.popDepth(); 359 out.println("} else {"); 360 out.pushDepth(); 361 362 generateSet(out, pstmt, index); 363 364 out.popDepth(); 365 out.println("}"); 366 } 367 else 368 generateSet(out, pstmt, index); 369 } 370 371 374 public void generateCheckCreateKey(JavaWriter out) 375 throws IOException 376 { 377 381 382 if ("identity".equals(_generator)) 383 return; 384 else if (_generator != null) { 385 if (getEntitySourceType().getGenerator(getName()) == null) 386 throw new IllegalStateException ("no sequence generator for " + getName()); 387 388 out.println("if (" + getType().generateIsNull(generateSuperGetter()) + ") {"); 389 out.pushDepth(); 390 391 String id = "home.nextGeneratorId(aConn, \"" + getName() + "\")"; 392 393 String javaType = getType().getJavaTypeName(); 394 395 if ("long".equals(javaType)) 396 id = "(" + javaType + ") " + id; 397 else if ("int".equals(javaType)) 398 id = "(" + javaType + ") " + id; 399 else if ("short".equals(javaType)) 400 id = "(" + javaType + ") " + id; 401 else if ("java.lang.Long".equals(javaType)) 402 id = "new Long(" + id + ")"; 403 else if ("java.lang.Integer".equals(javaType)) 404 id = "new Integer((int) " + id + ")"; 405 else if ("java.lang.Short".equals(javaType)) 406 id = "new Short((short) " + id + ")"; 407 else if ("java.lang.Byte".equals(javaType)) 408 id = "new Byte((byte) " + id + ")"; 409 else 410 throw new UnsupportedOperationException (L.l("{0} is an unsupported generated key type.", 411 javaType)); 412 413 out.println(generateSuperSetter(id) + ";"); 414 415 out.popDepth(); 416 out.println("}"); 417 418 return; 419 } 420 421 if (! getJavaType().isPrimitive()) { 422 out.println("if (" + getType().generateIsNull(generateSuperGetter()) + ")"); 423 out.println(" throw new com.caucho.amber.AmberException(\"primary key must not be null on creation. " + getGetterName() + "() must not return null.\");"); 424 } 425 } 426 427 430 public void generateSetGeneratedKeys(JavaWriter out, String pstmt) 431 throws IOException 432 { 433 if (! "identity".equals(_generator)) 434 return; 435 436 out.print("if ("); 437 out.print(getType().generateIsNull(generateSuperGetter())); 438 out.println(") {"); 439 out.pushDepth(); 440 441 String var = "__caucho_rs_" + out.generateId(); 442 443 out.println("java.sql.ResultSet " + var + " = " + pstmt + ".getGeneratedKeys();"); 444 out.println("if (" + var + ".next()) {"); 445 out.pushDepth(); 446 447 out.print(getType().getName() + " v1 = "); 448 getType().generateLoad(out, var, "", 1); 449 out.println(";"); 450 451 out.println(generateSuperSetter("v1") + ";"); 452 453 out.println("if (__caucho_log.isLoggable(java.util.logging.Level.FINER))"); 454 out.println(" __caucho_log.finer(\"create with new primaryKey \" + " + generateSuperGetter() + ");"); 455 456 out.popDepth(); 457 out.println("}"); 458 out.println("else throw new java.sql.SQLException();"); 459 460 out.popDepth(); 461 out.println("}"); 462 } 463 464 467 public String generateSetKeyProperty(String key, String value) 468 throws IOException 469 { 470 if (_isKeyField) 471 return key + "." + getName() + " = " + value; 472 else 473 return generateSet(key, value); 474 } 475 476 479 public String generateGetKeyProperty(String key) 480 { 481 if (_isKeyField) 482 return key + "." + getName(); 483 else 484 return generateGet(key); 485 } 486 487 492 public String generateGetProxyProperty(String value) 493 { 494 Class proxyClass = ((JClassWrapper) getEntitySourceType().getProxyClass()).getWrappedClass(); 496 497 try { 498 Method method = proxyClass.getMethod(getGetterName(), new Class [0]); 499 500 if (method != null) 501 return generateGet(value); 502 } catch (Throwable e) { 503 } 504 505 Id id = getEntitySourceType().getId(); 506 507 return generateGetKeyProperty("((" + id.getForeignTypeName() + ") " + value + ".getPrimaryKey())"); 508 } 509 510 513 public AmberExpr createExpr(QueryParser parser, PathExpr parent) 514 { 515 return new KeyColumnExpr(parent, getColumn()); 516 } 517 518 521 public String toObject(String value) 522 { 523 return getColumn().getType().toObject(value); 524 } 525 526 529 public String toValue(String value) 530 { 531 return getColumn().getType().generateCastFromObject(value); 532 } 533 } 534 | Popular Tags |