Skip to content
Snippets Groups Projects
Entity.java 3.95 KiB
Newer Older
  • Learn to ignore specific revisions
  • package com.ic.er;
    
    
    汤伟's avatar
    汤伟 committed
    import com.fasterxml.jackson.annotation.JsonIgnore;
    
    import com.ic.er.Exception.ERException;
    import com.ic.er.common.RelatedObjType;
    
    汤伟's avatar
    汤伟 committed
    import com.ic.er.entity.EntityDO;
    
    import com.ic.er.common.DataType;
    import com.ic.er.common.Utils;
    
    import lombok.Getter;
    import org.apache.ibatis.exceptions.PersistenceException;
    
    
    import java.util.Date;
    import java.util.List;
    
    
    @Getter
    
    public class Entity {
    
    汤伟's avatar
    汤伟 committed
        @JsonIgnore
    
        private Long ID;
        private String name;
    
    汤伟's avatar
    汤伟 committed
        @JsonIgnore
    
        private Long viewID;
        private List<Attribute> attributeList;
    
        private LayoutInfo layoutInfo;
    
    汤伟's avatar
    汤伟 committed
        @JsonIgnore
    
        private Date gmtCreate;
    
    汤伟's avatar
    汤伟 committed
        @JsonIgnore
    
        private Date gmtModified;
    
    
    汤伟's avatar
    汤伟 committed
        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;
            this.attributeList = attributeList;
    
            this.layoutInfo = layoutInfo;
    
            this.gmtCreate = gmtCreate;
            this.gmtModified = gmtModified;
            if (this.ID == 0) {
                if (ER.useDB) {
    
                    this.insertDB();
    
    汤伟's avatar
    汤伟 committed
                } else {
                    this.ID = Utils.generateID();
    
            if (this.layoutInfo == null) {
    
    汤伟's avatar
    汤伟 committed
                this.layoutInfo = new LayoutInfo(0L, this.ID, RelatedObjType.ENTITY, layoutX, layoutY, 0.0, 0.0);
    
    汤伟's avatar
    汤伟 committed
        public Attribute addAttribute(String attributeName, DataType dataType, int isPrimary) {
    
    汤伟's avatar
    汤伟 committed
            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);
            return attribute;
        }
    
    
    汤伟's avatar
    汤伟 committed
        public boolean deleteAttribute(Attribute attribute) {
    
            this.attributeList.remove(attribute);
            if (ER.useDB) {
    
    汤伟's avatar
    汤伟 committed
                attribute.deleteDB();
    
        private void insertDB() {
            try {
                EntityDO entityDO = new EntityDO(0L, this.name, this.viewID, 0, this.gmtCreate, this.gmtModified);
                int ret = ER.entityMapper.insert(entityDO);
                if (ret == 0) {
                    throw new ERException("insertDB fail");
                }
                this.ID = entityDO.getID();
            } catch (PersistenceException e) {
                throw new ERException("insertDB fail", e);
            }
    
        protected void deleteDB() {
    
    汤伟's avatar
    汤伟 committed
            for (Attribute attribute : attributeList) {
                attribute.deleteDB();
            }
    
            ER.entityMapper.deleteByID(this.ID);
    
        public void updateInfo(String name) throws ERException {
            if (name != null) {
                this.name = name;
            }
            int ret = ER.entityMapper.updateByID(new EntityDO(this.ID, this.name, this.viewID, 0, this.gmtCreate, new Date()));
            if (ret == 0) {
                throw new ERException(String.format("cannot find Attribute with ID: %d", this.ID));
    
    汤伟's avatar
    汤伟 committed
    
        public void updateLayoutInfo(Double layoutX, Double layoutY, Double height, Double width) {
            this.layoutInfo.update(layoutX, layoutY, height, width);
        }
    
        public static List<Entity> queryByEntity(EntityDO entityDO) {
            List<EntityDO> entityDOList = ER.entityMapper.selectByEntity(entityDO);
            return Trans.TransEntityListFormFromDB(entityDOList);
        }
    
        public static Entity queryByID(Long ID) {
            List<Entity> entityDOList = queryByEntity(new EntityDO(ID));
            if (entityDOList.size() == 0) {
                throw new ERException(String.format("Entity with ID: %d not found ", ID));
            } else {
                return entityDOList.get(0);
            }
        }