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.
136 lines
4.9 KiB
136 lines
4.9 KiB
/* |
|
* Copyright (C), 2015-2019 |
|
* FileName: ReportDataHandler |
|
* Author: Louis |
|
* 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.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 fr.open |
|
* @since 1.0.0 |
|
*/ |
|
@EnableMetrics |
|
public class ReportDataHandler extends BaseHttpHandler { |
|
|
|
public static final String API_REPORT = "/api/report"; |
|
|
|
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 int isErrorParams(JSONObject params) { |
|
if (!params.has("report_path") || StringUtils.isEmpty(params.getString("report_path"))) { |
|
return 10; |
|
} |
|
if (!params.has("start_page") || ComparatorUtils.equals(params.getInt("start_page"), 0)) { |
|
return 16; |
|
} |
|
if (!params.has("end_page") || ComparatorUtils.equals(params.getInt("end_page"), 0)) { |
|
return 17; |
|
} |
|
String appKey = ConfigTableDataService.getInstance().getAppKey(); |
|
if (StringUtils.isNotEmpty(appKey) && !FuncUtils.validateMD5(appKey, params.getString("report_path"), params.getString("timestamp"), params.getString("sign"))) { |
|
return 15; |
|
} |
|
return 0; |
|
} |
|
|
|
@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); |
|
LogKit.info("tabledataservice-ReportDataHandler:handle-params, {}", params.toString()); |
|
int errorCode = isErrorParams(params); |
|
if (!ComparatorUtils.equals(errorCode, 0)) { |
|
FuncUtils.printErrorJSON(res, errorCode); |
|
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); |
|
} |
|
} |