001: /*
002: * $Id: FieldToField.java,v 1.1 2003/08/17 06:06:12 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.envops;
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 map field 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 FieldToField extends MethodOperation {
041:
042: public static final String module = FieldToField.class.getName();
043:
044: ContextAccessor mapAcsr;
045: ContextAccessor fieldAcsr;
046: ContextAccessor toMapAcsr;
047: ContextAccessor toFieldAcsr;
048:
049: public FieldToField(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: toMapAcsr = new ContextAccessor(element
055: .getAttribute("to-map-name"));
056: toFieldAcsr = new ContextAccessor(element
057: .getAttribute("to-field-name"));
058:
059: // set toMapAcsr and toFieldAcsr to their defualt values of mapAcsr and fieldAcsr if empty
060: if (toMapAcsr.isEmpty()) {
061: toMapAcsr = mapAcsr;
062: }
063: if (toFieldAcsr.isEmpty()) {
064: toFieldAcsr = fieldAcsr;
065: }
066: }
067:
068: public boolean exec(MethodContext methodContext) {
069: Object fieldVal = null;
070:
071: if (!mapAcsr.isEmpty()) {
072: Map fromMap = (Map) mapAcsr.get(methodContext);
073:
074: if (fromMap == null) {
075: if (Debug.infoOn())
076: Debug.logInfo("Map not found with name " + mapAcsr
077: + ", not copying from this map", module);
078: return true;
079: }
080:
081: fieldVal = fieldAcsr.get(fromMap, methodContext);
082: } else {
083: // no map name, try the env
084: fieldVal = fieldAcsr.get(methodContext);
085: }
086:
087: if (fieldVal == null) {
088: if (Debug.verboseOn())
089: Debug.logVerbose("Field value not found with name "
090: + fieldAcsr + " in Map with name " + mapAcsr
091: + ", not copying field", module);
092: return true;
093: }
094:
095: // note that going to an env field will only work if it came from an env
096: // field because if not specified the to-map-name will be set to the map-name
097: // to go from a map field to an env field, use the field-to-env operation
098: Map toMap = null;
099:
100: if (!toMapAcsr.isEmpty()) {
101: toMap = (Map) toMapAcsr.get(methodContext);
102: if (toMap == null) {
103: if (Debug.verboseOn())
104: Debug.logVerbose("Map not found with name "
105: + toMapAcsr + ", creating new map", module);
106: toMap = new HashMap();
107: toMapAcsr.put(methodContext, toMap);
108: }
109: toFieldAcsr.put(toMap, fieldVal, methodContext);
110: } else {
111: // no to-map, so put in env
112: toFieldAcsr.put(methodContext, fieldVal);
113: }
114:
115: return true;
116: }
117: }
|