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 an environment field to a list
29: */
30: public class FieldToList extends MethodOperation {
31:
32: public static final String module = FieldToList.class.getName();
33:
34: ContextAccessor mapAcsr;
35: ContextAccessor fieldAcsr;
36: ContextAccessor listAcsr;
37:
38: public FieldToList(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: listAcsr = new ContextAccessor(element
44: .getAttribute("list-name"));
45: }
46:
47: public boolean exec(MethodContext methodContext) {
48: Object fieldVal = null;
49:
50: if (!mapAcsr.isEmpty()) {
51: Map fromMap = (Map) mapAcsr.get(methodContext);
52:
53: if (fromMap == null) {
54: Debug.logWarning("Map not found with name " + mapAcsr
55: + ", Not copying to list", module);
56: return true;
57: }
58:
59: fieldVal = fieldAcsr.get(fromMap, methodContext);
60: } else {
61: // no map name, try the env
62: fieldVal = fieldAcsr.get(methodContext);
63: }
64:
65: if (fieldVal == null) {
66: Debug.logWarning("Field value not found with name "
67: + fieldAcsr + " in Map with name " + mapAcsr
68: + ", Not copying to list", module);
69: return true;
70: }
71:
72: List toList = (List) listAcsr.get(methodContext);
73:
74: if (toList == null) {
75: if (Debug.verboseOn())
76: Debug.logVerbose("List not found with name " + listAcsr
77: + ", creating new list", module);
78: toList = new LinkedList();
79: listAcsr.put(methodContext, toList);
80: }
81:
82: toList.add(fieldVal);
83: return true;
84: }
85:
86: public String rawString() {
87: return "<field-to-list list-name=\"" + this .listAcsr
88: + "\" field-name=\"" + this .fieldAcsr
89: + "\" map-name=\"" + this .mapAcsr + "\"/>";
90: }
91:
92: public String expandedString(MethodContext methodContext) {
93: // TODO: something more than a stub/dummy
94: return this.rawString();
95: }
96: }
|