1 23 24 29 30 package com.sun.jdo.api.persistence.mapping.ejb; 31 32 import java.io.InputStream ; 33 import java.io.OutputStream ; 34 import java.io.File ; 35 36 import java.util.Collection ; 37 import java.util.Map ; 38 import java.util.List ; 39 import java.util.ArrayList ; 40 import java.util.HashMap ; 41 import java.util.ResourceBundle ; 42 import java.util.Iterator ; 43 import java.io.IOException ; 44 45 import java.text.MessageFormat ; 46 47 import org.netbeans.modules.dbschema.*; 48 49 import com.sun.jdo.api.persistence.mapping.ejb.beans.*; 50 import com.sun.jdo.spi.persistence.utility.StringHelper; 51 import com.sun.jdo.api.persistence.model.*; 52 import com.sun.jdo.api.persistence.model.mapping.*; 53 import com.sun.jdo.api.persistence.model.mapping.impl.*; 54 import com.sun.jdo.api.persistence.model.jdo.*; 55 import com.sun.jdo.api.persistence.model.jdo.impl.*; 56 import com.sun.jdo.spi.persistence.utility.logging.Logger; 57 import com.sun.jdo.spi.persistence.utility.I18NHelper; 58 59 import org.netbeans.modules.schema2beans.Schema2BeansException; 60 61 68 public class MappingFile { 69 70 private static final String JAVA_TYPE_SET = "java.util.Set"; private static final String JAVA_TYPE_COLLECTION = "java.util.Collection"; private static final List types = new ArrayList (); 73 74 75 public static String DEFAULT_LOCATION_IN_EJB_JAR = null; 76 77 78 private static final Logger logger = 79 LogHelperMappingConversion.getLogger(); 80 81 84 private final static ResourceBundle messages = I18NHelper.loadBundle( 85 MappingFile.class); 86 87 static { 88 types.add(JAVA_TYPE_SET); 89 types.add(JAVA_TYPE_COLLECTION); 90 91 DEFAULT_LOCATION_IN_EJB_JAR = new StringBuffer (I18NHelper.getMessage( 92 messages, "CONST_IAS_MAPPING_FILE_LOC")). append(File.separator). 94 append(I18NHelper.getMessage( 95 messages, "CONST_IAS_MAPPING_FILE")).toString(); } 97 98 private Map inverseRelationships = null; 99 private Map namedGroups = null; 100 private int groupCount = MappingFieldElement.GROUP_INDEPENDENT; 101 102 private ClassLoader classLoader = null; 103 104 private static int MINIMUM_PRECISION = 19; 105 106 111 private ConversionHelper helper = null; 112 113 private HashMap loadedSchema = new HashMap (); 114 115 116 public MappingFile() { 117 classLoader = null; 118 } 119 120 121 public MappingFile(ClassLoader cl) { 122 this(); 123 classLoader = cl; 124 } 125 126 138 public Map intoMappingClasses(SunCmpMappings content, 139 ConversionHelper helper) 140 throws DBException, ModelException, ConversionException { 141 Map mces = new java.util.HashMap (); 142 this.helper = helper; 143 boolean ensureValidation = helper.ensureValidation(); 144 145 for (int i = 0; i < content.sizeSunCmpMapping(); i++) { 146 SunCmpMapping beanSet = content.getSunCmpMapping(i); 147 inverseRelationships = new HashMap (); 148 namedGroups = new HashMap (); 149 String schema = beanSet.getSchema(); 150 151 if (helper.generateFields()) { 152 153 completeCmrMappings(beanSet); 155 } 156 157 for (int k = 0; k < beanSet.sizeEntityMapping(); k++) { 159 EntityMapping beanMapping = beanSet.getEntityMapping(k); 160 MappingClassElement aTpMapping = null; 161 if (ensureValidation) { 162 aTpMapping = mapFieldsOfBean(beanMapping, schema); 163 } 164 else { 165 try { 166 aTpMapping = mapFieldsOfBean(beanMapping, schema); 167 } 168 catch (ConversionException t) { 169 if (logger.isLoggable(Logger.FINE)) 170 logger.fine( 171 I18NHelper.getMessage( 172 messages, 173 "MESSAGE_CONV_EXC", t.toString())); 175 } 176 } 177 178 mces.put(beanMapping.getEjbName(), aTpMapping); 179 } 180 } 181 return mces; 182 } 183 184 196 public Map intoMappingClasses(InputStream content, ConversionHelper helper) 197 throws DBException, ModelException, ConversionException { 198 SunCmpMappings foo = null; 199 try { 200 foo = SunCmpMappings.createGraph(content); 201 } 202 catch (Schema2BeansException t) { 203 if (helper.ensureValidation()) { 204 throw new ConversionException( 205 I18NHelper.getMessage(messages, 206 "XML_ERROR_IN_MAPPING_FILE", DEFAULT_LOCATION_IN_EJB_JAR)); 208 } 209 foo = SunCmpMappings.createGraph(); 210 } 211 212 return intoMappingClasses(foo, helper); 213 } 214 215 226 public void fromMappingClasses(OutputStream dest, Map mappingClasses, 227 ConversionHelper helper) 228 throws IOException , Schema2BeansException { 229 SunCmpMappings tmp = fromMappingClasses(mappingClasses, helper); 230 tmp.write(dest); 231 } 232 233 243 public SunCmpMappings fromMappingClasses(Map mappingClasses, 244 ConversionHelper helper) throws Schema2BeansException { 245 Iterator keyIter = mappingClasses.keySet().iterator(); 246 Map mapOfMapping = new java.util.HashMap (); 247 while (keyIter.hasNext()) { 248 String ejbName = (String ) keyIter.next(); 249 MappingClassElement mce = (MappingClassElement) 250 mappingClasses.get(ejbName); 251 EntityMapping beanMapping = new EntityMapping(); 252 253 if (null != mce) { 254 setConsistency(mce, beanMapping); 255 256 String schemaName = mce.getDatabaseRoot(); 257 SunCmpMapping aMapping = (SunCmpMapping) mapOfMapping.get( 258 schemaName); 259 if (null == aMapping) { 260 aMapping = new SunCmpMapping(); 261 aMapping.setSchema(schemaName); 262 mapOfMapping.put(schemaName, aMapping); 263 } 264 List tables = mce.getTables(); 265 MappingTableElement primary = null; 266 if (tables.size() > 0) { 267 primary = (MappingTableElement) tables.get(0); 268 beanMapping.setTableName(primary.getName()); 269 } 270 beanMapping.setEjbName(ejbName); 271 if (null != primary) { 272 List refKeys = primary.getReferencingKeys(); 273 for (int i = 0; refKeys != null && i < refKeys.size(); i++) { 274 SecondaryTable sT = new SecondaryTable(); 275 MappingReferenceKeyElement mrke = 276 (MappingReferenceKeyElement) refKeys.get(i); 277 MappingTableElement mte = mrke.getTable(); 278 if (null != mte) { 279 sT.setTableName(mte.getName()); 280 List cpnames = mrke.getColumnPairNames(); 281 boolean hasPairs = false; 282 for (int j = 0; cpnames != null && 283 j < cpnames.size(); j++) { 284 List token = 285 StringHelper.separatedListToArray((String )cpnames.get(j),";"); ColumnPair cp = new ColumnPair(); 287 Iterator iter = token.iterator(); 288 while (iter.hasNext()) { 289 String columnName = (String )iter.next(); 290 cp.addColumnName(columnName); 291 } 292 sT.addColumnPair(cp); 293 hasPairs = true; 294 } 295 if (hasPairs) 296 beanMapping.addSecondaryTable(sT); 297 else 298 if (logger.isLoggable(Logger.FINE)) 299 logger.fine( 300 I18NHelper.getMessage( 301 messages, 302 "WARN_ILLEGAL_PAIR", new Object [] {ejbName, mte.getName(), cpnames})); 304 } 305 else { 306 if (logger.isLoggable(Logger.FINE)) 307 logger.fine( 308 I18NHelper.getMessage( 309 messages, 310 "WARN_MISSING_TABLE", new Object [] {ejbName, primary.getName()})); 312 } 313 } 314 } 315 else { 316 if (logger.isLoggable(Logger.FINE)) 317 logger.fine( 318 I18NHelper.getMessage( 319 messages, 320 "WARN_NO_PRIMARY", ejbName)); 322 } 323 324 PersistenceClassElement pce = null; 326 PersistenceFieldElement pfields[] = null; 327 if (mce instanceof MappingClassElementImpl) { 328 MappingClassElementImpl mcei = 329 (MappingClassElementImpl) mce; 330 pce = mcei.getPersistenceElement(); 331 pfields = pce.getFields(); 332 } 333 int len = 0; 334 if (null != pfields) 335 len = pfields.length; 336 for (int i = 0; i < len; i++) { 337 PersistenceFieldElement pfield = pfields[i]; 338 String fieldName = pfield.getName(); 339 if (helper.isGeneratedField(ejbName, fieldName)) { 340 continue; 341 } 342 if (pfield instanceof RelationshipElement) { 343 MappingRelationshipElement mre = 344 (MappingRelationshipElement) mce.getField(fieldName); 345 MappingFieldElement mfe = mre; 346 CmrFieldMapping cfm = new CmrFieldMapping(); 347 cfm.setCmrFieldName(fieldName); 348 List cols = null; 349 if (null != mfe) { 350 cols = mfe.getColumns(); 351 int fgVal = mfe.getFetchGroup(); 352 setFetchedWith(cfm, fgVal); 353 } 354 for (int j = 0; null != cols && j < cols.size(); j++) { 355 String cpstring = (String ) cols.get(j); 356 int slen = cpstring.indexOf(';'); 357 ColumnPair cp = new ColumnPair(); 358 cp.addColumnName(cpstring.substring(0,slen)); 359 cp.addColumnName(cpstring.substring(slen+1)); 360 cfm.addColumnPair(cp); 361 } 362 if (null != mre) 363 cols = mre.getAssociatedColumns(); 364 for (int j = 0; null != cols && j < cols.size(); j++) { 365 String cpstring = (String ) cols.get(j); 366 int slen = cpstring.indexOf(';'); 367 ColumnPair cp = new ColumnPair(); 368 cp.addColumnName(cpstring.substring(0,slen)); 369 cp.addColumnName(cpstring.substring(slen+1)); 370 cfm.addColumnPair(cp); 371 } 372 beanMapping.addCmrFieldMapping(cfm); 373 } 374 else { 375 MappingFieldElement mfe = mce.getField(fieldName); 376 CmpFieldMapping cfm = new CmpFieldMapping(); 377 cfm.setFieldName(fieldName); 378 List cols = null; 379 if (null != mfe) { 380 cols = mfe.getColumns(); 381 for (int j = 0; null != cols && 382 j < cols.size(); j++) { 383 cfm.addColumnName((String )cols.get(j)); 384 } 385 int fgVal = mfe.getFetchGroup(); 386 setFetchedWith(cfm,fgVal); 387 } 388 beanMapping.addCmpFieldMapping(cfm); 389 } 390 } 391 aMapping.addEntityMapping(beanMapping); 392 } 393 } 394 SunCmpMappings retVal = null; 395 retVal = new SunCmpMappings(); 396 Iterator mapOfMappingIter = mapOfMapping.values().iterator(); 397 while (mapOfMappingIter.hasNext()) { 398 SunCmpMapping aVal = (SunCmpMapping) mapOfMappingIter.next(); 399 retVal.addSunCmpMapping(aVal); 400 } 401 return retVal; 402 } 403 404 409 private void setFetchedWith(HasFetchedWith cfm, int fgVal) { 410 FetchedWith fw = new FetchedWith(); 411 if (fgVal <= MappingFieldElement.GROUP_INDEPENDENT) { 412 String key = "IndependentFetchGroup"+fgVal; fw.setNamedGroup(key); 414 } 415 else if (fgVal == MappingFieldElement.GROUP_DEFAULT) { 416 fw.setDefault(true); 417 } 418 else if (fgVal > MappingFieldElement.GROUP_DEFAULT) { 419 fw.setLevel(fgVal-1); 420 } 421 else if (fgVal == MappingFieldElement.GROUP_NONE) { 422 fw.setNone(true); 423 } 424 cfm.setFetchedWith(fw); 425 } 426 427 448 private MappingClassElement mapFieldsOfBean(EntityMapping mapping, 449 String schemaArg) 450 throws DBException, ModelException, ConversionException { 451 452 String beanName = mapping.getEjbName(); 453 MappingClassElement mce = null; 454 List tablesOfBean = new ArrayList (); 455 Map knownTables = new HashMap (); 456 457 if (logger.isLoggable(Logger.FINE)) 458 logger.fine( 459 I18NHelper.getMessage( 460 messages, 461 "MESSAGE_START_BEAN", beanName)); 463 464 String jdoClassName = helper.getMappedClassName(beanName); 465 if (logger.isLoggable(Logger.FINE)) 466 logger.fine( 467 I18NHelper.getMessage( 468 messages, 469 "MESSAGE_JDOCLASS_NAME", beanName, jdoClassName)); 471 472 if (null == jdoClassName) { 473 throw new ConversionException( 474 I18NHelper.getMessage( 475 messages, 476 "ERR_INVALID_CLASS", beanName)); 478 } 479 480 PersistenceClassElementImpl persistElImpl = 482 new PersistenceClassElementImpl(jdoClassName); 483 persistElImpl.setKeyClass(jdoClassName+".Oid"); mce = new MappingClassElementImpl( 485 new PersistenceClassElement(persistElImpl)); 486 487 SchemaElement schema = null; 488 489 if (!StringHelper.isEmpty(schemaArg)) 491 schema = setDatabaseRoot(mce, schemaArg, 492 helper.ensureValidation()); 493 494 if (!StringHelper.isEmpty(mapping.getTableName())) { 497 498 mapPrimaryTable(mapping, mce, 499 schema, knownTables, tablesOfBean); 500 mapSecondaryTables(mapping, mce, 501 schema, knownTables, tablesOfBean); 502 } 503 504 ColumnElement candidatePK = null; 505 506 candidatePK = mapNonRelationshipFields(mapping, mce, 508 beanName, schema, knownTables); 509 510 createMappingForUnknownPKClass(mapping, mce, 511 beanName, candidatePK); 512 513 createMappingForConsistency(mapping, mce, 514 beanName, knownTables); 515 516 mapRelationshipFields(mapping, mce, 518 beanName, schema, knownTables, tablesOfBean); 519 520 mapUnmappedFields(mce, beanName); 522 523 return mce; 524 } 525 526 536 private void createMappingForUnknownPKClass(EntityMapping mapping, 537 MappingClassElement mce, 538 String beanName, 539 ColumnElement candidatePK) 540 throws ModelException, ConversionException { 541 String primaryTableName = mapping.getTableName(); 542 543 if (helper.generateFields() 544 && helper.applyDefaultUnknownPKClassStrategy(beanName)) { 545 if (null != candidatePK) { 546 String fieldName = helper.getGeneratedPKFieldName(); 547 548 PersistenceFieldElement pfe = createPersistenceField(mce, 554 fieldName); 555 pfe.setKey(true); 556 MappingFieldElement mfe = createMappingField(mce, fieldName, 557 candidatePK); 558 } 559 else { 560 throw new ConversionException( 563 I18NHelper.getMessage( 564 messages, 565 "WARN_NO_PKCOLUMN", primaryTableName)); 567 } 568 } 569 } 570 571 583 private void createMappingForConsistency(EntityMapping mapping, 584 MappingClassElement mce, 585 String beanName, 586 Map knownTables) 587 throws ModelException, DBException, ConversionException { 588 589 List versionColumns = loadConsistency(mapping, mce); 591 592 if (versionColumns != null) { 597 598 String primaryTableName = mapping.getTableName(); 599 600 if (versionColumns.size() > 1) { 602 throw new ConversionException( 603 I18NHelper.getMessage( 604 messages, 605 "ERR_INVALID_VERSION_COLUMNS")); } 607 String versionColumn = (String )versionColumns.get(0); 608 609 String sourceTableName = getTableName(versionColumn, 610 primaryTableName); 611 612 if (!sourceTableName.equals(primaryTableName)) { 615 throw new ConversionException( 616 I18NHelper.getMessage( 617 messages, 618 "WARN_VERSION_COLUMN_INVALID_TABLE", primaryTableName, beanName, versionColumn)); 620 } 621 622 TableElement sourceTableEl = (TableElement) knownTables.get( 623 sourceTableName); 624 String versionColumnName = getColumnName(versionColumn); 625 ColumnElement versionCol = getColumnElement(sourceTableEl, 626 DBIdentifier.create(versionColumnName), helper); 627 628 if (null != versionCol) { 629 630 String fieldName = helper.getGeneratedVersionFieldNamePrefix(); 633 PersistenceFieldElement pfe = createPersistenceField(mce, 634 fieldName); 635 MappingFieldElement mfe = createMappingField(mce, fieldName, 636 versionCol); 637 mfe.setVersion(true); 638 } 639 else { 640 throw new ConversionException( 643 I18NHelper.getMessage( 644 messages, 645 "WARN_VERSION_COLUMN_MISSING", primaryTableName, beanName)); 647 } 648 } 649 } 650 651 665 private ColumnElement mapNonRelationshipFields(EntityMapping mapping, 666 MappingClassElement mce, 667 String beanName, 668 SchemaElement schema, 669 Map knownTables) 670 throws DBException, ModelException, ConversionException { 671 672 CmpFieldMapping [] mapOfFields = mapping.getCmpFieldMapping(); 673 String primaryTableName = mapping.getTableName(); 674 ColumnElement candidatePK = null; 675 676 if (helper.generateFields() 678 && helper.applyDefaultUnknownPKClassStrategy(beanName)) { 679 680 candidatePK = getCandidatePK(schema, primaryTableName); 681 } 682 683 for (int i = 0; i < mapOfFields.length; i++) { 684 CmpFieldMapping mapForOneField = mapOfFields[i]; 685 686 String fieldName = mapForOneField.getFieldName(); 687 if (!validateField(mce, beanName, fieldName, 688 helper.ensureValidation())) { 689 if (logger.isLoggable(Logger.FINE)) 690 logger.fine( 691 I18NHelper.getMessage( 692 messages, 693 "WARN_INVALID_FIELD", beanName, fieldName)); 695 696 continue; 697 } 698 699 String columnNames[] = mapForOneField.getColumnName(); 700 MappingFieldElement mfe = createUnmappedField(mce, beanName, 701 fieldName); 702 boolean fieldMappedToABlob = false; 703 704 for (int j = 0; j < columnNames.length; j++) { 705 String sourceTableName = getTableName(columnNames[j], 706 primaryTableName); 707 if (null == sourceTableName) { 708 throw new ConversionException( 709 I18NHelper.getMessage( 710 messages, 711 "ERR_UNDEFINED_TABLE")); } 713 714 String sourceColumnName = getColumnName(columnNames[j]); 715 716 TableElement sourceTableEl = getTableElement(sourceTableName, 718 knownTables, schema); 719 ColumnElement aCol = getColumnElement(sourceTableEl, 720 DBIdentifier.create(sourceColumnName), helper); 721 if (logger.isLoggable(Logger.FINE)) 722 logger.fine( 723 I18NHelper.getMessage( 724 messages, 725 "MESSAGE_ADD_COLUMN", new Object [] {aCol, fieldName})); 727 728 if (helper.generateFields() 731 && helper.applyDefaultUnknownPKClassStrategy(beanName)) { 732 if (candidatePK != null && candidatePK.equals(aCol)) { 733 candidatePK = null; 734 } 735 } 736 737 fieldMappedToABlob |= aCol.isBlobType(); 738 mfe.addColumn(aCol); 739 } 740 741 FetchedWith fw = mapForOneField.getFetchedWith(); 742 setFetchGroup(fw, mfe, beanName, fieldMappedToABlob); 743 mce.addField(mfe); 744 } 745 746 return candidatePK; 747 } 748 749 765 private void mapRelationshipFields(EntityMapping mapping, 766 MappingClassElement mce, 767 String beanName, 768 SchemaElement schema, 769 Map knownTables, 770 List tablesOfBean) 771 throws ModelException, DBException, ConversionException { 772 773 String primaryTableName = mapping.getTableName(); 774 CmrFieldMapping mapOfRelations[] = mapping.getCmrFieldMapping(); 775 PersistenceClassElement pce = ((MappingClassElementImpl)mce).getPersistenceElement(); 776 777 for (int i = 0; mapOfRelations != null && i < mapOfRelations.length; 778 i++) { 779 CmrFieldMapping aRelation = mapOfRelations[i]; 780 String fieldName = aRelation.getCmrFieldName(); 781 782 if (!validateField(mce, beanName, fieldName, helper.ensureValidation())) { 783 if (logger.isLoggable(Logger.FINE)) 784 logger.fine( 785 I18NHelper.getMessage( 786 messages, 787 "WARN_INVALID_CMRFIELD", beanName, fieldName)); 789 continue; 790 } 791 792 RelationshipElement rel = new RelationshipElement( 793 new RelationshipElementImpl(fieldName), pce); 794 MappingRelationshipElement mre = 795 new MappingRelationshipElementImpl(fieldName, mce); 796 797 registerInverseRelationshipElement(rel, beanName, fieldName); 799 800 final ColumnPair pairs[] = aRelation.getColumnPair(); 801 final String relationId = mapping.getEjbName() + "_Relationship_" + i; Collection primaryTableColumns = convertToColumnPairElements( 803 pairs, 804 primaryTableName, schema, knownTables, tablesOfBean, 805 relationId, 806 mre); 807 808 setUpperBound(rel, beanName, fieldName); 809 setCascadeDeleteAction(rel, beanName, fieldName); 810 setLowerBound(rel, 811 primaryTableColumns, 812 primaryTableName,schema, knownTables, 813 beanName, fieldName); 814 setFetchGroup(aRelation.getFetchedWith(), mre, beanName, false); 815 816 pce.addField(rel); 817 mce.addField(mre); 818 } 819 } 820 821 826 private void mapUnmappedFields(MappingClassElement mce, String beanName) 827 throws ModelException { 828 829 Object [] fields = helper.getFields(beanName); 830 if (!helper.ensureValidation()) { 831 for (int i = 0; i < fields.length; i++) { 832 String fieldName = (String ) fields[i]; 833 MappingFieldElement mfe = mce.getField(fieldName); 834 835 if (null == mfe) { 836 837 mfe = createUnmappedField(mce, beanName, fieldName); 839 mce.addField(mfe); 840 } 841 } 842 } 843 } 844 845 851 private void setConsistency(MappingClassElement mce, 852 EntityMapping beanMapping) { 853 int consistency = mce.getConsistencyLevel(); 854 if (MappingClassElement.NONE_CONSISTENCY != consistency) { 855 Consistency c = new Consistency(); 856 if (MappingClassElement.LOCK_WHEN_MODIFIED_CHECK_ALL_AT_COMMIT_CONSISTENCY == consistency) { 857 c.setLockWhenModified(true); 858 c.setCheckAllAtCommit(true); 859 } 860 if (MappingClassElement.LOCK_WHEN_MODIFIED_CONSISTENCY == consistency) 861 c.setLockWhenModified(true); 862 if (MappingClassElement.CHECK_ALL_AT_COMMIT_CONSISTENCY == consistency) 863 c.setCheckAllAtCommit(true); 864 if (MappingClassElement.LOCK_WHEN_LOADED_CONSISTENCY == consistency) 865 c.setLockWhenLoaded(true); 866 if (MappingClassElement.CHECK_MODIFIED_AT_COMMIT_CONSISTENCY == consistency) 867 c.setCheckModifiedAtCommit(true); 868 if (MappingClassElement.VERSION_CONSISTENCY == consistency) { 869 CheckVersionOfAccessedInstances versionIns = 870 new CheckVersionOfAccessedInstances(); 871 Iterator iter = mce.getVersionFields().iterator(); 872 while (iter.hasNext()) { 873 List columnNames = ((MappingFieldElement)iter.next()). 874 getColumns(); 875 876 if (columnNames != null && columnNames.size() > 0) 878 versionIns.addColumnName((String )columnNames.get(0)); 879 } 880 c.setCheckVersionOfAccessedInstances(versionIns); 881 } 882 beanMapping.setConsistency(c); 883 } 884 } 885 886 895 private List loadConsistency(EntityMapping mapping, MappingClassElement mce) 896 throws ModelException, ConversionException { 897 Consistency c = mapping.getConsistency(); 898 if (null == c) { 899 mce.setConsistencyLevel(MappingClassElement.NONE_CONSISTENCY); 900 } 901 else { 902 CheckVersionOfAccessedInstances versionIns = 903 (CheckVersionOfAccessedInstances) 904 c.getCheckVersionOfAccessedInstances(); 905 906 if (c.isCheckModifiedAtCommit()) 907 mce.setConsistencyLevel( 908 MappingClassElement.CHECK_MODIFIED_AT_COMMIT_CONSISTENCY); 909 else if(c.isLockWhenLoaded()) 910 mce.setConsistencyLevel( 911 MappingClassElement.LOCK_WHEN_LOADED_CONSISTENCY); 912 else if(c.isCheckAllAtCommit()) 913 mce.setConsistencyLevel( 914 MappingClassElement.CHECK_ALL_AT_COMMIT_CONSISTENCY); 915 else if(c.isLockWhenModified()) 916 mce.setConsistencyLevel( 917 MappingClassElement.LOCK_WHEN_MODIFIED_CONSISTENCY); 918 else if(c.isLockWhenModified() && c.isCheckAllAtCommit()) 919 mce.setConsistencyLevel(MappingClassElement. 920 LOCK_WHEN_MODIFIED_CHECK_ALL_AT_COMMIT_CONSISTENCY); 921 else if(c.isNone()) 922 mce.setConsistencyLevel(MappingClassElement.NONE_CONSISTENCY); 923 else if (versionIns != null) { 924 mce.setConsistencyLevel(MappingClassElement.VERSION_CONSISTENCY); 925 List versionColumns = new ArrayList (); 926 for (int i = 0; i < versionIns.sizeColumnName(); i++) { 927 versionColumns.add(versionIns.getColumnName(i)); 928 } 929 return versionColumns; 930 } 931 else { 932 throw new ConversionException( 933 I18NHelper.getMessage( 934 messages, 935 "ERR_INVALID_CONSISTENCY_VALUE", mce)); } 937 } 938 return null; 939 } 940 941 954 private void mapPrimaryTable(EntityMapping mapping, 955 MappingClassElement mce, 956 SchemaElement schema, 957 Map knownTables, 958 List tablesOfBean) 959 throws DBException, ModelException, ConversionException { 960 961 String primaryTableName = mapping.getTableName(); 962 TableElement primTabEl = getTableElement(primaryTableName, knownTables, schema); 963 964 mce.addTable(primTabEl); 965 tablesOfBean.add(primaryTableName); 966 } 967 968 976 private ColumnElement getCandidatePK(SchemaElement schema, 977 String primaryTableName) 978 throws DBException, ConversionException { 979 ColumnElement candidatePK = null; 980 981 TableElement primTabEl = getTableElement(schema, 982 DBIdentifier.create(primaryTableName), helper); 983 UniqueKeyElement uke = primTabEl.getPrimaryKey(); 986 987 if (null != uke) { 988 ColumnElement cols[] = uke.getColumns(); 989 if (null != cols && 1 == cols.length) { 990 candidatePK = cols[0]; 991 if (logger.isLoggable(Logger.FINE)) 992 logger.fine( 993 I18NHelper.getMessage( 994 messages, 995 "MESSAGE_CANDIDATE_PK", candidatePK.getName())); 997 998 Integer pre = candidatePK.getPrecision(); 999 if (null != candidatePK && !candidatePK.isNumericType()) { 1000 candidatePK = null; 1001 } 1002 if (null != candidatePK && (null != pre) && pre.intValue() < MINIMUM_PRECISION) { 1003 candidatePK = null; 1004 } 1005 } 1006 } 1007 return candidatePK; 1008 } 1009 1010 1030 private Collection convertToColumnPairElements(ColumnPair pairs[], 1031 String primaryTableName, 1032 SchemaElement schema, 1033 Map knownTables, 1034 List tablesOfBean, 1035 String relationId, 1036 MappingRelationshipElement mre) 1037 throws DBException, ModelException, ConversionException { 1038 1039 Collection primaryTableColumns = new ArrayList (); 1040 boolean isJoin = false; 1041 1042 for (int i = 0; null != pairs && i < pairs.length; i++) { 1043 ColumnPair pair = pairs[i]; 1044 ColumnPairElement cpe = new ColumnPairElement(); 1045 boolean localSet = false; 1046 1047 cpe.setName(DBIdentifier.create(relationId + "_ColumnPair_" + i)); 1049 for (int j = 0; j < 2; j++) { 1050 String columnName = pair.getColumnName(j); 1051 String sourceTableName = getTableName(columnName, 1052 primaryTableName); 1053 String sourceColumnName = getColumnName(columnName); 1054 TableElement sourceTableEl = getTableElement(sourceTableName, 1055 knownTables, schema); 1056 ColumnElement ce = getColumnElement(sourceTableEl, 1057 DBIdentifier.create(sourceColumnName), helper); 1058 ce.toString(); 1059 1060 if (j == 0) { 1067 if (tablesOfBean.contains(sourceTableName)) { 1068 localSet = true; 1069 1070 primaryTableColumns.add(ce); 1072 } 1073 else { 1074 1075 isJoin = true; 1077 localSet = false; 1078 } 1079 } 1080 1081 if (cpe.getLocalColumn() == null) { 1082 cpe.setLocalColumn(ce); 1083 } 1084 else { 1085 cpe.setReferencedColumn(ce); 1086 } 1087 } 1088 1089 if (localSet) { 1090 if (!isJoin) { 1091 mre.addColumn(cpe); 1092 } 1093 else { 1094 mre.addLocalColumn(cpe); 1095 } 1096 } 1097 else if (isJoin) { 1098 mre.addAssociatedColumn(cpe); 1099 } 1100 } 1101 1102 return primaryTableColumns; 1103 } 1104 1105 1132 private void setLowerBound(RelationshipElement rel, 1133 Collection primaryTableColumns, 1134 String primaryTableName, 1135 SchemaElement schema, 1136 Map knownTables, 1137 String beanName, 1138 String fieldName) 1139 throws ModelException, DBException, 1140 ConversionException { 1141 1142 rel.setLowerBound(0); 1143 if (logger.isLoggable(Logger.FINE)) 1144 logger.fine( 1145 I18NHelper.getMessage( 1146 messages, 1147 "MESSAGE_LWB_NULL", beanName, fieldName)); 1149 1150 if (1 == rel.getUpperBound() && null != primaryTableName) { 1151 1152 boolean isPartOfPrimaryKey = false; 1153 TableElement primaryTable = getTableElement(primaryTableName, 1154 knownTables, schema); 1155 UniqueKeyElement pk = primaryTable.getPrimaryKey(); 1156 ForeignKeyElement fks[] = primaryTable.getForeignKeys(); 1157 Iterator iter = primaryTableColumns.iterator(); 1158 1159 while (iter.hasNext() && 0 == rel.getLowerBound()) { 1160 ColumnElement ce = (ColumnElement) iter.next(); 1161 if (!ce.isNullable()) { 1162 isPartOfPrimaryKey |= isPartOfPrimaryKey(ce, pk); 1163 1164 if (!isPartOfPrimaryKey) { 1165 rel.setLowerBound(1); 1169 if (logger.isLoggable(Logger.FINE)) 1170 logger.fine( 1171 I18NHelper.getMessage( 1172 messages, 1173 "MESSAGE_LWB_NOPK", beanName, fieldName)); 1175 } 1176 else if (isPartOfForeignKey(ce, fks)) { 1178 rel.setLowerBound(1); 1181 if (logger.isLoggable(Logger.FINE)) 1182 logger.fine( 1183 I18NHelper.getMessage( 1184 messages, 1185 "MESSAGE_LWB_FK", beanName, fieldName)); 1187 } 1188 } 1189 } 1190 1191 if (0 == rel.getLowerBound() && isPartOfPrimaryKey) { 1192 if (primaryTableColumns.size() < pk.getColumns().length) { 1195 rel.setLowerBound(1); 1198 if (logger.isLoggable(Logger.FINE)) 1199 logger.fine( 1200 I18NHelper.getMessage( 1201 messages, 1202 "MESSAGE_LWB_PKSUBSET", beanName, fieldName)); 1204 } 1205 else if (isCascadeDelete(beanName, fieldName)) { 1206 rel.setLowerBound(1); 1208 if (logger.isLoggable(Logger.FINE)) 1209 logger.fine( 1210 I18NHelper.getMessage( 1211 messages, 1212 "MESSAGE_LWB_CASCADE", beanName, fieldName)); 1214 } 1215 else { 1216 if (logger.isLoggable(Logger.FINE)) 1217 logger.fine( 1218 I18NHelper.getMessage( 1219 messages, 1220 "MESSAGE_LWB_NODEPENDENT", beanName, fieldName)); 1222 } 1223 } 1224 } 1225 } 1226 1227 1241 private TableElement getTableElement(String tableName, 1242 Map knownTables, 1243 SchemaElement schema) 1244 throws DBException, ConversionException { 1245 1246 TableElement te = (TableElement) knownTables.get(tableName); 1247 1248 if (null == te) { 1249 te = getTableElement(schema, DBIdentifier.create(tableName), 1250 helper); 1251 knownTables.put(tableName, te); 1252 } 1253 1254 return te; 1255 } 1256 1257 1270 private boolean isPartOfPrimaryKey(ColumnElement ce, UniqueKeyElement pk) { 1271 return null != pk && ce.equals(pk.getColumn(ce.getName())); 1272 } 1273 1274 1287 private boolean isPartOfForeignKey(ColumnElement ce, 1288 ForeignKeyElement[] fks) { 1289 1290 1295 if (fks != null) { 1296 for (int index = 0; index < fks.length; index++) { 1297 if (ce.equals(fks[index].getColumn(ce.getName()))) { 1298 return true; 1300 } 1301 } 1302 } 1303 1304 return false; 1305 } 1306 1307 1317 private boolean isCascadeDelete(String beanName, String fieldName) { 1318 final String beanInField = helper.getRelationshipFieldContent(beanName, fieldName); 1319 final String inverseField = helper.getInverseFieldName(beanName, 1320 fieldName); 1321 1322 return (null != beanInField && null != inverseField) ? 1323 helper.relatedObjectsAreDeleted(beanInField, inverseField) : false; 1324 } 1325 1326 1341 private RelationshipElement registerInverseRelationshipElement( 1342 RelationshipElement rel, String beanName, String fieldName) 1343 throws ModelException { 1344 1345 String key = beanName + "." + fieldName; RelationshipElement inverse = (RelationshipElement) inverseRelationships.get(key); 1347 1348 if (null == inverse) { 1349 final String beanInField = helper.getRelationshipFieldContent( 1350 beanName, fieldName); 1351 final String inverseField = helper.getInverseFieldName(beanName, 1352 fieldName); 1353 1354 if (null != beanInField && null != inverseField) { 1355 key = beanInField + "." + inverseField; inverseRelationships.put(key, rel); 1357 } 1358 } 1359 else { 1360 rel.changeInverseRelationship(inverse); 1361 inverse.changeInverseRelationship(rel); 1362 } 1363 1364 return inverse; 1365 } 1366 1367 1379 private void setUpperBound(RelationshipElement rel, 1380 String beanName, String fieldName) 1381 throws ModelException, ConversionException { 1382 1383 String beanInField = helper.getRelationshipFieldContent(beanName, 1384 fieldName); 1385 String classInJdoField = helper.getMappedClassName(beanInField); 1386 String multiplicity = helper.getMultiplicity(beanName, fieldName); 1387 1388 if (multiplicity.equals(helper.MANY)) { 1390 rel.setUpperBound(Integer.MAX_VALUE); 1391 rel.setElementClass(classInJdoField); 1392 String collectionClass = helper.getRelationshipFieldType(beanName, 1393 fieldName); 1394 if (types.contains(collectionClass)) { 1395 rel.setCollectionClass(collectionClass); 1396 } 1397 else { 1398 rel.setCollectionClass(null); 1399 if (logger.isLoggable(Logger.WARNING)) 1400 logger.warning( 1401 I18NHelper.getMessage( 1402 messages, 1403 "WARN_INVALID_RELATIONSHIP_FIELDTYPE", beanName, fieldName, collectionClass)); 1405 } 1406 } 1407 else if (multiplicity.equals(helper.ONE)) { 1408 rel.setUpperBound(1); 1409 rel.setElementClass(classInJdoField); 1416 } 1417 else { 1418 throw new ConversionException( 1419 I18NHelper.getMessage( 1420 messages, 1421 "ERR_BAD_MULTIPLICTY", multiplicity, rel.getName())); 1423 } 1424 } 1425 1426 1439 private void setCascadeDeleteAction(RelationshipElement rel, 1440 String beanName, String fieldName) 1441 throws ModelException { 1442 1443 if (helper.relatedObjectsAreDeleted(beanName, fieldName)) { 1444 if (logger.isLoggable(Logger.FINE)) 1445 logger.fine( 1446 I18NHelper.getMessage( 1447 messages, 1448 "MESSAGE_REL_OBJ_DEL", beanName, fieldName)); 1450 rel.setDeleteAction(RelationshipElement.CASCADE_ACTION); 1451 } 1452 } 1453 1454 private SchemaElement setDatabaseRoot(MappingClassElement foo, 1455 String schemaElementValue, boolean strict) 1456 throws ModelException, DBException, ConversionException { 1457 SchemaElement bar = null; 1458 if (null != classLoader) { 1459 if (loadedSchema.get(schemaElementValue) == null) { 1460 SchemaElement.removeFromCache(schemaElementValue); 1461 loadedSchema.put(schemaElementValue, schemaElementValue); 1462 } 1463 bar = SchemaElement.forName(schemaElementValue,classLoader); 1464 } 1465 else 1466 bar = SchemaElement.forName(schemaElementValue); 1467 if (strict) { 1468 if (bar == null) { 1469 throw new ConversionException( 1471 I18NHelper.getMessage( 1472 messages, 1473 "ERR_CANNOT_FIND_SCHEMA", new Object [] {schemaElementValue, classLoader})); 1475 } 1476 } 1477 else { 1478 if (null == bar) { 1479 bar = new SchemaElement(); 1484 DBIdentifier n = DBIdentifier.create(schemaElementValue); 1485 n.setFullName(schemaElementValue); 1486 bar.setName(n); 1487 } 1488 } 1489 foo.setDatabaseRoot(bar); 1490 return bar; 1491 } 1492 1493 private String getTableName(String columnName, String defaultName) { 1494 String retVal = defaultName; 1495 int len = columnName.lastIndexOf('.'); 1496 if (len > 0) 1497 retVal = columnName.substring(0,len); 1498 return retVal; 1499 } 1500 1501 private String getColumnName(String columnName) { 1502 String retVal = columnName; 1503 int len = columnName.lastIndexOf('.'); 1504 if (len > 0) 1505 retVal = columnName.substring(len+1); 1506 return retVal; 1507 } 1508 1509 1522 private void mapSecondaryTables(EntityMapping mapping, 1523 MappingClassElement mce, 1524 SchemaElement schema, 1525 Map knownTables, 1526 List tablesOfBean) 1527 throws ModelException, DBException, ConversionException { 1528 1529 SecondaryTable [] tableList = mapping.getSecondaryTable(); 1530 List tl = mce.getTables(); 1531 1532 if (null != tl && tl.size() > 0 && null != tl.get(0)) { 1533 MappingTableElement primary = (MappingTableElement) tl.get(0); 1534 1535 for (int i = 0; null != tableList && i < tableList.length; i++) { 1536 String tn = tableList[i].getTableName(); 1537 if (StringHelper.isEmpty(tn)) 1538 continue; 1539 TableElement te = getTableElement(schema, 1540 DBIdentifier.create(tn.trim()), helper); 1541 ColumnPair pairs[] = tableList[i].getColumnPair(); 1542 int len = 0; 1543 if (null != pairs) 1544 len = pairs.length; 1545 if (0 == len) { 1546 if (logger.isLoggable(Logger.WARNING)) 1547 logger.warning( 1548 I18NHelper.getMessage( 1549 messages, 1550 "WARN_NO_PAIRS", new Object [] {mce, tn})); 1552 continue; 1553 } 1554 MappingReferenceKeyElement mrke = mce.addSecondaryTable( 1555 primary,te); 1556 for (int j = 0; null != pairs && j < pairs.length; j++) { 1557 ColumnPairElement cpe = new ColumnPairElement(); 1558 DBIdentifier dbId = DBIdentifier.create("SecondaryTable"+j); cpe.setName(dbId); 1560 ColumnPair pair = pairs[j]; 1561 for (int k = 0; k < 2; k++) { 1562 String nameOne = pair.getColumnName(k); 1563 String sourceTableName = getTableName( 1564 nameOne.trim(), 1565 primary.getName().toString()); 1566 String sourceColumnName = getColumnName(nameOne); 1567 dbId = DBIdentifier.create(sourceTableName); 1568 TableElement sourceTableEl = getTableElement(schema, 1569 dbId, helper); 1570 dbId = DBIdentifier.create(sourceColumnName); 1571 ColumnElement ce = getColumnElement(sourceTableEl, 1572 dbId, helper); 1573 if (k == 0) 1574 cpe.setLocalColumn(ce); 1575 else 1576 cpe.setReferencedColumn(ce); 1577 } 1578 mrke.addColumnPair(cpe); 1579 } 1580 knownTables.put(tn,te); 1581 tablesOfBean.add(tn); 1582 } 1583 } 1584 else 1585 throw new ConversionException( 1586 I18NHelper.getMessage( 1587 messages, 1588 "WARN_NOT_MAPPED_TO_PRIMARY", mce.getName())); 1590 } 1591 1592 private boolean validateField(MappingClassElement mce, String beanName, 1593 String fieldName, boolean throwEx) throws ConversionException { 1594 MappingFieldElement mfe = mce.getField(fieldName); 1595 if (null != mfe) { 1596 if (throwEx) 1597 throw new ConversionException( 1598 I18NHelper.getMessage( 1599 messages, 1600 "ERR_FIELD_MAPPED_TWICE", beanName, fieldName)); 1602 else 1603 return false; 1604 } 1605 if (!helper.hasField(beanName,fieldName)) { 1606 if (throwEx) 1607 throw new ConversionException( 1608 I18NHelper.getMessage( 1609 messages, 1610 "ERR_INVALID_FIELD", beanName, fieldName)); 1612 else 1613 return false; 1614 } 1615 return true; 1616 } 1617 1618 private Map getBean2EntityMappingMap(SunCmpMapping beanSet) { 1620 Map retVal = new HashMap (); 1621 EntityMapping [] entityMappingsInSet = beanSet.getEntityMapping(); 1622 int len = 0; 1623 if (null != entityMappingsInSet) 1624 len = entityMappingsInSet.length; 1625 for (int k = 0; k < len; k++) { 1626 EntityMapping anEntityMapping = entityMappingsInSet[k]; 1627 String beanName = anEntityMapping.getEjbName(); 1628 beanName.trim().charAt(0); 1629 retVal.put(beanName,anEntityMapping); 1630 } 1631 return retVal; 1632 } 1633 1634 private boolean completeCmrMappings(SunCmpMapping beanSet) 1638 throws ConversionException { 1639 1640 Map beanName2EntityMapping = getBean2EntityMappingMap(beanSet); 1642 Iterator emIter = beanName2EntityMapping.values().iterator(); 1643 boolean retVal = false; 1644 String errorMsg = I18NHelper.getMessage( 1645 messages, 1646 "ERR_BAD_CONVERSION_HELPER"); 1648 while (emIter.hasNext()) { 1649 EntityMapping anEM = (EntityMapping) emIter.next(); 1650 String beanName = anEM.getEjbName(); 1651 String pt = anEM.getTableName(); 1652 CmrFieldMapping[] cmrsInEM = anEM.getCmrFieldMapping(); 1653 int len = 0; 1654 if (null != cmrsInEM && !StringHelper.isEmpty(beanName)) 1655 len = cmrsInEM.length; 1656 for (int i = 0; i < len; i++) { 1657 String fieldName = cmrsInEM[i].getCmrFieldName(); 1658 if (!helper.hasField(beanName, fieldName)) { 1659 throw new ConversionException(I18NHelper.getMessage( 1660 messages, 1661 "WARN_INVALID_CMRFIELD", beanName, fieldName)); 1663 } 1664 fieldName.trim().charAt(0); 1665 String otherField = helper.getInverseFieldName(beanName, 1666 fieldName); 1667 if (otherField == null) { 1668 throw new ConversionException(errorMsg); 1669 } 1670 String otherBean = helper.getRelationshipFieldContent(beanName, 1671 fieldName); 1672 if (otherBean == null) { 1673 throw new ConversionException(errorMsg); 1674 } 1675 1676 if (helper.isGeneratedRelationship(otherBean,otherField)) { 1677 retVal = true; 1678 String otherBeanName = helper.getRelationshipFieldContent( 1679 beanName, fieldName); 1680 otherBeanName.trim().charAt(0); 1681 EntityMapping otherEM = 1682 (EntityMapping) beanName2EntityMapping.get( 1683 otherBeanName); 1684 CmrFieldMapping inverseMapping = new CmrFieldMapping(); 1685 inverseMapping.setCmrFieldName(otherField); 1686 inverseMapping.setColumnPair( 1687 reverseCPArray(cmrsInEM[i].getColumnPair(), pt, 1688 beanName, fieldName)); 1689 otherEM.addCmrFieldMapping(inverseMapping); 1690 } 1691 } 1692 } 1693 return retVal; 1694 } 1695 1696 private ColumnPair[] reverseCPArray(ColumnPair[] cpa, String primeTable, 1697 String beanName, String fieldName) 1698 throws ConversionException { 1699 int len = (cpa == null) ? 0 : cpa.length; 1700 if (len == 0) { 1701 throw new ConversionException( 1702 I18NHelper.getMessage( 1703 messages, 1704 "ERR_COLUMN_PAIR_MISSING", beanName, fieldName)); 1706 } 1707 ColumnPair [] retVal = new ColumnPair[len]; 1708 for (int index = 0; index < len; index++) { 1709 retVal[index] = new ColumnPair(); 1710 retVal[index].addColumnName( 1711 qualify(primeTable,cpa[index].getColumnName(1))); 1712 retVal[index].addColumnName( 1713 qualify(primeTable,cpa[index].getColumnName(0))); 1714 } 1715 return retVal; 1716 } 1717 1718 private String qualify(String tn, String cn) { 1719 int tmp = cn.indexOf('.'); 1720 String retVal = cn; 1721 if (-1 == tmp) 1722 retVal = tn + "." + cn; return retVal; 1724 } 1725 1726 private TableElement getTableElement(SchemaElement schema, 1727 DBIdentifier dbId, ConversionHelper helper) 1728 throws DBException, ConversionException { 1729 1730 TableElement retVal = ((schema != null) ? 1731 schema.getTable(dbId) : null); 1732 1733 if (null == retVal && !helper.ensureValidation()) { 1734 1740 retVal = new TableElement(); 1741 retVal.setName(dbId); 1742 retVal.setDeclaringSchema(schema); 1743 org.netbeans.modules.dbschema.UniqueKeyElement tkey = 1744 new org.netbeans.modules.dbschema.UniqueKeyElement(); 1745 ColumnElement fakeKeyCol = new ColumnElement(); 1746 fakeKeyCol.setName(DBIdentifier.create(retVal.getName().getName()+ "."+"fookeyng")); 1748 fakeKeyCol.setType(2); 1750 fakeKeyCol.setPrecision(new Integer (MINIMUM_PRECISION)); 1751 tkey.setPrimaryKey(true); 1752 tkey.addColumn(fakeKeyCol); 1753 retVal.addColumn(fakeKeyCol); 1754 retVal.addKey(tkey); 1755 } 1756 if (retVal == null) { 1757 throw new ConversionException( 1758 I18NHelper.getMessage( 1759 messages, 1760 "ERR_INVALID_TABLE", new Object [] {dbId.getName(), schema})); 1762 } 1763 return retVal; 1764 } 1765 1766 private ColumnElement getColumnElement(TableElement sourceTableEl, 1767 DBIdentifier sourceColumnName, ConversionHelper helper) 1768 throws DBException, ConversionException { 1769 1770 ColumnElement aCol = sourceTableEl.getColumn(sourceColumnName); 1771 if (null == aCol && !helper.ensureValidation()) { 1772 aCol = new ColumnElement(); 1773 aCol.setName(DBIdentifier.create(sourceTableEl.getName().toString()+"."+sourceColumnName.toString())); aCol.setDeclaringTable(sourceTableEl); 1775 aCol.setNullable(true); 1776 } 1777 if (aCol == null) { 1778 throw new ConversionException( 1779 I18NHelper.getMessage( 1780 messages, 1781 "ERR_INVALID_COLUMN", new Object [] {sourceColumnName, sourceTableEl})); 1783 } 1784 return aCol; 1785 } 1786 1787 private MappingFieldElement createUnmappedField(MappingClassElement mce, 1788 String beanName, String fieldName) 1789 throws ModelException { 1790 1791 PersistenceClassElement pce = ((MappingClassElementImpl)mce). 1792 getPersistenceElement(); 1793 PersistenceFieldElementImpl pfei = 1794 new PersistenceFieldElementImpl(fieldName); 1795 PersistenceFieldElement pfe = 1796 new PersistenceFieldElement(pfei, pce); 1797 1798 pfe.setKey(helper.isKey(beanName,fieldName,false)); 1799 pce.addField(pfe); 1800 MappingFieldElement mfe = new MappingFieldElementImpl(fieldName, mce); 1801 return mfe; 1802 } 1803 1804 1818 private void setFetchGroup(FetchedWith fw, MappingFieldElement mfe, 1819 String beanName, boolean fieldMappedToABlob) 1820 throws ModelException, ConversionException { 1821 if (null != fw) { 1822 boolean tryLevel = false; 1823 int level = 0; 1824 try { 1825 level = fw.getLevel(); 1826 tryLevel = true; 1827 } 1828 catch (RuntimeException e) { 1829 } 1835 if (tryLevel) { 1836 if (level < 1) { 1837 throw new ConversionException( 1838 I18NHelper.getMessage( 1839 messages, 1840 "ERR_INVALID_FG_LEVEL", beanName, mfe.getName(), ""+level)); } 1843 mfe.setFetchGroup(level+1); 1844 } 1845 String ig = fw.getNamedGroup(); 1846 if (null != ig) { 1847 Integer fgval = (Integer ) namedGroups.get(ig); 1848 if (null == fgval) { 1849 fgval = new Integer (groupCount--); 1850 namedGroups.put(ig,fgval); 1851 } 1852 mfe.setFetchGroup(fgval.intValue()); 1853 } 1854 if (fw.isNone()) 1855 mfe.setFetchGroup(MappingFieldElement.GROUP_NONE); 1856 if (fw.isDefault()) 1857 mfe.setFetchGroup(MappingFieldElement.GROUP_DEFAULT); 1858 } 1859 else { 1860 if (mfe instanceof MappingRelationshipElement) 1861 mfe.setFetchGroup(MappingFieldElement.GROUP_NONE); 1862 else { 1863 if (fieldMappedToABlob) 1864 mfe.setFetchGroup(MappingFieldElement.GROUP_NONE); 1865 else 1866 mfe.setFetchGroup(MappingFieldElement.GROUP_DEFAULT); 1867 } 1868 } 1869 } 1870 1871 private PersistenceFieldElement createPersistenceField( 1872 MappingClassElement mce, String fieldName) throws ModelException { 1873 PersistenceClassElement pce = 1874 ((MappingClassElementImpl)mce).getPersistenceElement(); 1875 PersistenceFieldElementImpl pfei = 1876 new PersistenceFieldElementImpl(fieldName); 1877 PersistenceFieldElement pfe = 1878 new PersistenceFieldElement(pfei, pce); 1879 pce.addField(pfe); 1880 return pfe; 1881 } 1882 1883 private MappingFieldElement createMappingField(MappingClassElement mce, 1884 String fieldName, ColumnElement col) throws ModelException { 1885 MappingFieldElement mfe = 1886 new MappingFieldElementImpl(fieldName, mce); 1887 mce.addField(mfe); 1888 if (col != null) 1889 mfe.addColumn(col); 1890 return mfe; 1891 } 1892} 1893 1894 | Popular Tags |