Skip to content

Commit

Permalink
Fix cycles in application (#95)
Browse files Browse the repository at this point in the history
* Removed cycle Schema change and Schema Evolution

* Solved cycle SchemaEvolution and ModelUtilities

* Updated naming of method and classes
  • Loading branch information
suarezgpablo authored Oct 17, 2024
1 parent 15a3dec commit 2011c5a
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package giis.modevo.model;

import java.io.File;
import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class DocumentReader {
public Document readDocumentGeneric (String path) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document doc = null;
// optional, but recommended
// process XML securely, avoid attacks like XML External Entities (XXE)
try {
dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
// parse XML file
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(new File(path));
// optional, but recommended
doc.getDocumentElement().normalize();
} catch (ParserConfigurationException | SAXException | IOException e) {
throw new DocumentException("A model could not be opened:" + e);
}
return doc;
}
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,13 @@
package giis.modevo.model;

import java.io.File;
import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import giis.modevo.model.schema.Column;
import giis.modevo.model.schema.Schema;
import giis.modevo.model.schema.Table;
import giis.modevo.model.schemaevolution.SchemaEvolution;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ModelUtilities {
public Document readDocumentGeneric (String path) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document doc = null;
// optional, but recommended
// process XML securely, avoid attacks like XML External Entities (XXE)
try {
dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
// parse XML file
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(new File(path));
// optional, but recommended
doc.getDocumentElement().normalize();
} catch (ParserConfigurationException | SAXException | IOException e) {
throw new DocumentException("A model could not be opened:" + e);
}
return doc;
}

/**
* Returns the table object from either the Schema or the Schema Evolution model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import giis.modevo.model.ModelUtilities;
import giis.modevo.model.DocumentReader;
import giis.modevo.model.schema.Column;
import giis.modevo.model.schema.Schema;
import giis.modevo.model.schema.Table;
Expand Down Expand Up @@ -48,8 +48,7 @@ public void addTable(MigrationTable t) {
*/
public DataMigration readDataMigrationModel(String dataMigrationPath, SchemaEvolution schemaEvolution, Schema schema) {
DataMigration dataMigration = new DataMigration();
ModelUtilities mu = new ModelUtilities();
Document doc = mu.readDocumentGeneric(dataMigrationPath);
Document doc = new DocumentReader().readDocumentGeneric(dataMigrationPath);
NodeList list = doc.getElementsByTagName("MigrationTable");
for (int temp = 0; temp < list.getLength(); temp++) {
Node node = list.item(temp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import giis.modevo.model.ModelUtilities;
import giis.modevo.model.DocumentReader;
import lombok.Getter;
import lombok.Setter;

Expand Down Expand Up @@ -48,8 +48,7 @@ public Table getTable(String name) {
*/
public Schema loadSchemaIntoApp(String fileSchema) {
Schema schema = new Schema(new ArrayList<>());
ModelUtilities mu = new ModelUtilities();
Document doc = mu.readDocumentGeneric(fileSchema);
Document doc = new DocumentReader().readDocumentGeneric(fileSchema);
NodeList list = doc.getElementsByTagName("Table");
for (int temp = 0; temp < list.getLength(); temp++) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package giis.modevo.model.schemaevolution;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

Expand All @@ -22,7 +25,8 @@ public AddColumn(Column c, Table t) {
}

@Override
protected void storeInfo(SchemaEvolution se, NodeList list, Element element) {
protected List<SchemaChange> changesSchemaModel (NodeList list, Element element) {
List<SchemaChange> changes = new ArrayList<>();
String idTable = element.getAttribute("tab");
Element table = getElementById(list, idTable);
if (table == null) {
Expand All @@ -39,8 +43,9 @@ protected void storeInfo(SchemaEvolution se, NodeList list, Element element) {
Column columnObject = super.columnFromModelToObject(column, t);
t.getColumns().add(columnObject);
AddColumn ac = new AddColumn(columnObject, t);
se.getChanges().add(ac);
changes.add(ac);
}
return changes;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package giis.modevo.model.schemaevolution;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
Expand All @@ -21,7 +24,8 @@ public AddTable(Table t) {
}

@Override
protected void storeInfo(SchemaEvolution se, NodeList list, Element element) {
protected List<SchemaChange> changesSchemaModel (NodeList list, Element element) {
List<SchemaChange> changes = new ArrayList<>();
String idTable = element.getAttribute("tab");
Element table = getElementById(list, idTable);
Node nodeTable = getNodeById(list, idTable);
Expand All @@ -33,7 +37,9 @@ protected void storeInfo(SchemaEvolution se, NodeList list, Element element) {
NodeList listColumns = nodeTable.getChildNodes(); // columns of the new table nodes
readColumnsTable(t, listColumns);
AddTable at = new AddTable(t);
se.getChanges().add(at);
changes.add(at);
return changes;

}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package giis.modevo.model.schemaevolution;


import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
Expand All @@ -26,7 +29,8 @@ public CopyTable(Table t, Table t2) {
}

@Override
protected void storeInfo(SchemaEvolution se, NodeList list, Node node) {
protected List<SchemaChange> changesSchemaModel (NodeList list, Node node) {
List<SchemaChange> changes = new ArrayList<>();
Element elementCopy = (Element) node;
String tableSource = elementCopy.getAttribute(TABLE_SOURCE);
String idCopiedTable = elementCopy.getAttribute("copiedTable");
Expand All @@ -41,7 +45,8 @@ protected void storeInfo(SchemaEvolution se, NodeList list, Node node) {
NodeList listColumns = nodeTable.getChildNodes(); // columns of the new table nodes
readColumnsTable(t, listColumns);
CopyTable ct = new CopyTable(source, t);
se.getChanges().add(ct);
changes.add(ct);
return changes;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public JoinColumn(Column c, Table t, String criteria) {
}

@Override
protected void storeInfo(SchemaEvolution se, NodeList list, Node node) {
protected List<SchemaChange> changesSchemaModel (NodeList list, Node node) {
List<SchemaChange> changes = new ArrayList<>();
Element elementCopy = (Element) node;
String table = elementCopy.getAttribute(TABLE);
String idTargetColumn = elementCopy.getAttribute("targetColumn");
Expand All @@ -53,7 +54,8 @@ protected void storeInfo(SchemaEvolution se, NodeList list, Node node) {
Column columnSource = columnFromModelToObject(column, source);
jt.getSourceColumns().add(columnSource);
}
se.getChanges().add(jt);
changes.add(jt);
return changes;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package giis.modevo.model.schemaevolution;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

Expand All @@ -26,14 +29,16 @@ public JoinTable(Table originalTable, Table newTable) {
}

@Override
protected void storeInfo(SchemaEvolution se, Node node) {
protected List<SchemaChange> changesSchemaModel (Node node) {
List<SchemaChange> changes = new ArrayList<>();
Element elementSplit = (Element) node;
String source = elementSplit.getAttribute(TABLE_SOURCE);
String target = elementSplit.getAttribute("tableTarget");
Table tableSource = new Table(source);
Table tableTarget = new Table(target);
JoinTable jt = new JoinTable(tableSource, tableTarget);
se.getChanges().add(jt);
changes.add(jt);
return changes;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package giis.modevo.model.schemaevolution;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

Expand Down Expand Up @@ -28,15 +31,17 @@ public RemovePK(Column c, Table t, String namePreviousTable) {
}

@Override
protected void storeInfo(SchemaEvolution se, Node node) {
protected List<SchemaChange> changesSchemaModel (Node node) {
List<SchemaChange> changes = new ArrayList<>();
Element elementCopy = (Element) node;
String table = elementCopy.getAttribute(TABLE);
String columnRemoved = elementCopy.getAttribute("columnRemoved");
String previousTable = elementCopy.getAttribute("previous");
Column c = new Column(columnRemoved);
Table t = new Table(table);
RemovePK rp = new RemovePK(c, t, previousTable);
se.getChanges().add(rp);
changes.add(rp);
return changes;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package giis.modevo.model.schemaevolution;

import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
Expand Down Expand Up @@ -98,15 +100,14 @@ private boolean isKey(String key) {
return key != null && key.equalsIgnoreCase("true");
}

protected void storeInfo (SchemaEvolution se, NodeList list, Node node) {
protected List<SchemaChange> changesSchemaModel (NodeList list, Node node) {
throw new UnsupportedOperationException (ERROR_STOREINFO);
}
protected void storeInfo(SchemaEvolution se, NodeList list, Element element) {
protected List<SchemaChange> changesSchemaModel(NodeList list, Element element) {
throw new UnsupportedOperationException (ERROR_STOREINFO);
}
protected void storeInfo (SchemaEvolution se, Node node) {
protected List<SchemaChange> changesSchemaModel (Node node) {
throw new UnsupportedOperationException (ERROR_STOREINFO);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import giis.modevo.model.ModelUtilities;
import giis.modevo.model.DocumentReader;
import giis.modevo.model.schema.Table;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -60,8 +60,7 @@ public Table getTable(String name) {
*/
public SchemaEvolution readSchemaEvolutionModel(String pathSchemaEvolutionModel) {
SchemaEvolution se = new SchemaEvolution();
ModelUtilities mu = new ModelUtilities();
Document doc = mu.readDocumentGeneric(pathSchemaEvolutionModel);
Document doc = new DocumentReader().readDocumentGeneric(pathSchemaEvolutionModel);
NodeList xmi = doc.getElementsByTagName("xmi:XMI");
Node xmiNode = xmi.item(0); // There is only just one
NodeList list = xmiNode.getChildNodes();
Expand All @@ -74,28 +73,28 @@ public SchemaEvolution readSchemaEvolutionModel(String pathSchemaEvolutionModel)
String nameElement = element.getNodeName();
if (nameElement.equalsIgnoreCase("Add")) { // Add column
log.info ("New Add Columns schema modification");
new AddColumn().storeInfo(se, list, element);
se.getChanges().addAll(new AddColumn().changesSchemaModel(list, element));
} else if (nameElement.equalsIgnoreCase("AddTable")) {
log.info ("New Add Table schema modification");
new AddTable().storeInfo(se, list, element);
se.getChanges().addAll(new AddTable().changesSchemaModel(list, element));
} else if (nameElement.equalsIgnoreCase("SplitColumn")) {
log.info ("New Split Column schema modification");
new SplitColumn().storeInfo(se, list, node);
se.getChanges().addAll(new SplitColumn().changesSchemaModel(list, node));
} else if (nameElement.equalsIgnoreCase("JoinTable")) {
log.info ("New Join Table schema modification");
new JoinTable().storeInfo(se, node);
se.getChanges().addAll(new JoinTable().changesSchemaModel(node));
} else if (nameElement.equalsIgnoreCase("CopyTable")) {
log.info ("New Copy Table schema modification");
new CopyTable().storeInfo(se, list, node);
se.getChanges().addAll(new CopyTable().changesSchemaModel(list, node));
} else if (nameElement.equalsIgnoreCase("SplitTable")) {
log.info ("New Split Table schema modification");
new SplitTable().storeInfo(se, list, node);
se.getChanges().addAll(new SplitTable().changesSchemaModel(list, node));
} else if (nameElement.equalsIgnoreCase("JoinColumn")) {
log.info ("New Join Column schema modification");
new JoinColumn().storeInfo(se, list, node);
se.getChanges().addAll(new JoinColumn().changesSchemaModel(list, node));
} else if (nameElement.equalsIgnoreCase("RemovePK")) {
log.info ("New Remove PK schema modification");
new RemovePK().storeInfo(se, node);
se.getChanges().addAll(new RemovePK().changesSchemaModel(node));
}
}
return se;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public SplitColumn(Column c, Table t, List<Column> rs, List<CriteriaSplit> cs) {
}

@Override
protected void storeInfo(SchemaEvolution se, NodeList list, Node node) {
protected List<SchemaChange> changesSchemaModel (NodeList list, Node node) {
List<SchemaChange> changes = new ArrayList<>();
Element elementSplit = (Element) node;
String nameTable = elementSplit.getAttribute(TABLE);
String oldColumnModel = elementSplit.getAttribute("oldColumn");
Expand Down Expand Up @@ -74,7 +75,8 @@ protected void storeInfo(SchemaEvolution se, NodeList list, Node node) {
CriteriaSplit criteriaObject = criteriaFromModelToObject(criteriaElement, columnObject);
splitChange.getCs().add(criteriaObject);
}
se.getChanges().add(splitChange);
changes.add(splitChange);
return changes;
}

private CriteriaSplit criteriaFromModelToObject(Element criteria, Column column) {
Expand Down
Loading

0 comments on commit 2011c5a

Please sign in to comment.