1 29 30 package com.caucho.amber.field; 31 32 import com.caucho.amber.manager.AmberPersistenceUnit; 33 import com.caucho.amber.table.Column; 34 import com.caucho.amber.type.AbstractStatefulType; 35 import com.caucho.amber.type.EmbeddableType; 36 import com.caucho.amber.type.PrimitiveType; 37 import com.caucho.amber.type.RelatedType; 38 import com.caucho.amber.type.Type; 39 import com.caucho.bytecode.JClass; 40 import com.caucho.bytecode.JMethod; 41 import com.caucho.java.JavaWriter; 42 import com.caucho.log.Log; 43 import com.caucho.util.CharBuffer; 44 import com.caucho.util.L10N; 45 46 import java.io.IOException ; 47 import java.util.ArrayList ; 48 import java.util.HashSet ; 49 import java.util.logging.Logger ; 50 51 52 55 public class CompositeId extends Id { 56 private static final L10N L = new L10N(CompositeId.class); 57 protected static final Logger log = Log.open(CompositeId.class); 58 59 private JClass _keyClass; 60 61 public CompositeId(RelatedType ownerType, 62 ArrayList <IdField> keys) 63 { 64 super(ownerType, keys); 65 } 66 67 public CompositeId(RelatedType ownerType, 68 EmbeddedIdField embeddedId) 69 { 70 super(ownerType, embeddedId); 71 } 72 73 76 public void setKeyClass(JClass keyClass) 77 { 78 _keyClass = keyClass; 79 80 getOwnerType().addDependency(keyClass); 81 } 82 83 86 public String getForeignTypeName() 87 { 88 if (_keyClass != null) 89 return _keyClass.getName(); 90 else if (isEmbeddedId()) 91 return getEmbeddedIdField().getJavaTypeName(); 92 else 93 return getOwnerType().getName(); 94 } 95 96 99 public String getForeignMakeKeyName() 100 { 101 return getOwnerType().getName().replace('.', '_').replace('/', '_'); 102 } 103 104 107 public void generatePrologue(JavaWriter out, HashSet <Object > completedSet) 108 throws IOException 109 { 110 super.generatePrologue(out, completedSet); 111 112 generatePrologue(out, completedSet, getForeignMakeKeyName()); 113 } 114 115 118 public void generatePrologue(JavaWriter out, 119 HashSet <Object > completedSet, 120 String name) 121 throws IOException 122 { 123 generatePrologueMake(out, completedSet); 124 generatePrologueLoad(out, completedSet); 125 } 126 127 130 public void generatePrologueMake(JavaWriter out, 131 HashSet <Object > completedSet) 132 throws IOException 133 { 134 String makeName = "__caucho_make_key_" + getForeignMakeKeyName(); 135 136 if (completedSet.contains(makeName)) 137 return; 138 139 completedSet.add(makeName); 140 141 out.println(); 142 out.print("private static "); 143 out.print(getForeignTypeName() + " " + makeName); 144 out.print("("); 145 146 if (! isEmbeddedId()) { 147 ArrayList <IdField> keys = getKeys(); 148 for (int i = 0; i < keys.size(); i++) { 149 if (i != 0) 150 out.print(", "); 151 152 IdField key = keys.get(i); 153 154 out.print(key.getJavaTypeName() + " a" + i); 155 } 156 } 157 else { 158 EmbeddableType embeddable = (EmbeddableType) getEmbeddedIdField().getType(); 159 160 ArrayList <AmberField> fields = embeddable.getFields(); 161 for (int i = 0; i < fields.size(); i++) { 162 if (i != 0) 163 out.print(", "); 164 165 AmberField field = fields.get(i); 166 167 out.print(field.getJavaTypeName() + " a" + i); 168 } 169 } 170 171 out.println(")"); 172 out.println("{"); 173 out.pushDepth(); 174 175 out.println(); 176 out.println(getForeignTypeName() + " key = new " + getForeignTypeName() + "();"); 177 178 if (! isEmbeddedId()) { 179 String args = ""; 180 181 ArrayList <IdField> keys = getKeys(); 182 for (int i = 0; i < keys.size(); i++) { 183 KeyPropertyField key = (KeyPropertyField) keys.get(i); 184 185 String name = key.getName(); 186 187 char ch = Character.toUpperCase(name.charAt(0)); 188 if (name.length() == 1) 189 name = "get" + ch; 190 else 191 name = "get" + ch + key.getName().substring(1); 192 193 JMethod method = AbstractStatefulType.getGetter(_keyClass, name); 194 195 if (key.isKeyField() || (method != null)) { 196 out.println(key.generateSetKeyProperty("key", "a" + i) + ";"); 197 } 198 else { 199 if (i != 0) 201 args += ", "; 202 203 args += " a" + i; 204 205 out.println("if (a" + i + " == null)"); 206 out.println(" return new " + getForeignTypeName() + "();"); 207 208 if (i + 1 == keys.size()) 209 out.print("key = new " + getForeignTypeName() + "(" + args + ");"); 210 } 211 } 212 213 } 214 else { 215 EmbeddableType embeddable = (EmbeddableType) getEmbeddedIdField().getType(); 216 217 ArrayList <AmberField> fields = embeddable.getFields(); 218 for (int i = 0; i < fields.size(); i++) { 219 AmberField field = fields.get(i); 220 221 if (getOwnerType().isFieldAccess()) 222 out.println(field.generateSet("key", "a" + i) + ";"); 223 else { 224 String setter = field.getName(); 225 226 setter = "set" + Character.toUpperCase(setter.charAt(0)) + 227 (setter.length() == 1 ? "" : setter.substring(1)); 228 229 out.println("key." + setter + "(a" + i + ");"); 230 } 231 } 232 } 233 234 out.println("return key;"); 235 236 out.popDepth(); 237 out.println("}"); 238 } 239 240 243 public void generatePrologueLoad(JavaWriter out, 244 HashSet <Object > completedSet) 245 throws IOException 246 { 247 String loadName = "__caucho_load_key_" + getForeignMakeKeyName(); 248 249 if (completedSet.contains(loadName)) 250 return; 251 252 completedSet.add(loadName); 253 254 out.println(); 255 out.print("private static "); 256 out.print(getForeignTypeName() + " " + loadName); 257 out.println("(com.caucho.amber.manager.AmberConnection aConn, java.sql.ResultSet rs, int index)"); 258 out.println(" throws java.sql.SQLException"); 259 260 out.println("{"); 261 out.pushDepth(); 262 263 int index = 0; 264 ArrayList <IdField> keys = getKeys(); 265 for (int i = 0; i < keys.size(); i++) { 266 IdField key = keys.get(i); 267 268 String javaType = key.getJavaTypeName(); 269 out.print(javaType + " a" + i + " = (" + javaType + ") "); 270 index = key.getType().generateLoad(out, "rs", "index", index); 271 out.println(";"); 272 273 out.println("if (rs.wasNull())"); 274 out.println(" return null;"); 275 } 276 277 out.print(getForeignTypeName() + " key = new " + getForeignTypeName() + "("); 278 279 if (isEmbeddedId()) { 280 out.println(");"); 281 282 for (int i = 0; i < keys.size(); i++) { 283 out.println(keys.get(i).generateSetKeyProperty("key", "a" + i) + ";"); 284 } 285 } 286 else { 287 for (int i = 0; i < keys.size(); i++) { 288 KeyPropertyField key = (KeyPropertyField) keys.get(i); 289 290 String name = key.getName(); 291 292 char ch = Character.toUpperCase(name.charAt(0)); 293 if (name.length() == 1) 294 name = "get" + ch; 295 else 296 name = "get" + ch + key.getName().substring(1); 297 298 JMethod method = AbstractStatefulType.getGetter(_keyClass, name); 299 300 if (key.isKeyField() || (method != null)) { 301 if (i == 0) 302 out.println(");"); 303 304 out.println(key.generateSetKeyProperty("key", "a" + i) + ";"); 305 } 306 else { 307 if (i != 0) 309 out.print(", "); 310 311 out.print(" a" + i); 312 313 if (i + 1 == keys.size()) 314 out.println(");"); 315 } 316 } 317 } 318 319 out.println("return key;"); 320 321 out.popDepth(); 322 out.println("}"); 323 } 324 325 328 public int generateLoadForeign(JavaWriter out, String rs, 329 String indexVar, int index) 330 throws IOException 331 { 332 return generateLoadForeign(out, rs, indexVar, index, 333 getForeignTypeName().replace('.', '_')); 334 } 335 336 339 public int generateLoadForeign(JavaWriter out, String rs, 340 String indexVar, int index, 341 String name) 342 throws IOException 343 { 344 out.print("__caucho_load_key_" + getForeignMakeKeyName()); 345 out.print("(aConn, " + rs + ", " + indexVar + " + " + index + ")"); 346 347 ArrayList <IdField> keys = getKeys(); 348 349 index += keys.size(); 350 351 return index; 352 } 353 354 357 public String generateSelect(String id) 358 { 359 ArrayList <IdField> keys = getKeys(); 360 361 CharBuffer cb = CharBuffer.allocate(); 362 363 for (int i = 0; i < keys.size(); i++) { 364 if (i != 0) 365 cb.append(", "); 366 367 cb.append(keys.get(i).generateSelect(id)); 368 } 369 370 return cb.close(); 371 } 372 373 376 public String generateJavaSelect(String id) 377 { 378 ArrayList <IdField> keys = getKeys(); 379 380 CharBuffer cb = CharBuffer.allocate(); 381 382 for (int i = 0; i < keys.size(); i++) { 383 if (i != 0) 384 cb.append(", "); 385 386 cb.append(keys.get(i).generateJavaSelect(id)); 387 } 388 389 return cb.close(); 390 } 391 392 395 public String generateLoadSelect(String id) 396 { 397 return null; 398 } 399 400 403 public String generateGetProperty(String value) 404 { 405 CharBuffer cb = CharBuffer.allocate(); 406 407 cb.append("__caucho_make_key_" + getForeignMakeKeyName()); 408 cb.append("("); 409 410 ArrayList <IdField> keys = getKeys(); 411 412 if (! isEmbeddedId()) { 413 for (int i = 0; i < keys.size(); i++) { 414 if (i != 0) 415 cb.append(", "); 416 417 cb.append(keys.get(i).generateGet(value)); 418 } 419 } 420 else { 421 getEmbeddedIdField().generateGetPrimaryKey(cb); 422 } 423 424 cb.append(")"); 425 426 return cb.close(); 427 } 428 429 432 public String generateGetProxyProperty(String value) 433 { 434 CharBuffer cb = CharBuffer.allocate(); 435 436 cb.append("__caucho_make_key_" + getForeignMakeKeyName()); 437 cb.append("("); 438 439 ArrayList <IdField> keys = getKeys(); 440 441 for (int i = 0; i < keys.size(); i++) { 442 if (i != 0) 443 cb.append(", "); 444 445 cb.append(keys.get(i).generateGetProxyProperty(value)); 446 } 447 448 cb.append(")"); 449 450 return cb.close(); 451 } 452 453 456 public void generateLoadFromObject(JavaWriter out, String obj) 457 throws IOException 458 { 459 ArrayList <IdField> keys = getKeys(); 460 461 for (int i = 0; i < keys.size(); i++) { 462 keys.get(i).generateLoadFromObject(out, obj); 463 } 464 } 465 466 469 public void generateSet(JavaWriter out, String obj) 470 throws IOException 471 { 472 out.println("if (" + obj + " != null) {"); 473 out.pushDepth(); 474 475 if (! isEmbeddedId()) { 476 477 479 AmberPersistenceUnit persistenceUnit 480 = getOwnerType().getPersistenceUnit(); 481 482 EmbeddableType embeddable 483 = persistenceUnit.getEmbeddable(_keyClass.getName()); 484 485 ArrayList <AmberField> keys = embeddable.getFields(); 487 488 for (int i = 0; i < keys.size(); i++) { 489 PropertyField key = (PropertyField) keys.get(i); 490 491 String getter = "__caucho_get_field(" + i + ")"; 492 493 String value 494 = "((com.caucho.amber.entity.Embeddable) key)." + getter; 495 496 out.println("Object field" + i + " = " + value + ";"); 497 498 out.println("if (field" + i + " == null)"); 499 out.println(" return;"); 500 501 KeyPropertyField prop = null; 502 503 Column column = key.getColumn(); 504 505 if (column == null) { 506 ArrayList <IdField> fields = getKeys(); 507 for (int j = 0; j < fields.size(); j++) { 508 IdField id = fields.get(j); 509 if (id.getName().equals(key.getName())) 510 if (id instanceof KeyPropertyField) 511 prop = (KeyPropertyField) id; 512 } 513 } 514 515 if (prop != null) 516 key = prop; 517 518 Type columnType = key.getColumn().getType(); 519 520 value = columnType.generateCastFromObject("field" + i); 521 522 key.generateSet(out, value); 523 } 524 525 out.println("__caucho_compound_key = (" + getForeignTypeName() + ") " + obj + ";"); 527 528 535 } 536 else { 537 out.println(getForeignTypeName() + " " + obj + "_key = (" + getForeignTypeName() + ") " + obj + ";"); 538 539 getEmbeddedIdField().generateSet(out, obj+"_key"); 540 } 541 542 out.popDepth(); 543 out.println("}"); 544 } 545 546 549 public void generateUpdateFromObject(JavaWriter out, String obj) 550 throws IOException 551 { 552 ArrayList <IdField> keys = getKeys(); 553 554 for (int i = 0; i < keys.size(); i++) { 555 keys.get(i).generateUpdateFromObject(out, obj); 556 } 557 } 558 559 562 public String generateWhere(String id) 563 { 564 ArrayList <IdField> keys = getKeys(); 565 566 CharBuffer cb = CharBuffer.allocate(); 567 568 for (int i = 0; i < keys.size(); i++) { 569 if (i != 0) 570 cb.append(" and "); 571 572 cb.append(keys.get(i).generateWhere(id)); 573 } 574 575 return cb.close(); 576 } 577 578 581 public String generateCreateTableSQL(AmberPersistenceUnit manager) 582 { 583 return null; 584 } 585 586 589 public void generateSetKey(JavaWriter out, String pstmt, 590 String obj, String index) 591 throws IOException 592 { 593 generateSet(out, pstmt, obj, index); 594 } 595 596 599 public void generateSet(JavaWriter out, String pstmt, 600 String obj, String index) 601 throws IOException 602 { 603 if (! isEmbeddedId()) { 604 605 607 ArrayList keys = getKeys(); 608 609 for (int i = 0; i < keys.size(); i++) { 610 PropertyField key = (PropertyField) keys.get(i); 611 612 String name = key.getName(); 613 614 char ch = Character.toUpperCase(name.charAt(0)); 615 if (name.length() == 1) 616 name = "get" + ch; 617 else 618 name = "get" + ch + key.getName().substring(1); 619 620 JMethod method = AbstractStatefulType.getGetter(_keyClass, name); 621 622 if ((key instanceof KeyPropertyField) && 623 (((KeyPropertyField) key).isKeyField() || (method != null))) { 624 key.generateSet(out, pstmt, obj, index); 625 } 626 else { 627 629 String getter = null; 630 631 AmberPersistenceUnit persistenceUnit 632 = getOwnerType().getPersistenceUnit(); 633 634 EmbeddableType embeddable 635 = persistenceUnit.getEmbeddable(_keyClass.getName()); 636 637 if (embeddable.isIdClass()) { 638 ArrayList <AmberField> fields = embeddable.getFields(); 639 for (int j = 0; j < fields.size(); j++) { 640 AmberField id = fields.get(j); 641 if (id.getName().equals(key.getName())) 642 if (id instanceof PropertyField) 643 getter = "__caucho_get_field(" + j + ")"; 644 } 645 } 646 else 647 getter = key.getGetterMethod().getName() + "()"; 648 649 Column column = key.getColumn(); 650 651 Type columnType = column.getType(); 652 653 String value 654 = "((com.caucho.amber.entity.Embeddable) __caucho_compound_key)." + getter; 655 656 value = columnType.generateCastFromObject(value); 657 658 columnType.generateSet(out, pstmt, obj, value); 659 } 660 } 661 } 662 else { 663 getEmbeddedIdField().generateSet(out, pstmt, obj, index); 664 } 665 } 666 667 670 public void generateSet(JavaWriter out, String pstmt, String index) 671 throws IOException 672 { 673 ArrayList <IdField> keys = getKeys(); 674 675 for (int i = 0; i < keys.size(); i++) { 676 keys.get(i).generateSet(out, pstmt, index); 677 } 678 } 679 680 683 public void generateSetInsert(JavaWriter out, String pstmt, String index) 684 throws IOException 685 { 686 ArrayList <IdField> keys = getKeys(); 687 688 for (int i = 0; i < keys.size(); i++) { 689 keys.get(i).generateSetInsert(out, pstmt, index); 690 } 691 } 692 693 696 public String generateCastFromObject(String value) 697 { 698 return value; 699 } 700 701 704 public void generateMatch(JavaWriter out, String key) 705 throws IOException 706 { 707 out.println("return __caucho_getPrimaryKey().equals(" + key + ");"); 708 } 709 710 713 public String generateEquals(String leftBase, String value) 714 { 715 return leftBase + ".equals(" + value + ")"; 716 } 717 720 public void generateCheckCreateKey(JavaWriter out) 721 throws IOException 722 { 723 } 724 725 728 734 735 738 public String toObject(String value) 739 { 740 return value; 741 } 742 } 743 | Popular Tags |