001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.minilang.method.eventops;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import org.ofbiz.base.util.Debug;
024: import org.ofbiz.base.util.collections.FlexibleServletAccessor;
025: import org.ofbiz.minilang.SimpleMethod;
026: import org.ofbiz.minilang.method.ContextAccessor;
027: import org.ofbiz.minilang.method.MethodContext;
028: import org.ofbiz.minilang.method.MethodOperation;
029: import org.w3c.dom.Element;
030:
031: /**
032: * Copies a Servlet request attribute to a map field
033: */
034: public class RequestToField extends MethodOperation {
035:
036: public static final String module = RequestToField.class.getName();
037:
038: ContextAccessor mapAcsr;
039: ContextAccessor fieldAcsr;
040: FlexibleServletAccessor requestAcsr;
041: String defaultVal;
042:
043: public RequestToField(Element element, SimpleMethod simpleMethod) {
044: super (element, simpleMethod);
045: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
046: fieldAcsr = new ContextAccessor(element
047: .getAttribute("field-name"));
048: requestAcsr = new FlexibleServletAccessor(element
049: .getAttribute("request-name"), element
050: .getAttribute("field-name"));
051: defaultVal = element.getAttribute("default");
052: }
053:
054: public boolean exec(MethodContext methodContext) {
055: String defaultVal = methodContext.expandString(this .defaultVal);
056:
057: Object fieldVal = null;
058: // only run this if it is in an EVENT context
059: if (methodContext.getMethodType() == MethodContext.EVENT) {
060: fieldVal = requestAcsr.get(methodContext.getRequest(),
061: methodContext.getEnvMap());
062: if (fieldVal == null) {
063: Debug.logWarning(
064: "Request attribute value not found with name "
065: + requestAcsr, module);
066: }
067: }
068:
069: // if fieldVal is null, or is a String and has zero length, use defaultVal
070: if (fieldVal == null) {
071: fieldVal = defaultVal;
072: } else if (fieldVal instanceof String) {
073: String strVal = (String) fieldVal;
074:
075: if (strVal.length() == 0) {
076: fieldVal = defaultVal;
077: }
078: }
079:
080: if (!mapAcsr.isEmpty()) {
081: Map fromMap = (Map) mapAcsr.get(methodContext);
082:
083: if (fromMap == null) {
084: Debug.logWarning("Map not found with name " + mapAcsr
085: + " creating a new map", module);
086: fromMap = new HashMap();
087: mapAcsr.put(methodContext, fromMap);
088: }
089:
090: fieldAcsr.put(fromMap, fieldVal, methodContext);
091: } else {
092: fieldAcsr.put(methodContext, fieldVal);
093: }
094: return true;
095: }
096:
097: public String rawString() {
098: // TODO: add all attributes and other info
099: return "<request-to-field request-name=\"" + this .requestAcsr
100: + "\" field-name=\"" + this .fieldAcsr
101: + "\" map-name=\"" + this .mapAcsr + "\"/>";
102: }
103:
104: public String expandedString(MethodContext methodContext) {
105: // TODO: something more than a stub/dummy
106: return this.rawString();
107: }
108: }
|