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.serviceops;
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 a Service result entry
29: */
30: public class FieldToResult extends MethodOperation {
31:
32: public static final String module = FieldToResult.class.getName();
33:
34: ContextAccessor mapAcsr;
35: ContextAccessor fieldAcsr;
36: ContextAccessor resultAcsr;
37:
38: public FieldToResult(Element element, SimpleMethod simpleMethod) {
39: super (element, simpleMethod);
40: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
41: fieldAcsr = new ContextAccessor(element
42: .getAttribute("field-name"));
43: resultAcsr = new ContextAccessor(element
44: .getAttribute("result-name"), element
45: .getAttribute("field-name"));
46: }
47:
48: public boolean exec(MethodContext methodContext) {
49: // only run this if it is in an SERVICE context
50: if (methodContext.getMethodType() == MethodContext.SERVICE) {
51: Object fieldVal = null;
52:
53: if (!mapAcsr.isEmpty()) {
54: Map fromMap = (Map) mapAcsr.get(methodContext);
55:
56: if (fromMap == null) {
57: Debug.logWarning("Map not found with name "
58: + mapAcsr, module);
59: return true;
60: }
61:
62: fieldVal = fieldAcsr.get(fromMap, methodContext);
63: } else {
64: // no map name, try the env
65: fieldVal = fieldAcsr.get(methodContext);
66: }
67:
68: if (fieldVal == null) {
69: Debug.logWarning("Field value not found with name "
70: + fieldAcsr + " in Map with name " + mapAcsr,
71: module);
72: return true;
73: }
74:
75: resultAcsr.put(methodContext.getResults(), fieldVal,
76: methodContext);
77: }
78: return true;
79: }
80:
81: public String rawString() {
82: // TODO: add all attributes and other info
83: return "<field-to-result field-name=\"" + this .fieldAcsr
84: + "\" map-name=\"" + this .mapAcsr + "\"/>";
85: }
86:
87: public String expandedString(MethodContext methodContext) {
88: // TODO: something more than a stub/dummy
89: return this.rawString();
90: }
91: }
|