Browse Source

数据导出-策略配置

master
王运杰 12 months ago
parent
commit
b68c4d52af
  1. 2
      modules/export/src/main/resources/domains/CommonBoolean.xml
  2. 2
      modules/export/src/main/resources/domains/ExportStrategy.xml
  3. 2
      modules/export/src/main/resources/domains/ExportTemplate.xml
  4. 16
      src/main/java/com/hypass/export/config/ExtendModule.java
  5. 119
      src/main/java/com/hypass/export/controller/ExportController.java
  6. 25
      src/main/java/com/hypass/export/model/CascadeModelFieldDTO.java
  7. 100
      src/main/java/com/hypass/export/model/ExportStrategyDTO.java
  8. 61
      src/main/java/com/hypass/export/model/ExportTemplateDTO.java
  9. 55
      src/main/java/com/hypass/export/model/MetaModelDTO.java
  10. 25
      src/main/java/com/hypass/export/service/IExportService.java
  11. 151
      src/main/java/com/hypass/export/service/impl/ExportServiceImpl.java
  12. 29
      src/main/java/com/hypass/export/utils/HypassBeanUtils.java

2
modules/export/src/main/resources/domains/CommonBoolean.xml

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<domain-models xmlns="http://hypaas.com/xml/ns/domain-models" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://hypaas.com/xml/ns/domain-models http://hypaas.com/xml/ns/domain-models/domain-models_5.2.xsd"> <domain-models xmlns="http://hypaas.com/xml/ns/domain-models" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://hypaas.com/xml/ns/domain-models http://hypaas.com/xml/ns/domain-models/domain-models_5.2.xsd">
<module name="com.export.enums" package="com.export.CommonBoolean"/> <module name="com.export.enums" package="com.export.common"/>
<enum name="CommonBoolean"> <enum name="CommonBoolean">
<item name="IS" title="是" value="IS"/> <item name="IS" title="是" value="IS"/>
<item name="NOT" title="否" value="NOT"/> <item name="NOT" title="否" value="NOT"/>

2
modules/export/src/main/resources/domains/ExportStrategy.xml

@ -5,7 +5,7 @@
<string actOverwrite="false" title="导出字段名" copy="true" massUpdate="false" name="fieldName" isJudg="false"/> <string actOverwrite="false" title="导出字段名" copy="true" massUpdate="false" name="fieldName" isJudg="false"/>
<string actOverwrite="false" title="导出字段类型" copy="true" massUpdate="false" name="fieldType" isJudg="false"/> <string actOverwrite="false" title="导出字段类型" copy="true" massUpdate="false" name="fieldType" isJudg="false"/>
<string actOverwrite="false" title="导出字段属性" copy="true" massUpdate="false" name="fieldAttr" isJudg="false"/> <string actOverwrite="false" title="导出字段属性" copy="true" massUpdate="false" name="fieldAttr" isJudg="false"/>
<enum title="是否支持导出" massUpdate="false" ref="com.export.CommonBoolean.CommonBoolean" name="isExport" copy="true"/> <enum title="是否支持导出" massUpdate="false" ref="com.export.common.CommonBoolean" name="isExport" copy="true"/>
<string actOverwrite="false" title="导出内容" copy="true" massUpdate="false" name="outContent" isJudg="false"/> <string actOverwrite="false" title="导出内容" copy="true" massUpdate="false" name="outContent" isJudg="false"/>
<string actOverwrite="false" title="导出字段名称" copy="true" massUpdate="false" name="exportFiledName" isJudg="false"/> <string actOverwrite="false" title="导出字段名称" copy="true" massUpdate="false" name="exportFiledName" isJudg="false"/>
<string actOverwrite="false" title="导出后的内容" copy="true" massUpdate="false" name="exportContent" isJudg="false"/> <string actOverwrite="false" title="导出后的内容" copy="true" massUpdate="false" name="exportContent" isJudg="false"/>

2
modules/export/src/main/resources/domains/ExportTemplate.xml

