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.
140 lines
5.0 KiB
140 lines
5.0 KiB
3 years ago
|
/*
|
||
|
* Copyright (C), 2015-2019
|
||
|
* FileName: ReportDataHandler
|
||
|
* Author: xx
|
||
|
* Date: 2019/8/22 8:34
|
||
|
* Description: ReportDataHandler
|
||
|
* 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.fanruan.api.report.ActorKit;
|
||
|
import com.fr.base.Parameter;
|
||
|
import com.fr.decision.fun.impl.BaseHttpHandler;
|
||
|
import com.fr.general.ComparatorUtils;
|
||
|
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.main.workbook.ResultWorkBook;
|
||
|
import com.fr.plugin.context.PluginContexts;
|
||
|
import com.fr.plugin.tabledataservice.ConfigTableDataService;
|
||
|
import com.fr.plugin.tabledataservice.Constants;
|
||
|
import com.fr.plugin.tabledataservice.exporter.JSONExporter;
|
||
|
import com.fr.plugin.tabledataservice.utils.FuncUtils;
|
||
|
import com.fr.record.analyzer.EnableMetrics;
|
||
|
import com.fr.stable.ActorConstants;
|
||
|
import com.fr.stable.ParameterProvider;
|
||
|
import com.fr.stable.StringUtils;
|
||
|
import com.fr.third.springframework.web.bind.annotation.RequestMethod;
|
||
|
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import javax.servlet.http.HttpServletResponse;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.Map;
|
||
|
|
||
|
/**
|
||
|
* 〈Function Description〉<br>
|
||
|
* 〈ReportDataHandler〉
|
||
|
*
|
||
|
* @author xx
|
||
|
* @since 1.0.0
|
||
|
*/
|
||
|
@EnableMetrics
|
||
|
public class ReportDataHandler extends BaseHttpHandler {
|
||
|
|
||
|
public static final String API_REPORT = "/api/report";
|
||
|
|
||
|
@Override
|
||
|
public RequestMethod getMethod() {
|
||
|
return RequestMethod.POST;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public String getPath() {
|
||
|
return API_REPORT;
|
||
|
}
|
||
|
|
||
|
@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-ReportDataHandler: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;
|
||
|
}
|
||
|
JSONArray paramsJSON = params.has("parameters") ? params.getJSONArray("parameters") : null;
|
||
|
ResultWorkBook resultWorkBook = workBook.execute(getParameters(paramsJSON), ActorKit.getActor(ActorConstants.TYPE_PAGE));
|
||
|
JSONExporter jsonExporter = new JSONExporter();
|
||
|
jsonExporter.setExporterObject(params);
|
||
|
jsonExporter.export(res.getOutputStream(), resultWorkBook);
|
||
|
}
|
||
|
|
||
|
private HashMap<String, Object> getParameters(JSONArray param) {
|
||
|
if (param == null) {
|
||
|
return null;
|
||
|
}
|
||
|
ParameterProvider[] parameters = new ParameterProvider[param.size()];
|
||
|
for (int i = 0; i < param.size(); i++) {
|
||
|
parameters[i] = Parameter.getParameterFromJson(param.getJSONObject(i));
|
||
|
}
|
||
|
HashMap<String, Object> parametersMap = new HashMap<String, Object>();
|
||
|
addPara2Map(parametersMap, parameters);
|
||
|
return parametersMap;
|
||
|
}
|
||
|
|
||
|
private void addPara2Map(Map<String, Object> parametersMap, ParameterProvider[] parameters) {
|
||
|
if (parameters == null) {
|
||
|
return;
|
||
|
}
|
||
|
for (ParameterProvider parameter : parameters) {
|
||
|
if (parameter == null || parameter.getName() == null) {
|
||
|
return;
|
||
|
}
|
||
|
parametersMap.put(parameter.getName().toUpperCase(), parameter.getValue());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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("start_page") || ComparatorUtils.equals(params.getInt("start_page"), 0)) {
|
||
|
FuncUtils.printErrorJSON(res, 16);
|
||
|
return true;
|
||
|
}
|
||
|
if (!params.has("end_page") || ComparatorUtils.equals(params.getInt("end_page"), 0)) {
|
||
|
FuncUtils.printErrorJSON(res, 17);
|
||
|
return true;
|
||
|
}
|
||
|
String appKey = ConfigTableDataService.getInstance().getAppKey();
|
||
|
if (StringUtils.isNotEmpty(appKey)
|
||
|
&& !FuncUtils.validateMD5(appKey, params)) {
|
||
|
FuncUtils.printErrorJSON(res, 15);
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
}
|