1 19 20 package org.apache.cayenne.gen; 21 22 import java.io.Writer ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import org.apache.cayenne.CayenneDataObject; 29 import org.apache.cayenne.CayenneRuntimeException; 30 import org.apache.cayenne.PersistentObject; 31 import org.apache.cayenne.map.DataMap; 32 import org.apache.cayenne.map.ObjEntity; 33 34 import foundrylogic.vpp.VPPConfig; 35 36 44 public abstract class MapClassGenerator { 45 46 public static final String SINGLE_CLASS_TEMPLATE_1_1 = "dotemplates/singleclass.vm"; 47 public static final String SUBCLASS_TEMPLATE_1_1 = "dotemplates/subclass.vm"; 48 public static final String SUPERCLASS_TEMPLATE_1_1 = "dotemplates/superclass.vm"; 49 50 public static final String SINGLE_CLASS_TEMPLATE_1_2 = "dotemplates/v1_2/singleclass.vm"; 51 public static final String SUBCLASS_TEMPLATE_1_2 = "dotemplates/v1_2/subclass.vm"; 52 public static final String SUPERCLASS_TEMPLATE_1_2 = "dotemplates/v1_2/superclass.vm"; 53 54 57 public static final String CLIENT_SUBCLASS_TEMPLATE_1_2 = "dotemplates/v1_2/client-subclass.vm"; 58 59 62 public static final String CLIENT_SUPERCLASS_TEMPLATE_1_2 = "dotemplates/v1_2/client-superclass.vm"; 63 64 public static final String SINGLE_CLASS_TEMPLATE = SINGLE_CLASS_TEMPLATE_1_1; 65 public static final String SUBCLASS_TEMPLATE = SUBCLASS_TEMPLATE_1_1; 66 public static final String SUPERCLASS_TEMPLATE = SUPERCLASS_TEMPLATE_1_1; 67 68 public static final String SUPERCLASS_PREFIX = "_"; 69 70 protected static final String VERSION_1_1 = ClassGenerator.VERSION_1_1; 71 protected static final String VERSION_1_2 = ClassGenerator.VERSION_1_2; 72 73 protected static final String DEFAULT_VERSION = VERSION_1_1; 74 75 protected static final String MODE_DATAMAP = "datamap"; 76 protected static final String MODE_ENTITY = "entity"; 77 78 protected String versionString = DEFAULT_VERSION; 79 80 protected List objEntities; 81 protected String superPkg; 82 protected DataMap dataMap; 83 protected VPPConfig vppConfig; 84 protected String mode = MODE_ENTITY; 85 protected boolean client; 86 87 public MapClassGenerator() { 88 } 89 90 public MapClassGenerator(DataMap dataMap) { 91 this(dataMap, new ArrayList (dataMap.getObjEntities())); 92 } 93 94 99 public MapClassGenerator(DataMap dataMap, List objEntities) { 100 this.dataMap = dataMap; 101 this.setObjEntities(objEntities); 102 } 103 104 protected String defaultSingleClassTemplate() { 105 if (client) { 107 throw new IllegalStateException ( 108 "Default generation for single classes on the client is not supported..."); 109 } 110 else if (VERSION_1_1.equals(versionString)) { 111 return SINGLE_CLASS_TEMPLATE_1_1; 112 } 113 else if (VERSION_1_2.equals(versionString)) { 114 return SINGLE_CLASS_TEMPLATE_1_2; 115 } 116 return SINGLE_CLASS_TEMPLATE; 117 } 118 119 protected String defaultSubclassTemplate() { 120 if (client) { 122 return CLIENT_SUBCLASS_TEMPLATE_1_2; 123 } 124 else if (VERSION_1_1.equals(versionString)) { 125 return SUBCLASS_TEMPLATE_1_1; 126 } 127 else if (VERSION_1_2.equals(versionString)) { 128 return SUBCLASS_TEMPLATE_1_2; 129 } 130 return SUBCLASS_TEMPLATE; 131 } 132 133 protected String defaultSuperclassTemplate() { 134 if (client) { 136 return CLIENT_SUPERCLASS_TEMPLATE_1_2; 137 } 138 else if (VERSION_1_1.equals(versionString)) { 139 return SUPERCLASS_TEMPLATE_1_1; 140 } 141 else if (VERSION_1_2.equals(versionString)) { 142 return SUPERCLASS_TEMPLATE_1_2; 143 } 144 return SUPERCLASS_TEMPLATE; 145 } 146 147 153 public abstract Writer openWriter(ObjEntity entity, String pkgName, String className) 154 throws Exception ; 155 156 160 public abstract void closeWriter(Writer out) throws Exception ; 161 162 166 public void generateClassPairs() throws Exception { 167 generateClassPairs( 168 defaultSubclassTemplate(), 169 defaultSuperclassTemplate(), 170 SUPERCLASS_PREFIX); 171 } 172 173 180 public void generateClassPairs( 181 String classTemplate, 182 String superTemplate, 183 String superPrefix) throws Exception { 184 185 if (client) { 187 generateClientClassPairs_1_2(classTemplate, superTemplate, superPrefix); 188 } 189 else if (VERSION_1_1.equals(versionString)) { 190 generateClassPairs_1_1(classTemplate, superTemplate, superPrefix); 191 } 192 else if (VERSION_1_2.equals(versionString)) { 193 generateClassPairs_1_2(classTemplate, superTemplate, superPrefix); 194 } 195 else { 196 throw new IllegalStateException ("Illegal Version in generateClassPairs: " 197 + versionString); 198 } 199 } 200 201 208 private void generateClassPairs_1_1( 209 String classTemplate, 210 String superTemplate, 211 String superPrefix) throws Exception { 212 213 ClassGenerator mainGenSetup = new ClassGenerator(classTemplate, versionString); 214 ClassGenerator superGenSetup = new ClassGenerator(superTemplate, versionString); 215 216 ClassGenerationInfo mainGen = mainGenSetup.getClassGenerationInfo(); 217 ClassGenerationInfo superGen = superGenSetup.getClassGenerationInfo(); 218 219 mainGen.setSuperPrefix(superPrefix); 221 superGen.setSuperPrefix(superPrefix); 222 223 Iterator it = objEntities.iterator(); 225 if (MODE_ENTITY.equals(mode)) { 226 it = objEntities.iterator(); 227 } 228 else { 229 if (objEntities.isEmpty()) { 230 it = objEntities.iterator(); 231 } 232 else { 233 it = Collections.singleton(objEntities.get(0)).iterator(); 234 } 235 } 236 237 while (it.hasNext()) { 238 ObjEntity ent = (ObjEntity) it.next(); 239 240 initClassGenerator_1_1(superGen, ent, true); 242 243 Writer superOut = openWriter(ent, superGen.getPackageName(), superPrefix 244 + superGen.getClassName()); 245 246 if (superOut != null) { 247 superGenSetup.generateClass(superOut, ent); 248 closeWriter(superOut); 249 } 250 251 initClassGenerator_1_1(mainGen, ent, false); 253 Writer mainOut = openWriter(ent, mainGen.getPackageName(), mainGen 254 .getClassName()); 255 if (mainOut != null) { 256 mainGenSetup.generateClass(mainOut, ent); 257 closeWriter(mainOut); 258 } 259 } 260 } 261 262 private void generateClientClassPairs_1_2( 263 String classTemplate, 264 String superTemplate, 265 String superPrefix) throws Exception { 266 267 ClassGenerator mainGenSetup = new ClassGenerator( 268 classTemplate, 269 ClassGenerator.VERSION_1_2, 270 vppConfig); 271 ClassGenerator superGenSetup = new ClassGenerator( 272 superTemplate, 273 ClassGenerator.VERSION_1_2, 274 vppConfig); 275 276 Iterator it = objEntities.iterator(); 278 if (MODE_ENTITY.equals(mode)) { 279 it = objEntities.iterator(); 280 } 281 else { 282 if (objEntities.isEmpty()) { 283 it = objEntities.iterator(); 284 } 285 else { 286 it = Collections.singleton(objEntities.get(0)).iterator(); 287 } 288 } 289 290 while (it.hasNext()) { 291 ObjEntity entity = (ObjEntity) it.next(); 292 293 String fqnSubClass = entity.getClientClassName(); 295 if (fqnSubClass == null) { 296 fqnSubClass = entity.getClassName(); 297 } 298 299 String fqnBaseClass = (entity.getClientSuperClassName() != null) ? entity 301 .getClientSuperClassName() : PersistentObject.class.getName(); 302 303 StringUtils stringUtils = StringUtils.getInstance(); 304 305 String subClassName = stringUtils.stripPackageName(fqnSubClass); 306 String subPackageName = stringUtils.stripClass(fqnSubClass); 307 308 String superClassName = superPrefix 309 + stringUtils.stripPackageName(fqnSubClass); 310 311 String superPackageName = this.superPkg; 312 String fqnSuperClass = superPackageName + "." + superClassName; 313 314 Writer superOut = openWriter(entity, superPackageName, superClassName); 315 316 if (superOut != null) { 317 superGenSetup.generateClass( 318 superOut, 319 dataMap, 320 entity, 321 fqnBaseClass, 322 fqnSuperClass, 323 fqnSubClass); 324 closeWriter(superOut); 325 } 326 327 Writer mainOut = openWriter(entity, subPackageName, subClassName); 328 if (mainOut != null) { 329 mainGenSetup.generateClass( 330 mainOut, 331 dataMap, 332 entity, 333 fqnBaseClass, 334 fqnSuperClass, 335 fqnSubClass); 336 closeWriter(mainOut); 337 } 338 } 339 } 340 341 348 private void generateClassPairs_1_2( 349 String classTemplate, 350 String superTemplate, 351 String superPrefix) throws Exception { 352 353 ClassGenerator mainGenSetup = new ClassGenerator( 354 classTemplate, 355 versionString, 356 vppConfig); 357 ClassGenerator superGenSetup = new ClassGenerator( 358 superTemplate, 359 versionString, 360 vppConfig); 361 362 Iterator it = objEntities.iterator(); 364 if (MODE_ENTITY.equals(mode)) { 365 it = objEntities.iterator(); 366 } 367 else { 368 if (objEntities.isEmpty()) { 369 it = objEntities.iterator(); 370 } 371 else { 372 it = Collections.singleton(objEntities.get(0)).iterator(); 373 } 374 } 375 376 while (it.hasNext()) { 377 ObjEntity ent = (ObjEntity) it.next(); 378 379 String fqnSubClass = ent.getClassName(); 380 String fqnBaseClass = (null != ent.getSuperClassName()) ? ent 381 .getSuperClassName() : CayenneDataObject.class.getName(); 382 383 StringUtils stringUtils = StringUtils.getInstance(); 384 385 String subClassName = stringUtils.stripPackageName(fqnSubClass); 386 String subPackageName = stringUtils.stripClass(fqnSubClass); 387 388 String superClassName = superPrefix 389 + stringUtils.stripPackageName(fqnSubClass); 390 391 String superPackageName = this.superPkg; 392 String fqnSuperClass = superPackageName + "." + superClassName; 393 394 Writer superOut = openWriter(ent, superPackageName, superClassName); 395 396 if (superOut != null) { 397 superGenSetup.generateClass( 398 superOut, 399 dataMap, 400 ent, 401 fqnBaseClass, 402 fqnSuperClass, 403 fqnSubClass); 404 closeWriter(superOut); 405 } 406 407 Writer mainOut = openWriter(ent, subPackageName, subClassName); 408 if (mainOut != null) { 409 mainGenSetup.generateClass( 410 mainOut, 411 dataMap, 412 ent, 413 fqnBaseClass, 414 fqnSuperClass, 415 fqnSubClass); 416 closeWriter(mainOut); 417 } 418 } 419 } 420 421 425 public void generateSingleClasses() throws Exception { 426 generateSingleClasses(defaultSingleClassTemplate(), SUPERCLASS_PREFIX); 427 } 428 429 432 private void generateSingleClasses_1_1(String classTemplate) throws Exception { 433 ClassGenerator gen = new ClassGenerator(classTemplate, versionString); 434 435 Iterator it = objEntities.iterator(); 437 if (MODE_ENTITY.equals(mode)) { 438 it = objEntities.iterator(); 439 } 440 else { 441 if (objEntities.isEmpty()) { 442 it = objEntities.iterator(); 443 } 444 else { 445 it = Collections.singleton(objEntities.get(0)).iterator(); 446 } 447 } 448 449 while (it.hasNext()) { 450 ObjEntity ent = (ObjEntity) it.next(); 451 452 initClassGenerator_1_1(gen.getClassGenerationInfo(), ent, false); 453 Writer out = openWriter( 454 ent, 455 gen.getClassGenerationInfo().getPackageName(), 456 gen.getClassGenerationInfo().getClassName()); 457 if (out == null) { 458 continue; 459 } 460 461 gen.generateClass(out, ent); 462 closeWriter(out); 463 } 464 } 465 466 469 private void generateSingleClasses_1_2(String classTemplate, String superPrefix) 470 throws Exception { 471 ClassGenerator gen = new ClassGenerator(classTemplate, versionString, vppConfig); 472 473 Iterator it = objEntities.iterator(); 475 if (MODE_ENTITY.equals(mode)) { 476 it = objEntities.iterator(); 477 } 478 else { 479 if (objEntities.isEmpty()) { 480 it = objEntities.iterator(); 481 } 482 else { 483 it = Collections.singleton(objEntities.get(0)).iterator(); 484 } 485 } 486 487 while (it.hasNext()) { 488 ObjEntity ent = (ObjEntity) it.next(); 489 490 String fqnSubClass = ent.getClassName(); 491 String fqnBaseClass = (null != ent.getSuperClassName()) ? ent 492 .getSuperClassName() : CayenneDataObject.class.getName(); 493 494 StringUtils stringUtils = StringUtils.getInstance(); 495 496 String subClassName = stringUtils.stripPackageName(fqnSubClass); 497 String subPackageName = stringUtils.stripClass(fqnSubClass); 498 499 String superClassName = superPrefix 500 + stringUtils.stripPackageName(fqnSubClass); 501 502 String superPackageName = this.superPkg; 503 String fqnSuperClass = superPackageName + "." + superClassName; 504 505 Writer out = openWriter(ent, subPackageName, subClassName); 506 if (out == null) { 507 continue; 508 } 509 510 gen 511 .generateClass( 512 out, 513 dataMap, 514 ent, 515 fqnBaseClass, 516 fqnSuperClass, 517 fqnSubClass); 518 closeWriter(out); 519 } 520 } 521 522 525 public void generateSingleClasses(String classTemplate, String superPrefix) 526 throws Exception { 527 if (client) { 529 generateSingleClasses_1_2(classTemplate, superPrefix); 530 } 531 else if (VERSION_1_1.equals(versionString)) { 532 generateSingleClasses_1_1(classTemplate); 533 } 534 else if (VERSION_1_2.equals(versionString)) { 535 generateSingleClasses_1_2(classTemplate, superPrefix); 536 } 537 else { 538 throw new IllegalStateException ("Illegal Version in generateClassPairs: " 539 + versionString); 540 } 541 } 542 543 544 protected void initClassGenerator_1_1( 545 ClassGenerationInfo gen, 546 ObjEntity entity, 547 boolean superclass) { 548 549 String fullClassName = entity.getClassName(); 551 int i = fullClassName.lastIndexOf("."); 552 553 String pkg = null; 554 String spkg = null; 555 String cname = null; 556 557 if (i == 0 || i + 1 == fullClassName.length()) { 559 throw new CayenneRuntimeException("Invalid class mapping: " + fullClassName); 560 } 561 else if (i < 0) { 562 pkg = (superclass) ? superPkg : null; 563 spkg = (superclass) ? null : superPkg; 564 cname = fullClassName; 565 } 566 else { 567 cname = fullClassName.substring(i + 1); 568 pkg = (superclass && superPkg != null) ? superPkg : fullClassName.substring( 569 0, 570 i); 571 572 spkg = (!superclass && superPkg != null && !pkg.equals(superPkg)) 573 ? superPkg 574 : null; 575 } 576 577 gen.setPackageName(pkg); 579 gen.setClassName(cname); 580 if (entity.getSuperClassName() != null) { 581 gen.setSuperClassName(entity.getSuperClassName()); 582 } 583 else { 584 gen.setSuperClassName(CayenneDataObject.class.getName()); 585 } 586 gen.setSuperPackageName(spkg); 587 } 588 589 593 public String getSuperPkg() { 594 return superPkg; 595 } 596 597 600 public void setSuperPkg(String superPkg) { 601 this.superPkg = superPkg; 602 } 603 604 609 public boolean isClient() { 610 return client; 611 } 612 613 618 public void setClient(boolean client) { 619 this.client = client; 620 } 621 622 625 public DataMap getDataMap() { 626 return dataMap; 627 } 628 629 632 public void setDataMap(DataMap dataMap) { 633 this.dataMap = dataMap; 634 } 635 636 public List getObjEntities() { 637 return objEntities; 638 } 639 640 645 public void setObjEntities(List objEntities) { 646 this.objEntities = objEntities != null 647 ? new ArrayList (objEntities) 648 : new ArrayList (); 649 650 Iterator it = objEntities.iterator(); 652 while (it.hasNext()) { 653 ObjEntity e = (ObjEntity) it.next(); 654 if (e.isGeneric()) { 655 it.remove(); 656 } 657 } 658 } 659 660 663 public String getVersionString() { 664 return versionString; 665 } 666 667 670 public void setVersionString(String versionString) { 671 if ((false == VERSION_1_1.equals(versionString)) 672 && (false == VERSION_1_2.equals(versionString))) { 673 throw new IllegalStateException ("'version' must be '" 674 + VERSION_1_1 675 + "' or '" 676 + VERSION_1_2 677 + "', but was '" 678 + versionString 679 + "'"); 680 } 681 this.versionString = versionString; 682 } 683 684 687 public VPPConfig getVppConfig() { 688 return vppConfig; 689 } 690 691 694 public void setVppConfig(VPPConfig vppConfig) { 695 this.vppConfig = vppConfig; 696 } 697 698 702 public void setMode(String mode) { 703 if ((false == MODE_ENTITY.equals(mode)) && (false == MODE_DATAMAP.equals(mode))) { 704 throw new IllegalStateException ("'mode' must be '" 705 + MODE_ENTITY 706 + "' or '" 707 + MODE_DATAMAP 708 + "', but was '" 709 + mode 710 + "'"); 711 } 712 this.mode = mode; 713 } 714 } 715 | Popular Tags |