diff --git a/pom.xml b/pom.xml index a4c5dd7c3..b6a91c5f9 100644 --- a/pom.xml +++ b/pom.xml @@ -276,6 +276,11 @@ flexmark-ext-tables 0.50.4 + + com.squareup.okhttp3 + okhttp + 4.0.1 + - - com.squareup.okhttp3 - okhttp - 4.0.1 - com.alibaba easyexcel 1.1.2-beta5 + + org.jxls + jxls + 2.6.0 + + + org.jxls + jxls-poi + 1.2.0 + + + org.apache.poi + poi + 4.1.0 + + + org.apache.poi + poi-ooxml + 4.1.0 + diff --git a/src/main/java/com/rebuild/server/business/datareport/ExtractTemplateVars.java b/src/main/java/com/rebuild/server/business/datareport/ExtractTemplateVars.java new file mode 100644 index 000000000..5dab3d704 --- /dev/null +++ b/src/main/java/com/rebuild/server/business/datareport/ExtractTemplateVars.java @@ -0,0 +1,71 @@ +/* +rebuild - Building your business-systems freely. +Copyright (C) 2019 devezhao + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package com.rebuild.server.business.datareport; + +import com.alibaba.excel.EasyExcelFactory; +import com.alibaba.excel.metadata.Sheet; +import com.rebuild.server.RebuildException; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * 提取模板中的变量 ${foo} > foo + * + * @author devezhao + * @since 2019/8/16 + */ +public class ExtractTemplateVars { + + private File template; + + public ExtractTemplateVars(File template) { + this.template = template; + } + + /** + * @return + */ + public Set extract() { + List rows = null; + try (InputStream is = new FileInputStream(this.template)) { + rows = EasyExcelFactory.read(is, new Sheet(1, 0)); + } catch (IOException ex) { + throw new RebuildException(ex); + } + + Set vars = new HashSet<>(); + for (Object row : rows) { + List list = (List) row; + for (Object cell : list) { + if (cell != null && cell.toString().matches("\\$\\{\\w+\\}")) { + String cellVar = cell.toString(); + cellVar = cellVar.substring(2, cellVar.length() - 1); + vars.add(cellVar); + } + } + } + return vars; + } +} diff --git a/src/main/java/com/rebuild/server/business/datareport/ReportGenerator.java b/src/main/java/com/rebuild/server/business/datareport/ReportGenerator.java index 5e83e0043..49ddfd067 100644 --- a/src/main/java/com/rebuild/server/business/datareport/ReportGenerator.java +++ b/src/main/java/com/rebuild/server/business/datareport/ReportGenerator.java @@ -18,6 +18,34 @@ along with this program. If not, see . package com.rebuild.server.business.datareport; +import cn.devezhao.persist4j.Entity; +import cn.devezhao.persist4j.Field; +import cn.devezhao.persist4j.Record; +import cn.devezhao.persist4j.engine.ID; +import com.rebuild.server.Application; +import com.rebuild.server.RebuildException; +import com.rebuild.server.configuration.DataReportManager; +import com.rebuild.server.configuration.portals.FieldValueWrapper; +import com.rebuild.server.helper.SysConfiguration; +import com.rebuild.server.metadata.MetadataHelper; +import org.apache.commons.lang.StringUtils; +import org.jxls.common.Context; +import org.jxls.util.JxlsHelper; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + /** * TODO * @@ -26,4 +54,89 @@ package com.rebuild.server.business.datareport; */ public class ReportGenerator { + private File template; + private ID record; + + private ID user; + + /** + * @param reportId + * @param record + */ + public ReportGenerator(ID reportId, ID record) { + this(DataReportManager.instance.getTemplate(MetadataHelper.getEntity(record.getEntityCode()), reportId), record); + } + + /** + * @param template + * @param record + */ + public ReportGenerator(File template, ID record) { + this.template = template; + this.record = record; + } + + /** + * @param user + */ + public void setUser(ID user) { + this.user = user; + } + + /** + * @return file in temp + */ + public File generate() { + String excelSuffix = this.template.getName().endsWith(".xlsx") ? ".xlsx" : ".xls"; + File dest = SysConfiguration.getFileOfTemp("REPORT-" + System.currentTimeMillis() + excelSuffix); + + try(InputStream is = new FileInputStream(template)) { + try (OutputStream os = new FileOutputStream(dest)) { + Map data = getDataOfRecord(); + Context context = new Context(data); + JxlsHelper.getInstance().processTemplate(is, os, context); + } + } catch (IOException ex) { + throw new RebuildException(ex); + } + return dest; + } + + + /** + * 从模板中读取变量并查询数据 + * + * @return + */ + protected Map getDataOfRecord() { + Entity entity = MetadataHelper.getEntity(this.record.getEntityCode()); + Set vars = new ExtractTemplateVars(this.template).extract(); + + List validFields = new ArrayList<>(); + for (String field : vars) { + if (MetadataHelper.getLastJoinField(entity, field) != null) { + validFields.add(field); + } + } + + if (validFields.isEmpty()) { + return Collections.emptyMap(); + } + + String sql = String.format("select %s from %s where %s = ?", + StringUtils.join(validFields, ","), entity.getName(), entity.getPrimaryField().getName()); + Record record = Application.getQueryFactory().createQuery(sql, this.user).setParameter(1, this.record).record(); + + Map data = new HashMap<>(); + for (Iterator iter = record.getAvailableFieldIterator(); iter.hasNext(); ) { + String name = iter.next(); + Field field = MetadataHelper.getLastJoinField(entity, name); + Object fieldValue = FieldValueWrapper.instance.wrapFieldValue(record.getObjectValue(name), field); + + // TODO 不同类型的处理 + + data.put(name, fieldValue); + } + return data; + } } diff --git a/src/main/java/com/rebuild/server/configuration/DataReportManager.java b/src/main/java/com/rebuild/server/configuration/DataReportManager.java index d4253f885..4c8a18640 100644 --- a/src/main/java/com/rebuild/server/configuration/DataReportManager.java +++ b/src/main/java/com/rebuild/server/configuration/DataReportManager.java @@ -19,6 +19,15 @@ along with this program. If not, see . package com.rebuild.server.configuration; import cn.devezhao.persist4j.Entity; +import cn.devezhao.persist4j.engine.ID; +import com.alibaba.fastjson.JSONArray; +import com.rebuild.server.Application; +import com.rebuild.server.helper.ConfigurationException; +import com.rebuild.server.helper.SysConfiguration; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; /** * 数据报表 @@ -31,7 +40,83 @@ public class DataReportManager implements ConfigManager { public static final DataReportManager instance = new DataReportManager(); private DataReportManager() {} + /** + * 获取报表列表 + * + * @param entity + * @return + */ + public JSONArray getReports(Entity entity) { + JSONArray list = new JSONArray(); + for (ConfigEntry e : getReportsRaw(entity)) { + if (!e.getBoolean("disabled")) { + list.add(e.toJSON()); + } + } + return list; + } + + /** + * 获取报表列表(含禁用) + * + * @param entity + * @return + */ + public ConfigEntry[] getReportsRaw(Entity entity) { + final String cKey = "DataReportManager-" + entity.getName(); + ConfigEntry[] reports = (ConfigEntry[]) Application.getCommonCache().getx(cKey); + if (reports != null) { + return reports; + } + + Object[][] array = Application.createQueryNoFilter( + "select configId,name,isDisabled,templateFile from DataReportConfig where belongEntity = ?") + .setParameter(1, entity.getName()) + .array(); + + List list = new ArrayList<>(); + for (Object[] o : array) { + ConfigEntry e = new ConfigEntry(); + e.set("id", o[0]); + e.set("name", o[1]); + e.set("disabled", o[2]); + e.set("template", o[3]); + list.add(e); + } + + reports = list.toArray(new ConfigEntry[list.size()]); + Application.getCommonCache().putx(cKey, reports); + return reports; + } + + /** + * @param entity + * @param reportId + * @return + */ + public File getTemplate(Entity entity, ID reportId) { + String template = null; + for (ConfigEntry e : getReportsRaw(entity)) { + if (e.getID("id").equals(reportId)) { + template = e.getString("template"); + break; + } + } + + if (template == null) { + throw new ConfigurationException("No template of report found : " + reportId); + } + + File file = SysConfiguration.getFileOfData(template); + if (!file.exists()) { + throw new ConfigurationException("Template file not extsts : " + file); + } + return file; + } + @Override public void clean(Entity cacheKey) { + final String cKey = "DataReportManager-" + cacheKey.getName(); + Application.getCommonCache().evict(cKey); } } diff --git a/src/main/java/com/rebuild/server/configuration/RobotApprovalManager.java b/src/main/java/com/rebuild/server/configuration/RobotApprovalManager.java index 5fa17806d..61765d6d7 100644 --- a/src/main/java/com/rebuild/server/configuration/RobotApprovalManager.java +++ b/src/main/java/com/rebuild/server/configuration/RobotApprovalManager.java @@ -58,7 +58,7 @@ public class RobotApprovalManager implements ConfigManager { } if (record != null) { - Object[] o = Application.getQueryFactory().unique( + Object[] o = Application.getQueryFactory().unique( record, EntityHelper.ApprovalId, EntityHelper.ApprovalState); if (o != null) { return (ApprovalState) ApprovalState.valueOf((Integer) o[1]); diff --git a/src/main/java/com/rebuild/web/admin/robot/RobotTriggerControll.java b/src/main/java/com/rebuild/web/admin/robot/RobotTriggerControll.java index ac1aceb75..8218f6f56 100644 --- a/src/main/java/com/rebuild/web/admin/robot/RobotTriggerControll.java +++ b/src/main/java/com/rebuild/web/admin/robot/RobotTriggerControll.java @@ -60,7 +60,7 @@ public class RobotTriggerControll extends BasePageControll { HttpServletRequest request, HttpServletResponse response) throws IOException { ID configId = ID.valueOf(id); Object[] config = Application.createQuery( - "select belongEntity,actionType,when,whenFilter,actionContent,priority from RobotTriggerConfig where configId = ?") + "select belongEntity,actionType,when,whenFilter,actionContent,priority,name from RobotTriggerConfig where configId = ?") .setParameter(1, configId) .unique(); if (config == null) { @@ -81,6 +81,7 @@ public class RobotTriggerControll extends BasePageControll { mv.getModel().put("whenFilter", StringUtils.defaultIfBlank((String) config[3], JSONUtils.EMPTY_OBJECT_STR)); mv.getModel().put("actionContent", StringUtils.defaultIfBlank((String) config[4], JSONUtils.EMPTY_OBJECT_STR)); mv.getModel().put("priority", config[5]); + mv.getModel().put("name", config[6]); return mv; } diff --git a/src/main/webapp/admin/entityhub/classification-editor.jsp b/src/main/webapp/admin/entityhub/classification-editor.jsp index 9ab662b70..b77d9341e 100644 --- a/src/main/webapp/admin/entityhub/classification-editor.jsp +++ b/src/main/webapp/admin/entityhub/classification-editor.jsp @@ -4,12 +4,12 @@ <%@ include file="/_include/Head.jsp"%> -分类数据编辑 +分类数据