From 950234000b0b0ea09dc30c804ec29ab4a31b7937 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=B1=A4=E4=BC=9F?= <tang@tangweideMacBook-Pro.local>
Date: Thu, 27 Oct 2022 23:10:16 +0100
Subject: [PATCH] minor modification to fit api

---
 pom.xml                                       |  8 ++++-
 src/main/java/com/ic/er/Attribute.java        |  4 +--
 src/main/java/com/ic/er/ER.java               | 24 +++++++++----
 src/main/java/com/ic/er/Entity.java           | 19 ++++++----
 src/main/java/com/ic/er/Trans.java            |  4 +--
 src/main/java/com/ic/er/View.java             | 36 +++++++++++--------
 src/main/java/com/ic/er/common/DataType.java  | 15 ++++++++
 src/main/resources/{sql => }/schema-v1.sql    |  0
 src/test/java/com/ic/er/TestAttribute.java    |  3 +-
 src/test/java/com/ic/er/TestER.java           |  7 ++--
 src/test/java/com/ic/er/TestEntity.java       |  3 +-
 src/test/java/com/ic/er/TestRelationship.java |  1 -
 src/test/java/com/ic/er/TestView.java         |  9 +++--
 .../com/ic/er/mapper/attributeMapperTest.java |  7 ++--
 src/test/java/com/ic/er/testMybatis.java      |  3 +-
 15 files changed, 90 insertions(+), 53 deletions(-)
 create mode 100644 src/main/java/com/ic/er/common/DataType.java
 rename src/main/resources/{sql => }/schema-v1.sql (100%)

diff --git a/pom.xml b/pom.xml
index 1dc8335..676fae9 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 83b7041..72f125e 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 e75c2f1..71e2991 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 796cb17..d6bfaad 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 3599dd6..e32b31a 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 0ceea2d..db287e6 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 0000000..2acfb6e
--- /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 2b4ded3..42fdc11 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 c93c99d..b39d441 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 f66cbc2..649c94e 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 86c13e3..5acf56f 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 448ccb7..966f9e3 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 b26e6d0..6f99dc6 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 b6bb95f..8cffd5c 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());
-- 
GitLab