1 24 package org.ofbiz.minilang.method.envops; 25 26 import org.ofbiz.base.util.Debug; 27 import org.ofbiz.base.util.GeneralException; 28 import org.ofbiz.base.util.ObjectType; 29 import org.ofbiz.base.util.UtilValidate; 30 import org.ofbiz.base.util.string.FlexibleStringExpander; 31 import org.ofbiz.minilang.SimpleMethod; 32 import org.ofbiz.minilang.method.ContextAccessor; 33 import org.ofbiz.minilang.method.MethodContext; 34 import org.ofbiz.minilang.method.MethodOperation; 35 import org.w3c.dom.Element ; 36 37 44 public class SetOperation extends MethodOperation { 45 public static final String module = SetOperation.class.getName(); 46 47 protected ContextAccessor field; 48 protected ContextAccessor fromField; 49 protected FlexibleStringExpander valueExdr; 50 protected FlexibleStringExpander defaultExdr; 51 protected String type; 52 protected boolean setIfNull; protected boolean setIfEmpty; 55 public SetOperation(Element element, SimpleMethod simpleMethod) { 56 super(element, simpleMethod); 57 this.field = new ContextAccessor(element.getAttribute("field")); 58 this.fromField = new ContextAccessor(element.getAttribute("from-field")); 59 this.valueExdr = new FlexibleStringExpander(element.getAttribute("value")); 60 this.defaultExdr = new FlexibleStringExpander(element.getAttribute("default-value")); 61 this.type = element.getAttribute("type"); 62 this.setIfNull = "true".equals(element.getAttribute("set-if-null")); 64 this.setIfEmpty = !"false".equals(element.getAttribute("set-if-empty")); 66 67 if (!this.fromField.isEmpty() && !this.valueExdr.isEmpty()) { 68 throw new IllegalArgumentException ("Cannot specify a from-field [" + element.getAttribute("from-field") + "] and a value [" + element.getAttribute("value") + "] on the set action in a screen widget"); 69 } 70 } 71 72 public boolean exec(MethodContext methodContext) { 73 Object newValue = null; 74 if (!this.fromField.isEmpty()) { 75 newValue = this.fromField.get(methodContext); 76 if (Debug.verboseOn()) Debug.logVerbose("In screen getting value for field from [" + this.fromField.toString() + "]: " + newValue, module); 77 } else if (!this.valueExdr.isEmpty()) { 78 newValue = methodContext.expandString(this.valueExdr); 79 } 80 81 if (ObjectType.isEmpty(newValue) && !this.defaultExdr.isEmpty()) { 83 newValue = methodContext.expandString(this.defaultExdr); 84 } 85 86 if (!setIfNull && newValue == null) { 87 if (Debug.verboseOn()) Debug.logVerbose("Field value not found (null) with name [" + fromField + "] and value [" + valueExdr + "], and there was not default value, not setting field", module); 88 return true; 89 } 90 if (!setIfEmpty && ObjectType.isEmpty(newValue)) { 91 if (Debug.verboseOn()) Debug.logVerbose("Field value not found (empty) with name [" + fromField + "] and value [" + valueExdr + "], and there was not default value, not setting field", module); 92 return true; 93 } 94 95 if (UtilValidate.isNotEmpty(this.type)) { 96 try { 97 newValue = ObjectType.simpleTypeConvert(newValue, this.type, null, null); 98 } catch (GeneralException e) { 99 String errMsg = "Could not convert field value for the field: [" + this.field.toString() + "] to the [" + this.type + "] type for the value [" + newValue + "]: " + e.toString(); 100 Debug.logError(e, errMsg, module); 101 methodContext.setErrorReturn(errMsg, simpleMethod); 102 return false; 103 } 104 } 105 106 if (Debug.verboseOn()) Debug.logVerbose("In screen setting field [" + this.field.toString() + "] to value: " + newValue, module); 107 this.field.put(methodContext, newValue); 108 return true; 109 } 110 111 public String rawString() { 112 return "<set field=\"" + this.field 113 + (this.valueExdr.isEmpty() ? "" : "\" value=\"" + this.valueExdr.getOriginal()) 114 + (this.fromField.isEmpty() ? "" : "\" from-field=\"" + this.fromField) 115 + (this.defaultExdr.isEmpty() ? "" : "\" default-value=\"" + this.defaultExdr.getOriginal()) 116 + (this.type == null || this.type.length() == 0 ? "" : "\" type=\"" + this.type) 117 + "\"/>"; 118 } 119 public String expandedString(MethodContext methodContext) { 120 return this.rawString(); 122 } 123 } 124 | Popular Tags |