提交 0be29099 authored 作者: 黄夏豪's avatar 黄夏豪

[数据模型] 更新了聚合对象生成的方法,现在支持多层嵌套的聚合对象了

上级 1c97e5f0
package com.tykj.workflowcore.model_layer.controller;
import com.tykj.workflowcore.base.result.ResultObj;
import com.tykj.workflowcore.base.result.ResultUtil;
import com.tykj.workflowcore.model_layer.dao.AggregationDao;
import com.tykj.workflowcore.model_layer.entity.Aggregation;
import com.tykj.workflowcore.model_layer.entity.vo.AggregationVO;
import com.tykj.workflowcore.model_layer.service.AggregationService;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
/**
* @ClassName AggregationController
* @Description TODO
......@@ -19,14 +28,55 @@ import org.springframework.web.bind.annotation.RestController;
*/
@RestController
@RequestMapping("/aggregation")
public class AggregationController {
@Autowired
AggregationService aggregationService;
@PostMapping("aggregation/add")
@Autowired
AggregationDao aggregationDao;
@PostMapping("/add")
public ResponseEntity add(@RequestBody AggregationVO aggregationVO) {
aggregationService.addAggregation(aggregationVO);
return ResultUtil.success("","保存成功!");
}
@PostMapping("/test")
public ResponseEntity test(@RequestBody AggregationVO AggregationVO) {
String result= "";
Document document = DocumentHelper.createDocument();
Element rootElement = DocumentHelper.createElement("rootElement");
String s = testDG(AggregationVO.getAggregations(), null, 1, rootElement).asXML();
document.add(rootElement);
return ResultUtil.success(aggregationDao.findById(3),"保存成功!");
}
//假设获得的是一份XML
public Element testDG(List<Aggregation> aggregations, Aggregation parentAggregation, Integer mainId, Element element){
//循环
Element elementCLass = DocumentHelper.createElement("class");
if (parentAggregation!=null){
elementCLass.addAttribute("name",parentAggregation.getSideTableId()+"_");
}else {
elementCLass.addAttribute("name",mainId+"_");
}
for (int i = 0; i < aggregations.size(); i++) {
Aggregation aggregationChild = aggregations.get(i);
if (aggregationChild.getAggregations()!=null&&aggregationChild.getAggregations().size()>0){
Element elementClassChild = testDG(aggregationChild.getAggregations(), aggregationChild, mainId, element);
Element elementSet = DocumentHelper.createElement("set");
elementSet.addAttribute("setName","c"+elementClassChild.attribute("name").getValue());
elementCLass.add(elementSet);
}else {
Element elementSet = DocumentHelper.createElement("set");
elementSet.addAttribute("setName","c"+aggregationChild.getSideTableId());
elementCLass.add(elementSet);
}
}
element.add(elementCLass);
return elementCLass;
}
}
......@@ -8,7 +8,8 @@ import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import javax.persistence.Entity;
import javax.persistence.*;
import java.util.List;
/**
* @ClassName AggregationRelationship
......@@ -42,4 +43,13 @@ public class Aggregation extends BaseEntity {
@ApiModelProperty("中间表名称")
private String connectionTableName;
@ApiModelProperty("多层嵌套时会生成虚拟对象class 该class为了不影响其他真实对象所以用uuid命名")
private String uuidTableName;
private Integer parentId;
@OneToMany(cascade = CascadeType.ALL) //表示级练操作
@JoinColumn(name = "parentId")
List<Aggregation> aggregations;
}
......@@ -24,6 +24,9 @@ public class AggregationVO {
@ApiModelProperty("名称")
private String name;
@ApiModelProperty("描述")
private String chineseName;
@ApiModelProperty("描述")
private String description;
......@@ -31,7 +34,7 @@ public class AggregationVO {
private Integer mainTableId;
@ApiModelProperty("关系集合")
private List<Aggregation> lists;
private List<Aggregation> aggregations;
public TableInfoEx getTableInfoEx(){
TableInfoEx tableInfoEx = new TableInfoEx();
......
......@@ -13,11 +13,18 @@ import com.tykj.workflowcore.model_layer.entity.vo.AggregationVO;
import com.tykj.workflowcore.model_layer.myEnum.AggregationType;
import com.tykj.workflowcore.model_layer.service.AggregationService;
import com.tykj.workflowcore.model_layer.utils.AggregationUtil;
import com.tykj.workflowcore.model_layer.utils.SessionUtil;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
......@@ -37,6 +44,8 @@ public class AggregationImpl implements AggregationService {
TableInfoExDao tableInfoExDao;
@Autowired
AggregationDao aggregationRelationshipDao;
@Autowired
SessionUtil sessionUtil;
@Override
public void addAggregation(AggregationVO aggregationVO) {
......@@ -53,39 +62,149 @@ public class AggregationImpl implements AggregationService {
AggregationUtil.changeClassName(aggregationVO.getName(),mainTableDocument );
// save TableInfoEx (name des mainTableId)
TableInfoEx tableInfoEx = tableInfoExDao.save(aggregationVO.getTableInfoEx());
// 遍历VO 根据聚合类型 生成对应的 XML 节点 并添加到DOCUMENT中
for (Aggregation aggregationRelationship : aggregationVO.getLists()) {
//取出副表的tableInfo
Optional<TableInfo> sideTableById = tableInfoDao.findById(aggregationRelationship.getSideTableId());
aggregationRelationship.setTableInfoExId(tableInfoEx.getId());
if (!sideTableById.isPresent()){
throw new ApiException("id为:"+aggregationRelationship.getSideTableId()+"的副表不存在");
}
if (aggregationRelationship.getRelationship().equals(AggregationType.ONE_TO_ONE)){
Document document = AggregationUtil.addOneToOne(aggregationRelationship.getSideTableConnectionKey(), sideTableById.get().getModelName(), mainTableDocument);
tableInfoEx.setXml(document.asXML());
try {
createAggregationObjectXml(aggregationVO.getAggregations(), null, tableInfoEx.getId(), mainTableById.get(), mainTableDocument.getRootElement());
tableInfoEx.setXml(mainTableDocument.asXML());
} catch (DocumentException e) {
e.printStackTrace();
}
tableInfoExDao.save(tableInfoEx);
}
//假设获得的是一份XML
public Element createAggregationObjectXml(List<Aggregation> aggregations, Aggregation parentAggregation, Integer tableInfoExId, TableInfo mainTableInfo, Element rootElement) throws DocumentException {
//这里需要一个class标签,
Boolean isFirst = false;
Element elementClass ;
String parentModeName;
if (parentAggregation!=null){
//如果不是第一层,则通过parentAggregation,查出副表的XML字符串
//将XML字符串转换为XML对象
//取出Class标签,将Class标签赋值给elementClass
Optional<TableInfo> sideTableInfoOptional = tableInfoDao.findById(parentAggregation.getSideTableId());
if (sideTableInfoOptional.isPresent()){
elementClass = (Element) DocumentHelper.parseText(sideTableInfoOptional.get().getXml()).getRootElement().element("class").clone();
}else {
throw new ApiException("id为:"+parentAggregation.getSideTableId()+"的副表不存在");
}
if (aggregationRelationship.getRelationship().equals(AggregationType.ONE_TO_MANY)){
//如果是一对多需要 为 aggregationRelationship 生成连接的KEY
String mainTableConnectionKey = sideTableById.get().getModelName();
Document document = AggregationUtil.addOneToMany(sideTableById.get().getModelName(), mainTableConnectionKey, aggregationRelationship.getSideTableConnectionKey(), mainTableDocument);
tableInfoEx.setXml(document.asXML());
}
if (aggregationRelationship.getRelationship().equals(AggregationType.MANY_TO_MANY)){
//如果是多对多需要 为 aggregationRelationship 生成连接的KEY
String mainTableConnectionKey = mainTableById.get().getModelName()+"_Id";
String sideTableConnectionKey = sideTableById.get().getModelName()+"_Id";
aggregationRelationship.setMainTableConnectionKey(mainTableConnectionKey);
aggregationRelationship.setSideTableConnectionKey(sideTableConnectionKey);
//如果是多对多需要 为 aggregationRelationship 生成连接的中间表名
String connectionTableName = mainTableById.get().getModelName()+"_"+sideTableById.get().getModelName()+"_"+ (UUID.randomUUID().toString().substring(0,8));
aggregationRelationship.setConnectionTableName(connectionTableName);
//Util.addManyToMany
Document document = AggregationUtil.addManyToMany(sideTableById.get().getModelName(),connectionTableName,mainTableConnectionKey,sideTableConnectionKey,mainTableDocument);
tableInfoEx.setXml(document.asXML());
parentModeName = sideTableInfoOptional.get().getModelName();
}else {
//如果是第一层 则通过rootElement 查出副表的XML字符串
//取出Class标签,将Class标签赋值给elementClass
elementClass = (Element) rootElement.element("class");
parentModeName = mainTableInfo.getModelName();
isFirst = true;
}
//循环
for (int i = 0; i < aggregations.size(); i++) {
Aggregation aggregationChild = aggregations.get(i);
if (aggregationChild.getAggregations()!=null&&aggregationChild.getAggregations().size()>0){
String uuid ="tykj"+UUID.randomUUID().toString().replaceAll("-","");
Element elementClassChild = createAggregationObjectXml(aggregationChild.getAggregations(), aggregationChild, tableInfoExId, mainTableInfo,rootElement);
//为element给一个UUID的entity_name
aggregationChild.setUuidTableName(uuid);
elementClassChild.attribute("entity-name").setValue(uuid);
Element elementSet = aggregationToXmlElement(aggregationChild, tableInfoExId, parentModeName);
elementClass.add(elementSet);
}else {
//addSet
Element elementSet = aggregationToXmlElement(aggregationChild, tableInfoExId, parentModeName);
elementClass.add(elementSet);
}
aggregationRelationshipDao.save(aggregationRelationship);
aggregationRelationshipDao.save(aggregationChild);
}
if (!isFirst){
rootElement.add(elementClass);
}
return elementClass;
}
public Element aggregationToXmlElement(Aggregation aggregation,Integer tableInfoExId,String parentModelName){
//设置tableInfoExId
aggregation.setTableInfoExId(tableInfoExId);
Optional<TableInfo> sideTableById = tableInfoDao.findById(aggregation.getSideTableId());
if (!sideTableById.isPresent()){
throw new ApiException("id为:"+aggregation.getSideTableId()+"的副表不存在");
}
if (aggregation.getRelationship().equals(AggregationType.ONE_TO_ONE)){
return AggregationUtil.createOneToOneElement(
sideTableById.get().getModelName(),
aggregation.getSideTableConnectionKey(),
StringUtils.isEmpty(aggregation.getUuidTableName())?sideTableById.get().getModelName():aggregation.getUuidTableName()
);
}
if (aggregation.getRelationship().equals(AggregationType.ONE_TO_MANY)){
return AggregationUtil.createOneToManyElement(
sideTableById.get().getModelName(),
StringUtils.isEmpty(aggregation.getUuidTableName())?sideTableById.get().getModelName():aggregation.getUuidTableName(),
aggregation.getSideTableConnectionKey());
}
if (aggregation.getRelationship().equals(AggregationType.MANY_TO_MANY)){
//如果是多对多需要 为 aggregationRelationship 生成连接的KEY
String mainTableConnectionKey = parentModelName+"_Id";
String sideTableConnectionKey = sideTableById.get().getModelName()+"_Id";
aggregation.setMainTableConnectionKey(mainTableConnectionKey);
aggregation.setSideTableConnectionKey(sideTableConnectionKey);
//如果是多对多需要 为 aggregationRelationship 生成连接的中间表名
String connectionTableName = parentModelName+"_"+sideTableById.get().getModelName()+"_"+ (UUID.randomUUID().toString().substring(0,8));
aggregation.setConnectionTableName(connectionTableName);
//Util.addManyToMany
return AggregationUtil.createManyToManyElement(sideTableById.get().getModelName(),aggregation.getUuidTableName(),connectionTableName,mainTableConnectionKey,sideTableConnectionKey);
}
return null;
}
// 假设获得的是一份XML
// public Element testDG(List<Aggregation> aggregations, Aggregation parentAggregation, Integer mainId, Element element){
// //循环
// Element elementCLass = DocumentHelper.createElement("class");
// if (parentAggregation!=null){
// elementCLass.addAttribute("name",parentAggregation.getSideTableId()+"_");
// }else {
// elementCLass.addAttribute("name",mainId+"_");
// }
// for (int i = 0; i < aggregations.size(); i++) {
// Aggregation aggregationChild = aggregations.get(i);
// if (aggregationChild.getAggregations()!=null&&aggregationChild.getAggregations().size()>0){
// Element elementClassChild = testDG(aggregationChild.getAggregations(), aggregationChild, mainId, element);
// Element elementSet = DocumentHelper.createElement("set");
// elementSet.addAttribute("setName","c"+elementClassChild.attribute("name").getValue());
// elementCLass.add(elementSet);
// }else {
// Element elementSet = DocumentHelper.createElement("set");
// elementSet.addAttribute("setName","c"+aggregationChild.getSideTableId());
// elementCLass.add(elementSet);
// }
// }
// element.add(elementCLass);
// return elementCLass;
// }
// Optional<TableInfo> sideTableById = tableInfoDao.findById(aggregationChild.getSideTableId());
// aggregationChild.setTableInfoExId(tableExId);
// if (!sideTableById.isPresent()){
// throw new ApiException("id为:"+aggregationChild.getSideTableId()+"的副表不存在");
// }
// if (aggregationChild.getRelationship().equals(AggregationType.ONE_TO_ONE)){
// AggregationUtil.addOneToOne(aggregationChild.getSideTableConnectionKey(), sideTableById.get().getModelName(), document);
// }
// if (aggregationChild.getRelationship().equals(AggregationType.ONE_TO_MANY)){
// //如果是一对多需要 为 aggregationRelationship 生成连接的KEY
// String mainTableConnectionKey = sideTableById.get().getModelName();
// AggregationUtil.addOneToMany(sideTableById.get().getModelName(), mainTableConnectionKey, aggregationChild.getSideTableConnectionKey(), document);
// }
// if (aggregationChild.getRelationship().equals(AggregationType.MANY_TO_MANY)){
// //如果是多对多需要 为 aggregationRelationship 生成连接的KEY
// String mainTableConnectionKey = mainTableInfo.getModelName()+"_Id";
// String sideTableConnectionKey = sideTableById.get().getModelName()+"_Id";
// aggregationChild.setMainTableConnectionKey(mainTableConnectionKey);
// aggregationChild.setSideTableConnectionKey(sideTableConnectionKey);
// //如果是多对多需要 为 aggregationRelationship 生成连接的中间表名
// String connectionTableName = mainTableInfo.getModelName()+"_"+sideTableById.get().getModelName()+"_"+ (UUID.randomUUID().toString().substring(0,8));
// aggregationChild.setConnectionTableName(connectionTableName);
// //Util.addManyToMany
// AggregationUtil.addManyToMany(sideTableById.get().getModelName(),connectionTableName,mainTableConnectionKey,sideTableConnectionKey,document);
// }
}
......@@ -256,7 +256,7 @@ public class ModelImpl implements ModelService {
map.put(propertyNames[i], value);
}
}
newSession.save(tableName, map);
newSession.saveOrUpdate(tableName, map);
newSession.getTransaction().begin();
newSession.getTransaction().commit();
newSession.close();
......@@ -307,8 +307,8 @@ public class ModelImpl implements ModelService {
columnVO.setPrimaryKey(0);
}
columnVO.setFieldType(getTypeName(genericType.toString()));
columnVO.setFieldName(getClassName(declaredField.toString()));
columnVO.setFieldLength(setLength(genericType.toString()));
//获得属性中文描述
if (declaredField.isAnnotationPresent(ApiModelProperty.class)) {
ApiModelProperty annotation = declaredField.getAnnotation(ApiModelProperty.class);
......@@ -340,7 +340,7 @@ public class ModelImpl implements ModelService {
ColumnInfo columnInfo = new ColumnInfo();
columnInfo.setFieldName(columnVO.getFieldName());
columnInfo.setFieldType(columnVO.getFieldType());
columnInfo.setFieldLength(setLength(genericType.toString()));
columnInfo.setFieldLength(columnVO.getFieldLength());
columnInfo.setFieldTitle(columnVO.getFieldTitle());
columnInfo.setPrimaryKey(columnVO.getPrimaryKey());
......
......@@ -6,8 +6,7 @@ import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.util.StringUtils;
/**
......@@ -63,14 +62,54 @@ public class AggregationUtil {
return document;
}
public static Element createOneToOneElement(String name,String connectionKey,String sideTableName ) {
Element element = DocumentHelper.createElement("one-to-one");
element.addAttribute("name", name);
element.addAttribute("property-ref", connectionKey);
element.addAttribute("class", sideTableName);
element.addAttribute("cascade", "save-update");
return element;
}
public static Element createOneToManyElement(String name,
String sideTableName,
String sideTableConnectionKey) {
//创建ELEMETN
Element setElement = createSetElement(
name,
sideTableName,
"",
"",
sideTableConnectionKey,
AggregationType.ONE_TO_MANY);
return setElement;
}
public static Element createManyToManyElement(String sideTableName,String uuidTableName,String connectionTableName,String mainTableConnectionKey, String sideTableConnectionKey) {
Element setElement = createSetElement(sideTableName,
StringUtils.isEmpty(uuidTableName)?sideTableName:uuidTableName,
connectionTableName,
mainTableConnectionKey,
sideTableConnectionKey,
AggregationType.MANY_TO_MANY);
return setElement;
}
public static Element getNode(Document document) {
return document.getRootElement().element("class");
}
public static Element createSetElement(String setName,String sideTableName,String connectionTableName,String mainTableConnectionKey, String sideTableConnectionKey,Integer type){
//创建ELEMTN
public static Element createSetElement(String name,
String sideTableName,
String connectionTableName,
String mainTableConnectionKey,
String sideTableConnectionKey,
Integer type){
//创建element
Element elementSet = DocumentHelper.createElement("set");
elementSet.addAttribute("name", setName);
elementSet.addAttribute("name", name);
elementSet.addAttribute("cascade", "save-update");
Element elementKey = DocumentHelper.createElement("key");
if (type.equals(AggregationType.MANY_TO_MANY)){
......@@ -92,69 +131,6 @@ public class AggregationUtil {
return elementSet;
}
// 字符串拼接
// private static String xmlStart = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
// "<!DOCTYPE hibernate-mapping PUBLIC\n" +
// " \"-//Hibernate/Hibernate Mapping DTD 3.0//EN\"\n" +
// " \"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd\">\n" +
// "<hibernate-mapping>";
//
// private static String xmlEnd = "</hibernate-mapping>";
//
//
// public static String getOldProperty(String xml) {
// String property = "";
// Document document = null;
// try {
// document = DocumentHelper.parseText(xml);
// } catch (DocumentException e) {
// e.printStackTrace();
// }
// List<Element> elements = document.getRootElement().elements().get(0).elements();
// for (Element element : elements) {
// property = property + element.asXML();
// }
// return property;
// }
//
//
//
//
// public static String addOneToMany(String mainTable, String sideTable, String mainField, String xml) {
//
//
// String addStr =
// "<class entity-name= \"" +mainTable + "_" + sideTable + "\""+" table=\"" + mainTable +"\""+ ">\n" +
// getOldProperty(xml) +
// " <set name=\"" + sideTable + "_list" + "\""+" inverse=\"true\" lazy=\"true\">\n" +
// " <key column=\"" + mainField + "\""+"/>\n" +
// " <one-to-many class=\"" + sideTable +"\""+ "/>\n" +
// "</set>\n" +
// " </class>";
//
//
// return xmlStart+ addStr+xmlEnd;
// }
//
//
// public static String addManyToMany(String mainTable, String sideTable, String xml) {
//
// String entityName = mainTable + "_" + sideTable + "_ref";
//
// String addStr = "<class entity-name=\"" + entityName +"\""+ " table=\"" + mainTable +"\""+ ">\n" +
// " \n"
// +
// getOldProperty(xml)
// +
// " <set name=\"" + sideTable+"\"" + " table=\"" + entityName+"\"" + ">\n" +
// " \t<key column=\"" + mainTable + "_id" +"\""+ "/>\n" +
// " \t<many-to-many class=\"" + sideTable +"\""+ " column=\"" + sideTable +"\""+ "_id" + "/>\n" +
// " </set>" +
// "</class>";
// return xmlStart+ addStr+xmlEnd;
// }
//
......
......@@ -37,9 +37,7 @@ public class CreateTableUtil {
}
xmlMapping += " </class>\n" +
"</hibernate-mapping>";
return xmlMapping;
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论