KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > envops > Iterate


1 /*
2  * $Id: Iterate.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2002-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.minilang.method.envops;
25
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.ofbiz.base.util.Debug;
32 import org.ofbiz.entity.GenericEntityException;
33 import org.ofbiz.entity.GenericValue;
34 import org.ofbiz.entity.util.EntityListIterator;
35 import org.ofbiz.minilang.SimpleMethod;
36 import org.ofbiz.minilang.method.ContextAccessor;
37 import org.ofbiz.minilang.method.MethodContext;
38 import org.ofbiz.minilang.method.MethodOperation;
39 import org.w3c.dom.Element JavaDoc;
40
41 /**
42  * Process sub-operations for each entry in the list
43  *
44  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
45  * @version $Rev: 5462 $
46  * @since 2.0
47  */

48 public class Iterate extends MethodOperation {
49     
50     public static final String JavaDoc module = Iterate.class.getName();
51
52     List JavaDoc subOps = new LinkedList JavaDoc();
53
54     ContextAccessor entryAcsr;
55     ContextAccessor listAcsr;
56
57     public Iterate(Element JavaDoc element, SimpleMethod simpleMethod) {
58         super(element, simpleMethod);
59         this.entryAcsr = new ContextAccessor(element.getAttribute("entry-name"));
60         this.listAcsr = new ContextAccessor(element.getAttribute("list-name"));
61
62         SimpleMethod.readOperations(element, subOps, simpleMethod);
63     }
64
65     public boolean exec(MethodContext methodContext) {
66         Object JavaDoc fieldVal = null;
67
68         if (listAcsr.isEmpty()) {
69             Debug.logWarning("No list-name specified in iterate tag, doing nothing: " + rawString(), module);
70             return true;
71         }
72
73         Object JavaDoc oldEntryValue = entryAcsr.get(methodContext);
74         Object JavaDoc objList = listAcsr.get(methodContext);
75         if (objList instanceof EntityListIterator) {
76             EntityListIterator eli = (EntityListIterator) objList;
77
78             GenericValue theEntry;
79             while ((theEntry = (GenericValue) eli.next()) != null) {
80                 entryAcsr.put(methodContext, theEntry);
81
82                 if (!SimpleMethod.runSubOps(subOps, methodContext)) {
83                     // only return here if it returns false, otherwise just carry on
84
return false;
85                 }
86             }
87
88             // close the iterator
89
try {
90                 eli.close();
91             } catch (GenericEntityException e) {
92                 Debug.logError(e, module);
93                 String JavaDoc errMsg = "ERROR: Error closing entityListIterator in " + simpleMethod.getShortDescription() + " [" + e.getMessage() + "]: " + rawString();
94                 if (methodContext.getMethodType() == MethodContext.EVENT) {
95                     methodContext.putEnv(simpleMethod.getEventErrorMessageName(), errMsg);
96                     methodContext.putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode());
97                 } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
98                     methodContext.putEnv(simpleMethod.getServiceErrorMessageName(), errMsg);
99                     methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode());
100                 }
101                 return false;
102             }
103         } else {
104             Collection JavaDoc theList = (Collection JavaDoc) objList;
105
106             if (theList == null) {
107                 if (Debug.infoOn()) Debug.logInfo("List not found with name " + listAcsr + ", doing nothing: " + rawString(), module);
108                 return true;
109             }
110             if (theList.size() == 0) {
111                 if (Debug.verboseOn()) Debug.logVerbose("List with name " + listAcsr + " has zero entries, doing nothing: " + rawString(), module);
112                 return true;
113             }
114
115             Iterator JavaDoc theIterator = theList.iterator();
116
117             while (theIterator.hasNext()) {
118                 Object JavaDoc theEntry = theIterator.next();
119                 entryAcsr.put(methodContext, theEntry);
120
121                 if (!SimpleMethod.runSubOps(subOps, methodContext)) {
122                     // only return here if it returns false, otherwise just carry on
123
return false;
124                 }
125             }
126         }
127         entryAcsr.put(methodContext, oldEntryValue);
128         return true;
129     }
130
131     public String JavaDoc rawString() {
132         // TODO: something more than the empty tag
133
return "<iterate list-name=\"" + this.listAcsr + "\" entry-name=\"" + this.entryAcsr + "\"/>";
134     }
135     public String JavaDoc expandedString(MethodContext methodContext) {
136         // TODO: something more than a stub/dummy
137
return this.rawString();
138     }
139 }
140
Popular Tags