1 24 package org.ofbiz.minilang.method.eventops; 25 26 import java.util.HashMap ; 27 import java.util.Map ; 28 29 import org.ofbiz.base.util.Debug; 30 import org.ofbiz.base.util.collections.FlexibleServletAccessor; 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 RequestToField extends MethodOperation { 45 46 public static final String module = RequestToField.class.getName(); 47 48 ContextAccessor mapAcsr; 49 ContextAccessor fieldAcsr; 50 FlexibleServletAccessor requestAcsr; 51 String defaultVal; 52 53 public RequestToField(Element element, SimpleMethod simpleMethod) { 54 super(element, simpleMethod); 55 mapAcsr = new ContextAccessor(element.getAttribute("map-name")); 56 fieldAcsr = new ContextAccessor(element.getAttribute("field-name")); 57 requestAcsr = new FlexibleServletAccessor(element.getAttribute("request-name"), element.getAttribute("field-name")); 58 defaultVal = element.getAttribute("default"); 59 } 60 61 public boolean exec(MethodContext methodContext) { 62 String defaultVal = methodContext.expandString(this.defaultVal); 63 64 Object fieldVal = null; 65 if (methodContext.getMethodType() == MethodContext.EVENT) { 67 fieldVal = requestAcsr.get(methodContext.getRequest(), methodContext.getEnvMap()); 68 if (fieldVal == null) { 69 Debug.logWarning("Request attribute value not found with name " + requestAcsr, module); 70 } 71 } 72 73 if (fieldVal == null) { 75 fieldVal = defaultVal; 76 } else if (fieldVal instanceof String ) { 77 String strVal = (String ) fieldVal; 78 79 if (strVal.length() == 0) { 80 fieldVal = defaultVal; 81 } 82 } 83 84 if (!mapAcsr.isEmpty()) { 85 Map fromMap = (Map ) mapAcsr.get(methodContext); 86 87 if (fromMap == null) { 88 Debug.logWarning("Map not found with name " + mapAcsr + " creating a new map", module); 89 fromMap = new HashMap (); 90 mapAcsr.put(methodContext, fromMap); 91 } 92 93 fieldAcsr.put(fromMap, fieldVal, methodContext); 94 } else { 95 fieldAcsr.put(methodContext, fieldVal); 96 } 97 return true; 98 } 99 100 public String rawString() { 101 return "<request-to-field request-name=\"" + this.requestAcsr + "\" field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>"; 103 } 104 public String expandedString(MethodContext methodContext) { 105 return this.rawString(); 107 } 108 } 109 | Popular Tags |