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.envops;
019:
020: import java.util.*;
021:
022: import org.w3c.dom.*;
023: import org.ofbiz.base.util.*;
024: import org.ofbiz.minilang.*;
025: import org.ofbiz.minilang.method.*;
026:
027: /**
028: * Copies a map field to a map field
029: */
030: public class FieldToField extends MethodOperation {
031:
032: public static final String module = FieldToField.class.getName();
033:
034: ContextAccessor mapAcsr;
035: ContextAccessor fieldAcsr;
036: ContextAccessor toMapAcsr;
037: ContextAccessor toFieldAcsr;
038:
039: public FieldToField(Element element, SimpleMethod simpleMethod) {
040: super (element, simpleMethod);
041: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
042: fieldAcsr = new ContextAccessor(element
043: .getAttribute("field-name"));
044: toMapAcsr = new ContextAccessor(element
045: .getAttribute("to-map-name"));
046: toFieldAcsr = new ContextAccessor(element
047: .getAttribute("to-field-name"));
048:
049: // set toMapAcsr and toFieldAcsr to their defualt values of mapAcsr and fieldAcsr if empty
050: if (toMapAcsr.isEmpty()) {
051: toMapAcsr = mapAcsr;
052: }
053: if (toFieldAcsr.isEmpty()) {
054: toFieldAcsr = fieldAcsr;
055: }
056: }
057:
058: public boolean exec(MethodContext methodContext) {
059: Object fieldVal = null;
060:
061: if (!mapAcsr.isEmpty()) {
062: Map fromMap = (Map) mapAcsr.get(methodContext);
063:
064: if (fromMap == null) {
065: if (Debug.infoOn())
066: Debug.logInfo("Map not found with name " + mapAcsr
067: + ", not copying from this map", module);
068: return true;
069: }
070:
071: fieldVal = fieldAcsr.get(fromMap, methodContext);
072: } else {
073: // no map name, try the env
074: fieldVal = fieldAcsr.get(methodContext);
075: }
076:
077: if (fieldVal == null) {
078: if (Debug.verboseOn())
079: Debug.logVerbose("Field value not found with name "
080: + fieldAcsr + " in Map with name " + mapAcsr
081: + ", not copying field", module);
082: return true;
083: }
084:
085: // note that going to an env field will only work if it came from an env
086: // field because if not specified the to-map-name will be set to the map-name
087: // to go from a map field to an env field, use the field-to-env operation
088: Map toMap = null;
089:
090: if (!toMapAcsr.isEmpty()) {
091: toMap = (Map) toMapAcsr.get(methodContext);
092: if (toMap == null) {
093: if (Debug.verboseOn())
094: Debug.logVerbose("Map not found with name "
095: + toMapAcsr + ", creating new map", module);
096: toMap = new HashMap();
097: toMapAcsr.put(methodContext, toMap);
098: }
099: toFieldAcsr.put(toMap, fieldVal, methodContext);
100: } else {
101: // no to-map, so put in env
102: toFieldAcsr.put(methodContext, fieldVal);
103: }
104:
105: return true;
106: }
107:
108: public String rawString() {
109: return "<field-to-field field-name=\"" + this .fieldAcsr
110: + "\" map-name=\"" + this .mapAcsr
111: + "\" to-field-name=\"" + this .toFieldAcsr
112: + "\" to-map-name=\"" + this .toMapAcsr + "\"/>";
113: }
114:
115: public String expandedString(MethodContext methodContext) {
116: // TODO: something more than a stub/dummy
117: return this.rawString();
118: }
119: }
|