@ -5,6 +5,6 @@
<string actOverwrite="false" title="模板编码" copy="true" massUpdate="false" name="code" isJudg="false"/> <string actOverwrite="false" title="模板编码" copy="true" massUpdate="false" name="code" isJudg="false"/>
<string actOverwrite="false" title="模板名称" copy="true" massUpdate="false" name="name" isJudg="false"/> <string actOverwrite="false" title="模板名称" copy="true" massUpdate="false" name="name" isJudg="false"/>
<many-to-one title="实体名称" massUpdate="false" ref="com.hypaas.meta.db.MetaJsonModel" name="entityName" copy="true"/> <many-to-one title="实体名称" massUpdate="false" ref="com.hypaas.meta.db.MetaJsonModel" name="entityName" copy="true"/>
<enum title="是否默认模板" massUpdate="false" ref="com.export.CommonBoolean.CommonBoolean" name="isDefaultTemplate" copy="true"/> <enum title="是否默认模板" massUpdate="false" ref="com.export.common.CommonBoolean" name="isDefaultTemplate" copy="true"/>
</entity> </entity>
</domain-models> </domain-models>

16
src/main/java/com/hypass/export/config/ExtendModule.java

@ -0,0 +1,16 @@
package com.hypass.export.config;
import com.google.inject.Inject;
import com.hypaas.app.HypaasModule;
import com.hypass.export.service.IExportService;
import com.hypass.export.service.impl.ExportServiceImpl;
import org.springframework.context.ApplicationContext;
public class ExtendModule extends HypaasModule {
@Inject private ApplicationContext context;
@Override
protected void configure() {
bind(IExportService.class).to(ExportServiceImpl.class);
}
}

119
src/main/java/com/hypass/export/controller/ExportController.java

