1 23 24 29 30 package com.sun.jdo.api.persistence.mapping.ejb; 31 32 import java.util.*; 33 import java.sql.Types ; 34 import java.io.IOException ; 35 36 import com.sun.jdo.api.persistence.mapping.ejb.beans.*; 37 38 import com.sun.jdo.api.persistence.model.Model; 39 import com.sun.jdo.api.persistence.model.ModelException; 40 import com.sun.jdo.api.persistence.model.mapping.MappingClassElement; 41 import com.sun.jdo.api.persistence.model.mapping.MappingFieldElement; 42 import com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement; 43 import com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement; 44 45 import com.sun.jdo.spi.persistence.utility.StringHelper; 46 import com.sun.jdo.spi.persistence.utility.JavaTypeHelper; 47 48 import com.sun.jdo.spi.persistence.generator.database.DatabaseGenerator; 49 import com.sun.jdo.spi.persistence.generator.database.MappingPolicy; 50 51 import org.netbeans.modules.dbschema.*; 52 import org.netbeans.modules.dbschema.jdbcimpl.SchemaElementImpl; 53 import org.netbeans.modules.dbschema.util.NameUtil; 54 55 import org.netbeans.modules.schema2beans.Schema2BeansException; 56 57 65 public class MappingGenerator { 66 67 private static final String CLASS_SUFFIX = "_JDOState"; 71 private static final String FAKE_NAME = "fakename"; 73 private final EJBInfoHelper infoHelper; 74 private final Model model; 75 private final AbstractNameMapper nameMapper; 76 private final ClassLoader loader; 77 private final ConversionHelper ddHelper; 78 79 82 private boolean skipGeneratedFields = false; 83 84 private List strongRefs = new ArrayList(); 86 87 96 public MappingGenerator(EJBInfoHelper infoHelper, 97 ClassLoader loader, boolean skipGeneratedFields) { 98 this.infoHelper = infoHelper; 99 this.model = infoHelper.getModel(); 100 this.loader = loader; 101 this.nameMapper = infoHelper.getNameMapper(); 102 this.ddHelper = infoHelper.createConversionHelper(); 103 this.skipGeneratedFields = skipGeneratedFields; 104 } 105 106 protected EJBInfoHelper getInfoHelper() { 107 return infoHelper; 108 } 109 110 protected ClassLoader getClassLoader() { 111 return loader; 112 } 113 114 protected AbstractNameMapper getNameMapper() { 115 return nameMapper; 116 } 117 118 protected ConversionHelper getConversionHelper() { 119 return ddHelper; 120 } 121 122 135 public DatabaseGenerator.Results generateMappingClasses(String dbName, 136 String uniqueTableNames, Properties userPolicy, 137 String inputFilesPath) 138 throws IOException , Schema2BeansException, ModelException, 139 DBException, ConversionException { 140 141 SunCmpMappings sunCmpMappings = null; 143 144 147 List pcClasses = new ArrayList(); 148 sunCmpMappings = getPartialSunCmpMappings( 149 pcClasses, 150 Boolean.valueOf(uniqueTableNames).booleanValue()); 151 152 ddHelper.setEnsureValidation(false); 154 155 SchemaElement fakeSchema = new SchemaElement(new SchemaElementImpl()); 157 fakeSchema.setName(DBIdentifier.create(FAKE_NAME)); 158 159 SchemaElement.addToCache(fakeSchema); 161 162 loadMappingClasses(sunCmpMappings, null); 165 166 DatabaseGenerator.Results results = generateSchema(pcClasses, 167 dbName, uniqueTableNames, userPolicy); 168 169 SchemaElement schema = results.getSchema(); 170 Set mappingClasses = results.getMappingClasses(); 171 172 SchemaElement.removeFromCache(FAKE_NAME); 174 175 SchemaElement.removeFromCache(schema.getName().getName()); 178 179 SchemaElement.addToCache(schema); 181 182 updateMappingClasses(mappingClasses); 184 185 if (skipGeneratedFields) { 190 Iterator iter = mappingClasses.iterator(); 191 while (iter.hasNext()) { 192 MappingClassElement mapClassElt = (MappingClassElement)iter.next(); 193 if (mapClassElt != null) { 194 String className = mapClassElt.getName(); 195 String ejbName = nameMapper.getEjbNameForPersistenceClass( 196 className); 197 198 PersistenceClassElement pce = (PersistenceClassElement) 199 model.getPersistenceClass(className); 200 PersistenceFieldElement[] allFields = pce.getFields(); 201 if (allFields != null) { 202 List generatedFieldList = new ArrayList(); 203 204 for (int i = 0; i < allFields.length; i++) { 209 PersistenceFieldElement pfe = allFields[i]; 210 if (pfe != null) { 211 String pFieldName = pfe.getName(); 212 String ejbFieldName = nameMapper. 213 getEjbFieldForPersistenceField(className, 214 pFieldName); 215 if (nameMapper.isGeneratedField(ejbName, 216 ejbFieldName)) { 217 generatedFieldList.add(pfe); 218 } 219 } 220 } 221 222 Iterator iterator = generatedFieldList.iterator(); 226 while (iterator.hasNext()) { 227 PersistenceFieldElement pfe = 228 (PersistenceFieldElement)iterator.next(); 229 MappingFieldElement mfe = mapClassElt. 230 getField(pfe.getName()); 231 if (mfe != null && (!mfe.isVersion())) { 232 model.removeFieldElement(pfe); 233 mapClassElt.removeField(mfe); 234 } 235 } 236 } 237 } 238 } 239 } 240 241 return results; 242 } 243 244 254 protected Map loadMappingClasses(SunCmpMappings sunMapping, 255 ClassLoader classLoader) 256 throws DBException, ModelException, ConversionException { 257 MappingFile mapFile = new MappingFile(classLoader); 258 259 Map allMappings = mapFile.intoMappingClasses(sunMapping, ddHelper); 260 261 updateMappingClasses(allMappings.values()); 262 263 return allMappings; 264 } 265 266 270 public void cleanup() { 271 strongRefs.clear(); 275 } 276 277 290 private DatabaseGenerator.Results generateSchema(List pcClasses, 291 String dbName, String useUniqueTableNames, 292 Properties userPolicy) 293 throws IOException , DBException, ModelException { 294 295 MappingPolicy mappingPolicy = MappingPolicy.getMappingPolicy(dbName); 296 mappingPolicy.setUserPolicy(userPolicy); 297 298 if (isPropertyDefined(useUniqueTableNames)) { 299 mappingPolicy.setUniqueTableName( 301 Boolean.valueOf(useUniqueTableNames).booleanValue()); 302 } 303 304 return DatabaseGenerator.generate( 305 model, pcClasses, mappingPolicy, 306 infoHelper.getSchemaNameToGenerate(), CLASS_SUFFIX, true); 307 } 308 309 313 private void updateMappingClasses(Collection mappingClasses) { 314 Iterator iter = mappingClasses.iterator(); 315 while (iter.hasNext()) { 316 MappingClassElement mapClassElt = (MappingClassElement)iter.next(); 317 model.updateKeyForClass(mapClassElt, null); 319 strongRefs.add(mapClassElt); 321 } 322 } 323 324 333 private SunCmpMappings getPartialSunCmpMappings(List pcClasses, 334 boolean useUniqueTableNames) throws Schema2BeansException { 335 336 AbstractNameMapper nameMapper2 = (useUniqueTableNames) ? 340 infoHelper.createUniqueNameMapper() : nameMapper; 341 342 SunCmpMappings mappings = null; 343 mappings = new SunCmpMappings(); 344 SunCmpMapping mapping = new SunCmpMapping(); 345 mapping.setSchema(FAKE_NAME); 346 347 Iterator iter = infoHelper.getEjbNames().iterator(); 348 while (iter.hasNext()) { 349 String ejbName = (String )iter.next(); 350 String pcClass = ddHelper.getMappedClassName(ejbName); 351 String hashClassName = JavaTypeHelper.getShortClassName(pcClass); 352 353 if (useUniqueTableNames & hashClassName.equals(ejbName)) { 358 hashClassName = JavaTypeHelper.getShortClassName( 359 nameMapper2.getPersistenceClassForEjbName(ejbName)); 360 pcClasses.add(new DatabaseGenerator.NameTuple( 361 pcClass, ejbName, hashClassName)); 362 } 363 else { 364 pcClasses.add(new DatabaseGenerator.NameTuple( 365 pcClass, ejbName)); 366 } 367 368 EntityMapping entity = new EntityMapping(); 369 entity.setEjbName(ejbName); 370 entity.setTableName(FAKE_NAME); 371 Collection fields = infoHelper.getFieldsForEjb(ejbName); 372 Collection rels = infoHelper.getRelationshipsForEjb(ejbName); 373 fields.removeAll(rels); 374 Iterator fIter = fields.iterator(); 376 while (fIter.hasNext()) { 377 String fieldName = (String )fIter.next(); 378 CmpFieldMapping cmpField = new CmpFieldMapping(); 379 cmpField.setFieldName(fieldName); 380 cmpField.addColumnName(FAKE_NAME); 381 entity.addCmpFieldMapping(cmpField); 382 } 383 fIter = rels.iterator(); 385 while (fIter.hasNext()) { 386 String fieldName = (String )fIter.next(); 387 CmrFieldMapping cmrField = new CmrFieldMapping(); 388 cmrField.setCmrFieldName(fieldName); 389 ColumnPair columnPair = new ColumnPair(); 390 columnPair.addColumnName(FAKE_NAME); 391 columnPair.addColumnName(FAKE_NAME); 392 cmrField.addColumnPair(columnPair); 393 entity.addCmrFieldMapping(cmrField); 394 } 395 mapping.addEntityMapping(entity); 396 } 397 398 mappings.addSunCmpMapping(mapping); 399 400 return mappings; 401 } 402 403 413 protected boolean isPropertyDefined(String propertyValue) { 414 return !StringHelper.isEmpty(propertyValue); 415 } 416 417 429 public static void updateColumn(ColumnElement column, int jdbcType, 430 Integer length, Integer scale, Integer precision) 431 throws DBException { 432 433 column.setType(jdbcType); 434 column.setLength(length); 435 column.setScale(scale); 436 column.setPrecision(precision); 437 } 438 439 452 public static void updateProperties(Properties prop, String className, 453 String fieldName, int jdbcType, Integer length, Integer scale, 454 Integer precision) { 455 456 prop.setProperty( 457 MappingPolicy.getOverrideForType(className, fieldName), 458 MappingPolicy.getJdbcTypeName(jdbcType)); 459 460 updateProperty(prop, MappingPolicy.getOverrideForLength( 461 className, fieldName), length); 462 463 updateProperty(prop, MappingPolicy.getOverrideForScale( 464 className, fieldName), scale); 465 466 updateProperty(prop, MappingPolicy.getOverrideForPrecision( 467 className, fieldName), precision); 468 } 469 470 478 private static void updateProperty(Properties prop, String key, 479 Integer value) { 480 if (value != null) { 481 prop.setProperty(key, value.toString()); 482 } 483 else { 484 prop.remove(key); 485 } 486 } 487 488 493 public static class SQLTypeUtil { 494 495 private static final Map characterMap = new HashMap(); 496 private static final Map numericMap = new HashMap(); 497 private static final Map blobMap = new HashMap(); 498 private static final Map timeMap = new HashMap(); 499 500 private static final String NONE_ATTRIBUTE = "none"; 501 private static final String LENGTH_ATTRIBUTE = "length"; 502 private static final String SCALE_ATTRIBUTE = "scale"; 503 private static final String SCALE_PRECISION_ATTRIBUTE = "scale-precision"; 504 505 static { 506 characterMap.put(new Integer (Types.CHAR), LENGTH_ATTRIBUTE); 507 characterMap.put(new Integer (Types.VARCHAR), LENGTH_ATTRIBUTE); 508 characterMap.put(new Integer (Types.CLOB), LENGTH_ATTRIBUTE); 509 510 numericMap.put(new Integer (Types.BIT), NONE_ATTRIBUTE); 511 numericMap.put(new Integer (Types.TINYINT), NONE_ATTRIBUTE); 512 numericMap.put(new Integer (Types.SMALLINT), NONE_ATTRIBUTE); 513 numericMap.put(new Integer (Types.BIGINT), NONE_ATTRIBUTE); 514 numericMap.put(new Integer (Types.INTEGER), NONE_ATTRIBUTE); 515 numericMap.put(new Integer (Types.DOUBLE), NONE_ATTRIBUTE); 516 numericMap.put(new Integer (Types.DECIMAL), SCALE_PRECISION_ATTRIBUTE); 517 numericMap.put(new Integer (Types.REAL), NONE_ATTRIBUTE); 518 519 blobMap.put(new Integer (Types.BLOB), LENGTH_ATTRIBUTE); 520 521 timeMap.put(new Integer (Types.DATE), NONE_ATTRIBUTE); 522 timeMap.put(new Integer (Types.TIME), NONE_ATTRIBUTE); 523 timeMap.put(new Integer (Types.TIMESTAMP), NONE_ATTRIBUTE); 524 } 525 526 531 public static boolean isNumeric (int jdbcType) { 532 return checkType(jdbcType, numericMap); 533 } 534 535 540 public static boolean isCharacter (int jdbcType) { 541 return checkType(jdbcType, characterMap); 542 } 543 544 549 public static boolean isBlob (int jdbcType) { 550 return checkType(jdbcType, blobMap); 551 } 552 553 558 public static boolean isTime (int jdbcType) { 559 return checkType(jdbcType, timeMap); 560 } 561 562 private static boolean checkType(int jdbcType, Map jdbcTypes) { 563 return jdbcTypes.containsKey(new Integer (jdbcType)); 564 } 565 566 570 public static Collection getCompatibleTypes(int jdbcType) { 571 if (isNumeric(jdbcType)) { 572 return numericMap.keySet(); 573 } 574 else if (isCharacter(jdbcType)) { 575 return characterMap.keySet(); 576 } 577 else if (isBlob(jdbcType)) { 578 return blobMap.keySet(); 579 } 580 else if (isTime(jdbcType)) { 581 return timeMap.keySet(); 582 } 583 return null; 584 } 585 586 592 public static boolean hasScale(int jdbcType) { 593 if (getAttribute(jdbcType).equals(SCALE_ATTRIBUTE) 594 || getAttribute(jdbcType).equals(SCALE_PRECISION_ATTRIBUTE)) 595 return true; 596 return false; 597 } 598 599 606 public static boolean hasPrecision(int jdbcType) { 607 if (getAttribute(jdbcType).equals(SCALE_PRECISION_ATTRIBUTE)) 608 return true; 609 return false; 610 } 611 612 618 public static boolean hasLength(int jdbcType) { 619 if (getAttribute(jdbcType).equals(LENGTH_ATTRIBUTE)) 620 return true; 621 return false; 622 } 623 624 private static String getAttribute(int jdbcType) { 625 if (isNumeric(jdbcType)) { 626 return (String )numericMap.get(new Integer (jdbcType)); 627 } 628 else if (isCharacter(jdbcType)) { 629 return (String )characterMap.get(new Integer (jdbcType)); 630 } 631 else if (isBlob(jdbcType)) { 632 return (String )blobMap.get(new Integer (jdbcType)); 633 } 634 else if (isTime(jdbcType)) { 635 return (String )timeMap.get(new Integer (jdbcType)); 636 } 637 return NONE_ATTRIBUTE; 638 } 639 } 640 } 641 | Popular Tags |