Browse Source

代码质量

pull/1/head
zack 5 years ago
parent
commit
419b219d6c
  1. 6
      src/main/java/com/fanruan/api/cal/CalculatorKit.java
  2. 17
      src/main/java/com/fanruan/api/cal/FormulaKit.java
  3. 48
      src/main/java/com/fanruan/api/function/shell/FineFunc.java
  4. 66
      src/main/java/com/fanruan/api/report/analy/AnalyKit.java
  5. 75
      src/main/java/com/fanruan/api/report/analy/AnalyKitHelper.java
  6. 4
      src/main/java/com/fanruan/api/util/TransmissionKit.java
  7. 2
      src/main/java/com/fanruan/api/util/trans/BaseSmsBody.java
  8. 2
      src/main/java/com/fanruan/api/util/trans/BatchSmsBody.java
  9. 2
      src/main/java/com/fanruan/api/util/trans/EmailBody.java
  10. 2
      src/main/java/com/fanruan/api/util/trans/SingleSmsBody.java
  11. 2
      src/test/java/com/fanruan/api/cal/FormulaKitTest.java
  12. 2
      src/test/java/com/fanruan/api/function/FunctionKitTest.java
  13. 4
      src/test/java/com/fanruan/api/util/TransmissionKitTest.java

6
src/main/java/com/fanruan/api/cal/CalculatorKit.java

