001: /*
002: * $Id: SessionToField.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.util.*;
027:
028: import org.w3c.dom.*;
029: import org.ofbiz.base.util.*;
030: import org.ofbiz.minilang.*;
031: import org.ofbiz.minilang.method.*;
032:
033: /**
034: * Copies a Servlet session attribute to a map field
035: *
036: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
037: * @version $Revision: 1.1 $
038: * @since 2.0
039: */
040: public class SessionToField extends MethodOperation {
041:
042: public static final String module = SessionToField.class.getName();
043:
044: ContextAccessor mapAcsr;
045: ContextAccessor fieldAcsr;
046: FlexibleServletAccessor sessionAcsr;
047: String defaultVal;
048:
049: public SessionToField(Element element, SimpleMethod simpleMethod) {
050: super (element, simpleMethod);
051: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
052: fieldAcsr = new ContextAccessor(element
053: .getAttribute("field-name"));
054: sessionAcsr = new FlexibleServletAccessor(element
055: .getAttribute("session-name"), element
056: .getAttribute("field-name"));
057: defaultVal = element.getAttribute("default");
058: }
059:
060: public boolean exec(MethodContext methodContext) {
061: String defaultVal = methodContext.expandString(this .defaultVal);
062:
063: Object fieldVal = null;
064: // only run this if it is in an EVENT context
065: if (methodContext.getMethodType() == MethodContext.EVENT) {
066: fieldVal = sessionAcsr.get(methodContext.getRequest()
067: .getSession(), methodContext.getEnvMap());
068: if (fieldVal == null) {
069: Debug.logWarning(
070: "Session attribute value not found with name "
071: + sessionAcsr, module);
072: }
073: }
074:
075: // if fieldVal is null, or is a String and has zero length, use defaultVal
076: if (fieldVal == null) {
077: fieldVal = defaultVal;
078: } else if (fieldVal instanceof String) {
079: String strVal = (String) fieldVal;
080:
081: if (strVal.length() == 0) {
082: fieldVal = defaultVal;
083: }
084: }
085:
086: if (!mapAcsr.isEmpty()) {
087: Map fromMap = (Map) mapAcsr.get(methodContext);
088:
089: if (fromMap == null) {
090: Debug.logWarning("Map not found with name " + mapAcsr
091: + " creating a new map", module);
092: fromMap = new HashMap();
093: mapAcsr.put(methodContext, fromMap);
094: }
095:
096: fieldAcsr.put(fromMap, fieldVal, methodContext);
097: } else {
098: fieldAcsr.put(methodContext, fieldVal);
099: }
100: return true;
101: }
102: }
|