田源
2024-03-07 4b4083fd73dc27ece42f4835483565eef0e4f608
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package com.vci.client.portal.form.defaultvalue;
 
import java.awt.Component;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
 
import javax.swing.text.JTextComponent;
 
import com.vci.client.common.FreeMarkerCommon;
import com.vci.client.common.FreemarkerParamObject;
import com.vci.client.common.objects.UserObject;
import com.vci.client.portal.utility.PRMItem;
import com.vci.client.ui.swing.components.VCIJComboBox;
import com.vci.client.uif.actions.client.AbstractBatchBusionessOperationAction;
import com.vci.client.uif.engine.client.controls.BaseCustomControl;
import com.vci.client.uif.engine.client.controls.DateControl;
import com.vci.client.uif.engine.client.controls.DateTimeControl;
import com.vci.client.uif.engine.client.controls.RefObjChooseControl;
import com.vci.client.uif.engine.client.controls.TimeControl;
import com.vci.client.uif.engine.client.custom.ICustomAttributeInteceptor;
import com.vci.client.uif.engine.client.objopt.ObjectAddEditPanel;
import com.vci.client.uif.engine.common.IDataNode;
import com.vci.mw.ClientContextVariable;
 
/**
 * @Title        :表单默认值处理控制器(ObjectAddEditPanel类目前太臃肿,将默认值处理逻辑进行分离)
 * @Description    : 
 * @Copyright    :宏博远达科技有限公司
 * @Author        :平台与规划部/ZhongGY/E-mail:zhonggy@vci-tech.com
 * @Date        :2015-6-24
 * @Version        :1
 * @Other        :产生注释:Alt+Shift+J
 */
public class DefaultValueController {
    
    private ObjectAddEditPanel objectAddEditPanel;
    
    public void setFormPanel(ObjectAddEditPanel objectAddEditPanel) {
        this.objectAddEditPanel = objectAddEditPanel;
    }
    public ObjectAddEditPanel getFormPanel(){
        return objectAddEditPanel;
    }
    /**
     * @Title        :统一设置属性卡字段默认值
     * @Description    :
     * @param sourceBtm:
     * @param iDataNode:
     */
    public Map<String, String> setUIFormFieldDefaultValue(AbstractBatchBusionessOperationAction sourceBtm, IDataNode iDataNode) {
        // 各组件的默认值Map
        Map<String, String> setDefValCompMap = new HashMap<String, String>();
        Iterator<String> it = this.getFormPanel().getItemMap().keySet().iterator();
        while (it.hasNext()) {
            String field = it.next();
            PRMItem item = this.getFormPanel().getItemMap().get(field);
            Component compt = this.getFormPanel().getCtrlComptMap().get(field);
            if (item == null || compt == null)
                continue;
            String itemStyleValue = item.getItemStyle();//add by caill 2016.2.19 获取附加属性的内容
            String itemValue = item.getItemValue();
            if ((itemValue == null || "".equals(itemValue)) && (itemStyleValue == null || "".equals(itemStyleValue)))//add by caill 2016.2.18 增加了对附加属性的判断
                //默认值定义为空不处理
                continue;
            String defaultValue = this.revieseExpressByGloableVariable(itemValue);            //全局变量与内置函数处理
            defaultValue = this.revieseExpressByCustomClass(defaultValue,sourceBtm);        //用户自定义类处理
            defaultValue = this.revieseExpressByFreemarker(defaultValue,iDataNode);        //freeMarker表达式处理
            if ((defaultValue == null || defaultValue.trim().equals("")) && (itemStyleValue == null || "".equals(itemStyleValue))) {//add by caill 2016.2.18 增加了对附加属性的判断
                continue;
            }
            // 加入到 设置过默认值的组件Map
            setDefValCompMap.put(field, defaultValue);
            if (compt instanceof JTextComponent) {
                // 文本控件
                JTextComponent txt = (JTextComponent) compt;
                txt.setText(defaultValue);
            } else if (compt instanceof VCIJComboBox) {
                // 枚举、值列表的下拉列表控件
                VCIJComboBox cbx = (VCIJComboBox) compt;
                this.getFormPanel().setComboxDefaultValue(item, cbx, defaultValue);
            } else if (compt instanceof RefObjChooseControl) {
                RefObjChooseControl roc = (RefObjChooseControl) compt;
                this.getFormPanel().setRocDefaultValue(item, roc, defaultValue);
            } else if (compt instanceof DateControl) {
                DateControl dtc = (DateControl) compt;
                dtc.setValue(defaultValue);
            } else if (compt instanceof TimeControl) {
                TimeControl dtc = (TimeControl) compt;
                dtc.setValue(defaultValue);
            } else if (compt instanceof DateTimeControl) {
                DateTimeControl dtc = (DateTimeControl) compt;
                //add by caill start 2016.2.18 当附加属性内容不为空时,将附加属性值进行传递
                dtc.setValue(defaultValue);
                String itemStyleName = item.getItemStyle();
                if(itemStyleName != null){
                    dtc.setItemStyle(itemStyleName);
                }
                //add by caill end
            } else if (compt instanceof BaseCustomControl) {
                BaseCustomControl bcc = (BaseCustomControl) compt;
                UserObject userObj = this.getFormPanel().getUserObjectByName(defaultValue);
                if (userObj != null) {
                    // 用户账号##VCI##用户姓名 存储值##VCI##显示值
                    String value = userObj.getUserName()
                            + ICustomAttributeInteceptor.SPLIT_CHAR
                            + userObj.getTrueName() + "("
                            + userObj.getUserName() + ")";
                    bcc.setValue(value);
                }
            }
        }
        return setDefValCompMap;
    }
    
    /**
     * @Title :freemarker表达式修正
     * @Description :
     * @param expression
     *            :复杂表达式,如:
     *            "${description}_#currentuser.name#_#CurrentDateTime#_!com.vci.ui.form.defaultvalue.InputDemo!_!com.vci.ui.form.defaultvalue.InitRevVer!"
     * @return:     
     */
    private String revieseExpressByFreemarker(String expression,IDataNode iDataNode) {
        String defaultValue = expression;
        if (!isFreemarkExpression(defaultValue)) {
            return defaultValue;
        }
        Map<String, String> valueMap = null;
        if (iDataNode != null && iDataNode.getValueMap() != null) {
            valueMap = iDataNode.getValueMap();
        }else {
            valueMap = new HashMap<String, String>();
        }
        String rule = expression;
        int start = rule.indexOf("${");
        int end = rule.indexOf("}");
        boolean allFieldExistInMap = true;    //是否所有字段都存在
        while (start >= 0 && end >= 0 && start < end && end < rule.length()) {
            String fieldRule = rule.substring(start + 2, end);
            rule = rule.substring(end + 1);
            if (!valueMap.containsKey(fieldRule)) {
                //不包含
                String tempValue = this.getFormPanel().getRegionPanel().getBaseLayoutPanel().getValue(fieldRule);
                if (tempValue.equals(fieldRule)) {
                    tempValue = "";
                } 
                valueMap.put(fieldRule, tempValue);
                allFieldExistInMap = false;
                continue;    
            }
            start = rule.indexOf("${");
            end = rule.indexOf("}");
        }
        Map<String, FreemarkerParamObject> rootMap = this.getFormPanel().convertValueMapToFPOMap(valueMap);
        allFieldExistInMap = true;    //allFieldExistInMap无效
        if (allFieldExistInMap) {
            defaultValue = FreeMarkerCommon.getValueByTempRule(rootMap,expression.replace(".", "_"));
            //defaultValue = this.regionPanel.getBaseLayoutPanel().getValue(defaultValue);
        }
        return defaultValue;
    }
 
    /**
     * @Title        :是否统一的参数表达式
     * @Description    :
     * @param expression
     * @param valueMap
     * @return
     */
    private boolean isFreemarkExpression(String expression){
        try {
            boolean res = true;
            String field = expression;
            int begin = field.indexOf("${");
            int end = field.indexOf("}");
            int len = field.length();
            String key = null;
            while(!"".equals(field) && begin >= 0 && end < len){
                key = field.substring(begin + 2, end);
                if (key == null || key.trim().equals("")) {
                    return false;
                }
                field = field.substring(end + 1);
                begin = field.indexOf("${");
                end = field.indexOf("}");
                len = field.length();
                res = true;
            }
            if (key == null || key.trim().equals("")) {
                return false;
            }
            return res;
        } catch (Exception e) {
            return false;
        }
    }
    
    /**
     * @Title        :根据全局变量与内置函数修正表达式
     * @Description    :
     * @param express
     * @return
     */
    private String revieseExpressByGloableVariable(String express) {
        String rtnExpress = express;
        Map<String,String> map = ClientContextVariable.getGlobalKeyValues();
        if(express.startsWith("#") && express.endsWith("#")){
            express = express.substring(1);
            express = express.substring(0, express.length() - 1);
        }
        if(map.containsKey(express)){
            rtnExpress = map.get(express);
        }
        if (rtnExpress.indexOf("#CURRENTTIME#")>-1) {
            rtnExpress = rtnExpress.replaceAll("#CURRENTTIME#", DefaultValueInnerFunction.getInstance().getCurrentTime());
        }
        if (rtnExpress.indexOf("#CURRENTDATE#")>-1) {
            rtnExpress = rtnExpress.replaceAll("#CURRENTDATE#", DefaultValueInnerFunction.getInstance().getCurrentDate());
        }
        if (rtnExpress.indexOf("#CURRENTDATETIME#")>-1) {
            rtnExpress = rtnExpress.replaceAll("#CURRENTDATETIME#", DefaultValueInnerFunction.getInstance().getCurrentDateTime());
        }
        return rtnExpress;
    }
    
    /**
     * @Title        :根据自定义类修正表达式
     * @Description    :
     * @param defaultValue
     * @return
     */
    private String revieseExpressByCustomClass(String defaultValueExpress,AbstractBatchBusionessOperationAction sourceBtm) {
        String rtnDefaultValueExpress = defaultValueExpress;
        Map<String,String> customClassExpress =  this.getCustomClassExpress(defaultValueExpress,sourceBtm);
        if (customClassExpress != null && customClassExpress.size() > 0) {
            for (Map.Entry<String,String> item : customClassExpress.entrySet()) {
                rtnDefaultValueExpress = rtnDefaultValueExpress.replaceAll(item.getKey(), item.getValue());
            }
        }
        return rtnDefaultValueExpress;
    }
    
    /**
     * @Title        :获取自定义表达式
     * @Description    :
     * @param defaultValueExpress = 
     * "${description}_#currentuser.name#_#CurrentDateTime#_!com.vci.ui.form.defaultvalue.InputDemo!_!com.vci.ui.form.defaultvalue.InitRevVer!"
     * @return
     */
    private Map<String, String> getCustomClassExpress(String defaultValueExpress,AbstractBatchBusionessOperationAction sourceBtm) {
        Map<String,String> customClassExpress = new HashMap<String, String>();
        try {
            /*boolean res = true;*/
            String field = defaultValueExpress;
            int begin = field.indexOf("!");
            String tempStr = field.replaceFirst("!", "~");
            int end = tempStr.indexOf("!");
            int len = field.length();
            String key = null;
            while(!"".equals(field) && begin >= 0 && end < len){
                key = field.substring(begin + 1, end);               //key = "com.vci.ui.form.defaultvalue.InitRevVer";
                if (key == null || key.trim().equals("")) {
                    continue;
                }else {
                    if (!customClassExpress.containsKey("!" + key + "!")) {
                        customClassExpress.put("!" + key + "!", key);
                    }
                }
                field = field.substring(end + 1);
                begin = field.indexOf("!");
                tempStr = field.replaceFirst("!", "~");
                end = tempStr.indexOf("!");
                len = field.length();
                /*res = true;*/
            }
            /*if (key == null || key.trim().equals("")) {
                return customClassExpress;
            }*/
            if (sourceBtm != null) {
                for (Map.Entry<String,String> item : customClassExpress.entrySet()) {
                    String classExpress = item.getValue();
                    IFormFieldDefaultValue instanceObject = null;
                    try {
                        instanceObject = (IFormFieldDefaultValue) Class.forName(classExpress).newInstance();
                    } catch (InstantiationException e1) {
                        e1.printStackTrace();
                    } catch (IllegalAccessException e1) {
                        e1.printStackTrace();
                    } catch (ClassNotFoundException e1) {
                        e1.printStackTrace();
                    }
                    if (instanceObject == null) {
                        continue;
                    }
                    String  defaultValue = "";
                    try {
                        defaultValue = instanceObject.getDefaultValue(sourceBtm);
                    } catch (Exception e) {
                    }
                    if (defaultValue != null) {
                        item.setValue(defaultValue);    //替换赋值真实值
                    }
                }
            }
        } catch (Exception e) {
        }
        return customClassExpress;
    }
}