001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.minilang.method.envops;
019:
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.LinkedList;
023: import java.util.List;
024:
025: import org.ofbiz.base.util.Debug;
026: import org.ofbiz.entity.GenericEntityException;
027: import org.ofbiz.entity.GenericValue;
028: import org.ofbiz.entity.util.EntityListIterator;
029: import org.ofbiz.minilang.SimpleMethod;
030: import org.ofbiz.minilang.method.ContextAccessor;
031: import org.ofbiz.minilang.method.MethodContext;
032: import org.ofbiz.minilang.method.MethodOperation;
033: import org.w3c.dom.Element;
034:
035: /**
036: * Process sub-operations for each entry in the list
037: */
038: public class Iterate extends MethodOperation {
039:
040: public static final String module = Iterate.class.getName();
041:
042: List subOps = new LinkedList();
043:
044: ContextAccessor entryAcsr;
045: ContextAccessor listAcsr;
046:
047: public Iterate(Element element, SimpleMethod simpleMethod) {
048: super (element, simpleMethod);
049: this .entryAcsr = new ContextAccessor(element
050: .getAttribute("entry-name"));
051: this .listAcsr = new ContextAccessor(element
052: .getAttribute("list-name"));
053:
054: SimpleMethod.readOperations(element, subOps, simpleMethod);
055: }
056:
057: public boolean exec(MethodContext methodContext) {
058:
059: if (listAcsr.isEmpty()) {
060: Debug.logWarning(
061: "No list-name specified in iterate tag, doing nothing: "
062: + rawString(), module);
063: return true;
064: }
065:
066: Object oldEntryValue = entryAcsr.get(methodContext);
067: Object objList = listAcsr.get(methodContext);
068: if (objList instanceof EntityListIterator) {
069: EntityListIterator eli = (EntityListIterator) objList;
070:
071: GenericValue theEntry;
072: while ((theEntry = (GenericValue) eli.next()) != null) {
073: entryAcsr.put(methodContext, theEntry);
074:
075: if (!SimpleMethod.runSubOps(subOps, methodContext)) {
076: // only return here if it returns false, otherwise just carry on
077: return false;
078: }
079: }
080:
081: // close the iterator
082: try {
083: eli.close();
084: } catch (GenericEntityException e) {
085: Debug.logError(e, module);
086: String errMsg = "ERROR: Error closing entityListIterator in "
087: + simpleMethod.getShortDescription()
088: + " ["
089: + e.getMessage() + "]: " + rawString();
090: if (methodContext.getMethodType() == MethodContext.EVENT) {
091: methodContext.putEnv(simpleMethod
092: .getEventErrorMessageName(), errMsg);
093: methodContext.putEnv(simpleMethod
094: .getEventResponseCodeName(), simpleMethod
095: .getDefaultErrorCode());
096: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
097: methodContext.putEnv(simpleMethod
098: .getServiceErrorMessageName(), errMsg);
099: methodContext.putEnv(simpleMethod
100: .getServiceResponseMessageName(),
101: simpleMethod.getDefaultErrorCode());
102: }
103: return false;
104: }
105: } else {
106: Collection theList = (Collection) objList;
107:
108: if (theList == null) {
109: if (Debug.infoOn())
110: Debug.logInfo("List not found with name "
111: + listAcsr + ", doing nothing: "
112: + rawString(), module);
113: return true;
114: }
115: if (theList.size() == 0) {
116: if (Debug.verboseOn())
117: Debug.logVerbose("List with name " + listAcsr
118: + " has zero entries, doing nothing: "
119: + rawString(), module);
120: return true;
121: }
122:
123: Iterator theIterator = theList.iterator();
124:
125: while (theIterator.hasNext()) {
126: Object theEntry = theIterator.next();
127: entryAcsr.put(methodContext, theEntry);
128:
129: if (!SimpleMethod.runSubOps(subOps, methodContext)) {
130: // only return here if it returns false, otherwise just carry on
131: return false;
132: }
133: }
134: }
135: entryAcsr.put(methodContext, oldEntryValue);
136: return true;
137: }
138:
139: public String rawString() {
140: // TODO: something more than the empty tag
141: return "<iterate list-name=\"" + this .listAcsr
142: + "\" entry-name=\"" + this .entryAcsr + "\"/>";
143: }
144:
145: public String expandedString(MethodContext methodContext) {
146: // TODO: something more than a stub/dummy
147: return this.rawString();
148: }
149: }
|