001: /*
002: * $Id: WebappPropertyToField.java,v 1.1 2003/08/17 06:06:13 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.minilang.method.eventops;
025:
026: import java.net.*;
027: import java.util.*;
028: import javax.servlet.*;
029:
030: import org.w3c.dom.*;
031: import org.ofbiz.base.util.*;
032: import org.ofbiz.minilang.*;
033: import org.ofbiz.minilang.method.*;
034:
035: /**
036: * Copies a property value from a properties file in a ServletContext resource to a field
037: *
038: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
039: * @version $Revision: 1.1 $
040: * @since 2.0
041: */
042: public class WebappPropertyToField extends MethodOperation {
043:
044: public static final String module = WebappPropertyToField.class
045: .getName();
046:
047: String resource;
048: String property;
049: String defaultVal;
050: ContextAccessor mapAcsr;
051: ContextAccessor fieldAcsr;
052:
053: public WebappPropertyToField(Element element,
054: SimpleMethod simpleMethod) {
055: super (element, simpleMethod);
056: resource = element.getAttribute("resource");
057: property = element.getAttribute("property");
058: defaultVal = element.getAttribute("default");
059: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
060: fieldAcsr = new ContextAccessor(element
061: .getAttribute("field-name"));
062: }
063:
064: public boolean exec(MethodContext methodContext) {
065: String resource = methodContext.expandString(this .resource);
066: String property = methodContext.expandString(this .property);
067: String defaultVal = methodContext.expandString(this .defaultVal);
068:
069: String fieldVal = null;
070:
071: // only run this if it is in an EVENT context
072: if (methodContext.getMethodType() == MethodContext.EVENT) {
073: ServletContext servletContext = (ServletContext) methodContext
074: .getRequest().getAttribute("servletContext");
075: URL propsUrl = null;
076:
077: try {
078: propsUrl = servletContext.getResource(resource);
079: } catch (java.net.MalformedURLException e) {
080: Debug.logWarning(e,
081: "Error finding webapp resource (properties file) not found with name "
082: + resource, module);
083: }
084:
085: if (propsUrl == null) {
086: Debug.logWarning(
087: "Webapp resource (properties file) not found with name "
088: + resource, module);
089: } else {
090: fieldVal = UtilProperties.getPropertyValue(propsUrl,
091: property);
092: if (fieldVal == null || fieldVal.length() == 0) {
093: Debug.logWarning(
094: "Webapp resource property value not found with name "
095: + property + " in resource "
096: + resource, module);
097: }
098: }
099: }
100:
101: // if fieldVal is null, or is a String and has zero length, use defaultVal
102: if (fieldVal == null) {
103: fieldVal = defaultVal;
104: } else if (fieldVal instanceof String) {
105: String strVal = (String) fieldVal;
106:
107: if (strVal.length() == 0) {
108: fieldVal = defaultVal;
109: }
110: }
111:
112: if (!mapAcsr.isEmpty()) {
113: Map fromMap = (Map) mapAcsr.get(methodContext);
114:
115: if (fromMap == null) {
116: Debug.logWarning("Map not found with name " + mapAcsr
117: + " creating a new map", module);
118: fromMap = new HashMap();
119: mapAcsr.put(methodContext, fromMap);
120: }
121:
122: fieldAcsr.put(fromMap, fieldVal, methodContext);
123: } else {
124: fieldAcsr.put(methodContext, fieldVal);
125: }
126: return true;
127: }
128: }
|