Skip to content
Snippets Groups Projects
Commit 883ca9e7 authored by 汤伟's avatar 汤伟 Committed by Wei
Browse files

finish attribute & Entity

save work
parent cdc2f103
No related branches found
No related tags found
No related merge requests found
Showing
with 216 additions and 118 deletions
......@@ -37,6 +37,17 @@
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
......@@ -63,13 +74,13 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<version>1.18.24</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>
......
......@@ -3,6 +3,7 @@ package com.ic.er;
import com.ic.er.bean.entity.AttributeDO;
import com.ic.er.common.DataType;
import com.ic.er.common.ResultState;
import com.ic.er.common.ResultStateCode;
import com.ic.er.common.Utils;
import com.ic.er.dao.AttributeMapper;
import lombok.Data;
......@@ -35,45 +36,47 @@ public class Attribute {
this.gmtCreate = gmtCreate;
this.gmtModified = gmtModified;
if (this.ID == 0) {
this.ID = Utils.generateID();
if (ER.useDB) {
insertDB();
this.insertDB();
} else {
this.ID = Utils.generateID();
}
}
}
public int insertDB() {
return ER.attributeMapper.insert(new AttributeDO(
0L,
this.entityID,
this.viewID,
this.name,
this.dataType,
this.isPrimary,
this.isForeign,
0,
this.gmtCreate,
this.gmtModified));
int insertDB() {
AttributeDO aDo = new AttributeDO(this.ID, this.entityID, this.viewID, this.name, this.dataType, this.isPrimary, this.isForeign, 0, this.gmtCreate, this.gmtModified);
int ret = ER.attributeMapper.insert(aDo);
this.ID = aDo.getId();
return ret;
}
ResultState deleteDB() {
int ret = ER.attributeMapper.deleteById(this.ID);
return ResultState.ok();
if (ret != 0) {
return ResultState.ok();
} else {
return ResultState.build(ResultStateCode.Success, "delete fail");
}
}
ResultState updateDB() {
int ret = ER.attributeMapper.updateById(new AttributeDO());
return ResultState.ok();
int ret = ER.attributeMapper.updateById(new AttributeDO(this.ID, this.entityID, this.viewID, this.name, this.dataType, this.isPrimary, this.isForeign, 0, this.gmtCreate, new Date()));
if (ret != 0) {
return ResultState.ok();
} else {
return ResultState.build(ResultStateCode.Success, "update fail");
}
}
// transform the data from db format to java class format
public static Attribute TransformFromDB(AttributeDO attributeDO) {
// transform the data from db format (xxxDO) to java class format
private static Attribute TransformFromDB(AttributeDO attributeDO) {
return new Attribute(attributeDO.getId(), attributeDO.getEntityId(), attributeDO.getViewId(),
attributeDO.getName(), attributeDO.getDataType(), attributeDO.getIsPrimary(),
attributeDO.getIsForeign(), attributeDO.getGmtCreate(), attributeDO.getGmtModified());
}
public static List<Attribute> TransListFormFromDB(List<AttributeDO> doList) {
private static List<Attribute> TransListFormFromDB(List<AttributeDO> doList) {
List<Attribute> ret = new ArrayList<>();
for (AttributeDO attributeDO : doList) {
ret.add(TransformFromDB(attributeDO));
......@@ -85,4 +88,12 @@ public class Attribute {
return TransListFormFromDB(ER.attributeMapper.selectByAttribute(attributeDO));
}
public static Attribute queryByID(Long ID) {
List<Attribute> attributeList = TransListFormFromDB(ER.attributeMapper.selectByAttribute(new AttributeDO(ID)));
if (attributeList.size() == 0) {
return null;
} else {
return attributeList.get(0);
}
}
}
package com.ic.er.util;
package com.ic.er;
import com.ic.er.common.ResultState;
import com.ic.er.common.ResultStateCode;
import com.ic.er.dao.AttributeMapper;
import com.ic.er.dao.EntityMapper;
import com.ic.er.dao.RelationshipMapper;
import com.ic.er.dao.ViewMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.BasicConfigurator;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
public class boot {
public class ER {
public static SqlSession sqlSession;
public static boolean useDB = false;
public static AttributeMapper attributeMapper;
public static EntityMapper entityMapper;
public static RelationshipMapper relationshipMapper;
public static ViewMapper viewMapper;
private static Map<Long, View> allViewsMap = new HashMap<>();
public static ResultState connectDB(){
ResultState resultState = null;
......@@ -24,8 +36,14 @@ public class boot {
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
sqlSession = sqlSessionFactory.openSession(true);
resultState = ResultState.ok();
attributeMapper = sqlSession.getMapper(AttributeMapper.class);
entityMapper = sqlSession.getMapper(EntityMapper.class);
relationshipMapper = sqlSession.getMapper(RelationshipMapper.class);
viewMapper = sqlSession.getMapper(ViewMapper.class);
BasicConfigurator.configure();
useDB = true;
} catch (IOException msg) {
resultState = ResultState.build(ResultStateCode.FAILRESULTCODE, msg.getMessage());
resultState = ResultState.build(ResultStateCode.Failure, msg.getMessage());
return resultState;
}
return resultState;
......@@ -99,4 +117,24 @@ public class boot {
" PRIMARY KEY (`id`)\n" +
");");
}
public static View createView(String name, String creator) {
View view = new View(0L, name, new ArrayList<>(), new ArrayList<>(), creator, new Date(), new Date());
allViewsMap.put(view.getID(), view);
return view;
}
public static void deleteView(View view) {
view.deleteDB();
allViewsMap.remove(view.getID());
}
public static List<View> queryAll() {
if (ER.useDB) {
return View.queryAll();
} else {
return new ArrayList<>(allViewsMap.values());
}
}
}
......@@ -29,9 +29,10 @@ public class Entity {
this.gmtCreate = gmtCreate;
this.gmtModified = gmtModified;
if (this.ID == 0) {
this.ID = Utils.generateID();
if (ER.useDB) {
insertDB();
this.insertDB();
} else {
this.ID = Utils.generateID();
}
}
}
......@@ -40,7 +41,7 @@ public class Entity {
int isPrimary, int isForeign) {
Attribute attribute = new Attribute(0L, this.ID, this.viewID, attributeName, dataType, isPrimary, isForeign, new Date(), new Date());
this.attributeList.add(attribute);
this.setGmtModified(new Date(System.currentTimeMillis()));
this.setGmtModified(new Date());
if (ER.useDB) {
this.updateDB();
}
......@@ -56,14 +57,13 @@ public class Entity {
return false;
}
public static Entity TransformFromDB(EntityDO EntityDO) {
// todo add entity_id
List<Attribute> attributeList = Attribute.queryByAttribute(null);
return new Entity(EntityDO.getId(), EntityDO.getName(), EntityDO.getViewId(), attributeList,
EntityDO.getGmtCreate(), EntityDO.getGmtModified());
private static Entity TransformFromDB(EntityDO entityDO) {
List<Attribute> attributeList = Attribute.queryByAttribute(new AttributeDO(entityDO.getId(), entityDO.getViewId()));
return new Entity(entityDO.getId(), entityDO.getName(), entityDO.getViewId(), attributeList,
entityDO.getGmtCreate(), entityDO.getGmtModified());
}
public static List<Entity> TransListFormFromDB(List<EntityDO> doList) {
private static List<Entity> TransListFormFromDB(List<EntityDO> doList) {
List<Entity> ret = new ArrayList<>();
for (EntityDO EntityDO : doList) {
ret.add(TransformFromDB(EntityDO));
......@@ -76,8 +76,20 @@ public class Entity {
return TransListFormFromDB(entityDOList);
}
public long insertDB() {
return ER.entityMapper.insert(new EntityDO(0L, this.name, this.viewID, 0, this.gmtCreate, this.gmtModified));
public static Entity queryByID(Long ID) {
List<Entity> entityDOList = queryByEntity(new EntityDO(ID));
if (entityDOList.size() == 0) {
return null;
} else {
return entityDOList.get(0);
}
}
public int insertDB() {
EntityDO entityDO = new EntityDO(0L, this.name, this.viewID, 0, this.gmtCreate, this.gmtModified);
int ret = ER.entityMapper.insert(entityDO);
this.ID = entityDO.getId();
return ret;
}
ResultState deleteDB() {
......@@ -90,7 +102,7 @@ public class Entity {
}
ResultState updateDB() {
int res = ER.entityMapper.updateById(new EntityDO(this.ID, "", 0L, 0, new Date(), new Date()));
int res = ER.entityMapper.updateById(new EntityDO(this.ID, this.name, this.viewID, 0, this.gmtCreate, new Date()));
if (res == 0) {
return ResultState.ok();
} else {
......
......@@ -37,30 +37,26 @@ public class Relationship {
this.gmtCreate = gmtCreate;
this.gmtModified = gmtModified;
if (this.ID == 0) {
this.ID = Utils.generateID();
if (ER.useDB) {
insertDB();
} else {
this.ID = Utils.generateID();
}
}
}
int insertDB() {
return ER.relationshipMapper.insert(new RelationshipDO(
RelationshipDO relationshipDO = new RelationshipDO(
0L,
this.name,
this.viewID,
this.firstEntityID,
this.secondEntityID,
this.firstAttributeID,
this.secondAttributeID,
this.cardinality,
0,
this.gmtCreate,
this.gmtModified
));
this.name, this.viewID, this.firstEntityID, this.secondEntityID,
this.firstAttributeID, this.secondAttributeID, this.cardinality,
0, this.gmtCreate, this.gmtModified);
int ret = ER.relationshipMapper.insert(relationshipDO);
this.ID = relationshipDO.getId();
return ret;
}
public static Relationship TransformFromDB(RelationshipDO relationshipDO) {
private static Relationship TransformFromDB(RelationshipDO relationshipDO) {
return new Relationship(relationshipDO.getId(), relationshipDO.getName(), relationshipDO.getViewId(),
relationshipDO.getFirstEntityId(), relationshipDO.getSecondEntityId(),
relationshipDO.getFirstAttributeId(), relationshipDO.getSecondAttributeId(),
......@@ -68,7 +64,7 @@ public class Relationship {
relationshipDO.getGmtCreate(), relationshipDO.getGmtModified());
}
public static List<Relationship> TransListFormFromDB(List<RelationshipDO> doList) {
private static List<Relationship> TransListFormFromDB(List<RelationshipDO> doList) {
List<Relationship> ret = new ArrayList<>();
for (RelationshipDO RelationshipDO : doList) {
ret.add(TransformFromDB(RelationshipDO));
......@@ -80,6 +76,15 @@ public class Relationship {
return TransListFormFromDB(ER.relationshipMapper.selectByRelationship(RelationshipDO));
}
public static Relationship queryByID(Long ID) {
List<Relationship> relationships = queryByRelationship(new RelationshipDO(ID));
if (relationships.size() == 0) {
return null;
} else {
return relationships.get(0);
}
}
ResultState deleteDB() {
int res = ER.relationshipMapper.deleteById(this.ID);
if (res == 0) {
......@@ -90,7 +95,7 @@ public class Relationship {
}
ResultState updateDB() {
int res = ER.relationshipMapper.updateById(new RelationshipDO());
int res = ER.relationshipMapper.updateById(new RelationshipDO(this.ID, this.name, this.viewID, this.firstEntityID, this.secondEntityID, this.firstAttributeID, this.secondAttributeID, this.cardinality, 0, this.gmtCreate, this.gmtModified));
if (res == 0) {
return ResultState.ok();
} else {
......
package com.ic.er;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ic.er.bean.entity.RelationshipDO;
import com.ic.er.bean.entity.ViewDO;
import com.ic.er.common.Cardinality;
import com.ic.er.common.ResultState;
......@@ -13,7 +12,6 @@ import lombok.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@Data
public class View {
......@@ -33,17 +31,14 @@ public class View {
this.gmtCreate = gmtCreate;
this.gmtModified = gmtModified;
if (this.ID == 0) {
this.ID = Utils.generateID();
if (ER.useDB) {
insertDB();
} else {
this.ID = Utils.generateID();
}
}
}
public static View createView(String name, String creator) {
return new View(0L, name, new ArrayList<>(), new ArrayList<>(), creator, new Date(), new Date());
}
public Entity addEntity(String entityName) {
Entity entity = new Entity(0L, entityName, this.ID, new ArrayList<>(), new Date(), new Date());
this.entityList.add(entity);
......@@ -69,8 +64,7 @@ public class View {
public Relationship createRelationship(String relationshipName, Long firstEntityID, Long secondEntityID,
Cardinality cardinality) {
Relationship relationship = new Relationship(0L, relationshipName, this.ID,
firstEntityID, secondEntityID, 0L, 0L, cardinality,
new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis()));
firstEntityID, secondEntityID, 0L, 0L, cardinality, new Date(), new Date());
this.getRelationshipList().add(relationship);
if (ER.useDB) {
this.updateDB();
......@@ -91,19 +85,14 @@ public class View {
}
int insertDB() {
return ER.viewMapper.insert(new ViewDO(
0L,
this.name,
this.creator,
0L,
0,
this.gmtCreate,
this.gmtModified
));
ViewDO viewDO = new ViewDO(0L, this.name, this.creator, 0L, 0, this.gmtCreate, this.gmtModified);
int ret = ER.viewMapper.insert(viewDO);
this.ID = viewDO.getId();
return ret;
}
public static List<View> queryAll() {
return null;
return TransListFormFromDB(ER.viewMapper.selectAll());
}
public String ToJSON() throws JsonProcessingException {
......@@ -112,14 +101,14 @@ public class View {
return json;
}
public static View TransformFromDB(ViewDO ViewDO) {
private static View TransformFromDB(ViewDO ViewDO) {
List<Entity> entityList = Entity.queryByEntity(null);
List<Relationship> relationshipList = Relationship.queryByRelationship(null);
return new View(ViewDO.getId(), ViewDO.getName(), entityList, relationshipList, ViewDO.getCreator(),
ViewDO.getGmtCreate(), ViewDO.getGmtModified());
}
public static List<View> TransListFormFromDB(List<ViewDO> doList) {
private static List<View> TransListFormFromDB(List<ViewDO> doList) {
List<View> ret = new ArrayList<>();
for (ViewDO ViewDO : doList) {
ret.add(TransformFromDB(ViewDO));
......@@ -128,11 +117,20 @@ public class View {
}
public static List<View> queryByView(ViewDO ViewDO) {
List<com.ic.er.bean.entity.ViewDO> viewDOList = ER.viewMapper.selectByView(ViewDO);
List<ViewDO> viewDOList = ER.viewMapper.selectByView(ViewDO);
return TransListFormFromDB(viewDOList);
}
public ResultState delete() {
public static View queryByID(Long ID) {
List<View> viewDOList = queryByView(new ViewDO(ID));
if (viewDOList.size() == 0) {
return null;
} else {
return viewDOList.get(0);
}
}
public ResultState deleteDB() {
int res = ER.viewMapper.deleteById(this.ID);
if (res == 0) {
return ResultState.ok();
......@@ -142,7 +140,7 @@ public class View {
}
ResultState updateDB() {
int res = ER.viewMapper.updateById(new ViewDO(this.ID, "", "", 0L, 0, new Date(), new Date()));
int res = ER.viewMapper.updateById(new ViewDO(this.ID, this.name, this.creator, 0L, 0, this.gmtCreate, new Date()));
if (res == 0) {
return ResultState.ok();
} else {
......
......@@ -21,14 +21,22 @@ public class AttributeDO {
private DataType dataType;
private int isPrimary;
private Integer isPrimary;
private int isForeign;
private Integer isForeign;
private int isDelete;
private Integer isDelete;
private Date gmtCreate;
private Date gmtModified;
public AttributeDO(Long id) {
this.id = id;
}
public AttributeDO(Long entityId, Long viewId) {
this.entityId = entityId;
this.viewId = viewId;
}
}
package com.ic.er.bean.entity;
import com.ic.er.Entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
......@@ -16,10 +17,13 @@ public class EntityDO {
private Long viewId;
private int isDelete;
private Integer isDelete;
private Date gmtCreate;
private Date gmtModified;
public EntityDO(Long id) {
this.id = id;
}
}
......@@ -27,10 +27,18 @@ public class RelationshipDO {
private Cardinality cardinality;
private int isDelete;
private Integer isDelete;
private Date gmtCreate;
private Date gmtModified;
public RelationshipDO(Long ID) {
this.id = ID;
}
public RelationshipDO(Long firstEntityId, Long secondEntityId) {
this.firstEntityId = firstEntityId;
this.secondEntityId = secondEntityId;
}
}
......@@ -18,10 +18,14 @@ public class ViewDO {
private Long parentId;
private int isDelete;
private Integer isDelete;
private Date gmtCreate;
private Date gmtModified;
public ViewDO(Long ID) {
this.id = ID;
}
}
package com.ic.er.common;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public enum Cardinality {
Unknown(0),
OneToOne(1),
OneToMany(2),
ManyToOne(3),
ManyToMany(4);
private final Integer value;
}
......@@ -32,7 +32,7 @@ public class ResultState {
}
public ResultState(Object data) {
this.status = ResultStateCode.SUCCESSRESULTCODE;
this.status = ResultStateCode.Success;
this.msg = "OK";
this.data = data;
}
......
package com.ic.er.common;
public class ResultStateCode {
public static Integer SUCCESSRESULTCODE = 200;
public static Integer FAILRESULTCODE = 400;
public static Integer Success = 0;
public static Integer Failure = 1;
}
......@@ -11,7 +11,6 @@ public interface AttributeMapper {
int insert(AttributeDO attributeDO);
// rarely use, please use update to change is_delete to 1
int deleteById(Long id);
int updateById(AttributeDO attributeDO);
......
......@@ -64,12 +64,9 @@
</where>
</select>
<insert id="insert" parameterType="com.ic.er.bean.entity.AttributeDO">
<insert id="insert" parameterType="com.ic.er.bean.entity.AttributeDO" useGeneratedKeys="true" keyProperty="id">
insert into attribute
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="entityId != null">
entity_id,
</if>
......@@ -90,9 +87,6 @@
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=BIGINT},
</if>
<if test="entityId != null">
#{entityId,jdbcType=BIGINT},
</if>
......
......@@ -48,12 +48,9 @@
</where>
</select>
<insert id="insert" parameterType="com.ic.er.bean.entity.EntityDO">
<insert id="insert" parameterType="com.ic.er.bean.entity.EntityDO" useGeneratedKeys="true" keyProperty="id">
insert into entity
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
name,
</if>
......@@ -62,9 +59,6 @@
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=BIGINT},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
......
......@@ -58,7 +58,7 @@
</where>
</select>
<insert id="insert" parameterType="com.ic.er.bean.entity.GraphInfoDO">
<insert id="insert" parameterType="com.ic.er.bean.entity.GraphInfoDO" useGeneratedKeys="true" keyProperty="id">
insert into graph_info
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
......
......@@ -68,12 +68,9 @@
</where>
</select>
<insert id="insert" parameterType="com.ic.er.bean.entity.RelationshipDO">
<insert id="insert" parameterType="com.ic.er.bean.entity.RelationshipDO" useGeneratedKeys="true" keyProperty="id">
insert into relationship
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
name,
</if>
......@@ -97,9 +94,6 @@
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=BIGINT},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
......@@ -119,7 +113,7 @@
#{secondAttributeId,jdbcType=BIGINT},
</if>
<if test="cardinality != null">
#{cardinality,jdbcType=SMALLINT},
#{cardinality.value,jdbcType=SMALLINT},
</if>
</trim>
</insert>
......@@ -152,7 +146,7 @@
second_attribute_id = #{secondAttributeId,jdbcType=BIGINT},
</if>
<if test="cardinality != null">
cardinality = #{cardinality,jdbcType=SMALLINT},
cardinality = #{cardinality.value,jdbcType=SMALLINT},
</if>
</set>
where id = #{id,jdbcType=BIGINT} and is_delete = 0
......
......@@ -58,12 +58,9 @@
where id = #{id,jdbcType=BIGINT} and is_delete = 0
</select>
<insert id="insert" parameterType="com.ic.er.bean.entity.ViewDO">
<insert id="insert" parameterType="com.ic.er.bean.entity.ViewDO" useGeneratedKeys="true" keyProperty="id">
insert into view
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
name,
</if>
......@@ -75,9 +72,6 @@
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=BIGINT},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
......
......@@ -4,6 +4,13 @@
'http://mybatis.org/dtd/mybatis-3-config.dtd'>
<configuration>
<properties resource='jdbc.properties'/>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeHandlers>
<typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler"
javaType="com.ic.er.common.Cardinality"/>
</typeHandlers>
<environments default='development'>
<environment id='development'>
<transactionManager type='JDBC'/>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment