1 56 57 package org.objectstyle.cayenne.gen; 58 59 import java.io.Writer ; 60 import java.util.ArrayList ; 61 import java.util.Collections ; 62 import java.util.Iterator ; 63 import java.util.List ; 64 65 import org.objectstyle.cayenne.CayenneDataObject; 66 import org.objectstyle.cayenne.CayenneRuntimeException; 67 import org.objectstyle.cayenne.PersistentObject; 68 import org.objectstyle.cayenne.map.DataMap; 69 import org.objectstyle.cayenne.map.ObjEntity; 70 71 import foundrylogic.vpp.VPPConfig; 72 73 82 public abstract class MapClassGenerator { 83 84 public static final String SINGLE_CLASS_TEMPLATE_1_1 = "dotemplates/singleclass.vm"; 85 public static final String SUBCLASS_TEMPLATE_1_1 = "dotemplates/subclass.vm"; 86 public static final String SUPERCLASS_TEMPLATE_1_1 = "dotemplates/superclass.vm"; 87 88 public static final String SINGLE_CLASS_TEMPLATE_1_2 = "dotemplates/v1_2/singleclass.vm"; 89 public static final String SUBCLASS_TEMPLATE_1_2 = "dotemplates/v1_2/subclass.vm"; 90 public static final String SUPERCLASS_TEMPLATE_1_2 = "dotemplates/v1_2/superclass.vm"; 91 92 95 public static final String CLIENT_SUBCLASS_TEMPLATE_1_2 = "dotemplates/v1_2/client-subclass.vm"; 96 97 100 public static final String CLIENT_SUPERCLASS_TEMPLATE_1_2 = "dotemplates/v1_2/client-superclass.vm"; 101 102 public static final String SINGLE_CLASS_TEMPLATE = SINGLE_CLASS_TEMPLATE_1_1; 103 public static final String SUBCLASS_TEMPLATE = SUBCLASS_TEMPLATE_1_1; 104 public static final String SUPERCLASS_TEMPLATE = SUPERCLASS_TEMPLATE_1_1; 105 106 public static final String SUPERCLASS_PREFIX = "_"; 107 108 protected static final String VERSION_1_1 = ClassGenerator.VERSION_1_1; 109 protected static final String VERSION_1_2 = ClassGenerator.VERSION_1_2; 110 111 protected static final String DEFAULT_VERSION = VERSION_1_1; 112 113 protected static final String MODE_DATAMAP = "datamap"; 114 protected static final String MODE_ENTITY = "entity"; 115 116 protected String versionString = DEFAULT_VERSION; 117 118 protected List objEntities; 119 protected String superPkg; 120 protected DataMap dataMap; 121 protected VPPConfig vppConfig; 122 protected String mode = MODE_ENTITY; 123 protected boolean client; 124 125 public MapClassGenerator() {} 126 127 public MapClassGenerator(DataMap dataMap) { 128 this(dataMap, new ArrayList (dataMap.getObjEntities())); 129 } 130 131 public MapClassGenerator(DataMap dataMap, List selectedObjEntities) { 132 this.dataMap = dataMap; 133 this.objEntities = selectedObjEntities; 134 } 135 136 140 public MapClassGenerator(List selectedObjEntities) { 141 this.objEntities = selectedObjEntities; 142 } 143 144 protected String defaultSingleClassTemplate() { 145 if (client) { 147 throw new IllegalStateException ( 148 "Default generation for single classes on the client is not supported..."); 149 } 150 else if (VERSION_1_1.equals(versionString)) { 151 return SINGLE_CLASS_TEMPLATE_1_1; 152 } 153 else if (VERSION_1_2.equals(versionString)) { 154 return SINGLE_CLASS_TEMPLATE_1_2; 155 } 156 return SINGLE_CLASS_TEMPLATE; 157 } 158 159 protected String defaultSubclassTemplate() { 160 if(client) { 162 return CLIENT_SUBCLASS_TEMPLATE_1_2; 163 } 164 else if (VERSION_1_1.equals(versionString)) { 165 return SUBCLASS_TEMPLATE_1_1; 166 } 167 else if (VERSION_1_2.equals(versionString)) { 168 return SUBCLASS_TEMPLATE_1_2; 169 } 170 return SUBCLASS_TEMPLATE; 171 } 172 173 protected String defaultSuperclassTemplate() { 174 if (client) { 176 return CLIENT_SUPERCLASS_TEMPLATE_1_2; 177 } 178 else if (VERSION_1_1.equals(versionString)) { 179 return SUPERCLASS_TEMPLATE_1_1; 180 } 181 else if (VERSION_1_2.equals(versionString)) { 182 return SUPERCLASS_TEMPLATE_1_2; 183 } 184 return SUPERCLASS_TEMPLATE; 185 } 186 187 194 public abstract Writer openWriter( 195 ObjEntity entity, 196 String pkgName, 197 String className) 198 throws Exception ; 199 200 204 public abstract void closeWriter(Writer out) throws Exception ; 205 206 210 public void generateClassPairs() throws Exception { 211 generateClassPairs(defaultSubclassTemplate(), defaultSuperclassTemplate(), SUPERCLASS_PREFIX); 212 } 213 214 222 public void generateClassPairs( 223 String classTemplate, 224 String superTemplate, 225 String superPrefix) 226 throws Exception { 227 228 if(client) { 230 generateClientClassPairs_1_2(classTemplate, superTemplate, superPrefix); 231 } 232 else if (VERSION_1_1.equals(versionString)) { 233 generateClassPairs_1_1(classTemplate, superTemplate, superPrefix); 234 } 235 else if (VERSION_1_2.equals(versionString)) { 236 generateClassPairs_1_2(classTemplate, superTemplate, superPrefix); 237 } 238 else { 239 throw new IllegalStateException ("Illegal Version in generateClassPairs: " + versionString); 240 } 241 } 242 243 251 private void generateClassPairs_1_1( 252 String classTemplate, 253 String superTemplate, 254 String superPrefix) 255 throws Exception { 256 257 ClassGenerator mainGenSetup = new ClassGenerator(classTemplate, versionString); 258 ClassGenerator superGenSetup = new ClassGenerator(superTemplate, versionString); 259 260 ClassGenerationInfo mainGen = mainGenSetup.getClassGenerationInfo(); 261 ClassGenerationInfo superGen = superGenSetup.getClassGenerationInfo(); 262 263 mainGen.setSuperPrefix(superPrefix); 265 superGen.setSuperPrefix(superPrefix); 266 267 Iterator it = objEntities.iterator(); 269 if (MODE_ENTITY.equals(mode)) 270 { 271 it = objEntities.iterator(); 272 } 273 else 274 { 275 if (objEntities.isEmpty()) 276 { 277 it = objEntities.iterator(); 278 } 279 else 280 { 281 it = Collections.singleton(objEntities.get(0)).iterator(); 282 } 283 } 284 285 while (it.hasNext()) { 286 ObjEntity ent = (ObjEntity) it.next(); 287 288 initClassGenerator_1_1(superGen, ent, true); 290 291 Writer superOut = 292 openWriter( 293 ent, 294 superGen.getPackageName(), 295 superPrefix + superGen.getClassName()); 296 297 if (superOut != null) { 298 superGenSetup.generateClass(superOut, ent); 299 closeWriter(superOut); 300 } 301 302 initClassGenerator_1_1(mainGen, ent, false); 304 Writer mainOut = 305 openWriter(ent, mainGen.getPackageName(), mainGen.getClassName()); 306 if (mainOut != null) { 307 mainGenSetup.generateClass(mainOut, ent); 308 closeWriter(mainOut); 309 } 310 } 311 } 312 313 private void generateClientClassPairs_1_2( 314 String classTemplate, 315 String superTemplate, 316 String superPrefix) throws Exception { 317 318 ClassGenerator mainGenSetup = new ClassGenerator( 319 classTemplate, 320 ClassGenerator.VERSION_1_2, 321 vppConfig); 322 ClassGenerator superGenSetup = new ClassGenerator( 323 superTemplate, 324 ClassGenerator.VERSION_1_2, 325 vppConfig); 326 327 Iterator it = objEntities.iterator(); 329 if (MODE_ENTITY.equals(mode)) { 330 it = objEntities.iterator(); 331 } 332 else { 333 if (objEntities.isEmpty()) { 334 it = objEntities.iterator(); 335 } 336 else { 337 it = Collections.singleton(objEntities.get(0)).iterator(); 338 } 339 } 340 341 while (it.hasNext()) { 342 ObjEntity ent = (ObjEntity) it.next(); 343 344 String fqnSubClass = ent.getClientClassName(); 346 if (fqnSubClass == null) { 347 fqnSubClass = ent.getClassName(); 348 } 349 350 String fqnBaseClass = (null != ent.getSuperClassName()) ? ent 354 .getSuperClassName() : PersistentObject.class.getName(); 355 356 StringUtils stringUtils = StringUtils.getInstance(); 357 358 String subClassName = stringUtils.stripPackageName(fqnSubClass); 359 String subPackageName = stringUtils.stripClass(fqnSubClass); 360 361 String superClassName = superPrefix 362 + stringUtils.stripPackageName(fqnSubClass); 363 364 String superPackageName = this.superPkg; 365 String fqnSuperClass = superPackageName + "." + superClassName; 366 367 Writer superOut = openWriter(ent, superPackageName, superClassName); 368 369 if (superOut != null) { 370 superGenSetup.generateClass(superOut, 371 dataMap, 372 ent, 373 fqnBaseClass, 374 fqnSuperClass, 375 fqnSubClass); 376 closeWriter(superOut); 377 } 378 379 Writer mainOut = openWriter(ent, subPackageName, subClassName); 380 if (mainOut != null) { 381 mainGenSetup.generateClass(mainOut, 382 dataMap, 383 ent, 384 fqnBaseClass, 385 fqnSuperClass, 386 fqnSubClass); 387 closeWriter(mainOut); 388 } 389 } 390 } 391 392 399 private void generateClassPairs_1_2( 400 String classTemplate, 401 String superTemplate, 402 String superPrefix) 403 throws Exception { 404 405 ClassGenerator mainGenSetup = new ClassGenerator( 406 classTemplate, 407 versionString, 408 vppConfig); 409 ClassGenerator superGenSetup = new ClassGenerator( 410 superTemplate, 411 versionString, 412 vppConfig); 413 414 Iterator it = objEntities.iterator(); 416 if (MODE_ENTITY.equals(mode)) { 417 it = objEntities.iterator(); 418 } 419 else { 420 if (objEntities.isEmpty()) { 421 it = objEntities.iterator(); 422 } 423 else { 424 it = Collections.singleton(objEntities.get(0)).iterator(); 425 } 426 } 427 428 while (it.hasNext()) { 429 ObjEntity ent = (ObjEntity) it.next(); 430 431 String fqnSubClass = ent.getClassName(); 432 String fqnBaseClass = (null != ent.getSuperClassName()) ? ent 433 .getSuperClassName() : CayenneDataObject.class.getName(); 434 435 StringUtils stringUtils = StringUtils.getInstance(); 436 437 String subClassName = stringUtils.stripPackageName(fqnSubClass); 438 String subPackageName = stringUtils.stripClass(fqnSubClass); 439 440 String superClassName = superPrefix 441 + stringUtils.stripPackageName(fqnSubClass); 442 443 String superPackageName = this.superPkg; 444 String fqnSuperClass = superPackageName + "." + superClassName; 445 446 Writer superOut = openWriter(ent, superPackageName, superClassName); 447 448 if (superOut != null) { 449 superGenSetup.generateClass(superOut, 450 dataMap, 451 ent, 452 fqnBaseClass, 453 fqnSuperClass, 454 fqnSubClass); 455 closeWriter(superOut); 456 } 457 458 Writer mainOut = openWriter(ent, subPackageName, subClassName); 459 if (mainOut != null) { 460 mainGenSetup.generateClass(mainOut, 461 dataMap, 462 ent, 463 fqnBaseClass, 464 fqnSuperClass, 465 fqnSubClass); 466 closeWriter(mainOut); 467 } 468 } 469 } 470 471 475 public void generateSingleClasses() throws Exception { 476 generateSingleClasses(defaultSingleClassTemplate(), SUPERCLASS_PREFIX); 477 } 478 479 483 private void generateSingleClasses_1_1(String classTemplate) throws Exception { 484 ClassGenerator gen = new ClassGenerator(classTemplate, versionString); 485 486 Iterator it = objEntities.iterator(); 488 if (MODE_ENTITY.equals(mode)) 489 { 490 it = objEntities.iterator(); 491 } 492 else 493 { 494 if (objEntities.isEmpty()) 495 { 496 it = objEntities.iterator(); 497 } 498 else 499 { 500 it = Collections.singleton(objEntities.get(0)).iterator(); 501 } 502 } 503 504 while (it.hasNext()) { 505 ObjEntity ent = (ObjEntity) it.next(); 506 507 initClassGenerator_1_1(gen.getClassGenerationInfo(), ent, false); 508 Writer out = openWriter(ent, gen.getClassGenerationInfo().getPackageName(), gen.getClassGenerationInfo().getClassName()); 509 if (out == null) { 510 continue; 511 } 512 513 gen.generateClass(out, ent); 514 closeWriter(out); 515 } 516 } 517 518 522 private void generateSingleClasses_1_2(String classTemplate, String superPrefix) throws Exception { 523 ClassGenerator gen = new ClassGenerator(classTemplate, versionString, vppConfig); 524 525 Iterator it = objEntities.iterator(); 527 if (MODE_ENTITY.equals(mode)) 528 { 529 it = objEntities.iterator(); 530 } 531 else 532 { 533 if (objEntities.isEmpty()) 534 { 535 it = objEntities.iterator(); 536 } 537 else 538 { 539 it = Collections.singleton(objEntities.get(0)).iterator(); 540 } 541 } 542 543 while (it.hasNext()) { 544 ObjEntity ent = (ObjEntity) it.next(); 545 546 String fqnSubClass = ent.getClassName(); 547 String fqnBaseClass = (null != ent.getSuperClassName()) 548 ? ent.getSuperClassName() : CayenneDataObject.class.getName(); 549 550 StringUtils stringUtils = StringUtils.getInstance(); 551 552 String subClassName = stringUtils.stripPackageName(fqnSubClass); 553 String subPackageName = stringUtils.stripClass(fqnSubClass); 554 555 String superClassName = superPrefix + stringUtils.stripPackageName(fqnSubClass); 556 557 String superPackageName = this.superPkg; 558 String fqnSuperClass = superPackageName + "." + superClassName; 559 560 Writer out = openWriter(ent, subPackageName, subClassName); 561 if (out == null) { 562 continue; 563 } 564 565 gen.generateClass(out, dataMap, ent, fqnBaseClass, fqnSuperClass, fqnSubClass); 566 closeWriter(out); 567 } 568 } 569 570 575 public void generateSingleClasses(String classTemplate) throws Exception { 576 generateSingleClasses(classTemplate, SUPERCLASS_PREFIX); 577 } 578 579 583 public void generateSingleClasses(String classTemplate, String superPrefix) throws Exception { 584 if(client) { 586 generateSingleClasses_1_2(classTemplate, superPrefix); 587 } 588 else if (VERSION_1_1.equals(versionString)) { 589 generateSingleClasses_1_1(classTemplate); 590 } 591 else if (VERSION_1_2.equals(versionString)) { 592 generateSingleClasses_1_2(classTemplate, superPrefix); 593 } 594 else { 595 throw new IllegalStateException ("Illegal Version in generateClassPairs: " + versionString); 596 } 597 } 598 599 600 protected void initClassGenerator_1_1( 601 ClassGenerationInfo gen, 602 ObjEntity entity, 603 boolean superclass) { 604 605 String fullClassName = entity.getClassName(); 607 int i = fullClassName.lastIndexOf("."); 608 609 String pkg = null; 610 String spkg = null; 611 String cname = null; 612 613 if (i == 0 || i + 1 == fullClassName.length()) { 615 throw new CayenneRuntimeException("Invalid class mapping: " + fullClassName); 616 } 617 else if (i < 0) { 618 pkg = (superclass) ? superPkg : null; 619 spkg = (superclass) ? null : superPkg; 620 cname = fullClassName; 621 } 622 else { 623 cname = fullClassName.substring(i + 1); 624 pkg = 625 (superclass && superPkg != null) ? superPkg : fullClassName.substring(0, i); 626 627 spkg = 628 (!superclass && superPkg != null && !pkg.equals(superPkg)) ? superPkg : null; 629 } 630 631 gen.setPackageName(pkg); 633 gen.setClassName(cname); 634 if(entity.getSuperClassName()!=null) { 635 gen.setSuperClassName(entity.getSuperClassName()); 636 } else { 637 gen.setSuperClassName(CayenneDataObject.class.getName()); 638 } 639 gen.setSuperPackageName(spkg); 640 } 641 642 647 public String getSuperPkg() { 648 return superPkg; 649 } 650 651 654 public void setSuperPkg(String superPkg) { 655 this.superPkg = superPkg; 656 } 657 658 663 public boolean isClient() { 664 return client; 665 } 666 667 672 public void setClient(boolean client) { 673 this.client = client; 674 } 675 676 679 public DataMap getDataMap() { 680 return dataMap; 681 } 682 683 686 public void setDataMap(DataMap dataMap) { 687 this.dataMap = dataMap; 688 } 689 690 public List getObjEntities() { 691 return objEntities; 692 } 693 694 public void setObjEntities(List objEntities) { 695 this.objEntities = objEntities; 696 } 697 698 701 public String getVersionString() { 702 return versionString; 703 } 704 707 public void setVersionString(String versionString) { 708 if ((false == VERSION_1_1.equals(versionString)) && (false == VERSION_1_2.equals(versionString))) { 709 throw new IllegalStateException ("'version' must be '" + VERSION_1_1 + "' or '" + VERSION_1_2 + "', but was '" + versionString + "'"); 710 } 711 this.versionString = versionString; 712 } 713 714 717 public VPPConfig getVppConfig() { 718 return vppConfig; 719 } 720 723 public void setVppConfig(VPPConfig vppConfig) { 724 this.vppConfig = vppConfig; 725 } 726 727 730 public void setMode(String mode) { 731 if ((false == MODE_ENTITY.equals(mode)) && (false == MODE_DATAMAP.equals(mode))) { 732 throw new IllegalStateException ("'mode' must be '" + MODE_ENTITY + "' or '" + MODE_DATAMAP + "', but was '" + mode + "'"); 733 } 734 this.mode = mode; 735 } 736 } | Popular Tags |