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