一、效果
二、代码
public class MideaNoAttrClassificationPickerDataUtility extends AbstractDataUtility {
@Override
public Object getDataValue(String component_id, Object datum, ModelContext modelcontext) throws WTException {
NmCommandBean commandBean = modelcontext.getNmCommandBean();
Object component = null;
ComponentMode componentMode = modelcontext.getDescriptorMode();
if (componentMode.equals(ComponentMode.EDIT) || componentMode.equals(ComponentMode.CREATE)) {
component = createPickerComponent(component_id, datum, modelcontext, componentMode);
} else {
com.ptc.core.components.rendering.guicomponents.Label lable = new com.ptc.core.components.rendering.guicomponents.Label("");
String rawValue = (String) modelcontext.getRawValue();
if (rawValue != null) {
LWCStructEnumAttTemplate claObj = MPLUtil.getLWCStructEnumAttTemplateByName(rawValue);
String resPath = ClassificationQueryUtil.getClassificationPath(claObj);
lable.setValue(resPath);
component = lable;
}
}
return component;
}
private Object createPickerComponent(String component_id, Object datum, ModelContext mc, ComponentMode componentMode) throws WTException {
ArrayList<GuiComponent> components = new ArrayList<GuiComponent>();
//默认值
String rawValue = (String) mc.getRawValue();
if(rawValue == null){
rawValue = "";
}
TextBox clsPageState11 = createTextBox("", componentMode);
clsPageState11.setId("==");
clsPageState11.setName("==");
clsPageState11.setInputType("hidden");
clsPageState11.setHidden(true);
String hiddenDefaultValue11 = "=============";
clsPageState11.setValue(hiddenDefaultValue11);//设置默认值
components.add(clsPageState11);
/***** TEXT BOX for data store *****/
TextBox textBoxForStore = createTextBox(component_id, componentMode);
textBoxForStore.setId(component_id);
//textBoxForStore.setName(component_id);
textBoxForStore.setInputType("hidden");
textBoxForStore.setColumnName(AttributeDataUtilityHelper.getColumnName(component_id, datum, mc));
textBoxForStore.setValue(rawValue);//设置默认值
components.add(textBoxForStore);
/***** TEXT BOX for data display *****/
TextBox textBoxDisplay = createTextBox(component_id + "_display", componentMode);
textBoxDisplay.setId(component_id + "_display");
textBoxDisplay.setName(component_id + "_display");
textBoxDisplay.setEnabled(true);
textBoxDisplay.setEditable(true);
textBoxDisplay.setReadOnly(true);
textBoxDisplay.setRequired(isRequired(mc, component_id));
//textBoxDisplay.setColumnName(AttributeDataUtilityHelper.getColumnName(component_id, datum, mc));
LWCStructEnumAttTemplate claObj = null;
String resPath = "";
if (!(rawValue ==null || rawValue.equals(""))) {//设置默认全路径名称
try{
claObj = MPLUtil.getLWCStructEnumAttTemplateByName(rawValue);
if(claObj==null){
textBoxDisplay.setValue(rawValue+"(错误分类!)");
}else{
resPath = ClassificationQueryUtil.getClassificationPath(claObj);
textBoxDisplay.setValue(resPath);
}
}catch (Exception e) {
e.printStackTrace();
textBoxDisplay.setValue(rawValue+"(错误分类!)");
}
}
components.add(textBoxDisplay);
// //如果有默认值,则增加隐藏字段,在页通过此隐藏字段决定是否显示分类页面。
// if (rawValue != null && !rawValue.equals("")) {
// if(claObj!=null){
// TextBox clsPageState = createTextBox("", componentMode);
// clsPageState.setId("_id_has_Cls_Value_");
// clsPageState.setName("_has_Cls_Value_");
// clsPageState.setInputType("hidden");
// clsPageState.setHidden(true);
// String hiddenDefaultValue = "{'clsInternalName':'" + claObj.getName() + "', 'clsOid':'" + PersistenceHelper.getObjectIdentifier(claObj).getStringValue() + "'," + " 'componentId':'"
// + component_id + "' }";
// clsPageState.setValue(hiddenDefaultValue);//设置默认值
// components.add(clsPageState);
// }
// }
//String imgClean = "netmarkets/images/clear_16x16.gif";
String host = com.midea.issue.ConstantResource.host;
String url = "javascript:window.open('" + host + "netmarkets/jsp/midea/common/noAttrClassificationSelectorWizardHome.jsp?componentId=" + component_id
+ "','','toolbar=no,menubar=no,location=no,status=no,resizable=yes')";
IconComponent picker = new IconComponent("Search...");
picker.setName("Search...");
picker.setSrc("netmarkets/images/search.gif");
picker.setId(component_id + "_picker");
picker.addJsAction("onClick", url);
picker.addJsAction("href", "javascript:void(0)");
components.add(picker);
IconComponent iconComponentClean = new IconComponent("Clean...");
iconComponentClean.setName("Clean...");
iconComponentClean.setSrc("netmarkets/images/clear_16x16.gif");
iconComponentClean.addJsAction("onClick", "javascript:cleanMideaClsValue('" + component_id + "','" + component_id + "_display')");
iconComponentClean.addJsAction("href", "javascript:void(0)");
components.add(iconComponentClean);
return new GUIComponentArray(components);
}
private Object displayDataUtility(String component_id, Object datum, ModelContext mc) throws WTException {
String value = "";
Label inheritedFrom = new Label("");
inheritedFrom.setColumnName(AttributeDataUtilityHelper.getColumnName(component_id, datum, mc));
inheritedFrom.setId(component_id);
String oids = (String) mc.getRawValue();
if (oids != null) {
String[] oidArray = oids.split(";");
for (int i = 0; i < oidArray.length; i++) {
String oid = oidArray[i];
ReferenceFactory rf = new ReferenceFactory();
Persistable p = rf.getReference(oid).getObject();
if (p instanceof WTDocumentMaster) {
WTDocumentMaster docMaster = (WTDocumentMaster) p;
if (i == 0) {
value = docMaster.getNumber();
} else {
value = value + ";" + docMaster.getNumber();
}
}
}
}
inheritedFrom.setValue(value);
return inheritedFrom;
}
private TextBox createTextBox(String component_id, ComponentMode componentMode) throws WTException {
TextBox text = new TextBox();
text.setMaxLength(200);
text.setWidth(30);
text.setId(component_id);
//text.setEditable(isComponentEditable(component_id, componentMode));
return text;
}
private static boolean isRequired(ModelContext mc, String attrName) {
boolean flag = false;
TypeIdentifier typeIden = null;
try {
typeIden = mc.getTypeIdentifier();
List<String> ret = getIBAConstraintBySofttypeIBA(typeIden, attrName);
for (String contString : ret) {
if (contString.indexOf("ValueRequiredConstraint") > -1) {
flag = true;
break;
}
}
} catch (WTException e) {
e.printStackTrace();
}
return flag;
}
private static List<String> getIBAConstraintBySofttypeIBA(
TypeIdentifier typeIden, String attrname) throws WTException {
List<String> ret = new ArrayList<String>();
TypeDefinitionReadView tdrv = TypeDefinitionServiceHelper.service
.getTypeDefView(typeIden);
AttributeDefinitionReadView adrv = tdrv.getAttributeByName(attrname);
if (adrv == null) {
return ret;
}
Collection<ConstraintDefinitionReadView> constraints = adrv
.getAllConstraints();
for (ConstraintDefinitionReadView constraint : constraints) {
ConstraintRuleDefinitionReadView crdrv = constraint.getRule();
if (crdrv != null) {
String defClassname = crdrv.getDefClassname();
String ruleClassName = crdrv.getRuleClassname();
String constraintStr = defClassname + ":" + ruleClassName;
ret.add(constraintStr);
}
}
return ret;
}
}