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;
|
}
|
}
|