@ -0,0 +1,119 @@
package com.hypass.export.controller;
import com.google.inject.Inject;
import com.google.inject.servlet.RequestScoped;
import com.hypaas.db.JpaSupport;
import com.hypaas.rpc.Response;
import com.hypass.export.model.CascadeModelFieldDTO;
import com.hypass.export.model.ExportStrategyDTO;
import com.hypass.export.model.ExportTemplateDTO;
import com.hypass.export.model.MetaModelDTO;
import com.hypass.export.service.IExportService;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
/**
* @ClassName InventoryMgrController @Description TODO @Author Will @Date 2023/7/23 18:58 @Version
* 1.0
*/
@RequestScoped
@Produces(MediaType.APPLICATION_JSON)
@Path("/export")
public class ExportController extends JpaSupport {
@Inject private IExportService exportService;
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Path("/getCascadeModelFieldList")
public Response getCascadeModelFieldList(
@QueryParam("modelName") String modelName, @QueryParam("fieldName") String fieldName) {
Response response = new Response();
List<CascadeModelFieldDTO> list = exportService.getCascadeModelFieldList(modelName, fieldName);
response.setData(list);
response.setStatus(Response.STATUS_SUCCESS);
return response;
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("/saveTemplate")
public Response saveTemplate(ExportTemplateDTO exportTemplateDTO) {
Response response = new Response();
response.setStatus(Response.STATUS_SUCCESS);
Boolean result = false;
try {
result = exportService.saveTemplate(exportTemplateDTO);
} catch (InvocationTargetException e) {
result = false;
response.setStatus(Response.STATUS_FAILURE);
} catch (IllegalAccessException e) {
result = false;
response.setStatus(Response.STATUS_FAILURE);
}
response.setData(result);
return response;
}
/**
* 查询模型列表
*
* @return
*/
@GET
@Path("/getModelList")
public Response getModelList() {
List<MetaModelDTO> modelList = exportService.getModelList();
Response response = new Response();
response.setData(modelList);
response.setStatus(Response.STATUS_SUCCESS);
return response;
}
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Path("/preConfigExportStrategy")
public Response preConfigExportStrategy(
@QueryParam("modelName") String modelName, @QueryParam("templateId") String templateId) {
List<ExportStrategyDTO> exportStrategyDTOS = null;
Response response = new Response();
exportStrategyDTOS = exportService.preConfigExportStrategy(modelName, templateId);
response.setData(exportStrategyDTOS);
response.setStatus(Response.STATUS_SUCCESS);
return response;
}
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Path("/getModelExportStrategy")
public Response getModelExportStrategy(@QueryParam("templateId") String templateId) {
ExportStrategyDTO modelExportStrategy = null;
Response response = new Response();
try {
modelExportStrategy = exportService.getModelExportStrategy(templateId);
} catch (InvocationTargetException e) {
response.setData(null);
response.setStatus(Response.STATUS_FAILURE);
} catch (IllegalAccessException e) {
response.setData(null);
response.setStatus(Response.STATUS_FAILURE);
}
response.setData(modelExportStrategy);
response.setStatus(Response.STATUS_SUCCESS);
return response;
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("/saveTemplateStrategy")
public Response saveTemplateStrategy(ExportStrategyDTO templateStrategyDTO) {
Boolean result = null;
Response response = new Response();
result = exportService.saveTemplateStrategy(templateStrategyDTO);
response.setData(result);
response.setStatus(Response.STATUS_SUCCESS);
return response;
}
}

25
src/main/java/com/hypass/export/model/CascadeModelFieldDTO.java

@ -0,0 +1,25 @@
package com.hypass.export.model;
/** @ClassName MetaModelDTO @Description TODO @Author wyj @Date 2023/11/18 15:56 @Version 1.0 */
public class CascadeModelFieldDTO {
private String name;
private String code;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}

100
src/main/java/com/hypass/export/model/ExportStrategyDTO.java

@ -0,0 +1,100 @@
package com.hypass.export.model;
import com.export.common.CommonBoolean;
import com.hypaas.auth.db.AuditableModel;
import javax.persistence.*;
public class ExportStrategyDTO extends AuditableModel {
private Long id;
private String fieldName;
private String fieldType;
private String fieldAttr;
private CommonBoolean isExport;
private String outContent;
private String exportFiledName;
private String exportContent;
private Integer templateId;
public Integer getTemplateId() {
return templateId;
}
public void setTemplateId(Integer templateId) {
this.templateId = templateId;
}
@Override
public Long getId() {
return id;
}
@Override
public void setId(Long id) {
this.id = id;
}
public String getFieldName() {
return fieldName;
}
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
public String getFieldType() {
return fieldType;
}
public void setFieldType(String fieldType) {
this.fieldType = fieldType;
}
public String getFieldAttr() {
return fieldAttr;
}
public void setFieldAttr(String fieldAttr) {
this.fieldAttr = fieldAttr;
}
public CommonBoolean getIsExport() {
return isExport;
}
public void setIsExport(CommonBoolean isExport) {
this.isExport = isExport;
}
public String getOutContent() {
return outContent;
}
public void setOutContent(String outContent) {
this.outContent = outContent;
}
public String getExportFiledName() {
return exportFiledName;
}
public void setExportFiledName(String exportFiledName) {
this.exportFiledName = exportFiledName;
}
public String getExportContent() {
return exportContent;
}
public void setExportContent(String exportContent) {
this.exportContent = exportContent;
}
}

61
src/main/java/com/hypass/export/model/ExportTemplateDTO.java

@ -0,0 +1,61 @@
package com.hypass.export.model;
import com.export.common.CommonBoolean;
import com.hypaas.meta.db.MetaJsonModel;
import javax.persistence.*;
/**
* @ClassName ExportTemplateDTO @Description TODO @Author wyj @Date 2023/11/24 10:19 @Version 1.0
*/
public class ExportTemplateDTO {
private Long id;
private String code;
private String name;
private MetaJsonModel entityName;
private CommonBoolean isDefaultTemplate;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public MetaJsonModel getEntityName() {
return entityName;
}
public void setEntityName(MetaJsonModel entityName) {
this.entityName = entityName;
}
public CommonBoolean getIsDefaultTemplate() {
return isDefaultTemplate;
}
public void setIsDefaultTemplate(CommonBoolean isDefaultTemplate) {
this.isDefaultTemplate = isDefaultTemplate;
}
}

55
src/main/java/com/hypass/export/model/MetaModelDTO.java

@ -0,0 +1,55 @@
package com.hypass.export.model;
/** @ClassName MetaModelDTO @Description TODO @Author wyj @Date 2023/11/18 15:56 @Version 1.0 */
public class MetaModelDTO {
private Long id;
private String name;
private String packageName;
private String tableName;
private String fullName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
}

25
src/main/java/com/hypass/export/service/IExportService.java

@ -0,0 +1,25 @@
package com.hypass.export.service;
import com.hypass.export.model.CascadeModelFieldDTO;
import com.hypass.export.model.ExportStrategyDTO;
import com.hypass.export.model.ExportTemplateDTO;
import com.hypass.export.model.MetaModelDTO;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
/** 文件导出接口 */
public interface IExportService {
List<MetaModelDTO> getModelList();
ExportStrategyDTO getModelExportStrategy(String templateId)
throws InvocationTargetException, IllegalAccessException;
boolean saveTemplateStrategy(ExportStrategyDTO exportStrategyDTO);
List<CascadeModelFieldDTO> getCascadeModelFieldList(String modelName, String fieldName);
List<ExportStrategyDTO> preConfigExportStrategy(String modelName, String templateId);
boolean saveTemplate(ExportTemplateDTO exportTemplateDTO)
throws InvocationTargetException, IllegalAccessException;
}

151
src/main/java/com/hypass/export/service/impl/ExportServiceImpl.java

@ -0,0 +1,151 @@
package com.hypass.export.service.impl;
import com.export.common.CommonBoolean;
import com.google.inject.Inject;
import com.hypaas.db.Query;
import com.hypaas.meta.db.MetaField;
import com.hypaas.meta.db.MetaModel;
import com.hypaas.meta.db.repo.MetaFieldRepository;
import com.hypaas.meta.db.repo.MetaModelRepository;
import com.hypass.export.ExportStrategy;
import com.hypass.export.ExportTemplate;
import com.hypass.export.model.CascadeModelFieldDTO;
import com.hypass.export.model.ExportStrategyDTO;
import com.hypass.export.model.ExportTemplateDTO;
import com.hypass.export.model.MetaModelDTO;
import com.hypass.export.repo.ExportStrategyRepository;
import com.hypass.export.repo.ExportTemplateRepository;
import com.hypass.export.service.IExportService;
import com.hypass.export.utils.HypassBeanUtils;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
/**
* @ClassName ExportServiceImpl @Description TODO @Author wyj @Date 2023/11/18 16:02 @Version 1.0
*/
public class ExportServiceImpl implements IExportService {
@Inject private MetaModelRepository metaModelRepository;
@Inject private ExportStrategyRepository exportStrategyRepository;
@Inject private ExportTemplateRepository exportTemplateRepository;
@Inject private MetaFieldRepository metaFieldRepository;
@Override
public List<MetaModelDTO> getModelList() {
List<MetaModel> fetch = metaModelRepository.all().fetch();
if (CollectionUtils.isNotEmpty(fetch)) {
return HypassBeanUtils.copyProperties4List(fetch, MetaModelDTO.class);
}
return null;
}
@Override
public ExportStrategyDTO getModelExportStrategy(String templateId)
throws InvocationTargetException, IllegalAccessException {
ExportTemplate exportTemplate = exportTemplateRepository.find(Long.valueOf(templateId));
ExportStrategy exportStrategy =
Query.of(ExportStrategy.class)
.filter("self.templateInfo = :templateInfo")
.bind("templateInfo", exportTemplate)
.fetchOne();
ExportStrategyDTO exportStrategyDTO = new ExportStrategyDTO();
HypassBeanUtils.copyProperties(exportStrategy, exportStrategyDTO);
return exportStrategyDTO;
}
@Override
public boolean saveTemplateStrategy(ExportStrategyDTO exportStrategyDTO) {
ExportStrategy exportStrategy = new ExportStrategy();
BeanUtils.copyProperties(exportStrategyDTO, exportStrategy);
exportStrategyRepository.save(exportStrategy);
return true;
}
@Override
public List<CascadeModelFieldDTO> getCascadeModelFieldList(String modelName, String fieldName) {
MetaModel metaModel = metaModelRepository.findByName(modelName);
MetaField metaField =
Query.of(MetaField.class)
.filter("self.metaModel = :metaModel and self.name = :name")
.bind("metaModel", metaModel)
.bind("name", fieldName)
.fetchOne();
String typeName = metaField.getTypeName();
MetaModel cascadeModel = metaModelRepository.findByName(typeName);
List<MetaField> fields =
Query.of(MetaField.class)
.filter("self.metaModel = :metaModel")
.bind("metaModel", cascadeModel)
.fetch();
if (CollectionUtils.isNotEmpty(fields)) {
List<CascadeModelFieldDTO> list = new ArrayList<>();
fields.forEach(
field -> {
CascadeModelFieldDTO cascadeModelDTO = new CascadeModelFieldDTO();
cascadeModelDTO.setCode(field.getName());
cascadeModelDTO.setName(field.getLabel());
list.add(cascadeModelDTO);
});
return list;
}
return null;
}
@Override
public List<ExportStrategyDTO> preConfigExportStrategy(String modelName, String templateId) {
List<ExportStrategyDTO> exportStrategyDTOS = new ArrayList<>();
ExportTemplate exportTemplate = exportTemplateRepository.find(Long.valueOf(templateId));
if (exportTemplate == null) {
MetaModel metaModel = metaModelRepository.findByName(modelName);
List<MetaField> fields =
Query.of(MetaField.class)
.filter("self.metaModel = :metaModel")
.bind("metaModel", metaModel)
.fetch();
if (CollectionUtils.isEmpty(fields)) {
return exportStrategyDTOS;
}
fields.forEach(
field -> {
ExportStrategyDTO exportStrategyDTO = new ExportStrategyDTO();
exportStrategyDTO.setFieldName(field.getName());
exportStrategyDTO.setFieldType(field.getTypeName());
if (StringUtils.isEmpty(field.getRelationship())) {
exportStrategyDTO.setFieldAttr("普通");
} else {
exportStrategyDTO.setFieldAttr(field.getRelationship());
}
exportStrategyDTO.setIsExport(CommonBoolean.NOT);
});
} else {
List<ExportStrategy> list =
Query.of(ExportStrategy.class)
.filter("self.templateInfo = :templateInfo")
.bind("templateInfo", exportTemplate)
.fetch();
exportStrategyDTOS = HypassBeanUtils.copyProperties4List(list, ExportStrategyDTO.class);
}
return null;
}
@Override
public boolean saveTemplate(ExportTemplateDTO exportTemplateDTO)
throws InvocationTargetException, IllegalAccessException {
ExportTemplate exportTemplate = new ExportTemplate();
HypassBeanUtils.copyProperties(exportTemplateDTO, exportTemplate);
if (exportTemplateDTO.getIsDefaultTemplate() != null) {
exportTemplateRepository.save(exportTemplate);
} else {
exportTemplateRepository.merge(exportTemplate);
}
return true;
}
}

29
src/main/java/com/hypass/export/utils/HypassBeanUtils.java

@ -0,0 +1,29 @@
package com.hypass.export.utils;
import cn.hutool.core.bean.BeanUtil;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
/** @ClassName HypassBeanUtils @Description TODO @Author wyj @Date 2023/2/8 18:26 @Version 1.0 */
public class HypassBeanUtils {
public static <T> List<T> copyProperties4List(List<?> sources, Class<T> destination) {
if (CollectionUtils.isEmpty(sources)) {
return new ArrayList<>();
}
List<T> list = new ArrayList<>();
for (Object source : sources) {
list.add(BeanUtil.copyProperties(source, destination));
}
return list;
}
public static void copyProperties(Object orig, Object dest)
throws InvocationTargetException, IllegalAccessException {
BeanUtils.copyProperties(orig, dest);
}
}
Loading…
Cancel
Save