001: /*
002: * $Id: IterateMap.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.Iterator;
027: import java.util.LinkedList;
028: import java.util.List;
029: import java.util.Map;
030:
031: import org.ofbiz.minilang.SimpleMethod;
032: import org.ofbiz.minilang.method.ContextAccessor;
033: import org.ofbiz.minilang.method.MethodContext;
034: import org.ofbiz.minilang.method.MethodOperation;
035: import org.ofbiz.base.util.Debug;
036: import org.w3c.dom.Element;
037:
038: /**
039: * Process sub-operations for each entry in the map
040: *
041: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
042: * @version $Revision: 1.1 $
043: * @since 2.0
044: */
045: public class IterateMap extends MethodOperation {
046:
047: public static final String module = IterateMap.class.getName();
048:
049: List subOps = new LinkedList();
050:
051: ContextAccessor keyAcsr;
052: ContextAccessor valueAcsr;
053: ContextAccessor mapAcsr;
054:
055: public IterateMap(Element element, SimpleMethod simpleMethod) {
056: super (element, simpleMethod);
057: this .keyAcsr = new ContextAccessor(element
058: .getAttribute("key-name"));
059: this .valueAcsr = new ContextAccessor(element
060: .getAttribute("value-name"));
061: this .mapAcsr = new ContextAccessor(element
062: .getAttribute("map-name"));
063:
064: SimpleMethod.readOperations(element, subOps, simpleMethod);
065: }
066:
067: public boolean exec(MethodContext methodContext) {
068: Object fieldVal = null;
069:
070: if (mapAcsr.isEmpty()) {
071: Debug
072: .logWarning(
073: "No map-name specified in iterate tag, doing nothing",
074: module);
075: return true;
076: }
077:
078: Object oldKey = keyAcsr.get(methodContext);
079: Object oldValue = valueAcsr.get(methodContext);
080: Map theMap = (Map) mapAcsr.get(methodContext);
081:
082: if (theMap == null) {
083: if (Debug.infoOn())
084: Debug.logInfo("Map not found with name " + mapAcsr
085: + ", doing nothing", module);
086: return true;
087: }
088: if (theMap.size() == 0) {
089: if (Debug.verboseOn())
090: Debug.logVerbose("Map with name " + mapAcsr
091: + " has zero entries, doing nothing", module);
092: return true;
093: }
094:
095: Iterator theIterator = theMap.entrySet().iterator();
096: while (theIterator.hasNext()) {
097: Map.Entry theEntry = (Map.Entry) theIterator.next();
098: keyAcsr.put(methodContext, theEntry.getKey());
099: valueAcsr.put(methodContext, theEntry.getValue());
100:
101: if (!SimpleMethod.runSubOps(subOps, methodContext)) {
102: // only return here if it returns false, otherwise just carry on
103: return false;
104: }
105: }
106:
107: keyAcsr.put(methodContext, oldKey);
108: valueAcsr.put(methodContext, oldValue);
109: return true;
110: }
111: }
|