diff --git a/pom.xml b/pom.xml index 1dc8335244da53b1805240173692196e0c0e9685..676fae93bdff16d4163c1a1f3e1a41da87c68cd1 100644 --- a/pom.xml +++ b/pom.xml @@ -77,6 +77,12 @@ <version>1.18.24</version> <scope>compile</scope> </dependency> + <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + <version>2.5</version> + </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> @@ -84,5 +90,5 @@ <scope>test</scope> </dependency> </dependencies> - + </project> \ No newline at end of file diff --git a/src/main/java/com/ic/er/Attribute.java b/src/main/java/com/ic/er/Attribute.java index 83b70419a6f9d2f0114b47e7e0221e85c4c11df0..72f125e361a1bad2add5ddad1060083e4ccbf866 100644 --- a/src/main/java/com/ic/er/Attribute.java +++ b/src/main/java/com/ic/er/Attribute.java @@ -31,7 +31,7 @@ public class Attribute { private Date gmtModified; protected Attribute(Long ID, Long entityID, Long viewID, String name, DataType dataType, - int isPrimary, LayoutInfo layoutInfo, Date gmtCreate, Date gmtModified) { + int isPrimary, LayoutInfo layoutInfo, Double layoutX, Double layoutY, Date gmtCreate, Date gmtModified) { this.ID = ID; this.entityID = entityID; this.viewID = viewID; @@ -49,7 +49,7 @@ public class Attribute { } } if (this.layoutInfo == null) { - this.layoutInfo = new LayoutInfo(0L, this.ID, RelatedObjType.ATTRIBUTE, 0.0, 0.0, 0.0, 0.0); + this.layoutInfo = new LayoutInfo(0L, this.ID, RelatedObjType.ATTRIBUTE, layoutX, layoutY, 0.0, 0.0); } } diff --git a/src/main/java/com/ic/er/ER.java b/src/main/java/com/ic/er/ER.java index e75c2f1b49130abc122c9195ef0921891e8d9113..71e299163ff4b13c72ab0925e42ddf264a3852cd 100644 --- a/src/main/java/com/ic/er/ER.java +++ b/src/main/java/com/ic/er/ER.java @@ -9,13 +9,14 @@ import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.apache.log4j.BasicConfigurator; +import org.apache.commons.io.IOUtils; + import java.io.IOException; import java.io.InputStream; -import java.nio.charset.Charset; -import java.nio.file.Files; -import java.nio.file.Path; +import java.nio.charset.StandardCharsets; import java.sql.Connection; +import java.sql.SQLException; import java.sql.Statement; import java.util.*; @@ -29,7 +30,7 @@ public class ER { public static LayoutInfoMapper layoutInfoMapper; private static Map<Long, View> allViewsMap = new HashMap<>(); - public static void connectDB(boolean useDBLog) throws IOException { + public static void connectDB(boolean useDBLog) throws SQLException, IOException { InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is); @@ -42,14 +43,15 @@ public class ER { if (useDBLog) { BasicConfigurator.configure(); } + createTables(); useDB = true; } - public static void createTables() throws Exception { + private static void createTables() throws SQLException, IOException { Connection conn = sqlSession.getConnection(); Statement stmt = conn.createStatement(); - String content = Files.readString(Path.of("src/main/resources/sql/schema-v1.sql"), Charset.defaultCharset()); - stmt.execute(content); + String sql = new String(Resources.getResourceAsStream("schema-v1.sql").readAllBytes(), StandardCharsets.UTF_8); + stmt.execute(sql); } public static View createView(String name, String creator) { @@ -72,6 +74,14 @@ public class ER { } } + public static View queryViewByID(Long ID) { + if (ER.useDB) { + return View.queryByID(ID); + } else { + return allViewsMap.get(ID); + } + } + public static View loadFromJSON(String json) throws ERException { try { View view = new ObjectMapper().readValue(json, View.class); diff --git a/src/main/java/com/ic/er/Entity.java b/src/main/java/com/ic/er/Entity.java index 796cb17a55db234ed4d96991e6aa586034067a2c..d6bfaad751ccf75f34cd460633a1641f3eedb55c 100644 --- a/src/main/java/com/ic/er/Entity.java +++ b/src/main/java/com/ic/er/Entity.java @@ -26,7 +26,7 @@ public class Entity { @JsonIgnore private Date gmtModified; - protected Entity(Long ID, String name, Long viewID, List<Attribute> attributeList, LayoutInfo layoutInfo, Date gmtCreate, Date gmtModified) { + protected Entity(Long ID, String name, Long viewID, List<Attribute> attributeList, LayoutInfo layoutInfo, Double layoutX, Double layoutY, Date gmtCreate, Date gmtModified) { this.ID = ID; this.name = name; this.viewID = viewID; @@ -42,17 +42,20 @@ public class Entity { } } if (this.layoutInfo == null) { - this.layoutInfo = new LayoutInfo(0L, this.ID, RelatedObjType.ENTITY, 0.0, 0.0, 0.0, 0.0); + this.layoutInfo = new LayoutInfo(0L, this.ID, RelatedObjType.ENTITY, layoutX, layoutY, 0.0, 0.0); } } public Attribute addAttribute(String attributeName, DataType dataType, int isPrimary) { - Attribute attribute = new Attribute(0L, this.ID, this.viewID, attributeName, dataType, isPrimary, null, new Date(), new Date()); + Attribute attribute = new Attribute(0L, this.ID, this.viewID, attributeName, dataType, isPrimary, null, 0.0, 0.0, new Date(), new Date()); + this.attributeList.add(attribute); + return attribute; + } + + public Attribute addAttribute(String attributeName, DataType dataType, int isPrimary, Double layoutX, Double layoutY) { + Attribute attribute = new Attribute(0L, this.ID, this.viewID, attributeName, dataType, isPrimary, null, layoutX, layoutY, new Date(), new Date()); this.attributeList.add(attribute); - if (ER.useDB) { - this.updateInfo(null); - } return attribute; } @@ -60,7 +63,6 @@ public class Entity { this.attributeList.remove(attribute); if (ER.useDB) { attribute.deleteDB(); - this.updateInfo(null); } return false; } @@ -79,6 +81,9 @@ public class Entity { } protected void deleteDB() { + for (Attribute attribute : attributeList) { + attribute.deleteDB(); + } ER.entityMapper.deleteByID(this.ID); } diff --git a/src/main/java/com/ic/er/Trans.java b/src/main/java/com/ic/er/Trans.java index 3599dd62788537bd108b6a5f7197324885589fed..e32b31a3711acea40a3cbd9bed6f7948756c24be 100644 --- a/src/main/java/com/ic/er/Trans.java +++ b/src/main/java/com/ic/er/Trans.java @@ -24,7 +24,7 @@ public class Trans { LayoutInfo layoutInfo = LayoutInfo.queryByObjIDAndObjType(attributeDO.getID(), RelatedObjType.ATTRIBUTE); return new Attribute(attributeDO.getID(), attributeDO.getEntityID(), attributeDO.getViewID(), attributeDO.getName(), attributeDO.getDataType(), attributeDO.getIsPrimary(), - layoutInfo, attributeDO.getGmtCreate(), attributeDO.getGmtModified()); + layoutInfo, 0.0, 0.0, attributeDO.getGmtCreate(), attributeDO.getGmtModified()); } protected static List<Attribute> TransAttributeListFromDB(List<AttributeDO> doList) { @@ -38,7 +38,7 @@ public class Trans { protected static Entity TransformFromDB(EntityDO entityDO) { List<Attribute> attributeList = Attribute.queryByAttribute(new AttributeDO(entityDO.getID(), entityDO.getViewID())); LayoutInfo layoutInfo = LayoutInfo.queryByObjIDAndObjType(entityDO.getID(), RelatedObjType.ENTITY); - return new Entity(entityDO.getID(), entityDO.getName(), entityDO.getViewID(), attributeList, layoutInfo, + return new Entity(entityDO.getID(), entityDO.getName(), entityDO.getViewID(), attributeList, layoutInfo, null, null, entityDO.getGmtCreate(), entityDO.getGmtModified()); } diff --git a/src/main/java/com/ic/er/View.java b/src/main/java/com/ic/er/View.java index 0ceea2d75e27bd588f38298f85b510dee704e764..db287e6ca4e293e3b73ae6ae6c67486f27e0c3d8 100644 --- a/src/main/java/com/ic/er/View.java +++ b/src/main/java/com/ic/er/View.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.ic.er.Exception.ERException; +import com.ic.er.common.DataType; import com.ic.er.common.ViewDeserializer; import com.ic.er.entity.ViewDO; import com.ic.er.common.Cardinality; @@ -49,11 +50,14 @@ public class View { } public Entity addEntity(String entityName) { - Entity entity = new Entity(0L, entityName, this.ID, new ArrayList<>(), null, new Date(), new Date()); + Entity entity = new Entity(0L, entityName, this.ID, new ArrayList<>(), null, 0.0, 0.0, new Date(), new Date()); + this.entityList.add(entity); + return entity; + } + + public Entity addEntity(String entityName, Double layoutX, Double layoutY) { + Entity entity = new Entity(0L, entityName, this.ID, new ArrayList<>(), null, layoutX, layoutY, new Date(), new Date()); this.entityList.add(entity); - if (ER.useDB) { - this.updateInfo(null); - } return entity; } @@ -61,7 +65,6 @@ public class View { this.entityList.remove(entity); if (ER.useDB) { entity.deleteDB(); - this.updateInfo(null); } return false; } @@ -70,18 +73,14 @@ public class View { Cardinality firstCardinality, Cardinality secondCardinality) { Relationship relationship = new Relationship(0L, relationshipName, this.ID, firstEntity, secondEntity, firstCardinality, secondCardinality, null, new Date(), new Date()); - this.getRelationshipList().add(relationship); - if (ER.useDB) { - this.updateInfo(null); - } + this.relationshipList.add(relationship); return relationship; } public boolean deleteRelationship(Relationship relationship) { - this.getRelationshipList().remove(relationship); + this.relationshipList.remove(relationship); if (ER.useDB) { relationship.deleteDB(); - this.updateInfo(null); } return false; } @@ -99,10 +98,6 @@ public class View { } } - public static List<View> queryAll() { - return Trans.TransViewListFromDB(ER.viewMapper.selectAll()); - } - public String ToJSON() { ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); String json; @@ -114,6 +109,10 @@ public class View { return json; } + public static List<View> queryAll() { + return Trans.TransViewListFromDB(ER.viewMapper.selectAll()); + } + public static List<View> queryByView(ViewDO ViewDO) { List<ViewDO> viewDOList = ER.viewMapper.selectByView(ViewDO); return Trans.TransViewListFromDB(viewDOList); @@ -129,6 +128,13 @@ public class View { } protected void deleteDB() { + // cascade delete the entities and relationships in this view + for (Entity entity : entityList) { + entity.deleteDB(); + } + for (Relationship relationship : relationshipList) { + relationship.deleteDB(); + } ER.viewMapper.deleteByID(this.ID); } diff --git a/src/main/java/com/ic/er/common/DataType.java b/src/main/java/com/ic/er/common/DataType.java new file mode 100644 index 0000000000000000000000000000000000000000..2acfb6e34d74ff1bf65a203b63360e0e7056d2fb --- /dev/null +++ b/src/main/java/com/ic/er/common/DataType.java @@ -0,0 +1,15 @@ +package com.ic.er.common; + +public enum DataType { + UNKNOWN, + CHAR, + VARCHAR, + TEXT, + TINYINT, + SMALLINT, + INT, + BIGINT, + FLOAT, + DOUBLE, + DATETIME, +} diff --git a/src/main/resources/sql/schema-v1.sql b/src/main/resources/schema-v1.sql similarity index 100% rename from src/main/resources/sql/schema-v1.sql rename to src/main/resources/schema-v1.sql diff --git a/src/test/java/com/ic/er/TestAttribute.java b/src/test/java/com/ic/er/TestAttribute.java index 2b4ded38828040d8e8bc1c5d1e4c54fb678ed1bf..42fdc114d74dd61f80ac7de79ba1421554b94bcb 100644 --- a/src/test/java/com/ic/er/TestAttribute.java +++ b/src/test/java/com/ic/er/TestAttribute.java @@ -17,7 +17,6 @@ public class TestAttribute { @Before public void init() throws Exception { ER.connectDB(true); - ER.createTables(); testView = ER.createView("testView", "wt22"); testEntity = testView.addEntity("teacher"); } @@ -26,7 +25,7 @@ public class TestAttribute { public void addAttributeTest() { Attribute a1 = testEntity.addAttribute("teacher_id", DataType.VARCHAR, 1); Attribute a2 = testEntity.addAttribute("name", DataType.VARCHAR, 0); - Attribute a3 = testEntity.addAttribute("age", DataType.INTEGER, 0); + Attribute a3 = testEntity.addAttribute("age", DataType.INT, 0); System.out.printf("a1 ID: %d\n", a1.getID()); System.out.printf("a2 ID: %d\n", a2.getID()); System.out.printf("a3 ID: %d\n", a3.getID()); diff --git a/src/test/java/com/ic/er/TestER.java b/src/test/java/com/ic/er/TestER.java index c93c99d6c23d3e6c83699ac217d7fffa0cec10dc..b39d4410be5948edeba822fc33200392915da1c7 100644 --- a/src/test/java/com/ic/er/TestER.java +++ b/src/test/java/com/ic/er/TestER.java @@ -11,7 +11,6 @@ public class TestER { @Before public void setUp() throws Exception { ER.connectDB(true); - ER.createTables(); } @Test @@ -35,12 +34,12 @@ public class TestER { Entity teacher = firstView.addEntity("teacher"); teacher.addAttribute("teacher_id", DataType.VARCHAR, 1); teacher.addAttribute("name", DataType.VARCHAR, 0); - teacher.addAttribute("age", DataType.INTEGER, 0); + teacher.addAttribute("age", DataType.INT, 0); Entity student = firstView.addEntity("student"); student.addAttribute("student_id", DataType.VARCHAR, 1); student.addAttribute("name", DataType.VARCHAR, 0); - student.addAttribute("grade", DataType.INTEGER, 0); + student.addAttribute("grade", DataType.INT, 0); Relationship ts = firstView.createRelationship("teaches", teacher, student, Cardinality.OneToMany, Cardinality.OneToMany); @@ -49,7 +48,7 @@ public class TestER { View view = ER.loadFromJSON(jsonString); Assert.assertNotNull(view); } - + @Test public void getCardi() { System.out.println(Cardinality.getFromValue("1:N")); diff --git a/src/test/java/com/ic/er/TestEntity.java b/src/test/java/com/ic/er/TestEntity.java index f66cbc2bc757e4eb079c2401e4cdd6ee14602f3c..649c94ecd47612cee17b1a468243907b3d1766ae 100644 --- a/src/test/java/com/ic/er/TestEntity.java +++ b/src/test/java/com/ic/er/TestEntity.java @@ -14,7 +14,6 @@ public class TestEntity { @Before public void init() throws Exception { ER.connectDB(true); - ER.createTables(); testView = ER.createView("testView", "wt22"); } @@ -57,7 +56,7 @@ public class TestEntity { @Test(expected = ERException.class) public void attributeTest() { Entity teacher = testView.addEntity("teacher"); - Attribute teacherID = teacher.addAttribute("teacher_id", DataType.INTEGER, 1); + Attribute teacherID = teacher.addAttribute("teacher_id", DataType.INT, 1); Assert.assertNotEquals(teacher.getID(), Long.valueOf(0)); teacher.updateInfo("new teacher name"); diff --git a/src/test/java/com/ic/er/TestRelationship.java b/src/test/java/com/ic/er/TestRelationship.java index 86c13e331b9947073c65dfe1d4460a475c3d2266..5acf56fdd5a30175eb5ddf525d1e943272e74fee 100644 --- a/src/test/java/com/ic/er/TestRelationship.java +++ b/src/test/java/com/ic/er/TestRelationship.java @@ -18,7 +18,6 @@ public class TestRelationship { @Before public void init() throws Exception { ER.connectDB(true); - ER.createTables(); testView = ER.createView("testView", "wt22"); teacher = testView.addEntity("teacher"); student = testView.addEntity("student"); diff --git a/src/test/java/com/ic/er/TestView.java b/src/test/java/com/ic/er/TestView.java index 448ccb7dc39a3862193e9c51bd40cb9ea733cd9a..966f9e3a1261f1acb5c7366a4839b28f7217d99e 100644 --- a/src/test/java/com/ic/er/TestView.java +++ b/src/test/java/com/ic/er/TestView.java @@ -18,7 +18,6 @@ public class TestView { @Before public void init() throws Exception { ER.connectDB(true); - ER.createTables(); } @Test @@ -28,12 +27,12 @@ public class TestView { Entity teacher = testView.addEntity("teacher"); teacher.addAttribute("teacher_id", DataType.VARCHAR, 1); teacher.addAttribute("name", DataType.VARCHAR, 0); - teacher.addAttribute("age", DataType.INTEGER, 0); + teacher.addAttribute("age", DataType.INT, 0); Entity student = testView.addEntity("student"); student.addAttribute("student_id", DataType.VARCHAR, 1); student.addAttribute("name", DataType.VARCHAR, 0); - student.addAttribute("grade", DataType.INTEGER, 0); + student.addAttribute("grade", DataType.INT, 0); Relationship ts = testView.createRelationship("teaches", teacher, student, Cardinality.ZeroToMany, Cardinality.ZeroToMany); @@ -98,12 +97,12 @@ public class TestView { Entity teacher = firstView.addEntity("teacher"); teacher.addAttribute("teacher_id", DataType.VARCHAR, 1); teacher.addAttribute("name", DataType.VARCHAR, 0); - teacher.addAttribute("age", DataType.INTEGER, 0); + teacher.addAttribute("age", DataType.INT, 0); Entity student = firstView.addEntity("student"); student.addAttribute("student_id", DataType.VARCHAR, 1); student.addAttribute("name", DataType.VARCHAR, 0); - student.addAttribute("grade", DataType.INTEGER, 0); + student.addAttribute("grade", DataType.INT, 0); Relationship ts = firstView.createRelationship("teaches", teacher, student, Cardinality.ZeroToMany, Cardinality.ZeroToMany); Assert.assertNotNull(ts); diff --git a/src/test/java/com/ic/er/mapper/attributeMapperTest.java b/src/test/java/com/ic/er/mapper/attributeMapperTest.java index b26e6d0e81dc677a4665786f9f34af6a6da21395..6f99dc6dcf8bec97ea779d4a13367c72d7043e1e 100644 --- a/src/test/java/com/ic/er/mapper/attributeMapperTest.java +++ b/src/test/java/com/ic/er/mapper/attributeMapperTest.java @@ -20,7 +20,6 @@ public class attributeMapperTest { @Before public void init() throws Exception { ER.connectDB(true); - ER.createTables(); } @Test @@ -43,9 +42,9 @@ public class attributeMapperTest { Date gmtModified = new Date(); - AttributeDO attributeDO = new AttributeDO(0L, entityID, viewID, name, DataType.INTEGER, isPrimary, isDelete, gmtCreate, gmtModified); - AttributeDO attributeDO2 = new AttributeDO(0L, entityID, viewID, name, DataType.INTEGER, isPrimary, isDelete, gmtCreate, gmtModified); - AttributeDO attributeDO3 = new AttributeDO(0L, entityID, viewID, name, DataType.INTEGER, isPrimary, isDelete, gmtCreate, gmtModified); + AttributeDO attributeDO = new AttributeDO(0L, entityID, viewID, name, DataType.INT, isPrimary, isDelete, gmtCreate, gmtModified); + AttributeDO attributeDO2 = new AttributeDO(0L, entityID, viewID, name, DataType.INT, isPrimary, isDelete, gmtCreate, gmtModified); + AttributeDO attributeDO3 = new AttributeDO(0L, entityID, viewID, name, DataType.INT, isPrimary, isDelete, gmtCreate, gmtModified); int ret = ER.attributeMapper.insert(attributeDO); int ret2 = ER.attributeMapper.insert(attributeDO2); diff --git a/src/test/java/com/ic/er/testMybatis.java b/src/test/java/com/ic/er/testMybatis.java index b6bb95f478c78ea542e4d6b4507bde4643791a14..8cffd5c1e8631f9a8c282aebbcb7d74b7f8c93cb 100644 --- a/src/test/java/com/ic/er/testMybatis.java +++ b/src/test/java/com/ic/er/testMybatis.java @@ -10,6 +10,7 @@ import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; +import java.sql.SQLException; public class testMybatis { @@ -28,7 +29,7 @@ public class testMybatis { } @Test - public void testDBConnection() throws IOException { + public void testDBConnection() throws IOException, SQLException { ER.connectDB(true); Assert.assertNotNull(ER.sqlSession); System.out.println(ER.sqlSession.getConnection());