001: /*
002: * $Id: Iterate.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.Collection;
027: import java.util.Iterator;
028: import java.util.LinkedList;
029: import java.util.List;
030:
031: import org.ofbiz.base.util.Debug;
032: import org.ofbiz.entity.GenericEntityException;
033: import org.ofbiz.entity.GenericValue;
034: import org.ofbiz.entity.util.EntityListIterator;
035: import org.ofbiz.minilang.SimpleMethod;
036: import org.ofbiz.minilang.method.ContextAccessor;
037: import org.ofbiz.minilang.method.MethodContext;
038: import org.ofbiz.minilang.method.MethodOperation;
039: import org.w3c.dom.Element;
040:
041: /**
042: * Process sub-operations for each entry in the list
043: *
044: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
045: * @version $Revision: 1.1 $
046: * @since 2.0
047: */
048: public class Iterate extends MethodOperation {
049:
050: public static final String module = Iterate.class.getName();
051:
052: List subOps = new LinkedList();
053:
054: ContextAccessor entryAcsr;
055: ContextAccessor listAcsr;
056:
057: public Iterate(Element element, SimpleMethod simpleMethod) {
058: super (element, simpleMethod);
059: this .entryAcsr = new ContextAccessor(element
060: .getAttribute("entry-name"));
061: this .listAcsr = new ContextAccessor(element
062: .getAttribute("list-name"));
063:
064: SimpleMethod.readOperations(element, subOps, simpleMethod);
065: }
066:
067: public boolean exec(MethodContext methodContext) {
068: Object fieldVal = null;
069:
070: if (listAcsr.isEmpty()) {
071: Debug
072: .logWarning(
073: "No list-name specified in iterate tag, doing nothing",
074: module);
075: return true;
076: }
077:
078: Object oldEntryValue = entryAcsr.get(methodContext);
079: Object objList = listAcsr.get(methodContext);
080: if (objList instanceof EntityListIterator) {
081: EntityListIterator eli = (EntityListIterator) objList;
082:
083: GenericValue theEntry;
084: while ((theEntry = (GenericValue) eli.next()) != null) {
085: entryAcsr.put(methodContext, theEntry);
086:
087: if (!SimpleMethod.runSubOps(subOps, methodContext)) {
088: // only return here if it returns false, otherwise just carry on
089: return false;
090: }
091: }
092:
093: // close the iterator
094: try {
095: eli.close();
096: } catch (GenericEntityException e) {
097: Debug.logError(e, module);
098: String errMsg = "ERROR: Error closing entityListIterator in "
099: + simpleMethod.getShortDescription()
100: + " ["
101: + e.getMessage() + "]";
102:
103: if (methodContext.getMethodType() == MethodContext.EVENT) {
104: methodContext.putEnv(simpleMethod
105: .getEventErrorMessageName(), errMsg);
106: methodContext.putEnv(simpleMethod
107: .getEventResponseCodeName(), simpleMethod
108: .getDefaultErrorCode());
109: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
110: methodContext.putEnv(simpleMethod
111: .getServiceErrorMessageName(), errMsg);
112: methodContext.putEnv(simpleMethod
113: .getServiceResponseMessageName(),
114: simpleMethod.getDefaultErrorCode());
115: }
116: return false;
117: }
118: } else {
119: Collection theList = (Collection) objList;
120:
121: if (theList == null) {
122: if (Debug.infoOn())
123: Debug.logInfo("List not found with name "
124: + listAcsr + ", doing nothing", module);
125: return true;
126: }
127: if (theList.size() == 0) {
128: if (Debug.verboseOn())
129: Debug.logVerbose("List with name " + listAcsr
130: + " has zero entries, doing nothing",
131: module);
132: return true;
133: }
134:
135: Iterator theIterator = theList.iterator();
136:
137: while (theIterator.hasNext()) {
138: Object theEntry = theIterator.next();
139: entryAcsr.put(methodContext, theEntry);
140:
141: if (!SimpleMethod.runSubOps(subOps, methodContext)) {
142: // only return here if it returns false, otherwise just carry on
143: return false;
144: }
145: }
146: }
147: entryAcsr.put(methodContext, oldEntryValue);
148: return true;
149: }
150: }
|