report generator

This commit is contained in:
devezhao 2019-08-16 22:20:27 +08:00
parent 24e0a30911
commit f222e60c73
14 changed files with 397 additions and 35 deletions

30
pom.xml
View file

@ -276,6 +276,11 @@
<artifactId>flexmark-ext-tables</artifactId>
<version>0.50.4</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.0.1</version>
</dependency>
<!--
<dependency>
<groupId>org.springframework.data</groupId>
@ -288,15 +293,30 @@
<version>140</version>
</dependency>
-->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>1.1.2-beta5</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-poi</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
</project>

View file

@ -0,0 +1,71 @@
/*
rebuild - Building your business-systems freely.
Copyright (C) 2019 devezhao <zhaofang123@gmail.com>
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 <https://www.gnu.org/licenses/>.
*/
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<String> extract() {
List<Object> 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<String> 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;
}
}

View file

@ -18,6 +18,34 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
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<String, Object> 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<String, Object> getDataOfRecord() {
Entity entity = MetadataHelper.getEntity(this.record.getEntityCode());
Set<String> vars = new ExtractTemplateVars(this.template).extract();
List<String> 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<String, Object> data = new HashMap<>();
for (Iterator<String> 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;
}
}

View file

@ -19,6 +19,15 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
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<Entity> {
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<ConfigEntry> 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);
}
}

View file

@ -58,7 +58,7 @@ public class RobotApprovalManager implements ConfigManager<Entity> {
}
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]);

View file

@ -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;
}

View file

@ -4,12 +4,12 @@
<head>
<%@ include file="/_include/Head.jsp"%>
<link rel="stylesheet" type="text/css" href="${baseUrl}/assets/css/classification.css">
<title>分类数据编辑</title>
<title>分类数据</title>
</head>
<body>
<div class="rb-wrapper rb-fixed-sidebar rb-collapsible-sidebar rb-collapsible-sidebar-hide-logo rb-color-header">
<jsp:include page="/_include/NavTop.jsp">
<jsp:param value="分类数据编辑" name="pageTitle"/>
<jsp:param value="分类数据" name="pageTitle"/>
</jsp:include>
<jsp:include page="/_include/NavLeftAdmin.jsp">
<jsp:param value="classifications" name="activeNav"/>
@ -17,7 +17,7 @@
<div class="rb-content">
<div class="page-head">
<div class="float-left">
<div class="page-head-title">编辑分类数据<span class="sub-title">${name}</span></div>
<div class="page-head-title">分类数据<span class="sub-title">${name}</span></div>
</div>
<div class="float-right pt-1">
<button class="btn btn-secondary J_imports" type="button"><i class="zmdi zmdi-cloud-download"></i> 导入公共数据</button>

View file

@ -154,6 +154,6 @@
</div>
</div>
<%@ include file="/_include/Foot.jsp"%>
<script src="${baseUrl}/assets/js/re/data-importer.jsx" type="text/babel"></script>
<script src="${baseUrl}/assets/js/entityhub/data-importer.jsx" type="text/babel"></script>
</body>
</html>

View file

@ -4,12 +4,12 @@
<head>
<%@ include file="/_include/Head.jsp"%>
<link rel="stylesheet" type="text/css" href="${baseUrl}/assets/css/approvals.css">
<title>编辑审批流程</title>
<title>审批流程</title>
</head>
<body>
<div class="rb-wrapper rb-fixed-sidebar rb-collapsible-sidebar rb-collapsible-sidebar-hide-logo rb-color-header">
<jsp:include page="/_include/NavTop.jsp">
<jsp:param value="编辑触发器" name="pageTitle"/>
<jsp:param value="审批流程" name="pageTitle"/>
</jsp:include>
<jsp:include page="/_include/NavLeftAdmin.jsp">
<jsp:param value="robot-approval" name="activeNav"/>
@ -17,7 +17,7 @@
<div class="rb-content">
<div class="page-head">
<div class="float-left">
<div class="page-head-title">编辑审批流程<span class="sub-title">${name}</span></div>
<div class="page-head-title">审批流程<span class="sub-title">${name}</span></div>
</div>
<div class="float-right pt-1">
<div class="btn-group btn-space">
@ -30,17 +30,15 @@
</div>
<div class="clearfix"></div>
</div>
<div class="main-content container-fluid pt-1">
<div class="main-content container-fluid pt-0">
<div class="row wizard-row">
<div class="col-md-12 fuelux">
<div class="wizard wizard-ux rounded">
<div class="steps-container">
<ul class="steps">
<li data-step="1">基本配置 <i class="chevron"></i></li>
<li data-step="2" class="active">流程设计 <i class="chevron"></i></li>
</ul>
<div class="step-content p-0">
<div data-step="1" class="step-pane"></div>
<div data-step="2" class="step-pane active">
<div class="rbflow-design rb-loading rb-loading-active" id="rbflow">
<%@ include file="/_include/spinner.jsp"%>

View file

