From 15682fbf2a799b439a05d27c91011cb4d1787e5f Mon Sep 17 00:00:00 2001 From: plough Date: Thu, 18 Apr 2019 17:49:15 +0800 Subject: [PATCH 1/5] =?UTF-8?q?REPORT-15314=20103=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E6=94=B6=E9=9B=86=3D>=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fr/design/mainframe/JTemplate.java | 22 +- .../fr/design/mainframe/JVirtualTemplate.java | 2 +- .../mainframe/template/info/SendHelper.java | 52 ++ .../mainframe/template/info/TemplateInfo.java | 230 ++++++++ .../template/info/TemplateInfoCollector.java | 299 ++++++++++ .../info}/TemplateProcessInfo.java | 6 +- .../templateinfo/TemplateInfoCollector.java | 550 ------------------ .../designerbase/TemplateProcessTracker.aj | 58 -- .../info/TemplateInfoCollectorTest.java | 46 ++ .../designerchart/TemplateProcessTracker.aj | 3 - .../java/com/fr/design/mainframe/JForm.java | 4 +- .../info}/JFormProcessInfo.java | 2 +- .../designerform/TemplateProcessTracker.aj | 2 - .../mainframe/InformationCollector.java | 2 +- .../com/fr/design/mainframe/JWorkBook.java | 4 +- .../alphafine/component/AlphaFineDialog.java | 5 - .../info}/JWorkBookProcessInfo.java | 2 +- .../src/main/java/com/fr/start/Designer.java | 2 +- .../designer/TemplateProcessTracker.aj | 2 - 19 files changed, 645 insertions(+), 648 deletions(-) create mode 100644 designer-base/src/main/java/com/fr/design/mainframe/template/info/SendHelper.java create mode 100644 designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfo.java create mode 100644 designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java rename designer-base/src/main/java/com/fr/design/mainframe/{templateinfo => template/info}/TemplateProcessInfo.java (74%) delete mode 100644 designer-base/src/main/java/com/fr/design/mainframe/templateinfo/TemplateInfoCollector.java delete mode 100644 designer-base/src/main/resources/com/fr/aspectj/designerbase/TemplateProcessTracker.aj create mode 100644 designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoCollectorTest.java rename designer-form/src/main/java/com/fr/design/mainframe/{templateinfo => template/info}/JFormProcessInfo.java (95%) rename designer-realize/src/main/java/com/fr/design/mainframe/{templateinfo => template/info}/JWorkBookProcessInfo.java (98%) diff --git a/designer-base/src/main/java/com/fr/design/mainframe/JTemplate.java b/designer-base/src/main/java/com/fr/design/mainframe/JTemplate.java index 415ff948c6..bedeb47914 100644 --- a/designer-base/src/main/java/com/fr/design/mainframe/JTemplate.java +++ b/designer-base/src/main/java/com/fr/design/mainframe/JTemplate.java @@ -34,8 +34,8 @@ import com.fr.design.gui.imenu.UIMenuItem; import com.fr.design.gui.itree.filetree.TemplateFileTree; import com.fr.design.i18n.Toolkit; import com.fr.design.layout.FRGUIPaneFactory; -import com.fr.design.mainframe.templateinfo.TemplateInfoCollector; -import com.fr.design.mainframe.templateinfo.TemplateProcessInfo; +import com.fr.design.mainframe.template.info.TemplateInfoCollector; +import com.fr.design.mainframe.template.info.TemplateProcessInfo; import com.fr.design.mainframe.toolbar.ToolBarMenuDockPlus; import com.fr.design.mainframe.toolbar.VcsScene; import com.fr.design.menu.MenuDef; @@ -92,8 +92,6 @@ public abstract class JTemplate> private DesignModelAdapter designModel; private PreviewProvider previewType; private long openTime = 0L; // 打开模板的时间点(包括新建模板) - private TemplateInfoCollector tic = TemplateInfoCollector.getInstance(); - private StringBuilder process = new StringBuilder(""); // 制作模板的过程 public int resolution = ScreenResolution.getScreenResolution(); public JTemplate() { @@ -125,11 +123,9 @@ public abstract class JTemplate> this.undoState = createUndoState(); designModel = createDesignModel(); // 如果不是新建模板,并且在收集列表中 - if (!isNewFile && tic.inList(t)) { + if (!isNewFile && TemplateInfoCollector.getInstance().contains(t.getTemplateID())) { openTime = System.currentTimeMillis(); - process.append(tic.loadProcess(t)); } - } // 刷新右侧属性面板 @@ -153,7 +149,7 @@ public abstract class JTemplate> } long saveTime = System.currentTimeMillis(); // 保存模板的时间点 try { - tic.collectInfo(template, this, openTime, saveTime); + TemplateInfoCollector.getInstance().collectInfo(template.getTemplateID(), getProcessInfo(), openTime, saveTime); } catch (Throwable th) { // 不管收集过程中出现任何异常,都不应该影响模版保存 } openTime = saveTime; // 更新 openTime,准备下一次计算 @@ -161,16 +157,6 @@ public abstract class JTemplate> public abstract TemplateProcessInfo getProcessInfo(); - // 追加过程记录 - public void appendProcess(String s) { - process.append(s); - } - - // 获取过程记录 - public String getProcess() { - return process.toString(); - } - public U getUndoState() { return undoState; } diff --git a/designer-base/src/main/java/com/fr/design/mainframe/JVirtualTemplate.java b/designer-base/src/main/java/com/fr/design/mainframe/JVirtualTemplate.java index cea1088920..2c18186147 100644 --- a/designer-base/src/main/java/com/fr/design/mainframe/JVirtualTemplate.java +++ b/designer-base/src/main/java/com/fr/design/mainframe/JVirtualTemplate.java @@ -7,7 +7,7 @@ import com.fr.design.file.HistoryTemplateListPane; import com.fr.design.gui.frpane.HyperlinkGroupPane; import com.fr.design.gui.frpane.HyperlinkGroupPaneActionProvider; import com.fr.design.gui.imenu.UIMenuItem; -import com.fr.design.mainframe.templateinfo.TemplateProcessInfo; +import com.fr.design.mainframe.template.info.TemplateProcessInfo; import com.fr.design.menu.ShortCut; import com.fr.design.menu.ToolBarDef; import com.fr.file.FILE; diff --git a/designer-base/src/main/java/com/fr/design/mainframe/template/info/SendHelper.java b/designer-base/src/main/java/com/fr/design/mainframe/template/info/SendHelper.java new file mode 100644 index 0000000000..1a69dc4d70 --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/mainframe/template/info/SendHelper.java @@ -0,0 +1,52 @@ +package com.fr.design.mainframe.template.info; + +import com.fr.design.mainframe.SiteCenterToken; +import com.fr.general.CloudCenter; +import com.fr.general.ComparatorUtils; +import com.fr.general.http.HttpClient; +import com.fr.json.JSONObject; + +import java.util.HashMap; + +/** + * 负责向服务器发送信息 + * Created by plough on 2019/4/18. + */ +class SendHelper { + private static final String CONSUMING_URL = CloudCenter.getInstance().acquireUrlByKind("tempinfo.consuming") + "/single"; + private static final String PROCESS_URL = CloudCenter.getInstance().acquireUrlByKind("tempinfo.process") + "/single"; + + private static boolean sendConsumingInfo(String content) { + return sendSingleTemplateInfo(CONSUMING_URL, content); + } + + private static boolean sendProcessInfo(String content) { + return sendSingleTemplateInfo(PROCESS_URL, content); + } + + static boolean sendTemplateInfo(TemplateInfo templateInfo) { + return SendHelper.sendConsumingInfo(templateInfo.getConsumingMapJsonString()) && SendHelper.sendProcessInfo(templateInfo.getProcessMapJsonString()); + } + + private static boolean sendSingleTemplateInfo(String url, String content) { + HashMap para = new HashMap<>(); + para.put("token", SiteCenterToken.generateToken()); + para.put("content", content); + HttpClient httpClient = new HttpClient(url, para, true); + httpClient.setTimeout(5000); + httpClient.asGet(); + + if (!httpClient.isServerAlive()) { + return false; + } + + String res = httpClient.getResponseText(); + boolean success; + try { + success = ComparatorUtils.equals(new JSONObject(res).get("status"), "success"); + } catch (Exception ex) { + success = false; + } + return success; + } +} diff --git a/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfo.java b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfo.java new file mode 100644 index 0000000000..0d8696d636 --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfo.java @@ -0,0 +1,230 @@ +package com.fr.design.mainframe.template.info; + +import com.fr.config.MarketConfig; +import com.fr.design.DesignerEnvManager; +import com.fr.general.GeneralUtils; +import com.fr.json.JSONObject; +import com.fr.stable.ProductConstants; +import com.fr.stable.StringUtils; +import com.fr.stable.xml.XMLPrintWriter; +import com.fr.stable.xml.XMLReadable; +import com.fr.stable.xml.XMLWriter; +import com.fr.stable.xml.XMLableReader; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.HashMap; + +/** + * 对应一张模版的记录 + * Created by plough on 2019/4/18. + */ +class TemplateInfo implements XMLReadable, XMLWriter { + static final String XML_TAG = "TemplateInfo"; + + private static final String XML_PROCESS_MAP = "processMap"; + private static final String XML_CONSUMING_MAP = "consumingMap"; + private static final String ATTR_DAY_COUNT = "day_count"; + private static final String ATTR_TEMPLATE_ID = "templateID"; + private static final String ATTR_PROCESS = "process"; + private static final String ATTR_FLOAT_COUNT = "float_count"; + private static final String ATTR_WIDGET_COUNT = "widget_count"; + private static final String ATTR_CELL_COUNT = "cell_count"; + private static final String ATTR_BLOCK_COUNT = "block_count"; + private static final String ATTR_REPORT_TYPE = "report_type"; + private static final String ATTR_ACTIVITYKEY = "activitykey"; + private static final String ATTR_JAR_TIME = "jar_time"; + private static final String ATTR_CREATE_TIME = "create_time"; + private static final String ATTR_UUID = "uuid"; + private static final String ATTR_TIME_CONSUME = "time_consume"; + private static final String ATTR_VERSION = "version"; + private static final String ATTR_USERNAME = "username"; + + private static final int VALID_CELL_COUNT = 5; // 有效报表模板的格子数 + private static final int VALID_WIDGET_COUNT = 5; // 有效报表模板的控件数 + private static final int COMPLETE_DAY_COUNT = 15; // 判断模板是否完成的天数 + + + private int idleDayCount; // 到现在为止,模版闲置(上次保存后没有再编辑过)的天数 + private String templateID; + private HashMap processMap = new HashMap<>(); + private HashMap consumingMap = new HashMap<>(); + + private TemplateInfo() { + } + + static TemplateInfo newInstance(XMLableReader reader) { + TemplateInfo templateInfo = new TemplateInfo(); + reader.readXMLObject(templateInfo); + return templateInfo; + } + + static TemplateInfo newInstance(String templateID, long timeConsume) { + HashMap consumingMap = new HashMap<>(); + + String username = MarketConfig.getInstance().getBbsUsername(); + String uuid = DesignerEnvManager.getEnvManager().getUUID(); + String activitykey = DesignerEnvManager.getEnvManager().getActivationKey(); + String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(Calendar.getInstance().getTime()); + String jarTime = GeneralUtils.readBuildNO(); + String version = ProductConstants.VERSION; + consumingMap.put(ATTR_USERNAME, username); + consumingMap.put(ATTR_UUID, uuid); + consumingMap.put(ATTR_ACTIVITYKEY, activitykey); + consumingMap.put(ATTR_TEMPLATE_ID, templateID); + consumingMap.put(ATTR_CREATE_TIME, createTime); + consumingMap.put(ATTR_TIME_CONSUME, timeConsume); + consumingMap.put(ATTR_JAR_TIME, jarTime); + consumingMap.put(ATTR_VERSION, version); + + TemplateInfo templateInfo = new TemplateInfo(); + templateInfo.consumingMap = consumingMap; + + return templateInfo; + } + + String getTemplateID() { + return templateID; + } + + HashMap getTemplateInfo() { + HashMap templateInfo = new HashMap<>(); + templateInfo.put(XML_PROCESS_MAP, processMap); + templateInfo.put(XML_CONSUMING_MAP, consumingMap); + templateInfo.put(ATTR_DAY_COUNT, idleDayCount); + return templateInfo; + } + + public void writeXML(XMLPrintWriter writer) { + writer.startTAG(XML_TAG); + if (StringUtils.isNotEmpty(templateID)) { + writer.attr(ATTR_TEMPLATE_ID, this.templateID); + } + if (idleDayCount >= 0) { + writer.attr(ATTR_DAY_COUNT, this.idleDayCount); + } + writeProcessMap(writer); + writeConsumingMap(writer); + + writer.end(); + } + + private void writeProcessMap(XMLPrintWriter writer) { + writer.startTAG(XML_PROCESS_MAP); + writer.attr(ATTR_PROCESS, (String) processMap.get(ATTR_PROCESS)); + writer.attr(ATTR_FLOAT_COUNT, (int) processMap.get(ATTR_FLOAT_COUNT)); + writer.attr(ATTR_WIDGET_COUNT, (int) processMap.get(ATTR_WIDGET_COUNT)); + writer.attr(ATTR_CELL_COUNT, (int) processMap.get(ATTR_CELL_COUNT)); + writer.attr(ATTR_BLOCK_COUNT, (int) processMap.get(ATTR_BLOCK_COUNT)); + writer.attr(ATTR_REPORT_TYPE, (int) processMap.get(ATTR_REPORT_TYPE)); + writer.end(); + } + + private void writeConsumingMap(XMLPrintWriter writer) { + writer.startTAG(XML_CONSUMING_MAP); + writer.attr(ATTR_ACTIVITYKEY, (String) consumingMap.get(ATTR_ACTIVITYKEY)); + writer.attr(ATTR_JAR_TIME, (String) consumingMap.get(ATTR_JAR_TIME)); + writer.attr(ATTR_CREATE_TIME, (String) consumingMap.get(ATTR_CREATE_TIME)); + writer.attr(ATTR_UUID, (String) consumingMap.get(ATTR_UUID)); + writer.attr(ATTR_TIME_CONSUME, (long) consumingMap.get(ATTR_TIME_CONSUME)); + writer.attr(ATTR_VERSION, (String) consumingMap.get(ATTR_VERSION)); + writer.attr(ATTR_USERNAME, (String) consumingMap.get(ATTR_USERNAME)); + writer.end(); + } + + public void readXML(XMLableReader reader) { + if (!reader.isChildNode()) { + idleDayCount = reader.getAttrAsInt(ATTR_DAY_COUNT, 0); + templateID = reader.getAttrAsString(ATTR_TEMPLATE_ID, StringUtils.EMPTY); + } else { + try { + String name = reader.getTagName(); + if (XML_PROCESS_MAP.equals(name)) { + processMap.put(ATTR_PROCESS, reader.getAttrAsString(ATTR_PROCESS, StringUtils.EMPTY)); + processMap.put(ATTR_FLOAT_COUNT, reader.getAttrAsInt(ATTR_FLOAT_COUNT, 0)); + processMap.put(ATTR_WIDGET_COUNT, reader.getAttrAsInt(ATTR_WIDGET_COUNT, 0)); + processMap.put(ATTR_CELL_COUNT, reader.getAttrAsInt(ATTR_CELL_COUNT, 0)); + processMap.put(ATTR_BLOCK_COUNT, reader.getAttrAsInt(ATTR_BLOCK_COUNT, 0)); + processMap.put(ATTR_REPORT_TYPE, reader.getAttrAsInt(ATTR_REPORT_TYPE, 0)); + processMap.put(ATTR_TEMPLATE_ID, templateID); + } else if (XML_CONSUMING_MAP.equals(name)) { + consumingMap.put(ATTR_ACTIVITYKEY, reader.getAttrAsString(ATTR_ACTIVITYKEY, StringUtils.EMPTY)); + consumingMap.put(ATTR_JAR_TIME, reader.getAttrAsString(ATTR_JAR_TIME, StringUtils.EMPTY)); + consumingMap.put(ATTR_CREATE_TIME, reader.getAttrAsString(ATTR_CREATE_TIME, StringUtils.EMPTY)); + consumingMap.put(ATTR_TEMPLATE_ID, templateID); + consumingMap.put(ATTR_UUID, reader.getAttrAsString(ATTR_UUID, StringUtils.EMPTY)); + consumingMap.put(ATTR_TIME_CONSUME, reader.getAttrAsLong(ATTR_TIME_CONSUME, 0)); + consumingMap.put(ATTR_VERSION, reader.getAttrAsString(ATTR_VERSION, "8.0")); + consumingMap.put(ATTR_USERNAME, reader.getAttrAsString(ATTR_USERNAME, StringUtils.EMPTY)); + } + } catch (Exception ex) { + // 什么也不做,使用默认值 + } + } + } + + boolean isTestTemplate() { + if (!isComplete()) { + return false; + } + + int reportType = (int) processMap.get(ATTR_REPORT_TYPE); + int cellCount = (int) processMap.get(ATTR_CELL_COUNT); + int floatCount = (int) processMap.get(ATTR_FLOAT_COUNT); + int blockCount = (int) processMap.get(ATTR_BLOCK_COUNT); + int widgetCount = (int) processMap.get(ATTR_WIDGET_COUNT); + boolean isTestTemplate = false; + if (reportType == 0) { // 普通报表 + isTestTemplate = cellCount <= VALID_CELL_COUNT && floatCount <= 1 && widgetCount <= VALID_WIDGET_COUNT; + } else if (reportType == 1) { // 聚合报表 + isTestTemplate = blockCount <= 1 && widgetCount <= VALID_WIDGET_COUNT; + } else { // 表单(reportType == 2) + isTestTemplate = widgetCount <= 1; + } + return isTestTemplate; + } + + boolean isComplete() { + return idleDayCount > COMPLETE_DAY_COUNT; + } + + String getConsumingMapJsonString() { + return new JSONObject(consumingMap).toString(); + } + + String getProcessMapJsonString() { + return new JSONObject(processMap).toString(); + } + + boolean isReadyForSend() { + return isComplete() && !isTestTemplate(); + } + + void addTimeConsume(long timeConsume) { + timeConsume += (long) consumingMap.get(ATTR_TIME_CONSUME); // 加上之前的累计编辑时间 + consumingMap.put(ATTR_TIME_CONSUME, timeConsume); + } + + void updateProcessMap(TemplateProcessInfo processInfo) { + HashMap processMap = new HashMap<>(); + + // 暂不支持模版制作过程的收集 + processMap.put(ATTR_PROCESS, StringUtils.EMPTY); + + processMap.put(ATTR_REPORT_TYPE, processInfo.getReportType()); + processMap.put(ATTR_CELL_COUNT, processInfo.getCellCount()); + processMap.put(ATTR_FLOAT_COUNT, processInfo.getFloatCount()); + processMap.put(ATTR_BLOCK_COUNT, processInfo.getBlockCount()); + processMap.put(ATTR_WIDGET_COUNT, processInfo.getWidgetCount()); + + this.processMap = processMap; + } + + void setIdleDayCount(int idleDayCount) { + this.idleDayCount = idleDayCount; + } + + void addIdleDayCountByOne() { + this.idleDayCount += 1; + } +} \ No newline at end of file diff --git a/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java new file mode 100644 index 0000000000..d7239886f3 --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java @@ -0,0 +1,299 @@ +package com.fr.design.mainframe.template.info; + +import com.fr.base.FRContext; +import com.fr.design.DesignerEnvManager; +import com.fr.general.ComparatorUtils; +import com.fr.general.IOUtils; +import com.fr.log.FineLoggerFactory; +import com.fr.stable.EncodeConstants; +import com.fr.stable.ProductConstants; +import com.fr.stable.StableUtils; +import com.fr.stable.StringUtils; +import com.fr.stable.xml.XMLPrintWriter; +import com.fr.stable.xml.XMLReadable; +import com.fr.stable.xml.XMLTools; +import com.fr.stable.xml.XMLWriter; +import com.fr.stable.xml.XMLableReader; +import com.fr.third.javax.xml.stream.XMLStreamException; +import com.fr.workspace.WorkContext; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.ObjectInputStream; +import java.io.Serializable; +import java.io.UnsupportedEncodingException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.HashMap; +import java.util.Map; + +/** + * 做模板的过程和耗时收集,辅助类 + * Created by plough on 2017/2/21. + */ +public class TemplateInfoCollector implements Serializable, XMLReadable, XMLWriter { + private static final String XML_TAG = "TplInfo"; + private static final String XML_DESIGNER_OPEN_DATE = "DesignerOpenDate"; + private static final String XML_TEMPLATE_INFO_LIST = "TemplateInfoList"; + + static final long serialVersionUID = 2007L; + private static final String XML_FILE_NAME = "tpl.info"; + private static final String OBJECT_FILE_NAME = "tplInfo.ser"; + + private static final int ONE_THOUSAND = 1000; + + private static TemplateInfoCollector instance; + private Map templateInfoMap; + private String designerOpenDate; //设计器最近一次打开日期 + + + @SuppressWarnings("unchecked") + private TemplateInfoCollector() { + templateInfoMap = new HashMap<>(); + setDesignerOpenDate(); + } + + /** + * 获取缓存文件存放路径 + */ + private static File getInfoFile(String fileName) { + return new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), fileName)); + } + + /** + * 获取缓存文件存放路径 + */ + private static File getInfoFile() { + return getInfoFile(XML_FILE_NAME); + } + + public static TemplateInfoCollector getInstance() { + if (instance == null) { + instance = new TemplateInfoCollector(); + + File xmlFile = getInfoFile(); + File objFile = getInfoFile(OBJECT_FILE_NAME); + if (xmlFile.exists()) { + readXMLFile(instance, xmlFile); + } else if (objFile.exists()) { + readFromObjectFile(objFile); + } + } + return instance; + } + + private static void readFromObjectFile(File objFile) { + try { + ObjectInputStream is = new ObjectInputStream(new FileInputStream(objFile)); + instance = (TemplateInfoCollector) is.readObject(); + } catch (Exception ex) { + // 什么也不做,instance 使用新值 + } + } + + /** + * 根据模板ID是否在收集列表中,判断是否需要收集当前模板的信息 + */ + public boolean contains(String templateID) { + return StringUtils.isNotEmpty(templateID) && templateInfoMap.containsKey(templateID); + } + + + + /** + * 收集模板信息。如果之前没有记录,则新增;如果已有记录,则更新。 + * 同时将最新数据保存到文件中。 + */ + @SuppressWarnings("unchecked") + public void collectInfo(String templateID, TemplateProcessInfo processInfo, long openTime, long saveTime) { + if (!shouldCollectInfo()) { + return; + } + + long timeConsume = ((saveTime - openTime) / ONE_THOUSAND); // 制作模板耗时(单位:s) + + + TemplateInfo templateInfo; + + if (templateInfoMap.containsKey(templateID)) { // 已有记录 + templateInfo = templateInfoMap.get(templateID); + templateInfo.addTimeConsume(timeConsume); + } else { // 新增 + templateInfo = TemplateInfo.newInstance(templateID, timeConsume); + } + + templateInfo.updateProcessMap(processInfo); + + // 保存模板时,让 day_count 归零 + templateInfo.setIdleDayCount(0); + + templateInfoMap.put(templateID, templateInfo); + + saveInfo(); // 每次更新之后,都同步到暂存文件中 + } + + /** + * 发送本地模板信息到服务器,并清空已发送模版的本地记录 + */ + public void sendTemplateInfo() { + addDayCount(); + + removeTestTemplates(); + + for (String key : templateInfoMap.keySet()) { + TemplateInfo templateInfo = templateInfoMap.get(key); + if (templateInfo.isReadyForSend()) { + if (SendHelper.sendTemplateInfo(templateInfo)) { + // 清空记录 + removeFromTemplateInfoList(templateInfo.getTemplateID()); + } + } + } + + saveInfo(); + } + + private static void readXMLFile(XMLReadable xmlReadable, File xmlFile) { + String charset = EncodeConstants.ENCODING_UTF_8; + try { + String fileContent = getFileContent(xmlFile); + InputStream xmlInputStream = new ByteArrayInputStream(fileContent.getBytes(charset)); + InputStreamReader inputStreamReader = new InputStreamReader(xmlInputStream, charset); + XMLableReader xmlReader = XMLableReader.createXMLableReader(inputStreamReader); + + if (xmlReader != null) { + xmlReader.readXMLObject(xmlReadable); + } + xmlInputStream.close(); + } catch (FileNotFoundException e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + } catch (IOException e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + } catch (XMLStreamException e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + } + + } + + private static String getFileContent(File xmlFile) throws FileNotFoundException, UnsupportedEncodingException { + InputStream is = new FileInputStream(xmlFile); + return IOUtils.inputStream2String(is); + } + + /** + * 把设计器最近打开日期设定为当前日期 + */ + private void setDesignerOpenDate() { + designerOpenDate = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()); + } + + /** + * 判断今天是否第一次打开设计器 + */ + private boolean designerOpenFirstTime() { + String today = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()); + return !ComparatorUtils.equals(today, designerOpenDate); + } + + private boolean shouldCollectInfo() { + //只收集本地环境的 + if (!WorkContext.getCurrent().isLocal()) { + return false; + } + return DesignerEnvManager.getEnvManager().isJoinProductImprove() && FRContext.isChineseEnv(); + } + + /** + * 将包含所有信息的对象保存到文件 + */ + private void saveInfo() { + try { + FileOutputStream out = new FileOutputStream(getInfoFile()); + XMLTools.writeOutputStreamXML(this, out); + } catch (Exception ex) { + FineLoggerFactory.getLogger().error(ex.getMessage()); + } + } + + /** + * 更新 day_count:打开设计器却未编辑模板的连续日子 + */ + private void addDayCount() { + if (designerOpenFirstTime()) { + for (TemplateInfo templateInfo : templateInfoMap.values()) { + templateInfo.addIdleDayCountByOne(); + } + setDesignerOpenDate(); + } + } + + + private void removeTestTemplates() { + // 删除所有已完成的测试模版 + ArrayList testTemplateKeys = new ArrayList<>(); // 保存测试模板的key + for (String key : templateInfoMap.keySet()) { + if (templateInfoMap.get(key).isTestTemplate()) { + testTemplateKeys.add(key); + } + } + // 删除测试模板 + for (String key : testTemplateKeys) { + removeFromTemplateInfoList(key); + } + } + + private void removeFromTemplateInfoList(String key) { + templateInfoMap.remove(key); + } + + @Override + public void readXML(XMLableReader reader) { + if (reader.isChildNode()) { + try { + String name = reader.getTagName(); + if (XML_DESIGNER_OPEN_DATE.equals(name)) { + this.designerOpenDate = reader.getElementValue(); + } else if (XML_TEMPLATE_INFO_LIST.equals(name)) { + readTemplateInfoList(reader); + } + } catch (Exception ex) { + // 什么也不做,使用默认值 + } + } + } + + private void readTemplateInfoList(XMLableReader reader) { + reader.readXMLObject(new XMLReadable() { + public void readXML(XMLableReader reader) { + if (TemplateInfo.XML_TAG.equals(reader.getTagName())) { + TemplateInfo templateInfo = TemplateInfo.newInstance(reader); + templateInfoMap.put(templateInfo.getTemplateID(), templateInfo); + } + } + }); + } + + @Override + public void writeXML(XMLPrintWriter writer) { + writer.startTAG(XML_TAG); + + writer.startTAG(XML_DESIGNER_OPEN_DATE); + writer.textNode(designerOpenDate); + writer.end(); + + writer.startTAG(XML_TEMPLATE_INFO_LIST); + for (TemplateInfo templateInfo : templateInfoMap.values()) { + templateInfo.writeXML(writer); + } + writer.end(); + + writer.end(); + } +} diff --git a/designer-base/src/main/java/com/fr/design/mainframe/templateinfo/TemplateProcessInfo.java b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateProcessInfo.java similarity index 74% rename from designer-base/src/main/java/com/fr/design/mainframe/templateinfo/TemplateProcessInfo.java rename to designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateProcessInfo.java index 0307e4fd6d..427d48c73b 100644 --- a/designer-base/src/main/java/com/fr/design/mainframe/templateinfo/TemplateProcessInfo.java +++ b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateProcessInfo.java @@ -1,10 +1,14 @@ -package com.fr.design.mainframe.templateinfo; +package com.fr.design.mainframe.template.info; import com.fr.base.io.BaseBook; /** * Created by plough on 2017/3/17. */ +// todo: 重构 +// 1. 命名不好,表意不清晰。 +// 2. 逻辑混乱,到底是一个 Info 类,还是一个 InfoCollector 类? +// 3. 耦合太强,用组合替代继承 public abstract class TemplateProcessInfo { protected T template; diff --git a/designer-base/src/main/java/com/fr/design/mainframe/templateinfo/TemplateInfoCollector.java b/designer-base/src/main/java/com/fr/design/mainframe/templateinfo/TemplateInfoCollector.java deleted file mode 100644 index 57ff2ce8a9..0000000000 --- a/designer-base/src/main/java/com/fr/design/mainframe/templateinfo/TemplateInfoCollector.java +++ /dev/null @@ -1,550 +0,0 @@ -package com.fr.design.mainframe.templateinfo; - -import com.fr.base.FRContext; -import com.fr.base.io.BaseBook; -import com.fr.config.MarketConfig; -import com.fr.design.DesignerEnvManager; -import com.fr.design.mainframe.DesignerContext; -import com.fr.design.mainframe.JTemplate; -import com.fr.design.mainframe.SiteCenterToken; -import com.fr.general.CloudCenter; -import com.fr.general.ComparatorUtils; -import com.fr.general.GeneralUtils; -import com.fr.general.IOUtils; -import com.fr.general.http.HttpClient; -import com.fr.json.JSONObject; -import com.fr.log.FineLoggerFactory; -import com.fr.stable.EncodeConstants; -import com.fr.stable.ProductConstants; -import com.fr.stable.StableUtils; -import com.fr.stable.StringUtils; -import com.fr.stable.xml.XMLPrintWriter; -import com.fr.stable.xml.XMLReadable; -import com.fr.stable.xml.XMLTools; -import com.fr.stable.xml.XMLWriter; -import com.fr.stable.xml.XMLableReader; -import com.fr.third.javax.xml.stream.XMLStreamException; -import com.fr.workspace.WorkContext; - -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.ObjectInputStream; -import java.io.Serializable; -import java.io.UnsupportedEncodingException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.HashMap; -import java.util.Map; - -/** - * 做模板的过程和耗时收集,辅助类 - * Created by plough on 2017/2/21. - */ -public class TemplateInfoCollector implements Serializable, XMLReadable, XMLWriter { - static final long serialVersionUID = 2007L; - private static final String FILE_NAME = "tpl.info"; - private static final String OBJECT_FILE_NAME = "tplInfo.ser"; - private static final int VALID_CELL_COUNT = 5; // 有效报表模板的格子数 - private static final int VALID_WIDGET_COUNT = 5; // 有效报表模板的控件数 - private static final int COMPLETE_DAY_COUNT = 15; // 判断模板是否完成的天数 - private static final int ONE_THOUSAND = 1000; - private static final String XML_DESIGNER_OPEN_DATE = "DesignerOpenDate"; - private static final String XML_TEMPLATE_INFO_LIST = "TemplateInfoList"; - private static final String XML_TEMPLATE_INFO = "TemplateInfo"; - private static final String XML_PROCESS_MAP = "processMap"; - private static final String XML_CONSUMING_MAP = "consumingMap"; - private static final String ATTR_DAY_COUNT = "day_count"; - private static final String ATTR_TEMPLATE_ID = "templateID"; - private static final String ATTR_PROCESS = "process"; - private static final String ATTR_FLOAT_COUNT = "float_count"; - private static final String ATTR_WIDGET_COUNT = "widget_count"; - private static final String ATTR_CELL_COUNT = "cell_count"; - private static final String ATTR_BLOCK_COUNT = "block_count"; - private static final String ATTR_REPORT_TYPE = "report_type"; - private static final String ATTR_ACTIVITYKEY = "activitykey"; - private static final String ATTR_JAR_TIME = "jar_time"; - private static final String ATTR_CREATE_TIME = "create_time"; - private static final String ATTR_UUID = "uuid"; - private static final String ATTR_TIME_CONSUME = "time_consume"; - private static final String ATTR_VERSION = "version"; - private static final String ATTR_USERNAME = "username"; - private static final String JSON_CONSUMING_MAP = "jsonConsumingMap"; - private static final String JSON_PROCESS_MAP = "jsonProcessMap"; - private static TemplateInfoCollector instance; - private Map> templateInfoList; - private String designerOpenDate; //设计器最近一次打开日期 - - - @SuppressWarnings("unchecked") - private TemplateInfoCollector() { - templateInfoList = new HashMap<>(); - setDesignerOpenDate(); - } - - /** - * 获取缓存文件存放路径 - */ - private static File getInfoFile() { - return new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), FILE_NAME)); - } - - private static File getObjectInfoFile() { - return new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), OBJECT_FILE_NAME)); - } - - public static TemplateInfoCollector getInstance() { - if (instance == null) { - instance = new TemplateInfoCollector(); - readXMLFile(instance, getInfoFile()); - // 兼容过渡。如果没有新文件,则从老文件读取数据。以后都是读写新的 xml 文件 - if (!getInfoFile().exists() && getObjectInfoFile().exists()) { - try { - ObjectInputStream is = new ObjectInputStream(new FileInputStream(getObjectInfoFile())); - instance = (TemplateInfoCollector) is.readObject(); - } catch (Exception ex) { - // 什么也不做,instance 使用新值 - } - } - } - return instance; - } - - private static void readXMLFile(XMLReadable xmlReadable, File xmlFile) { - if (xmlFile == null || !xmlFile.exists()) { - return; - } - String charset = EncodeConstants.ENCODING_UTF_8; - try { - String fileContent = getFileContent(xmlFile); - InputStream xmlInputStream = new ByteArrayInputStream(fileContent.getBytes(charset)); - InputStreamReader inputStreamReader = new InputStreamReader(xmlInputStream, charset); - XMLableReader xmlReader = XMLableReader.createXMLableReader(inputStreamReader); - - if (xmlReader != null) { - xmlReader.readXMLObject(xmlReadable); - } - xmlInputStream.close(); - } catch (FileNotFoundException e) { - FineLoggerFactory.getLogger().error(e.getMessage(), e); - } catch (IOException e) { - FineLoggerFactory.getLogger().error(e.getMessage(), e); - } catch (XMLStreamException e) { - FineLoggerFactory.getLogger().error(e.getMessage(), e); - } - - } - - private static String getFileContent(File xmlFile) throws FileNotFoundException, UnsupportedEncodingException { - InputStream is = new FileInputStream(xmlFile); - return IOUtils.inputStream2String(is); - } - - public static void main(String[] args) { - TemplateInfoCollector tic = TemplateInfoCollector.getInstance(); - tic.sendTemplateInfo(); - } - - /** - * 把设计器最近打开日期设定为当前日期 - */ - private void setDesignerOpenDate() { - designerOpenDate = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()); - } - - /** - * 判断今天是否第一次打开设计器 - */ - private boolean designerOpenFirstTime() { - String today = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()); - return !ComparatorUtils.equals(today, designerOpenDate); - } - - private boolean shouldCollectInfo() { - //只收集本地环境的 - if (!WorkContext.getCurrent().isLocal()) { - return false; - } - return DesignerEnvManager.getEnvManager().isJoinProductImprove() && FRContext.isChineseEnv(); - } - - public void appendProcess(String log) { - if (!shouldCollectInfo()) { - return; - } - // 获取当前编辑的模板 - JTemplate jt = DesignerContext.getDesignerFrame().getSelectedJTemplate(); - // 追加过程记录 - jt.appendProcess(log); - } - - /** - * 加载已经存储的模板过程 - */ - @SuppressWarnings("unchecked") - public String loadProcess(T t) { - HashMap processMap = (HashMap) templateInfoList.get(t.getTemplateID()).get(XML_PROCESS_MAP); - return (String) processMap.get(ATTR_PROCESS); - } - - /** - * 根据模板ID是否在收集列表中,判断是否需要收集当前模板的信息 - */ - public boolean inList(T t) { - return templateInfoList.containsKey(t.getTemplateID()); - } - - /** - * 将包含所有信息的对象保存到文件 - */ - private void saveInfo() { - try { - FileOutputStream out = new FileOutputStream(getInfoFile()); - XMLTools.writeOutputStreamXML(this, out); - } catch (Exception ex) { - FineLoggerFactory.getLogger().error(ex.getMessage()); - } - } - - /** - * 更新 day_count:打开设计器却未编辑模板的连续日子 - */ - private void addDayCount() { - if (designerOpenFirstTime()) { - for (String key : templateInfoList.keySet()) { - HashMap templateInfo = templateInfoList.get(key); - int dayCount = (int) templateInfo.get(ATTR_DAY_COUNT) + 1; - templateInfo.put(ATTR_DAY_COUNT, dayCount); - } - setDesignerOpenDate(); - } - } - - /** - * 收集模板信息。如果之前没有记录,则新增;如果已有记录,则更新。 - * 同时将最新数据保存到文件中。 - */ - @SuppressWarnings("unchecked") - public void collectInfo(T t, JTemplate jt, long openTime, long saveTime) { - if (!shouldCollectInfo()) { - return; - } - - HashMap templateInfo; - - long timeConsume = ((saveTime - openTime) / ONE_THOUSAND); // 制作模板耗时(单位:s) - String templateID = t.getTemplateID(); - - if (inList(t)) { // 已有记录 - templateInfo = templateInfoList.get(templateID); - // 更新 conusmingMap - HashMap consumingMap = (HashMap) templateInfo.get(XML_CONSUMING_MAP); - timeConsume += (long) consumingMap.get(ATTR_TIME_CONSUME); // 加上之前的累计编辑时间 - consumingMap.put(ATTR_TIME_CONSUME, timeConsume); - } else { // 新增 - templateInfo = new HashMap<>(); - templateInfo.put(XML_CONSUMING_MAP, getNewConsumingMap(templateID, openTime, timeConsume)); - } - - // 直接覆盖 processMap - templateInfo.put(XML_PROCESS_MAP, getProcessMap(templateID, jt)); - - // 保存模板时,让 day_count 归零 - templateInfo.put(ATTR_DAY_COUNT, 0); - - templateInfoList.put(templateID, templateInfo); - - saveInfo(); // 每次更新之后,都同步到暂存文件中 - } - - private HashMap getNewConsumingMap(String templateID, long openTime, long timeConsume) { - HashMap consumingMap = new HashMap<>(); - - String username = MarketConfig.getInstance().getBbsUsername(); - String uuid = DesignerEnvManager.getEnvManager().getUUID(); - String activitykey = DesignerEnvManager.getEnvManager().getActivationKey(); - String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(Calendar.getInstance().getTime()); - String jarTime = GeneralUtils.readBuildNO(); - String version = ProductConstants.VERSION; - consumingMap.put(ATTR_USERNAME, username); - consumingMap.put(ATTR_UUID, uuid); - consumingMap.put(ATTR_ACTIVITYKEY, activitykey); - consumingMap.put(ATTR_TEMPLATE_ID, templateID); - consumingMap.put(ATTR_CREATE_TIME, createTime); - consumingMap.put(ATTR_TIME_CONSUME, timeConsume); - consumingMap.put(ATTR_JAR_TIME, jarTime); - consumingMap.put(ATTR_VERSION, version); - - return consumingMap; - } - - private HashMap getProcessMap(String templateID, JTemplate jt) { - HashMap processMap = new HashMap<>(); - - processMap.put(ATTR_TEMPLATE_ID, templateID); - processMap.put(ATTR_PROCESS, jt.getProcess()); - - TemplateProcessInfo info = jt.getProcessInfo(); - processMap.put(ATTR_REPORT_TYPE, info.getReportType()); - processMap.put(ATTR_CELL_COUNT, info.getCellCount()); - processMap.put(ATTR_FLOAT_COUNT, info.getFloatCount()); - processMap.put(ATTR_BLOCK_COUNT, info.getBlockCount()); - processMap.put(ATTR_WIDGET_COUNT, info.getWidgetCount()); - - return processMap; - } - - /** - * 发送本地模板信息到服务器 - */ - public void sendTemplateInfo() { - addDayCount(); - String consumingUrl = CloudCenter.getInstance().acquireUrlByKind("tempinfo.consuming") + "/single"; - String processUrl = CloudCenter.getInstance().acquireUrlByKind("tempinfo.process") + "/single"; - ArrayList> completeTemplatesInfo = getCompleteTemplatesInfo(); - for (HashMap templateInfo : completeTemplatesInfo) { - String jsonConsumingMap = templateInfo.get(JSON_CONSUMING_MAP); - String jsonProcessMap = templateInfo.get(JSON_PROCESS_MAP); - if (sendSingleTemplateInfo(consumingUrl, jsonConsumingMap) && sendSingleTemplateInfo(processUrl, jsonProcessMap)) { - // 清空记录 - removeFromTemplateInfoList(templateInfo.get(ATTR_TEMPLATE_ID)); - } - } - saveInfo(); - } - - private boolean sendSingleTemplateInfo(String url, String content) { - HashMap para = new HashMap<>(); - para.put("token", SiteCenterToken.generateToken()); - para.put("content", content); - HttpClient httpClient = new HttpClient(url, para, true); - httpClient.setTimeout(5000); - httpClient.asGet(); - - if (!httpClient.isServerAlive()) { - return false; - } - - String res = httpClient.getResponseText(); - boolean success; - try { - success = ComparatorUtils.equals(new JSONObject(res).get("status"), "success"); - } catch (Exception ex) { - success = false; - } - return success; - } - - /** - * 返回已完成的模板信息 - */ - @SuppressWarnings("unchecked") - private ArrayList> getCompleteTemplatesInfo() { - ArrayList> completeTemplatesInfo = new ArrayList<>(); - ArrayList testTemplateKeys = new ArrayList<>(); // 保存测试模板的key - for (String key : templateInfoList.keySet()) { - HashMap templateInfo = templateInfoList.get(key); - if ((int) templateInfo.get(ATTR_DAY_COUNT) <= COMPLETE_DAY_COUNT) { // 未完成模板 - continue; - } - if (isTestTemplate(templateInfo)) { - testTemplateKeys.add(key); - continue; - } - HashMap consumingMap = (HashMap) templateInfo.get(XML_CONSUMING_MAP); - HashMap processMap = (HashMap) templateInfo.get(XML_PROCESS_MAP); - String jsonConsumingMap = new JSONObject(consumingMap).toString(); - String jsonProcessMap = new JSONObject(processMap).toString(); - HashMap jsonTemplateInfo = new HashMap<>(); - jsonTemplateInfo.put(JSON_CONSUMING_MAP, jsonConsumingMap); - jsonTemplateInfo.put(JSON_PROCESS_MAP, jsonProcessMap); - jsonTemplateInfo.put(ATTR_TEMPLATE_ID, key); - completeTemplatesInfo.add(jsonTemplateInfo); - } - // 删除测试模板 - for (String key : testTemplateKeys) { - removeFromTemplateInfoList(key); - } - return completeTemplatesInfo; - } - - private void removeFromTemplateInfoList(String key) { - templateInfoList.remove(key); - } - - @SuppressWarnings("unchecked") - private boolean isTestTemplate(HashMap templateInfo) { - HashMap processMap = (HashMap) templateInfo.get(XML_PROCESS_MAP); - int reportType = (int) processMap.get(ATTR_REPORT_TYPE); - int cellCount = (int) processMap.get(ATTR_CELL_COUNT); - int floatCount = (int) processMap.get(ATTR_FLOAT_COUNT); - int blockCount = (int) processMap.get(ATTR_BLOCK_COUNT); - int widgetCount = (int) processMap.get(ATTR_WIDGET_COUNT); - boolean isTestTemplate = false; - if (reportType == 0) { // 普通报表 - isTestTemplate = cellCount <= VALID_CELL_COUNT && floatCount <= 1 && widgetCount <= VALID_WIDGET_COUNT; - } else if (reportType == 1) { // 聚合报表 - isTestTemplate = blockCount <= 1 && widgetCount <= VALID_WIDGET_COUNT; - } else { // 表单(reportType == 2) - isTestTemplate = widgetCount <= 1; - } - return isTestTemplate; - } - - @SuppressWarnings("unchecked") - @Override - public void readXML(XMLableReader reader) { - if (reader.isChildNode()) { - try { - String name = reader.getTagName(); - if (XML_DESIGNER_OPEN_DATE.equals(name)) { - this.designerOpenDate = reader.getElementValue(); - } else if (XML_TEMPLATE_INFO_LIST.equals(name)) { - readTemplateInfoList(reader); - } - } catch (Exception ex) { - // 什么也不做,使用默认值 - } - } - } - - private void readTemplateInfoList(XMLableReader reader) { - reader.readXMLObject(new XMLReadable() { - public void readXML(XMLableReader reader) { - if (XML_TEMPLATE_INFO.equals(reader.getTagName())) { - TemplateInfo templateInfo = new TemplateInfo(); - reader.readXMLObject(templateInfo); - templateInfoList.put(templateInfo.getTemplateID(), templateInfo.getTemplateInfo()); - } - } - }); - } - - @Override - public void writeXML(XMLPrintWriter writer) { - writer.startTAG("TplInfo"); - - writer.startTAG(XML_DESIGNER_OPEN_DATE); - writer.textNode(designerOpenDate); - writer.end(); - - writeTemplateInfoList(writer); - - writer.end(); - } - - private void writeTemplateInfoList(XMLPrintWriter writer) { - //启停 - writer.startTAG(XML_TEMPLATE_INFO_LIST); - for (String templateID : templateInfoList.keySet()) { - new TemplateInfo(templateInfoList.get(templateID)).writeXML(writer); - } - writer.end(); - } - - private class TemplateInfo implements XMLReadable, XMLWriter { - - private int dayCount; - private String templateID; - private HashMap processMap = new HashMap<>(); - private HashMap consumingMap = new HashMap<>(); - - @SuppressWarnings("unchecked") - public TemplateInfo(HashMap templateInfo) { - this.dayCount = (int) templateInfo.get(ATTR_DAY_COUNT); - this.processMap = (HashMap) templateInfo.get(XML_PROCESS_MAP); - this.consumingMap = (HashMap) templateInfo.get(XML_CONSUMING_MAP); - this.templateID = (String) processMap.get(ATTR_TEMPLATE_ID); - } - - public TemplateInfo() { - } - - public String getTemplateID() { - return templateID; - } - - public HashMap getTemplateInfo() { - HashMap templateInfo = new HashMap<>(); - templateInfo.put(XML_PROCESS_MAP, processMap); - templateInfo.put(XML_CONSUMING_MAP, consumingMap); - templateInfo.put(ATTR_DAY_COUNT, dayCount); - return templateInfo; - } - - public void writeXML(XMLPrintWriter writer) { - writer.startTAG(XML_TEMPLATE_INFO); - if (StringUtils.isNotEmpty(templateID)) { - writer.attr(ATTR_TEMPLATE_ID, this.templateID); - } - if (dayCount >= 0) { - writer.attr(ATTR_DAY_COUNT, this.dayCount); - } - writeProcessMap(writer); - writeConsumingMap(writer); - - writer.end(); - } - - private void writeProcessMap(XMLPrintWriter writer) { - writer.startTAG(XML_PROCESS_MAP); - writer.attr(ATTR_PROCESS, (String) processMap.get(ATTR_PROCESS)); - writer.attr(ATTR_FLOAT_COUNT, (int) processMap.get(ATTR_FLOAT_COUNT)); - writer.attr(ATTR_WIDGET_COUNT, (int) processMap.get(ATTR_WIDGET_COUNT)); - writer.attr(ATTR_CELL_COUNT, (int) processMap.get(ATTR_CELL_COUNT)); - writer.attr(ATTR_BLOCK_COUNT, (int) processMap.get(ATTR_BLOCK_COUNT)); - writer.attr(ATTR_REPORT_TYPE, (int) processMap.get(ATTR_REPORT_TYPE)); - writer.end(); - } - - private void writeConsumingMap(XMLPrintWriter writer) { - writer.startTAG(XML_CONSUMING_MAP); - writer.attr(ATTR_ACTIVITYKEY, (String) consumingMap.get(ATTR_ACTIVITYKEY)); - writer.attr(ATTR_JAR_TIME, (String) consumingMap.get(ATTR_JAR_TIME)); - writer.attr(ATTR_CREATE_TIME, (String) consumingMap.get(ATTR_CREATE_TIME)); - writer.attr(ATTR_UUID, (String) consumingMap.get(ATTR_UUID)); - writer.attr(ATTR_TIME_CONSUME, (long) consumingMap.get(ATTR_TIME_CONSUME)); - writer.attr(ATTR_VERSION, (String) consumingMap.get(ATTR_VERSION)); - writer.attr(ATTR_USERNAME, (String) consumingMap.get(ATTR_USERNAME)); - writer.end(); - } - - public void readXML(XMLableReader reader) { - if (!reader.isChildNode()) { - dayCount = reader.getAttrAsInt(ATTR_DAY_COUNT, 0); - templateID = reader.getAttrAsString(ATTR_TEMPLATE_ID, StringUtils.EMPTY); - } else { - try { - String name = reader.getTagName(); - if (XML_PROCESS_MAP.equals(name)) { - processMap.put(ATTR_PROCESS, reader.getAttrAsString(ATTR_PROCESS, StringUtils.EMPTY)); - processMap.put(ATTR_FLOAT_COUNT, reader.getAttrAsInt(ATTR_FLOAT_COUNT, 0)); - processMap.put(ATTR_WIDGET_COUNT, reader.getAttrAsInt(ATTR_WIDGET_COUNT, 0)); - processMap.put(ATTR_CELL_COUNT, reader.getAttrAsInt(ATTR_CELL_COUNT, 0)); - processMap.put(ATTR_BLOCK_COUNT, reader.getAttrAsInt(ATTR_BLOCK_COUNT, 0)); - processMap.put(ATTR_REPORT_TYPE, reader.getAttrAsInt(ATTR_REPORT_TYPE, 0)); - processMap.put(ATTR_TEMPLATE_ID, templateID); - } else if (XML_CONSUMING_MAP.equals(name)) { - consumingMap.put(ATTR_ACTIVITYKEY, reader.getAttrAsString(ATTR_ACTIVITYKEY, StringUtils.EMPTY)); - consumingMap.put(ATTR_JAR_TIME, reader.getAttrAsString(ATTR_JAR_TIME, StringUtils.EMPTY)); - consumingMap.put(ATTR_CREATE_TIME, reader.getAttrAsString(ATTR_CREATE_TIME, StringUtils.EMPTY)); - consumingMap.put(ATTR_TEMPLATE_ID, templateID); - consumingMap.put(ATTR_UUID, reader.getAttrAsString(ATTR_UUID, StringUtils.EMPTY)); - consumingMap.put(ATTR_TIME_CONSUME, reader.getAttrAsLong(ATTR_TIME_CONSUME, 0)); - consumingMap.put(ATTR_VERSION, reader.getAttrAsString(ATTR_VERSION, "8.0")); - consumingMap.put(ATTR_USERNAME, reader.getAttrAsString(ATTR_USERNAME, StringUtils.EMPTY)); - } - } catch (Exception ex) { - // 什么也不做,使用默认值 - } - } - } - - } -} diff --git a/designer-base/src/main/resources/com/fr/aspectj/designerbase/TemplateProcessTracker.aj b/designer-base/src/main/resources/com/fr/aspectj/designerbase/TemplateProcessTracker.aj deleted file mode 100644 index 1ab23100b1..0000000000 --- a/designer-base/src/main/resources/com/fr/aspectj/designerbase/TemplateProcessTracker.aj +++ /dev/null @@ -1,58 +0,0 @@ -package com.fr.aspectj.designerbase; - -/** - * 记录模板过程 - * Created by plough on 2017/3/3. - */ - -import org.aspectj.lang.reflect.SourceLocation; - -import java.awt.event.ActionEvent; -import java.awt.event.MouseEvent; - -public aspect TemplateProcessTracker { - //声明一个pointcut,匹配你需要的方法 - pointcut onMouseClicked(MouseEvent e): - execution(* mouseClicked(MouseEvent)) && args(e); - pointcut onMousePressed(MouseEvent e): - execution(* mousePressed(MouseEvent)) && args(e); - pointcut onMouseReleased(MouseEvent e): - execution(* mouseReleased(MouseEvent)) && args(e); - pointcut onActionPerformed(ActionEvent e): - execution(* actionPerformed(ActionEvent)) && args(e); - pointcut onSetValueAt(Object v, int r, int c): - execution(* setValueAt(java.lang.Object, int, int)) && args(v, r, c); - - //before表示之前的意思 - //这整个表示在MouseAdapter的public void mouseXXX(MouseEvent)方法调用之前,你想要执行的代码 - before(MouseEvent e): onMouseClicked(e) || onMousePressed(e) || onMouseReleased(e) { - SourceLocation sl = thisJoinPoint.getSourceLocation();//切面对应的代码位置 - - //String log = String.format("%s:\n%s\n%s\n%s\n\n", new Date(), sl, e, e.getSource()); - String log = ""; - //TemplateInfoCollector.appendProcess(log); - } - //同上 - before(ActionEvent e): onActionPerformed(e) { - SourceLocation sl = thisJoinPoint.getSourceLocation(); - // !within(LogHandlerBar) 没用, 手动过滤 - if (e != null && e.getSource().toString().contains("javax.swing.Timer")) { - return; - } - - //String log = String.format("%s:\n%s\n%s\n%s\n\n", new Date(), sl, e, e.getSource()); - String log = ""; - //TemplateInfoCollector.appendProcess(log); - - } - //同上 - before(Object v, int r, int c): onSetValueAt(v, r, c) { - SourceLocation sl = thisJoinPoint.getSourceLocation(); - - //String log getSourceLocation= String.format("%s:\n%s\nset value: %s at (%d, %d)\n\n", new Date(), sl, v, r, c); - String log = ""; - // TemplateInfoCollector.appendProcess(log); - } - - -} diff --git a/designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoCollectorTest.java b/designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoCollectorTest.java new file mode 100644 index 0000000000..d3ced29222 --- /dev/null +++ b/designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoCollectorTest.java @@ -0,0 +1,46 @@ +package com.fr.design.mainframe.template.info; + +import com.fr.invoke.Reflect; +import com.fr.stable.ProductConstants; +import org.easymock.EasyMock; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.api.easymock.PowerMock; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Created by plough on 2019/4/18. + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest(ProductConstants.class) +public class TemplateInfoCollectorTest { + + @Before + public void setUp() { + PowerMock.mockStatic(ProductConstants.class); + EasyMock.expect(ProductConstants.getEnvHome()).andReturn("/Users/plough/.FineReport100").anyTimes(); + EasyMock.replay(); + PowerMock.replayAll(); + } + + // todo: 单元测试需要一般化,不能依赖特定电脑 + @Test + public void testReadXML() { + TemplateInfoCollector collector = TemplateInfoCollector.getInstance(); + assertEquals("2019-04-18", Reflect.on(collector).field("designerOpenDate").get()); + assertTrue(((Map)Reflect.on(collector).field("templateInfoMap").get()).size() > 0); + } + + @Test + public void testSend() { + TemplateInfoCollector tic = TemplateInfoCollector.getInstance(); +// tic.sendTemplateInfo(); + } +} diff --git a/designer-chart/src/main/resources/com/fr/aspectj/designerchart/TemplateProcessTracker.aj b/designer-chart/src/main/resources/com/fr/aspectj/designerchart/TemplateProcessTracker.aj index 89ed91033f..a2d2fb7b48 100644 --- a/designer-chart/src/main/resources/com/fr/aspectj/designerchart/TemplateProcessTracker.aj +++ b/designer-chart/src/main/resources/com/fr/aspectj/designerchart/TemplateProcessTracker.aj @@ -4,13 +4,10 @@ package com.fr.aspectj.designerchart; * Created by plough on 2017/3/3. */ import com.fr.chart.chartattr.Chart; -import com.fr.design.mainframe.templateinfo.TemplateInfoCollector; import org.aspectj.lang.reflect.SourceLocation; -import javax.swing.event.ListSelectionEvent; import java.awt.event.ActionEvent; import java.awt.event.MouseEvent; -import java.util.Date; public aspect TemplateProcessTracker { //声明一个pointcut,匹配你需要的方法 diff --git a/designer-form/src/main/java/com/fr/design/mainframe/JForm.java b/designer-form/src/main/java/com/fr/design/mainframe/JForm.java index d25ba6feee..bcf761fac5 100644 --- a/designer-form/src/main/java/com/fr/design/mainframe/JForm.java +++ b/designer-form/src/main/java/com/fr/design/mainframe/JForm.java @@ -37,8 +37,8 @@ import com.fr.design.gui.xpane.FormHyperlinkGroupPaneNoPop; import com.fr.design.layout.FRGUIPaneFactory; import com.fr.design.mainframe.form.FormECCompositeProvider; import com.fr.design.mainframe.form.FormECDesignerProvider; -import com.fr.design.mainframe.templateinfo.JFormProcessInfo; -import com.fr.design.mainframe.templateinfo.TemplateProcessInfo; +import com.fr.design.mainframe.template.info.JFormProcessInfo; +import com.fr.design.mainframe.template.info.TemplateProcessInfo; import com.fr.design.mainframe.toolbar.ToolBarMenuDock; import com.fr.design.mainframe.toolbar.ToolBarMenuDockPlus; import com.fr.design.menu.KeySetUtils; diff --git a/designer-form/src/main/java/com/fr/design/mainframe/templateinfo/JFormProcessInfo.java b/designer-form/src/main/java/com/fr/design/mainframe/template/info/JFormProcessInfo.java similarity index 95% rename from designer-form/src/main/java/com/fr/design/mainframe/templateinfo/JFormProcessInfo.java rename to designer-form/src/main/java/com/fr/design/mainframe/template/info/JFormProcessInfo.java index 15f501b138..fce5ab8090 100644 --- a/designer-form/src/main/java/com/fr/design/mainframe/templateinfo/JFormProcessInfo.java +++ b/designer-form/src/main/java/com/fr/design/mainframe/template/info/JFormProcessInfo.java @@ -1,4 +1,4 @@ -package com.fr.design.mainframe.templateinfo; +package com.fr.design.mainframe.template.info; import com.fr.form.main.Form; import com.fr.form.ui.container.WLayout; diff --git a/designer-form/src/main/resources/com/fr/aspectj/designerform/TemplateProcessTracker.aj b/designer-form/src/main/resources/com/fr/aspectj/designerform/TemplateProcessTracker.aj index 3be1a77fa7..9daec50091 100644 --- a/designer-form/src/main/resources/com/fr/aspectj/designerform/TemplateProcessTracker.aj +++ b/designer-form/src/main/resources/com/fr/aspectj/designerform/TemplateProcessTracker.aj @@ -3,12 +3,10 @@ package com.fr.aspectj.designerform; /** * Created by plough on 2017/3/3. */ -import com.fr.design.mainframe.templateinfo.TemplateInfoCollector; import org.aspectj.lang.reflect.SourceLocation; import java.awt.event.ActionEvent; import java.awt.event.MouseEvent; -import java.util.Date; public aspect TemplateProcessTracker { //声明一个pointcut,匹配你需要的方法 diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/InformationCollector.java b/designer-realize/src/main/java/com/fr/design/mainframe/InformationCollector.java index 7a3966c175..61f39b81db 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/InformationCollector.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/InformationCollector.java @@ -8,7 +8,7 @@ import com.fr.config.MarketConfig; import com.fr.design.DesignerEnvManager; import com.fr.design.mainframe.errorinfo.ErrorInfoUploader; import com.fr.design.mainframe.messagecollect.impl.FocusPointMessageUploader; -import com.fr.design.mainframe.templateinfo.TemplateInfoCollector; +import com.fr.design.mainframe.template.info.TemplateInfoCollector; import com.fr.general.CloudCenter; import com.fr.general.ComparatorUtils; import com.fr.general.DateUtils; diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/JWorkBook.java b/designer-realize/src/main/java/com/fr/design/mainframe/JWorkBook.java index 44becbd9cc..07136bc1ef 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/JWorkBook.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/JWorkBook.java @@ -41,8 +41,8 @@ import com.fr.design.gui.ibutton.UIButton; import com.fr.design.gui.icontainer.UIModeControlContainer; import com.fr.design.gui.imenu.UIMenuItem; import com.fr.design.mainframe.cell.QuickEditorRegion; -import com.fr.design.mainframe.templateinfo.JWorkBookProcessInfo; -import com.fr.design.mainframe.templateinfo.TemplateProcessInfo; +import com.fr.design.mainframe.template.info.JWorkBookProcessInfo; +import com.fr.design.mainframe.template.info.TemplateProcessInfo; import com.fr.design.mainframe.toolbar.ToolBarMenuDockPlus; import com.fr.design.menu.KeySetUtils; import com.fr.design.menu.MenuDef; diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/component/AlphaFineDialog.java b/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/component/AlphaFineDialog.java index c8ecd93036..044a484b16 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/component/AlphaFineDialog.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/alphafine/component/AlphaFineDialog.java @@ -33,15 +33,12 @@ import com.fr.design.mainframe.alphafine.search.manager.impl.RecentSearchManager import com.fr.design.mainframe.alphafine.search.manager.impl.RecommendSearchManager; import com.fr.design.mainframe.alphafine.search.manager.impl.SegmentationManager; import com.fr.design.mainframe.alphafine.search.manager.impl.SimilarSearchManager; -import com.fr.design.mainframe.errorinfo.ErrorInfoUploader; -import com.fr.design.mainframe.templateinfo.TemplateInfoCollector; import com.fr.form.main.Form; import com.fr.form.main.FormIO; import com.fr.general.ComparatorUtils; import com.fr.general.http.HttpClient; import com.fr.io.TemplateWorkBookIO; import com.fr.io.exporter.ImageExporter; -import com.fr.json.JSONException; import com.fr.json.JSONObject; import com.fr.log.FineLoggerFactory; import com.fr.main.impl.WorkBook; @@ -958,8 +955,6 @@ public class AlphaFineDialog extends UIDialog { RecentSearchManager searchManager = RecentSearchManager.getInstance(); searchManager.addModel(storeText, cellModel); sendDataToServer(storeText, cellModel); - TemplateInfoCollector.getInstance().sendTemplateInfo(); - ErrorInfoUploader.getInstance().sendErrorInfo(); } } }); diff --git a/designer-realize/src/main/java/com/fr/design/mainframe/templateinfo/JWorkBookProcessInfo.java b/designer-realize/src/main/java/com/fr/design/mainframe/template/info/JWorkBookProcessInfo.java similarity index 98% rename from designer-realize/src/main/java/com/fr/design/mainframe/templateinfo/JWorkBookProcessInfo.java rename to designer-realize/src/main/java/com/fr/design/mainframe/template/info/JWorkBookProcessInfo.java index 306c6b0d11..eec7773282 100644 --- a/designer-realize/src/main/java/com/fr/design/mainframe/templateinfo/JWorkBookProcessInfo.java +++ b/designer-realize/src/main/java/com/fr/design/mainframe/template/info/JWorkBookProcessInfo.java @@ -1,4 +1,4 @@ -package com.fr.design.mainframe.templateinfo; +package com.fr.design.mainframe.template.info; import com.fr.base.parameter.ParameterUI; import com.fr.main.impl.WorkBook; diff --git a/designer-realize/src/main/java/com/fr/start/Designer.java b/designer-realize/src/main/java/com/fr/start/Designer.java index d262359dda..23bf2f0ffe 100644 --- a/designer-realize/src/main/java/com/fr/start/Designer.java +++ b/designer-realize/src/main/java/com/fr/start/Designer.java @@ -28,7 +28,7 @@ import com.fr.design.mainframe.JWorkBook; import com.fr.design.mainframe.alphafine.component.AlphaFinePane; import com.fr.design.mainframe.bbs.UserInfoLabel; import com.fr.design.mainframe.bbs.UserInfoPane; -import com.fr.design.mainframe.templateinfo.TemplateInfoCollector; +import com.fr.design.mainframe.template.info.TemplateInfoCollector; import com.fr.design.mainframe.toolbar.ToolBarMenuDockPlus; import com.fr.design.menu.KeySetUtils; import com.fr.design.menu.MenuDef; diff --git a/designer-realize/src/main/resources/com/fr/aspectj/designer/TemplateProcessTracker.aj b/designer-realize/src/main/resources/com/fr/aspectj/designer/TemplateProcessTracker.aj index 8e4eeb7164..2c54338186 100644 --- a/designer-realize/src/main/resources/com/fr/aspectj/designer/TemplateProcessTracker.aj +++ b/designer-realize/src/main/resources/com/fr/aspectj/designer/TemplateProcessTracker.aj @@ -5,13 +5,11 @@ package com.fr.aspectj.designer; * Created by plough on 2017/3/3. */ -import com.fr.design.mainframe.templateinfo.TemplateInfoCollector; import com.fr.grid.Grid; import org.aspectj.lang.reflect.SourceLocation; import java.awt.event.ActionEvent; import java.awt.event.MouseEvent; -import java.util.Date; public aspect TemplateProcessTracker { //声明一个pointcut,匹配你需要的方法 From 61fd215bacec88366751847112c9348c4e88aaf3 Mon Sep 17 00:00:00 2001 From: plough Date: Thu, 18 Apr 2019 17:49:46 +0800 Subject: [PATCH 2/5] =?UTF-8?q?REPORT-15314=20103=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E6=94=B6=E9=9B=86=3D>=E9=87=8D=E6=9E=84=3D>?= =?UTF-8?q?=E5=8E=BB=E6=8E=898.0=E6=97=B6=E6=9C=9F=E7=9A=84=E5=85=BC?= =?UTF-8?q?=E5=AE=B9=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../template/info/TemplateInfoCollector.java | 69 ++++--------------- 1 file changed, 15 insertions(+), 54 deletions(-) diff --git a/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java index d7239886f3..2a4fa1a04b 100644 --- a/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java +++ b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java @@ -22,11 +22,10 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.ObjectInputStream; -import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -38,14 +37,12 @@ import java.util.Map; * 做模板的过程和耗时收集,辅助类 * Created by plough on 2017/2/21. */ -public class TemplateInfoCollector implements Serializable, XMLReadable, XMLWriter { +public class TemplateInfoCollector implements XMLReadable, XMLWriter { private static final String XML_TAG = "TplInfo"; private static final String XML_DESIGNER_OPEN_DATE = "DesignerOpenDate"; private static final String XML_TEMPLATE_INFO_LIST = "TemplateInfoList"; - static final long serialVersionUID = 2007L; private static final String XML_FILE_NAME = "tpl.info"; - private static final String OBJECT_FILE_NAME = "tplInfo.ser"; private static final int ONE_THOUSAND = 1000; @@ -56,48 +53,37 @@ public class TemplateInfoCollector implements Serializable, XMLReadable, XMLWrit @SuppressWarnings("unchecked") private TemplateInfoCollector() { - templateInfoMap = new HashMap<>(); setDesignerOpenDate(); + + loadTemplateInfoMap(); } - /** - * 获取缓存文件存放路径 - */ - private static File getInfoFile(String fileName) { - return new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), fileName)); + private void loadTemplateInfoMap() { + templateInfoMap = new HashMap<>(); + try { + XMLableReader xmlReader = XMLableReader.createXMLableReader(new FileReader(getInfoFile())); + xmlReader.readXMLObject(this); + } catch (XMLStreamException e) { + e.printStackTrace(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } } /** * 获取缓存文件存放路径 */ private static File getInfoFile() { - return getInfoFile(XML_FILE_NAME); + return new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), XML_FILE_NAME)); } public static TemplateInfoCollector getInstance() { if (instance == null) { instance = new TemplateInfoCollector(); - - File xmlFile = getInfoFile(); - File objFile = getInfoFile(OBJECT_FILE_NAME); - if (xmlFile.exists()) { - readXMLFile(instance, xmlFile); - } else if (objFile.exists()) { - readFromObjectFile(objFile); - } } return instance; } - private static void readFromObjectFile(File objFile) { - try { - ObjectInputStream is = new ObjectInputStream(new FileInputStream(objFile)); - instance = (TemplateInfoCollector) is.readObject(); - } catch (Exception ex) { - // 什么也不做,instance 使用新值 - } - } - /** * 根据模板ID是否在收集列表中,判断是否需要收集当前模板的信息 */ @@ -105,13 +91,10 @@ public class TemplateInfoCollector implements Serializable, XMLReadable, XMLWrit return StringUtils.isNotEmpty(templateID) && templateInfoMap.containsKey(templateID); } - - /** * 收集模板信息。如果之前没有记录,则新增;如果已有记录,则更新。 * 同时将最新数据保存到文件中。 */ - @SuppressWarnings("unchecked") public void collectInfo(String templateID, TemplateProcessInfo processInfo, long openTime, long saveTime) { if (!shouldCollectInfo()) { return; @@ -160,28 +143,6 @@ public class TemplateInfoCollector implements Serializable, XMLReadable, XMLWrit saveInfo(); } - private static void readXMLFile(XMLReadable xmlReadable, File xmlFile) { - String charset = EncodeConstants.ENCODING_UTF_8; - try { - String fileContent = getFileContent(xmlFile); - InputStream xmlInputStream = new ByteArrayInputStream(fileContent.getBytes(charset)); - InputStreamReader inputStreamReader = new InputStreamReader(xmlInputStream, charset); - XMLableReader xmlReader = XMLableReader.createXMLableReader(inputStreamReader); - - if (xmlReader != null) { - xmlReader.readXMLObject(xmlReadable); - } - xmlInputStream.close(); - } catch (FileNotFoundException e) { - FineLoggerFactory.getLogger().error(e.getMessage(), e); - } catch (IOException e) { - FineLoggerFactory.getLogger().error(e.getMessage(), e); - } catch (XMLStreamException e) { - FineLoggerFactory.getLogger().error(e.getMessage(), e); - } - - } - private static String getFileContent(File xmlFile) throws FileNotFoundException, UnsupportedEncodingException { InputStream is = new FileInputStream(xmlFile); return IOUtils.inputStream2String(is); From 5e5815d88b473121af76b73357796b5fc17a615a Mon Sep 17 00:00:00 2001 From: plough Date: Thu, 18 Apr 2019 19:13:18 +0800 Subject: [PATCH 3/5] =?UTF-8?q?REPORT-15314=20103=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E6=94=B6=E9=9B=86=3D>=E9=87=8D=E6=9E=84=3D>?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fr/design/mainframe/JTemplate.java | 4 +- .../mainframe/template/info/TemplateInfo.java | 6 +- .../template/info/TemplateInfoCollector.java | 117 +++++++----------- .../info/TemplateInfoCollectorTest.java | 16 +-- .../design/mainframe/template/info/tpl.info | 35 ++++++ 5 files changed, 92 insertions(+), 86 deletions(-) create mode 100644 designer-base/src/test/resources/com/fr/design/mainframe/template/info/tpl.info diff --git a/designer-base/src/main/java/com/fr/design/mainframe/JTemplate.java b/designer-base/src/main/java/com/fr/design/mainframe/JTemplate.java index bedeb47914..f4aa5bc9ba 100644 --- a/designer-base/src/main/java/com/fr/design/mainframe/JTemplate.java +++ b/designer-base/src/main/java/com/fr/design/mainframe/JTemplate.java @@ -78,6 +78,7 @@ import java.util.regex.Pattern; public abstract class JTemplate> extends TargetComponent implements ToolBarMenuDockPlus, DesignerProxy { // TODO ALEX_SEP editingFILE这个属性一定要吗?如果非要不可,有没有可能保证不为null private static final int PREFIX_NUM = 3000; + private static final int ONE_THOUSAND = 1000; private FILE editingFILE = null; // alex:初始状态为saved,这样不管是新建模板,还是打开模板,如果未做任何操作直接关闭,不提示保存 private boolean saved = true; @@ -149,7 +150,8 @@ public abstract class JTemplate> } long saveTime = System.currentTimeMillis(); // 保存模板的时间点 try { - TemplateInfoCollector.getInstance().collectInfo(template.getTemplateID(), getProcessInfo(), openTime, saveTime); + long timeConsume = ((saveTime - openTime) / ONE_THOUSAND); // 制作模板耗时(单位:s) + TemplateInfoCollector.getInstance().collectInfo(template.getTemplateID(), getProcessInfo(), timeConsume); } catch (Throwable th) { // 不管收集过程中出现任何异常,都不应该影响模版保存 } openTime = saveTime; // 更新 openTime,准备下一次计算 diff --git a/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfo.java b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfo.java index 0d8696d636..0def268abd 100644 --- a/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfo.java +++ b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfo.java @@ -53,13 +53,13 @@ class TemplateInfo implements XMLReadable, XMLWriter { private TemplateInfo() { } - static TemplateInfo newInstance(XMLableReader reader) { + static TemplateInfo newInstanceByRead(XMLableReader reader) { TemplateInfo templateInfo = new TemplateInfo(); reader.readXMLObject(templateInfo); return templateInfo; } - static TemplateInfo newInstance(String templateID, long timeConsume) { + static TemplateInfo newInstance(String templateID) { HashMap consumingMap = new HashMap<>(); String username = MarketConfig.getInstance().getBbsUsername(); @@ -73,7 +73,7 @@ class TemplateInfo implements XMLReadable, XMLWriter { consumingMap.put(ATTR_ACTIVITYKEY, activitykey); consumingMap.put(ATTR_TEMPLATE_ID, templateID); consumingMap.put(ATTR_CREATE_TIME, createTime); - consumingMap.put(ATTR_TIME_CONSUME, timeConsume); + consumingMap.put(ATTR_TIME_CONSUME, 0); consumingMap.put(ATTR_JAR_TIME, jarTime); consumingMap.put(ATTR_VERSION, version); diff --git a/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java index 2a4fa1a04b..2bd302282b 100644 --- a/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java +++ b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java @@ -3,9 +3,7 @@ package com.fr.design.mainframe.template.info; import com.fr.base.FRContext; import com.fr.design.DesignerEnvManager; import com.fr.general.ComparatorUtils; -import com.fr.general.IOUtils; import com.fr.log.FineLoggerFactory; -import com.fr.stable.EncodeConstants; import com.fr.stable.ProductConstants; import com.fr.stable.StableUtils; import com.fr.stable.StringUtils; @@ -17,16 +15,10 @@ import com.fr.stable.xml.XMLableReader; import com.fr.third.javax.xml.stream.XMLStreamException; import com.fr.workspace.WorkContext; -import java.io.ByteArrayInputStream; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; @@ -41,40 +33,13 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter { private static final String XML_TAG = "TplInfo"; private static final String XML_DESIGNER_OPEN_DATE = "DesignerOpenDate"; private static final String XML_TEMPLATE_INFO_LIST = "TemplateInfoList"; - private static final String XML_FILE_NAME = "tpl.info"; - - private static final int ONE_THOUSAND = 1000; - private static TemplateInfoCollector instance; private Map templateInfoMap; private String designerOpenDate; //设计器最近一次打开日期 - - @SuppressWarnings("unchecked") private TemplateInfoCollector() { - setDesignerOpenDate(); - - loadTemplateInfoMap(); - } - - private void loadTemplateInfoMap() { - templateInfoMap = new HashMap<>(); - try { - XMLableReader xmlReader = XMLableReader.createXMLableReader(new FileReader(getInfoFile())); - xmlReader.readXMLObject(this); - } catch (XMLStreamException e) { - e.printStackTrace(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - - /** - * 获取缓存文件存放路径 - */ - private static File getInfoFile() { - return new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), XML_FILE_NAME)); + init(); } public static TemplateInfoCollector getInstance() { @@ -94,32 +59,23 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter { /** * 收集模板信息。如果之前没有记录,则新增;如果已有记录,则更新。 * 同时将最新数据保存到文件中。 + * @param timeConsume 本次制作耗时,单位为 s */ - public void collectInfo(String templateID, TemplateProcessInfo processInfo, long openTime, long saveTime) { + public void collectInfo(String templateID, TemplateProcessInfo processInfo, long timeConsume) { if (!shouldCollectInfo()) { return; } - long timeConsume = ((saveTime - openTime) / ONE_THOUSAND); // 制作模板耗时(单位:s) - - - TemplateInfo templateInfo; - - if (templateInfoMap.containsKey(templateID)) { // 已有记录 - templateInfo = templateInfoMap.get(templateID); - templateInfo.addTimeConsume(timeConsume); - } else { // 新增 - templateInfo = TemplateInfo.newInstance(templateID, timeConsume); - } - + TemplateInfo templateInfo = getOrCreateTemplateInfoByID(templateID); + // 收集制作耗时 + templateInfo.addTimeConsume(timeConsume); + // 收集模版基本信息 templateInfo.updateProcessMap(processInfo); - - // 保存模板时,让 day_count 归零 + // 刷新闲置日计数器 templateInfo.setIdleDayCount(0); - templateInfoMap.put(templateID, templateInfo); - - saveInfo(); // 每次更新之后,都同步到暂存文件中 + // 每次更新之后,都同步到暂存文件中 + saveInfo(); } /** @@ -143,9 +99,37 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter { saveInfo(); } - private static String getFileContent(File xmlFile) throws FileNotFoundException, UnsupportedEncodingException { - InputStream is = new FileInputStream(xmlFile); - return IOUtils.inputStream2String(is); + /** + * 获取缓存文件存放路径 + */ + private static File getInfoFile() { + return new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), XML_FILE_NAME)); + } + + private void init() { + templateInfoMap = new HashMap<>(); + setDesignerOpenDate(); + + if (!getInfoFile().exists()) { + return; + } + try { + XMLableReader xmlReader = XMLableReader.createXMLableReader(new FileReader(getInfoFile())); + xmlReader.readXMLObject(this); + } catch (XMLStreamException e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + } catch (FileNotFoundException e) { + // do nothing + } + } + + private TemplateInfo getOrCreateTemplateInfoByID(String templateID) { + if (templateInfoMap.containsKey(templateID)) { + return templateInfoMap.get(templateID); + } + TemplateInfo templateInfo = TemplateInfo.newInstance(templateID); + templateInfoMap.put(templateID, templateInfo); + return templateInfo; } /** @@ -156,7 +140,7 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter { } /** - * 判断今天是否第一次打开设计器 + * 判断今天是否第一次打开设计器,为了防止同一天内,多次 addDayCount */ private boolean designerOpenFirstTime() { String today = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()); @@ -195,7 +179,6 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter { } } - private void removeTestTemplates() { // 删除所有已完成的测试模版 ArrayList testTemplateKeys = new ArrayList<>(); // 保存测试模板的key @@ -221,8 +204,9 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter { String name = reader.getTagName(); if (XML_DESIGNER_OPEN_DATE.equals(name)) { this.designerOpenDate = reader.getElementValue(); - } else if (XML_TEMPLATE_INFO_LIST.equals(name)) { - readTemplateInfoList(reader); + } else if (TemplateInfo.XML_TAG.equals(name)) { + TemplateInfo templateInfo = TemplateInfo.newInstanceByRead(reader); + templateInfoMap.put(templateInfo.getTemplateID(), templateInfo); } } catch (Exception ex) { // 什么也不做,使用默认值 @@ -230,17 +214,6 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter { } } - private void readTemplateInfoList(XMLableReader reader) { - reader.readXMLObject(new XMLReadable() { - public void readXML(XMLableReader reader) { - if (TemplateInfo.XML_TAG.equals(reader.getTagName())) { - TemplateInfo templateInfo = TemplateInfo.newInstance(reader); - templateInfoMap.put(templateInfo.getTemplateID(), templateInfo); - } - } - }); - } - @Override public void writeXML(XMLPrintWriter writer) { writer.startTAG(XML_TAG); diff --git a/designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoCollectorTest.java b/designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoCollectorTest.java index d3ced29222..1c38235017 100644 --- a/designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoCollectorTest.java +++ b/designer-base/src/test/java/com/fr/design/mainframe/template/info/TemplateInfoCollectorTest.java @@ -13,7 +13,6 @@ import org.powermock.modules.junit4.PowerMockRunner; import java.util.Map; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; /** * Created by plough on 2019/4/18. @@ -25,22 +24,19 @@ public class TemplateInfoCollectorTest { @Before public void setUp() { PowerMock.mockStatic(ProductConstants.class); - EasyMock.expect(ProductConstants.getEnvHome()).andReturn("/Users/plough/.FineReport100").anyTimes(); + + String filePath = getClass().getResource("tpl.info").getPath(); + String dirPath = filePath.substring(0, filePath.indexOf("tpl.info")); + EasyMock.expect(ProductConstants.getEnvHome()).andReturn(dirPath).anyTimes(); + EasyMock.replay(); PowerMock.replayAll(); } - // todo: 单元测试需要一般化,不能依赖特定电脑 @Test public void testReadXML() { TemplateInfoCollector collector = TemplateInfoCollector.getInstance(); assertEquals("2019-04-18", Reflect.on(collector).field("designerOpenDate").get()); - assertTrue(((Map)Reflect.on(collector).field("templateInfoMap").get()).size() > 0); - } - - @Test - public void testSend() { - TemplateInfoCollector tic = TemplateInfoCollector.getInstance(); -// tic.sendTemplateInfo(); + assertEquals(7, ((Map) Reflect.on(collector).field("templateInfoMap").get()).size()); } } diff --git a/designer-base/src/test/resources/com/fr/design/mainframe/template/info/tpl.info b/designer-base/src/test/resources/com/fr/design/mainframe/template/info/tpl.info new file mode 100644 index 0000000000..39bec00067 --- /dev/null +++ b/designer-base/src/test/resources/com/fr/design/mainframe/template/info/tpl.info @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 1599fffaa991fed6f07aa9c3f357a64025f374cd Mon Sep 17 00:00:00 2001 From: plough Date: Thu, 18 Apr 2019 19:24:00 +0800 Subject: [PATCH 4/5] =?UTF-8?q?REPORT-15314=20103=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E6=94=B6=E9=9B=86=3D>=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../design/mainframe/template/info/TemplateInfo.java | 6 +++--- .../mainframe/template/info/TemplateInfoCollector.java | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfo.java b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfo.java index 0def268abd..7f2e1e7661 100644 --- a/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfo.java +++ b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfo.java @@ -44,9 +44,9 @@ class TemplateInfo implements XMLReadable, XMLWriter { private static final int VALID_WIDGET_COUNT = 5; // 有效报表模板的控件数 private static final int COMPLETE_DAY_COUNT = 15; // 判断模板是否完成的天数 - private int idleDayCount; // 到现在为止,模版闲置(上次保存后没有再编辑过)的天数 private String templateID; + // todo: processMap 和 consumingMap 还可以再拆解为小类,以后继续重构 private HashMap processMap = new HashMap<>(); private HashMap consumingMap = new HashMap<>(); @@ -220,8 +220,8 @@ class TemplateInfo implements XMLReadable, XMLWriter { this.processMap = processMap; } - void setIdleDayCount(int idleDayCount) { - this.idleDayCount = idleDayCount; + void resetIdleDayCount() { + this.idleDayCount = 0; } void addIdleDayCountByOne() { diff --git a/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java index 2bd302282b..103047be75 100644 --- a/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java +++ b/designer-base/src/main/java/com/fr/design/mainframe/template/info/TemplateInfoCollector.java @@ -72,7 +72,7 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter { // 收集模版基本信息 templateInfo.updateProcessMap(processInfo); // 刷新闲置日计数器 - templateInfo.setIdleDayCount(0); + templateInfo.resetIdleDayCount(); // 每次更新之后,都同步到暂存文件中 saveInfo(); @@ -82,7 +82,7 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter { * 发送本地模板信息到服务器,并清空已发送模版的本地记录 */ public void sendTemplateInfo() { - addDayCount(); + addIdleDayCount(); removeTestTemplates(); @@ -140,7 +140,7 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter { } /** - * 判断今天是否第一次打开设计器,为了防止同一天内,多次 addDayCount + * 判断今天是否第一次打开设计器,为了防止同一天内,多次 addIdleDayCount */ private boolean designerOpenFirstTime() { String today = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()); @@ -170,7 +170,7 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter { /** * 更新 day_count:打开设计器却未编辑模板的连续日子 */ - private void addDayCount() { + private void addIdleDayCount() { if (designerOpenFirstTime()) { for (TemplateInfo templateInfo : templateInfoMap.values()) { templateInfo.addIdleDayCountByOne(); @@ -179,8 +179,8 @@ public class TemplateInfoCollector implements XMLReadable, XMLWriter { } } + // 删除所有已完成的测试模版 private void removeTestTemplates() { - // 删除所有已完成的测试模版 ArrayList testTemplateKeys = new ArrayList<>(); // 保存测试模板的key for (String key : templateInfoMap.keySet()) { if (templateInfoMap.get(key).isTestTemplate()) { From 2a520423a459a4d48f3fc5c4ed87389a4af9080a Mon Sep 17 00:00:00 2001 From: plough Date: Thu, 18 Apr 2019 21:29:34 +0800 Subject: [PATCH 5/5] =?UTF-8?q?REPORT-15314=20103=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E6=94=B6=E9=9B=86=3D>=E9=87=8D=E6=9E=84=3D>?= =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mainframe/template/info/SendHelper.java | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/designer-base/src/main/java/com/fr/design/mainframe/template/info/SendHelper.java b/designer-base/src/main/java/com/fr/design/mainframe/template/info/SendHelper.java index 1a69dc4d70..1f7aeec63d 100644 --- a/designer-base/src/main/java/com/fr/design/mainframe/template/info/SendHelper.java +++ b/designer-base/src/main/java/com/fr/design/mainframe/template/info/SendHelper.java @@ -3,9 +3,10 @@ package com.fr.design.mainframe.template.info; import com.fr.design.mainframe.SiteCenterToken; import com.fr.general.CloudCenter; import com.fr.general.ComparatorUtils; -import com.fr.general.http.HttpClient; +import com.fr.general.http.HttpToolbox; import com.fr.json.JSONObject; +import java.io.IOException; import java.util.HashMap; /** @@ -32,21 +33,12 @@ class SendHelper { HashMap para = new HashMap<>(); para.put("token", SiteCenterToken.generateToken()); para.put("content", content); - HttpClient httpClient = new HttpClient(url, para, true); - httpClient.setTimeout(5000); - httpClient.asGet(); - if (!httpClient.isServerAlive()) { - return false; - } - - String res = httpClient.getResponseText(); - boolean success; try { - success = ComparatorUtils.equals(new JSONObject(res).get("status"), "success"); - } catch (Exception ex) { - success = false; + String res = HttpToolbox.get(url, para); + return ComparatorUtils.equals(new JSONObject(res).get("status"), "success"); + } catch (IOException ignore) { } - return success; + return false; } }