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 bedeb4791..f4aa5bc9b 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 0d8696d63..0def268ab 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 2a4fa1a04..2bd302282 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 d3ced2922..1c3823501 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 000000000..39bec0006 --- /dev/null +++ b/designer-base/src/test/resources/com/fr/design/mainframe/template/info/tpl.info @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +