zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
commit | author | age
a18bfa 1 package cn.autoform.web.client.util;
Z 2
3 import cn.autoform.bean.form.autowhere.ScreenCondition;
4 import cn.autoform.bean.form.autowhere.ScreenCondition.Condition;
5 import cn.autoform.bean.form.autowhere.ScreenCondition.Condition.Type;
6 import cn.autoform.util.form.AutoConditionUtil;
7 import cn.autoform.util.tool.JSONTool;
8 import cn.autoform.util.tool.ObjectUtil;
9 import com.googlecode.aviator.AviatorEvaluator;
10 import com.googlecode.aviator.exception.CompileExpressionErrorException;
11 import com.googlecode.aviator.runtime.function.AbstractFunction;
12 import com.googlecode.aviator.runtime.type.AviatorBoolean;
13 import com.googlecode.aviator.runtime.type.AviatorObject;
14 import lombok.extern.slf4j.Slf4j;
15 import org.springframework.util.PropertyPlaceholderHelper;
16 import org.springframework.util.SystemPropertyUtils;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Objects;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25 @Slf4j
26 public class FormatParsingTool {
27
28     static {
29         AviatorEvaluator.addFunction(new AbstractFunction(){
30
31             @Override
32             public String getName() {
33                 return "if";
34             }
35
36             @Override
37             public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3) {
38                 return arg1.booleanValue(env) ? arg2 : arg3;
39             }
40         });
41
42         AviatorEvaluator.addFunction(new AbstractFunction(){
43
44             @Override
45             public String getName() {
46                 return "empty";
47             }
48
49             @Override
50             public AviatorObject call(Map<String, Object> env, AviatorObject arg1) {
51                 return AviatorBoolean.valueOf(ObjectUtil.empty(arg1.stringValue(env)));
52             }
53         });
54     }
55 public static void main(String... args){
56     System.out.println(AviatorEvaluator.execute("\nif(empty(''), \"\", \"and budget_project = ''\")\n".replace("", "")));
57 }
58     public static void main1(String... args){
59         System.out.println(parsingExpression(
60                 JSONTool.toObj("{\n" +
61                         "        \"child\":[\n" +
62                         "            {\n" +
63                         "                \"fieldKey\":\"budget_project\",\n" +
64                         "                \"side\":\"thi\",\n" +
65                         "                \"pIndex\":0,\n" +
66                         "                \"value\":\"\"\n" +
67                         "            },\n" +
68                         "            {\n" +
69                         "                \"fieldKey\":\"budget_project\",\n" +
70                         "                \"side\":\"ref\",\n" +
71                         "                \"pIndex\":1\n" +
72                         "            },\n" +
73                         "            {\n" +
74                         "                \"fieldKey\":\"budget_project\",\n" +
75                         "                \"side\":\"thi\",\n" +
76                         "                \"pIndex\":2,\n" +
77                         "                \"value\":\"\"\n" +
78                         "            },\n" +
79                         "            {\n" +
80                         "                \"fieldKey\":\"budgetStartDate\",\n" +
81                         "                \"side\":\"thi\",\n" +
82                         "                \"pIndex\":3,\n" +
83                         "                \"value\":\"2018-10-19\"\n" +
84                         "            },\n" +
85                         "            {\n" +
86                         "                \"fieldKey\":\"budgetStartDate\",\n" +
87                         "                \"side\":\"ref\",\n" +
88                         "                \"pIndex\":4\n" +
89                         "            }\n" +
90                         "        ],\n" +
91                         "        \"count\":\"processstate = 30 \n" +
92                         "and stateExecution=\\\"\\\" \n" +
93                         "$(\n" +
94                         "if(empty({0}), \\\"\\\", \\\"and {1} = {2}\\\")\n" +
95                         ") \n" +
96                         "and {3} > {4}\"\n" +
97                         "    }", ScreenCondition.class)
98         ));
99     }
100     
101     public static String parsingExpression(ScreenCondition screenCondition){
102         String val = parsing(screenCondition);
103         if(ObjectUtil.empty(val)){
104             return val;
105         }
106         PropertyPlaceholderHelper p = new PropertyPlaceholderHelper(
107                 "$("
108                 , ")"
109                 , SystemPropertyUtils.VALUE_SEPARATOR
110                 , true);
111         return p.replacePlaceholders(val, (expression)->{
112             try {
113                 return Objects.toString(AviatorEvaluator.execute(Objects.toString(expression, "").replace("\n", "")));
114             }catch (CompileExpressionErrorException e){
115                 log.error(e.getMessage(), e);
116                 throw e;
117             }
118         });
119     }
120     
121     public static String parsing(ScreenCondition screenCondition){
122         List<Condition> child = screenCondition.getChild();
123         String count = screenCondition.getCount();
124         List<String> li = null;
125         if(child.size()> 0){
126             li = new ArrayList<>();
127             for(Condition m : child){
128                 if(m.getSide() == Type.thi){
129                     li.add("'"+ getVal(m.getValue())+ "'");
130                 }else{
131                     li.add(m.getFieldKey());
132                 }
133             }
134             count = screenCondition.getCount();
135             
136         }
137         if( null == count || "".equals(count) || count.contains(" or 1=1 ")){
138             return null;
139         }else{
140             return parsingString(count, li);
141         }    
142     }
143     
144     public static String getVal(Object val){
145         if(val instanceof String)
146             return AutoConditionUtil.escapeSql(val);
147         else if(val instanceof List<?>){
148             if(((List<?>)val).size() == 1){
149                 return AutoConditionUtil.escapeSql(((List<?>)val).get(0));
150             }else if(((List<?>)val).size() > 1){
151                 final StringBuilder vals = new StringBuilder();
152                 ((List<?>)val).stream().filter(v->vals.append(',') != null).forEach(vals::append);
153                 return AutoConditionUtil.escapeSql(vals.toString().replaceAll("^,", ""));
154             }
155         }
156         return "";
157     }
158
159     public static String parsingString(String count, List<?> li){
160         Pattern pattern = Pattern.compile("[{][^}]+[}]");
161         Matcher matcher = pattern.matcher(count);
162         String group = null;
163         Integer index = null;
164         while(matcher.find()){
165             group = matcher.group();
166             index = Integer.parseInt(group.replaceAll("[{]|[}]", ""));
167             count = count.replace(group, (CharSequence) li.get(index));
168         }
169         return count;
170     }
171     
172 }