@ -4,22 +4,22 @@
<head>
<%@ include file="/_include/Head.jsp"%>
<link rel="stylesheet" type="text/css" href="${baseUrl}/assets/css/triggers.css">
<title>编辑触发器</title>
<title>触发器</title>
</head>
<body>
<div class="rb-wrapper rb-fixed-sidebar rb-collapsible-sidebar rb-collapsible-sidebar-hide-logo rb-color-header">
<jsp:include page="/_include/NavTop.jsp">
<jsp:param value="编辑触发器" name="pageTitle"/>
<jsp:param value="触发器" name="pageTitle"/>
</jsp:include>
<jsp:include page="/_include/NavLeftAdmin.jsp">
<jsp:param value="robot-trigger" name="activeNav"/>
</jsp:include>
<div class="rb-content">
<div class="page-head">
<div class="float-left"><div class="page-head-title">编辑触发器</div></div>
<div class="page-head-title">触发器<span class="sub-title">${name}</span></div>
<div class="clearfix"></div>
</div>
<div class="main-content container-fluid pt-1">
<div class="main-content container-fluid pt-0">
<div class="card mb-0">
<div class="card-body">
<ul class="timeline spare">

View file

@ -1952,13 +1952,16 @@ form {
font-size: 1.077rem;
display: inline-block;
margin-left: 10px;
font-weight: bold;
line-height: 20px;
}
.page-head-title .sub-title::before {
display: inline-block;
padding-right: .6154rem;
color: #878787;
color: #aaa;
content: "/";
font-weight: normal;
}
.approval-pane .alert .message {

View file

@ -18,25 +18,26 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
package com.rebuild.server;
import java.io.File;
import java.net.URL;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import cn.devezhao.persist4j.Entity;
import cn.devezhao.persist4j.Record;
import cn.devezhao.persist4j.engine.ID;
import com.alibaba.fastjson.JSON;
import com.rebuild.server.business.rbstore.MetaschemaImporter;
import com.rebuild.server.metadata.EntityHelper;
import com.rebuild.server.metadata.MetadataHelper;
import com.rebuild.server.metadata.entity.DisplayType;
import com.rebuild.server.metadata.entity.Entity2Schema;
import com.rebuild.server.metadata.entity.Field2Schema;
import com.rebuild.server.service.bizz.UserService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import cn.devezhao.persist4j.Entity;
import cn.devezhao.persist4j.engine.ID;
import java.io.File;
import java.net.URL;
/**
* 测试基类
@ -98,8 +99,7 @@ public class TestSupport {
Entity testEntity = MetadataHelper.getEntity(entityName);
for (DisplayType dt : DisplayType.values()) {
if (dt == DisplayType.ID || dt == DisplayType.LOCATION || dt == DisplayType.ANYREFERENCE
|| dt == DisplayType.BOOL || dt == DisplayType.AVATAR) {
if (dt == DisplayType.ID || dt == DisplayType.LOCATION || dt == DisplayType.ANYREFERENCE) {
continue;
}
@ -150,4 +150,21 @@ public class TestSupport {
new MetaschemaImporter(UserService.ADMIN_USER, JSON.parseObject(content)).exec();
}
}
/**
* @return
*/
protected static ID addRecordOfTestAllFields() {
final ID opUser = UserService.ADMIN_USER;
Entity test = MetadataHelper.getEntity(TEST_ENTITY);
Record record = EntityHelper.forNew(test.getEntityCode(), opUser);
record.setString("text", "TEXT-" + RandomUtils.nextLong());
try {
Application.getSessionStore().set(opUser);
record = Application.getGeneralEntityService().create(record);
return record.getPrimary();
} finally {
Application.getSessionStore().clean();
}
}
}

View file

@ -0,0 +1,54 @@
/*
rebuild - Building your business-systems freely.
Copyright (C) 2019 devezhao <zhaofang123@gmail.com>
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 <https://www.gnu.org/licenses/>.
*/
package com.rebuild.server.business;
import cn.devezhao.persist4j.engine.ID;
import com.rebuild.server.TestSupport;
import com.rebuild.server.business.datareport.ExtractTemplateVars;
import com.rebuild.server.business.datareport.ReportGenerator;
import com.rebuild.server.service.bizz.UserService;
import org.junit.Test;
import org.springframework.util.ResourceUtils;
import java.io.File;
import java.util.Set;
/**
* @author devezhao
* @since 2019/8/16
*/
public class DataReportGeneratorTest extends TestSupport {
@Test
public void testGenerator() throws Exception {
File template = ResourceUtils.getFile("classpath:data-report.xlsx");
ID record = addRecordOfTestAllFields();
ReportGenerator generator = new ReportGenerator(template, record);
generator.setUser(UserService.ADMIN_USER);
File file = generator.generate();
System.out.println(file);
}
@Test
public void testExtractTemplateVars() throws Exception {
File template = ResourceUtils.getFile("classpath:data-report.xlsx");
Set<String> vars = new ExtractTemplateVars(template).extract();
System.out.println(vars);
}
}

Binary file not shown.