01: /*******************************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: *******************************************************************************/package org.ofbiz.minilang.method.envops;
19:
20: import java.util.*;
21:
22: import org.w3c.dom.*;
23: import org.ofbiz.base.util.*;
24: import org.ofbiz.minilang.*;
25: import org.ofbiz.minilang.method.*;
26:
27: /**
28: * Copies a map field to an environment field
29: */
30: public class FieldToEnv extends MethodOperation {
31:
32: public static final String module = FieldToEnv.class.getName();
33:
34: ContextAccessor envAcsr;
35: ContextAccessor mapAcsr;
36: ContextAccessor fieldAcsr;
37:
38: public FieldToEnv(Element element, SimpleMethod simpleMethod) {
39: super (element, simpleMethod);
40: envAcsr = new ContextAccessor(element.getAttribute("env-name"));
41: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
42: fieldAcsr = new ContextAccessor(element
43: .getAttribute("field-name"));
44:
45: // set fieldAcsr to their defualt value of envAcsr if empty - this is the way it USED to work, so still supporting it, but a parsing error will result
46: if (fieldAcsr.isEmpty()) {
47: fieldAcsr = envAcsr;
48: }
49: // this is the new way that makes more sense, ie the destination should default to the source
50: if (envAcsr.isEmpty()) {
51: envAcsr = fieldAcsr;
52: }
53: }
54:
55: public boolean exec(MethodContext methodContext) {
56: Object fieldVal = null;
57:
58: if (!mapAcsr.isEmpty()) {
59: Map fromMap = (Map) mapAcsr.get(methodContext);
60:
61: if (fromMap == null) {
62: Debug.logWarning("Map not found with name " + mapAcsr
63: + ", not copying field", module);
64: return true;
65: }
66:
67: fieldVal = fieldAcsr.get(fromMap, methodContext);
68: } else {
69: // no map name, try the env
70: fieldVal = fieldAcsr.get(methodContext);
71: }
72:
73: if (fieldVal == null) {
74: if (Debug.verboseOn())
75: Debug.logVerbose("Field value not found with name "
76: + fieldAcsr + " in Map with name " + mapAcsr
77: + ", not copying field", module);
78: return true;
79: }
80:
81: envAcsr.put(methodContext, fieldVal);
82: return true;
83: }
84:
85: public String rawString() {
86: return "<field-to-env env-name=\"" + this .envAcsr
87: + "\" field-name=\"" + this .fieldAcsr
88: + "\" map-name=\"" + this .mapAcsr + "\"/>";
89: }
90:
91: public String expandedString(MethodContext methodContext) {
92: // TODO: something more than a stub/dummy
93: return this.rawString();
94: }
95: }
|