Changeset 7999 for mauRepo/xmi2sqlalchemy
- Timestamp:
- 01/12/11 23:27:32 (9 years ago)
- Location:
- mauRepo/xmi2sqlalchemy/trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
mauRepo/xmi2sqlalchemy/trunk/pom.xml
r7998 r7999 23 23 24 24 <build> 25 <plugins>26 <plugin>27 <groupId>org.apache.maven.plugins</groupId>28 <artifactId>maven-compiler-plugin</artifactId>29 <configuration>30 <source>1.6</source>31 <target>1.6</target>32 </configuration>33 </plugin>34 </plugins>35 25 <resources> 36 26 <resource> -
mauRepo/xmi2sqlalchemy/trunk/src/main/java/ndg/services/newmoon/velocity/python/GenerateClassesClosure.java
r7998 r7999 38 38 import java.util.ArrayList; 39 39 import java.util.List; 40 import java.util.Map;41 import java.util.Set;42 40 43 41 import ndg.services.newmoon.NewmoonException; … … 45 43 import ndg.services.newmoon.velocity.python.support.ASEnumeration; 46 44 import ndg.services.newmoon.velocity.python.support.ASUnion; 47 import ndg.services.newmoon.velocity.python.support.AttributeModel;48 45 import ndg.services.newmoon.velocity.python.support.ClassModel; 49 import ndg.services.newmoon.velocity.python.support.ImportCollector;50 46 import ndg.services.newmoon.velocity.python.support.ClassModel.STEREOTYPE; 51 47 import ndg.services.newmoon.xmiModel.UMLClass; -
mauRepo/xmi2sqlalchemy/trunk/src/main/java/ndg/services/newmoon/velocity/python/InnerGenerateTablesClosure.java
r7998 r7999 101 101 private List<NewmoonException> exceptions = new ArrayList<NewmoonException>(); 102 102 103 private final Logger logger = LoggerFactory.getLogger(InnerGenerateTablesClosure.class); 103 private final Logger logger = LoggerFactory 104 .getLogger(InnerGenerateTablesClosure.class); 104 105 105 106 /** … … 141 142 142 143 private ClassModel getClassModel(UMLClassDataType umlClass) { 143 return UMLElementsArchive.getInstance().getClassModelByUMLClass(umlClass); 144 } 145 146 private TableModel processUMLclass(ClassModel classModel) throws NewmoonException, IOException { 144 return UMLElementsArchive.getInstance().getClassModelByUMLClass( 145 umlClass); 146 } 147 148 private TableModel processUMLclass(ClassModel classModel) 149 throws NewmoonException, IOException { 147 150 if (tableModelExists(classModel)) 148 151 return getTableModel(classModel); … … 154 157 } 155 158 156 private void collectData(TableModel tm) throws NewmoonException, IOException { 159 private void collectData(TableModel tm) throws NewmoonException, 160 IOException { 157 161 updateImportToSQLTables(tm.getAssociatedClass()); 158 162 parseAttributes(tm); … … 161 165 } 162 166 163 private void parseAttributes(TableModel parent) throws NewmoonException, IOException { 167 private void parseAttributes(TableModel parent) throws NewmoonException, 168 IOException { 164 169 AttributeModel am = null; 165 Iterator<AttributeModel> iter = getClassModel(parent.getAssociatedClass()).getAttributeModel().iterator(); 170 Iterator<AttributeModel> iter = getClassModel( 171 parent.getAssociatedClass()).getAttributeModel().iterator(); 166 172 while (iter.hasNext()) { 167 173 am = iter.next(); 168 174 169 175 ClassModel cm = getClassModel(am.getAssociatedType()); 170 171 if (cm == null ) {176 177 if (cm == null || isToSkip(cm)) { 172 178 parent.getAttributeModel().add(am); 173 179 continue; 174 180 } 175 176 181 177 182 TableModel child = processUMLclass(cm); 178 183 /* … … 180 185 * column 181 186 */ 182 boolean nullable = am.getLowerBound().equals("0") || am.isVoidable(); 183 184 if (!child.isSkipIt() && !isMapped(am.getAssociatedType())) { 185 // adds relations between the child table and a notSQL-type 186 // attribute 187 Relationship rl = new Relationship(parent, child, am.getName(), nullable); 188 189 if (am.hasMultiplicity() && parent.equals(child)) { 190 rl.setRelationType(RELATION_TYPE.MANY_TO_MANY); 191 associationTable.add(new AssociationTable(rl)); 187 boolean nullable = am.getLowerBound().equals("0") 188 || am.isVoidable(); 189 190 Relationship rl = new Relationship(parent, child, am.getName(), 191 nullable); 192 193 if (am.hasMultiplicity() && parent.equals(child)) { 194 rl.setRelationType(RELATION_TYPE.MANY_TO_MANY); 195 associationTable.add(new AssociationTable(rl)); 196 } 197 198 if (!parent.equals(child)) { 199 ForeignKey fk = new ForeignKey(parent, am.getName()); 200 child.getFk().add(fk); 201 if (am.hasMultiplicity()) { 202 rl.setRelationType(RELATION_TYPE.ONE_TO_MANY); 203 } else { 204 rl.setRelationType(RELATION_TYPE.ONE_TO_ONE); 192 205 } 193 194 if (!parent.equals(child)) { 195 ForeignKey fk = new ForeignKey(parent, am.getName()); 196 child.getFk().add(fk); 197 if (am.hasMultiplicity()) { 198 rl.setRelationType(RELATION_TYPE.ONE_TO_MANY); 199 } else { 200 rl.setRelationType(RELATION_TYPE.ONE_TO_ONE); 201 } 202 parent.getRelationship().add(rl); 203 } 204 } else { 205 parent.getAttributeModel().add(am); 206 } 207 } 206 parent.getRelationship().add(rl); 207 } 208 209 } 210 } 211 212 private boolean isToSkip(ClassModel classModel) { 213 return (classModel.isCodeList() || classModel.isEnumeration() || isMapped(classModel 214 .getAssociatedClass())); 208 215 } 209 216 210 217 private boolean tableModelExists(ClassModel classModel) { 211 return tableModels.contains(new TableModel(classModel.getAssociatedClass())); 212 } 213 218 return tableModels.contains(new TableModel(classModel 219 .getAssociatedClass())); 220 } 221 214 222 private TableModel getTableModel(ClassModel classModel) { 215 223 TableModel tm = new TableModel(classModel.getAssociatedClass()); … … 223 231 } 224 232 225 private void collectInheritedParents(TableModel tm) throws NewmoonException, IOException { 226 Iterator<ClassModel> iter = getClassModel(tm.getAssociatedClass()).getParents().iterator(); 233 private void collectInheritedParents(TableModel tm) 234 throws NewmoonException, IOException { 235 Iterator<ClassModel> iter = getClassModel(tm.getAssociatedClass()) 236 .getParents().iterator(); 227 237 while (iter.hasNext()) { 228 CollectionUtils.addIgnoreNull(tm.getInherited(), processUMLclass(iter.next())); 229 } 230 } 231 232 private <E> void applyTemplate(Collection<E> models, CONTEXT contextName, StrBuilder sb, String templateName) { 238 CollectionUtils.addIgnoreNull(tm.getInherited(), 239 processUMLclass(iter.next())); 240 } 241 } 242 243 private <E> void applyTemplate(Collection<E> models, CONTEXT contextName, 244 StrBuilder sb, String templateName) { 233 245 Iterator<E> iter = models.iterator(); 234 246 Object item = null; … … 240 252 try { 241 253 getVcontext().put(contextName.name(), item); 242 sb.append(NmVelocity.getInstance().fillTemplate(getVcontext(), templateName)); 254 sb.append(NmVelocity.getInstance().fillTemplate(getVcontext(), 255 templateName)); 243 256 } catch (Exception e) { 244 257 e.printStackTrace(); … … 253 266 List<TableModel> tbs = sortTableModels(); 254 267 // -- THINK BEFORE CHANGE THE ORDER!! --// 255 applyTemplate(tbs, CONTEXT.tableModel, sqlTablesStrBuffer, tableTemplateFile); 256 applyTemplate(notSortable, CONTEXT.tableModel, sqlTablesStrBuffer, tableTemplateFile); 257 258 applyTemplate(associationTable, CONTEXT.associationTable, sqlTablesStrBuffer, associationTemplateFile); 259 260 applyTemplate(tbs, CONTEXT.tableModel, sqlTablesStrBuffer, mapperTemplateFile); 268 applyTemplate(tbs, CONTEXT.tableModel, sqlTablesStrBuffer, 269 tableTemplateFile); 270 applyTemplate(notSortable, CONTEXT.tableModel, sqlTablesStrBuffer, 271 tableTemplateFile); 272 273 applyTemplate(associationTable, CONTEXT.associationTable, 274 sqlTablesStrBuffer, associationTemplateFile); 275 276 applyTemplate(tbs, CONTEXT.tableModel, sqlTablesStrBuffer, 277 mapperTemplateFile); 261 278 262 279 Set<TableModel> nsSimple = new HashSet<TableModel>(); … … 270 287 } 271 288 272 applyTemplate(nsSimple, CONTEXT.tableModel, sqlTablesStrBuffer, notSortableMapperTemplateFile_1); 273 applyTemplate(notSortable, CONTEXT.tableModel, sqlTablesStrBuffer, notSortableMapperTemplateFile_1); 274 applyTemplate(nsSimple, CONTEXT.tableModel, sqlTablesStrBuffer, notSortableMapperTemplateFile_2); 275 applyTemplate(notSortable, CONTEXT.tableModel, sqlTablesStrBuffer, notSortableMapperTemplateFile_2); 289 applyTemplate(nsSimple, CONTEXT.tableModel, sqlTablesStrBuffer, 290 notSortableMapperTemplateFile_1); 291 applyTemplate(notSortable, CONTEXT.tableModel, sqlTablesStrBuffer, 292 notSortableMapperTemplateFile_1); 293 applyTemplate(nsSimple, CONTEXT.tableModel, sqlTablesStrBuffer, 294 notSortableMapperTemplateFile_2); 295 applyTemplate(notSortable, CONTEXT.tableModel, sqlTablesStrBuffer, 296 notSortableMapperTemplateFile_2); 276 297 // -------------------------------------// 277 298 278 Writer sqlTablesWriter = new FileWriter(new File(getHelper().getBaseDir(), "sqlTables.py"), true); 299 Writer sqlTablesWriter = new FileWriter(new File(getHelper() 300 .getBaseDir(), "sqlTables.py"), true); 279 301 sqlTablesWriter 280 302 .append("from sqlalchemy import Table, Column, Integer, String, ForeignKey, Sequence, event, MetaData\n"); 281 sqlTablesWriter.append("from sqlalchemy.orm import relationship, backref, mapper, clear_mappers\n"); 282 sqlTablesWriter.append("from sqlalchemy.ext.declarative import declarative_base\n"); 283 sqlTablesWriter.append("from sqlalchemy.dialects.postgresql import ARRAY, TEXT, NUMERIC, BOOLEAN, DATE\n"); 303 sqlTablesWriter 304 .append("from sqlalchemy.orm import relationship, backref, mapper, clear_mappers\n"); 305 sqlTablesWriter 306 .append("from sqlalchemy.ext.declarative import declarative_base\n"); 307 sqlTablesWriter 308 .append("from sqlalchemy.dialects.postgresql import ARRAY, TEXT, NUMERIC, BOOLEAN, DATE\n"); 284 309 sqlTablesWriter.append("from sqlalchemy.types import DateTime\n"); 285 sqlTablesWriter.append("from sqlalchemy.orm.util import class_mapper\n"); 286 sqlTablesWriter.append("from sqlalchemy.schema import ForeignKeyConstraint\n\n"); 310 sqlTablesWriter 311 .append("from sqlalchemy.orm.util import class_mapper\n"); 312 sqlTablesWriter 313 .append("from sqlalchemy.schema import ForeignKeyConstraint\n\n"); 287 314 288 315 sqlTablesWriter.flush(); 289 316 290 317 // import 291 getVcontext().put(CONTEXT.imports.name(), importCollector.getImportMap()); 292 sqlTablesWriter.append(NmVelocity.getInstance().fillTemplate(getVcontext(), importTemplateFile)); 318 getVcontext().put(CONTEXT.imports.name(), 319 importCollector.getImportMap()); 320 sqlTablesWriter.append(NmVelocity.getInstance().fillTemplate( 321 getVcontext(), importTemplateFile)); 293 322 sqlTablesWriter.append("\n\n"); 294 323 sqlTablesWriter.append("clear_mappers()\n"); … … 337 366 ovm.clear(); 338 367 for (TableModel inherited : tb.getInherited()) { 339 for (AttributeModel inhAttribute : inherited.getAttributeModel()) { 368 for (AttributeModel inhAttribute : inherited 369 .getAttributeModel()) { 340 370 if (inhAttribute.getName().equals(am.getName())) { 341 371 ovm.add(tb); … … 413 443 private final Relationship relationship; 414 444 415 public LoopFinder(Relationship relationship, List<TableModel> oldPath, int maxPathLength) { 445 public LoopFinder(Relationship relationship, List<TableModel> oldPath, 446 int maxPathLength) { 416 447 super(); 417 448 this.relationship = relationship; … … 433 464 checkReciprocalReference(relationship); 434 465 } else { 435 Iterator<Relationship> iterator = relationship.getToTable().getRelationship().iterator(); 466 Iterator<Relationship> iterator = relationship.getToTable() 467 .getRelationship().iterator(); 436 468 while (iterator.hasNext()) { 437 469 Relationship item = iterator.next(); … … 469 501 private void checkReciprocalReference(Relationship rl) { 470 502 // if the table has a self-reference is ok 471 if (!rl.getFromTable().equals(rl.getToTable()) && !rl.isManyToMany()) { 472 if (associationTable.contains(new AssociationTable(rl.getFromTable(), rl.getToTable())) 473 || associationTable.contains(new AssociationTable(rl.getToTable(), rl.getFromTable()))) 503 if (!rl.getFromTable().equals(rl.getToTable()) 504 && !rl.isManyToMany()) { 505 if (associationTable.contains(new AssociationTable(rl 506 .getFromTable(), rl.getToTable())) 507 || associationTable.contains(new AssociationTable(rl 508 .getToTable(), rl.getFromTable()))) 474 509 return; 475 510 … … 480 515 } 481 516 482 private void updateRelationToManyToMany(TableModel child, Relationship onParent) { 483 Iterator<Relationship> onChildIterator = child.getRelationship().iterator(); 517 private void updateRelationToManyToMany(TableModel child, 518 Relationship onParent) { 519 Iterator<Relationship> onChildIterator = child.getRelationship() 520 .iterator(); 484 521 while (onChildIterator.hasNext()) { 485 522 Relationship onChild = onChildIterator.next(); … … 504 541 505 542 private void removeFKFromTables(Relationship onParent, Relationship onChild) { 506 removeFKFromTableModel(onParent.getFromTable(), onChild.getName(), onParent.getToTable()); 507 removeFKFromTableModel(onParent.getToTable(), onParent.getName(), onParent.getFromTable()); 508 } 509 510 private void useAlterInFKFromTableModel(Relationship onParent, Relationship onChild) { 511 useAlterInFKFromTableModel(onParent.getFromTable(), onChild.getName(), onParent.getToTable()); 512 useAlterInFKFromTableModel(onParent.getToTable(), onParent.getName(), onParent.getFromTable()); 513 } 514 515 private void removeFKFromTableModel(TableModel model, String attrName, TableModel fromModel) { 543 removeFKFromTableModel(onParent.getFromTable(), onChild.getName(), 544 onParent.getToTable()); 545 removeFKFromTableModel(onParent.getToTable(), onParent.getName(), 546 onParent.getFromTable()); 547 } 548 549 private void useAlterInFKFromTableModel(Relationship onParent, 550 Relationship onChild) { 551 useAlterInFKFromTableModel(onParent.getFromTable(), onChild.getName(), 552 onParent.getToTable()); 553 useAlterInFKFromTableModel(onParent.getToTable(), onParent.getName(), 554 onParent.getFromTable()); 555 } 556 557 private void removeFKFromTableModel(TableModel model, String attrName, 558 TableModel fromModel) { 516 559 Iterator<ForeignKey> fkchilditerator = model.getFk().iterator(); 517 560 while (fkchilditerator.hasNext()) { 518 561 ForeignKey cfk = fkchilditerator.next(); 519 if (cfk.getParentAttributeName().equals(attrName) && cfk.getTableModel().equals(fromModel)) { 562 if (cfk.getParentAttributeName().equals(attrName) 563 && cfk.getTableModel().equals(fromModel)) { 520 564 fkchilditerator.remove(); 521 565 return; … … 524 568 } 525 569 526 private void useAlterInFKFromTableModel(TableModel model, String attrName, TableModel fromModel) { 570 private void useAlterInFKFromTableModel(TableModel model, String attrName, 571 TableModel fromModel) { 527 572 Iterator<ForeignKey> fkchilditerator = model.getFk().iterator(); 528 573 while (fkchilditerator.hasNext()) { 529 574 ForeignKey cfk = fkchilditerator.next(); 530 if (cfk.getParentAttributeName().equals(attrName) && cfk.getTableModel().equals(fromModel)) { 575 if (cfk.getParentAttributeName().equals(attrName) 576 && cfk.getTableModel().equals(fromModel)) { 531 577 cfk.setUseAlter(true); 532 578 return; … … 535 581 } 536 582 537 private void updateRelationToManyToMany(TableModel child, TableModel onParent) { 583 private void updateRelationToManyToMany(TableModel child, 584 TableModel onParent) { 538 585 for (Relationship rl : child.getRelationship()) { 539 586 if (!rl.getToTable().equals(onParent)) … … 551 598 while (iter.hasNext()) { 552 599 tb = iter.next(); 553 if (tb.getRelationship().size() == 0 && tb.getInherited().size() == 0) { 600 if (tb.getRelationship().size() == 0 601 && tb.getInherited().size() == 0) { 554 602 finalTable.add(tb); 555 603 notSortable.remove(tb); … … 561 609 index = finalTable.size(); 562 610 if (logger.isDebugEnabled()) 563 logger.debug("finalTable.size() : tableModels.size() - " + String.valueOf(finalTable.size()) + ":" 611 logger.debug("finalTable.size() : tableModels.size() - " 612 + String.valueOf(finalTable.size()) + ":" 564 613 + String.valueOf(getTableModels().size())); 565 614 updateFinalTable(finalTable); … … 588 637 while (innerIter.hasNext()) { 589 638 innerRelation = innerIter.next(); 590 if (innerRelation.getFromTable().equals(innerRelation.getToTable())) 639 if (innerRelation.getFromTable().equals( 640 innerRelation.getToTable())) 591 641 continue; 592 642 add = finalTable.contains(innerRelation.getToTable()) && add; … … 625 675 return helper; 626 676 } 627 677 628 678 /** 629 * Verifies if the associated UMLClass is mapped by the {@link PropertyMap#XMI_TO_SQL_MAP} file. 679 * Verifies if the associated UMLClass is mapped by the 680 * {@link PropertyMap#XMI_TO_SQL_MAP} file. 681 * 630 682 * @return <code>true</code> if is mapped, <code>false</code> otherwise 631 683 **/ 632 684 public boolean isMapped(UMLClassDataType associatedClass) { 633 String typeName = StringUtils.defaultIfEmpty( 634 GenerateModuleHelper.getXMIClassToSQLMap().getProperty(associatedClass.getName()), ""); 685 String typeName = StringUtils.defaultIfEmpty(GenerateModuleHelper 686 .getXMIClassToSQLMap().getProperty(associatedClass.getName()), 687 ""); 635 688 return typeName.equals(associatedClass.getName()); 636 689 }
Note: See TracChangeset
for help on using the changeset viewer.