You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
185 lines
6.7 KiB
185 lines
6.7 KiB
3 years ago
|
/*
|
||
|
* Copyright (C), 2015-2019
|
||
|
* FileName: TableDataHandler
|
||
|
* Author: xx
|
||
|
* Date: 2019/8/17 22:28
|
||
|
* Description: PublicHttpHandler
|
||
|
* History:
|
||
|
* <author> <time> <version> <desc>
|
||
|
*/
|
||
|
package com.fr.plugin.tabledataservice.request;
|
||
|
|
||
|
import com.fanruan.api.i18n.I18nKit;
|
||
|
import com.fanruan.api.log.LogKit;
|
||
|
import com.fr.base.Parameter;
|
||
|
import com.fr.base.TableData;
|
||
|
import com.fr.data.impl.ConditionTableData;
|
||
|
import com.fr.decision.fun.impl.BaseHttpHandler;
|
||
|
import com.fr.general.ComparatorUtils;
|
||
|
import com.fr.general.data.DataModel;
|
||
|
import com.fr.intelli.record.Focus;
|
||
|
import com.fr.intelli.record.Original;
|
||
|
import com.fr.io.TemplateWorkBookIO;
|
||
|
import com.fr.json.JSONArray;
|
||
|
import com.fr.json.JSONObject;
|
||
|
import com.fr.log.FineLoggerFactory;
|
||
|
import com.fr.main.TemplateWorkBook;
|
||
|
import com.fr.plugin.context.PluginContexts;
|
||
|
import com.fr.plugin.tabledataservice.ConfigTableDataService;
|
||
|
import com.fr.plugin.tabledataservice.Constants;
|
||
|
import com.fr.plugin.tabledataservice.utils.FuncUtils;
|
||
|
import com.fr.record.analyzer.EnableMetrics;
|
||
|
import com.fr.script.Calculator;
|
||
|
import com.fr.stable.StringUtils;
|
||
|
import com.fr.third.springframework.web.bind.annotation.RequestMethod;
|
||
|
import com.fr.web.utils.WebUtils;
|
||
|
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
|
|
||
|
/**
|
||
|
* 〈Function Description〉<br>
|
||
|
* 〈TableDataHandler〉
|
||
|
*
|
||
|
* @author xx
|
||
|
* @since 1.0.0
|
||
|
*/
|
||
|
@EnableMetrics
|
||
|
public class TableDataHandler extends BaseHttpHandler {
|
||
|
|
||
|
public static final int TOTAL_PAGE_NUMBER = 1;
|
||
|
public static final int START_ROW = 0;
|
||
|
|
||
|
@Override
|
||
|
public RequestMethod getMethod() {
|
||
|
return RequestMethod.POST;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public String getPath() {
|
||
|
return "/api/data";
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean isPublic() {
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
@Focus(id = Constants.PLUGIN_ID, text = "Plugin-tabledataservice", source = Original.PLUGIN)
|
||
|
public void handle(HttpServletRequest req, HttpServletResponse res) throws Exception {
|
||
|
if (!PluginContexts.currentContext().isAvailable()) {
|
||
|
LogKit.error(I18nKit.getLocText("Plugin-tabledataservice_Licence_Expired"));
|
||
|
FuncUtils.printErrorJSON(res, 21);
|
||
|
return;
|
||
|
}
|
||
|
JSONObject params = FuncUtils.getParams(req);
|
||
|
FineLoggerFactory.getLogger().info("tabledataservice:handle-params, {}", params.toString());
|
||
|
if (isErrorParams(res, params)) {
|
||
|
return;
|
||
|
}
|
||
|
TemplateWorkBook workBook = TemplateWorkBookIO.readTemplateWorkBook(params.getString("report_path"));
|
||
|
if (workBook == null) {
|
||
|
FuncUtils.printErrorJSON(res, 14);
|
||
|
return;
|
||
|
}
|
||
|
TableData tableData = workBook.getTableData(params.getString("datasource_name"));
|
||
|
if (tableData == null) {
|
||
|
FuncUtils.printErrorJSON(res, 20);
|
||
|
return;
|
||
|
}
|
||
|
if (params.has("parameters")) {
|
||
|
if (tableData instanceof ConditionTableData) {
|
||
|
((ConditionTableData) tableData).setDefineParameters(getParameters(params.getJSONArray("parameters")));
|
||
|
} else {
|
||
|
tableData.setParameters(getParameters(params.getJSONArray("parameters")));
|
||
|
}
|
||
|
}
|
||
|
JSONObject resultJSON = tableDataToJSON(tableData, params.getInt("page_number"), params.getInt("page_size"));
|
||
|
WebUtils.printAsJSON(res, resultJSON);
|
||
|
}
|
||
|
|
||
|
private Parameter[] getParameters(JSONArray param) {
|
||
|
if (param == null) {
|
||
|
return null;
|
||
|
}
|
||
|
Parameter[] parameters = new Parameter[param.size()];
|
||
|
for (int i = 0; i < param.size(); i++) {
|
||
|
parameters[i] = Parameter.getParameterFromJson(param.getJSONObject(i));
|
||
|
}
|
||
|
return parameters;
|
||
|
}
|
||
|
|
||
|
private boolean isErrorParams(HttpServletResponse res, JSONObject params) throws Exception {
|
||
|
if (!params.has("report_path") || StringUtils.isEmpty(params.getString("report_path"))) {
|
||
|
FuncUtils.printErrorJSON(res, 10);
|
||
|
return true;
|
||
|
}
|
||
|
if (!params.has("datasource_name") || StringUtils.isEmpty(params.getString("datasource_name"))) {
|
||
|
FuncUtils.printErrorJSON(res, 11);
|
||
|
return true;
|
||
|
}
|
||
|
if (!params.has("page_number") || ComparatorUtils.equals(params.getInt("page_number"), 0)) {
|
||
|
FuncUtils.printErrorJSON(res, 12);
|
||
|
return true;
|
||
|
}
|
||
|
if (!params.has("page_size") || ComparatorUtils.equals(params.getInt("page_size"), 0)) {
|
||
|
FuncUtils.printErrorJSON(res, 13);
|
||
|
return true;
|
||
|
}
|
||
|
String appKey = ConfigTableDataService.getInstance().getAppKey();
|
||
|
if (StringUtils.isNotEmpty(appKey)
|
||
|
&& !FuncUtils.validateMD5(appKey, params)) {
|
||
|
FuncUtils.printErrorJSON(res, 15);
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* tableData转为JSON
|
||
|
* 参考函数 ReportUtils.tableDataToFlexgridObject4WebPreview()修改
|
||
|
*
|
||
|
* @param tableData
|
||
|
* @param pageNumber
|
||
|
* @param pageSize
|
||
|
* @return
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
private JSONObject tableDataToJSON(TableData tableData, int pageNumber, int pageSize) throws Exception {
|
||
|
JSONObject resultJSON = JSONObject.create();
|
||
|
DataModel dataModel = null;
|
||
|
try {
|
||
|
dataModel = tableData.createDataModel(Calculator.createCalculator());
|
||
|
int rowCount = dataModel.getRowCount();
|
||
|
int totalPageNumber = TOTAL_PAGE_NUMBER;
|
||
|
int startRow = START_ROW;
|
||
|
int endRow = rowCount - 1;
|
||
|
if (pageNumber > 0 && pageSize > 0) {
|
||
|
totalPageNumber = (rowCount + pageSize - 1) / pageSize;
|
||
|
startRow = (pageNumber - 1) * pageSize;
|
||
|
endRow = (pageNumber * pageSize) - 1;
|
||
|
}
|
||
|
resultJSON.put("err_code", 0).put("err_msg", "")
|
||
|
.put("total_page_number", totalPageNumber)
|
||
|
.put("page_number", pageNumber)
|
||
|
.put("page_size", pageSize);
|
||
|
JSONArray data = JSONArray.create();
|
||
|
for (int row = startRow; row <= endRow && row < rowCount; row++) {
|
||
|
JSONObject rowObject = JSONObject.create();
|
||
|
for (int col = 0; col < dataModel.getColumnCount(); col++) {
|
||
|
rowObject.put(dataModel.getColumnName(col), dataModel.getValueAt(row, col));
|
||
|
}
|
||
|
data.put(rowObject);
|
||
|
}
|
||
|
FuncUtils.encryptData(data.encode(), resultJSON);
|
||
|
} catch (Exception e) {
|
||
|
FineLoggerFactory.getLogger().error(e.getMessage(), e);
|
||
|
} finally {
|
||
|
if (dataModel != null) {
|
||
|
dataModel.release();
|
||
|
}
|
||
|
}
|
||
|
return resultJSON;
|
||
|
}
|
||
|
}
|