1 24 package org.ofbiz.minilang.method.eventops; 25 26 import java.net.*; 27 import java.util.*; 28 import javax.servlet.*; 29 30 import org.w3c.dom.*; 31 import org.ofbiz.base.util.*; 32 import org.ofbiz.minilang.*; 33 import org.ofbiz.minilang.method.*; 34 35 42 public class WebappPropertyToField extends MethodOperation { 43 44 public static final String module = WebappPropertyToField.class.getName(); 45 46 String resource; 47 String property; 48 String defaultVal; 49 ContextAccessor mapAcsr; 50 ContextAccessor fieldAcsr; 51 52 public WebappPropertyToField(Element element, SimpleMethod simpleMethod) { 53 super(element, simpleMethod); 54 resource = element.getAttribute("resource"); 55 property = element.getAttribute("property"); 56 defaultVal = element.getAttribute("default"); 57 mapAcsr = new ContextAccessor(element.getAttribute("map-name")); 58 fieldAcsr = new ContextAccessor(element.getAttribute("field-name")); 59 } 60 61 public boolean exec(MethodContext methodContext) { 62 String resource = methodContext.expandString(this.resource); 63 String property = methodContext.expandString(this.property); 64 String defaultVal = methodContext.expandString(this.defaultVal); 65 66 String fieldVal = null; 67 68 if (methodContext.getMethodType() == MethodContext.EVENT) { 70 ServletContext servletContext = (ServletContext) methodContext.getRequest().getAttribute("servletContext"); 71 URL propsUrl = null; 72 73 try { 74 propsUrl = servletContext.getResource(resource); 75 } catch (java.net.MalformedURLException e) { 76 Debug.logWarning(e, "Error finding webapp resource (properties file) not found with name " + resource, module); 77 } 78 79 if (propsUrl == null) { 80 Debug.logWarning("Webapp resource (properties file) not found with name " + resource, module); 81 } else { 82 fieldVal = UtilProperties.getPropertyValue(propsUrl, property); 83 if (fieldVal == null || fieldVal.length() == 0) { 84 Debug.logWarning("Webapp resource property value not found with name " + property + " in resource " + resource, module); 85 } 86 } 87 } 88 89 if (fieldVal == null) { 91 fieldVal = defaultVal; 92 } else if (fieldVal instanceof String ) { 93 String strVal = (String ) fieldVal; 94 95 if (strVal.length() == 0) { 96 fieldVal = defaultVal; 97 } 98 } 99 100 if (!mapAcsr.isEmpty()) { 101 Map fromMap = (Map) mapAcsr.get(methodContext); 102 103 if (fromMap == null) { 104 Debug.logWarning("Map not found with name " + mapAcsr + " creating a new map", module); 105 fromMap = new HashMap(); 106 mapAcsr.put(methodContext, fromMap); 107 } 108 109 fieldAcsr.put(fromMap, fieldVal, methodContext); 110 } else { 111 fieldAcsr.put(methodContext, fieldVal); 112 } 113 return true; 114 } 115 116 public String rawString() { 117 return "<webapp-property-to-field field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>"; 119 } 120 public String expandedString(MethodContext methodContext) { 121 return this.rawString(); 123 } 124 } 125 | Popular Tags |