@ -29,7 +29,7 @@ public class CalculatorKit {
/**
* 创建一个基础算子基础算子仅支持函数计算.比如SUM()函数
*
* @return 算子
* @return 算子对象
*/
public static CalculatorProvider createCalculator() {
return Calculator.createCalculator();
@ -54,7 +54,7 @@ public class CalculatorKit {
*
* @param sessionID 模板的sessionID
* @param paraMap 其他参数
* @return
* @return 算子对象
*/
public static CalculatorProvider createCalculator(@Nullable String sessionID, @Nullable Map paraMap) {
ParameterMapNameSpace nameSpace = ParameterMapNameSpace.create(paraMap);
@ -80,7 +80,7 @@ public class CalculatorKit {
/**
* 返回服务器数据集的算子空间可以通过调用calculator.pushNameSpace()将算子空间塞进算子实例从而支持服务器数据集相关的函数计算
* @return
* @return 算子空间对象
*/
public static NameSpace getServerTableDataNameSpace(){
return TableDataNameSpace.getInstance();

17
src/main/java/com/fanruan/api/cal/FormulaKit.java

@ -3,19 +3,16 @@ package com.fanruan.api.cal;
import com.fanruan.api.err.KitError;
import com.fr.base.BaseFormula;
import com.fr.log.FineLoggerFactory;
import com.fr.parser.FRLexer;
import com.fr.parser.FRParser;
import com.fr.script.Calculator;
import com.fr.stable.FormulaProvider;
import com.fr.stable.StringUtils;
import com.fr.stable.UtilEvalError;
import com.fr.stable.script.CalculatorProvider;
import com.fr.stable.script.Expression;
import com.fr.third.antlr.ANTLRException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.StringReader;
/**
* @author richie
* @version 10.0
@ -64,24 +61,20 @@ public class FormulaKit {
/**
* 检查公式内容合法性
*
* @param content 公式文本公式的开头等号要先去掉如果想校验等号开头的内容需要传Formula对象
* @param content 公式文本
* @return 如果非空且不合法返回false, 反之返回true
*/
public static boolean checkFormulaContent(String content) {
String formulaText = content.trim();
if (StringUtils.isNotEmpty(formulaText)) {
StringReader in = new StringReader(formulaText);
FRLexer lexer = new FRLexer(in);
FRParser parser = new FRParser(lexer);
FormulaProvider formula = newFormula(formulaText);
Expression expression = null;
try {
expression = parser.parse();
} catch (Exception e) {
expression = formula.parse(Calculator.createCalculator());
} catch (ANTLRException e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
return null != expression;
}
return true;

48
src/main/java/com/fanruan/api/function/shell/FineFunc.java

@ -1,46 +1,24 @@
package com.fanruan.api.function.shell;
import com.fr.stable.StringUtils;
import com.fr.stable.script.FunctionDef;
/**
* 自定义函数封装
* ClassName FineFunc
*
* @Author zack
* @Date 2019/8/23
* @Version 10.0
* 自定义函数实体
*/
public class FineFunc {
private String name;//函数名
private String description;//描述
private String className;//完整类名
public FineFunc(String name, String description, String className) {
this.name = name;
this.description = description;
this.className = className;
}
public FineFunc(String name, String className) {
this(name, StringUtils.EMPTY, className);
public class FineFunc extends FunctionDef {
public FineFunc() {
}
public String getName() {
return name;
public FineFunc(String name, String descrption) {
super(name, descrption);
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
public FineFunc(String name, String description, String className) {
super(name, description, className);
}
}

66
src/main/java/com/fanruan/api/report/analy/AnalyKit.java

@ -1,9 +1,14 @@
package com.fanruan.api.report.analy;
import com.fanruan.api.report.analy.data.TreeNode;
import com.fr.cache.list.IntList;
import com.fr.form.ui.Widget;
import com.fr.main.workbook.ResultWorkBook;
import com.fr.report.cell.ResultCellElement;
import com.fr.report.cell.WidgetAttrElem;
import com.fr.report.report.Report;
import com.fr.report.report.ResultReport;
import com.fr.report.web.button.form.TreeNodeToggleButton;
import com.fr.report.worksheet.AnalysisRWorkSheet;
import com.fr.script.Calculator;
import com.fr.web.core.TreeHTMLWriter;
@ -11,7 +16,10 @@ import com.fr.web.core.reportcase.WebElementReportCase;
import com.fr.web.output.html.chwriter.ViewCellWriter;
import com.fr.web.request.EmptyRepository;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* @ClassName AnalyKit
* @Author zack
@ -42,6 +50,62 @@ public class AnalyKit {
htmlWriter.writeReportToHtml(new WebElementReportCase(analysisRWorkSheet, new EmptyRepository()), 1, cellHtmlWriter, new EmptyRepository(), "");
cellHtmlWriter.dealWithAllTreeNodeRelation(c);
return AnalyKitHelper.generateNodeTree(analysisRWorkSheet);
return generateNodeTree(analysisRWorkSheet);
}
private static Map<Integer, TreeNode> generateNodeTree(AnalysisRWorkSheet resultWS) {
int rowSize = resultWS.getRowCount();
Map<Integer, TreeNode> nodeMap = new HashMap<Integer, TreeNode>();
for (int rowIndex = 0; rowIndex < rowSize; rowIndex++) {//遍历行
ResultCellElement treeNodeCell = findToggleCell(resultWS, rowIndex);
if (treeNodeCell != null) {
Widget widget = ((WidgetAttrElem) treeNodeCell).getWidget();
IntList sonList = ((TreeNodeToggleButton) widget).getRelativeIndexList();
if (sonList != null && sonList.size() > 1) {
if (sonList.get(0) == -1) {
//折叠行
if (nodeMap.containsKey(treeNodeCell.getRow())) {
continue;
}
buildNodeMap(resultWS, treeNodeCell, nodeMap, -1);
} else {
//折叠列 暂不处理
}
}
}
}
return nodeMap;
}
private static ResultCellElement findToggleCell(AnalysisRWorkSheet reportCase, int rowIndex) {
Iterator cellIterator = reportCase.getRow(rowIndex);
while (cellIterator.hasNext()) {
ResultCellElement tmpCell = (ResultCellElement) cellIterator.next();
if (tmpCell instanceof WidgetAttrElem) {
if (((WidgetAttrElem) tmpCell).getWidget() instanceof TreeNodeToggleButton) {
return tmpCell;
}
}
}
return null;
}
private static void buildNodeMap(AnalysisRWorkSheet reportCase, ResultCellElement cellElment, Map<Integer, TreeNode> nodeMap, int parent) {
if (cellElment == null) {
return;
}
TreeNodeToggleButton toggleButton = (TreeNodeToggleButton) ((WidgetAttrElem) cellElment).getWidget();
int self = cellElment.getRow();
IntList sonList = toggleButton.getRelativeIndexList();
if (sonList != null && sonList.size() > 1) {
if (sonList.get(0) == -1) {//折叠行
nodeMap.put(self, new TreeNode(self, parent, sonList));
int size = sonList.size();
for (int i = 0; i < size; i++) {
buildNodeMap(reportCase, findToggleCell(reportCase, sonList.get(i)), nodeMap, self);
}
}
}
}
}

75
src/main/java/com/fanruan/api/report/analy/AnalyKitHelper.java

@ -1,75 +0,0 @@
package com.fanruan.api.report.analy;
import com.fanruan.api.report.analy.data.TreeNode;
import com.fr.cache.list.IntList;
import com.fr.form.ui.Widget;
import com.fr.report.cell.ResultCellElement;
import com.fr.report.cell.WidgetAttrElem;
import com.fr.report.web.button.form.TreeNodeToggleButton;
import com.fr.report.worksheet.AnalysisRWorkSheet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* 数据分析相关的业务类此类是为了接盘AnalyKit中的庞杂的私有方法增强AnalyKit的可读性
*/
public class AnalyKitHelper {
public static Map<Integer, TreeNode> generateNodeTree(AnalysisRWorkSheet resultWS) {
int rowSize = resultWS.getRowCount();
Map<Integer, TreeNode> nodeMap = new HashMap<Integer, TreeNode>();
for (int rowIndex = 0; rowIndex < rowSize; rowIndex++) {//遍历行
ResultCellElement treeNodeCell = findToggleCell(resultWS, rowIndex);
if (treeNodeCell != null) {
Widget widget = ((WidgetAttrElem) treeNodeCell).getWidget();
IntList sonList = ((TreeNodeToggleButton) widget).getRelativeIndexList();
if (sonList != null && sonList.size() > 1) {
if (sonList.get(0) == -1) {
//折叠行
if (nodeMap.containsKey(treeNodeCell.getRow())) {
continue;
}
buildNodeMap(resultWS, treeNodeCell, nodeMap, -1);
} else {
//折叠列 暂不处理
}
}
}
}
return nodeMap;
}
private static ResultCellElement findToggleCell(AnalysisRWorkSheet reportCase, int rowIndex) {
Iterator cellIterator = reportCase.getRow(rowIndex);
while (cellIterator.hasNext()) {
ResultCellElement tmpCell = (ResultCellElement) cellIterator.next();
if (tmpCell instanceof WidgetAttrElem) {
if (((WidgetAttrElem) tmpCell).getWidget() instanceof TreeNodeToggleButton) {
return tmpCell;
}
}
}
return null;
}
private static void buildNodeMap(AnalysisRWorkSheet reportCase, ResultCellElement cellElment, Map<Integer, TreeNode> nodeMap, int parent) {
if (cellElment == null) {
return;
}
TreeNodeToggleButton toggleButton = (TreeNodeToggleButton) ((WidgetAttrElem) cellElment).getWidget();
int self = cellElment.getRow();
IntList sonList = toggleButton.getRelativeIndexList();
if (sonList != null && sonList.size() > 1) {
if (sonList.get(0) == -1) {//折叠行
nodeMap.put(self, new TreeNode(self, parent, sonList));
int size = sonList.size();
for (int i = 0; i < size; i++) {
buildNodeMap(reportCase, findToggleCell(reportCase, sonList.get(i)), nodeMap, self);
}
}
}
}
}

4
src/main/java/com/fanruan/api/util/TransmissionKit.java

@ -1,7 +1,7 @@
package com.fanruan.api.util;
import com.fanruan.api.util.shell.BaseSmsBody;
import com.fanruan.api.util.shell.EmailBody;
import com.fanruan.api.util.trans.BaseSmsBody;
import com.fanruan.api.util.trans.EmailBody;
import com.fr.base.EmailManager;
import com.fr.base.sms.SMSManager;

2
src/main/java/com/fanruan/api/util/shell/BaseSmsBody.java → src/main/java/com/fanruan/api/util/trans/BaseSmsBody.java

@ -1,4 +1,4 @@
package com.fanruan.api.util.shell;
package com.fanruan.api.util.trans;
/**
* 抽象短信体

2
src/main/java/com/fanruan/api/util/shell/BatchSmsBody.java → src/main/java/com/fanruan/api/util/trans/BatchSmsBody.java

@ -1,4 +1,4 @@
package com.fanruan.api.util.shell;
package com.fanruan.api.util.trans;
import com.fr.base.sms.SMSManager;
import com.fr.json.JSONArray;

2
src/main/java/com/fanruan/api/util/shell/EmailBody.java → src/main/java/com/fanruan/api/util/trans/EmailBody.java

@ -1,4 +1,4 @@
package com.fanruan.api.util.shell;
package com.fanruan.api.util.trans;
import com.fr.base.EmailAttachment;
import org.jetbrains.annotations.Nullable;

2
src/main/java/com/fanruan/api/util/shell/SingleSmsBody.java → src/main/java/com/fanruan/api/util/trans/SingleSmsBody.java

@ -1,4 +1,4 @@
package com.fanruan.api.util.shell;
package com.fanruan.api.util.trans;
import com.fr.base.sms.SMSManager;
import com.fr.json.JSONObject;

2
src/test/java/com/fanruan/api/cal/FormulaKitTest.java

@ -50,7 +50,7 @@ public class FormulaKitTest extends Prepare {
assertEquals(true, FormulaKit.checkFormulaContent(""));
assertEquals(false, FormulaKit.checkFormulaContent(("=SUM(1,1)")));
assertEquals(true, FormulaKit.checkFormulaContent(("=SUM(1,1)")));
assertEquals(true, FormulaKit.checkFormulaContent(("SUM(1,1)")));

2
src/test/java/com/fanruan/api/function/FunctionKitTest.java

@ -33,7 +33,7 @@ public class FunctionKitTest extends Prepare {
public void testFuncRemove() {
FunctionKit.removeFunc("com.fanruan.api.function.TestFunc");
FunctionKit.addFunc(new FineFunc("TestFunc", "com.fanruan.api.function.TestFunc"));
FunctionKit.removeFunc("com.fr.sdk.FuncTest");
FunctionKit.removeFunc("com.fanruan.api.function.TestFunc");
String result = "ERROR";
try {
result = Calculator.createCalculator().evalValue("TESTFUNC()").toString();

4
src/test/java/com/fanruan/api/util/TransmissionKitTest.java

@ -1,7 +1,7 @@
package com.fanruan.api.util;
import com.fanruan.api.util.shell.BatchSmsBody;
import com.fanruan.api.util.shell.SingleSmsBody;
import com.fanruan.api.util.trans.BatchSmsBody;
import com.fanruan.api.util.trans.SingleSmsBody;
import org.junit.Test;
import static org.junit.Assert.assertTrue;

Loading…
Cancel
Save