1 24 package org.ofbiz.minilang.method.envops; 25 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 import java.util.LinkedList ; 29 import java.util.List ; 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 ; 40 41 48 public class Iterate extends MethodOperation { 49 50 public static final String module = Iterate.class.getName(); 51 52 List subOps = new LinkedList (); 53 54 ContextAccessor entryAcsr; 55 ContextAccessor listAcsr; 56 57 public Iterate(Element 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 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 oldEntryValue = entryAcsr.get(methodContext); 74 Object 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 return false; 85 } 86 } 87 88 try { 90 eli.close(); 91 } catch (GenericEntityException e) { 92 Debug.logError(e, module); 93 String 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 theList = (Collection ) 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 theIterator = theList.iterator(); 116 117 while (theIterator.hasNext()) { 118 Object theEntry = theIterator.next(); 119 entryAcsr.put(methodContext, theEntry); 120 121 if (!SimpleMethod.runSubOps(subOps, methodContext)) { 122 return false; 124 } 125 } 126 } 127 entryAcsr.put(methodContext, oldEntryValue); 128 return true; 129 } 130 131 public String rawString() { 132 return "<iterate list-name=\"" + this.listAcsr + "\" entry-name=\"" + this.entryAcsr + "\"/>"; 134 } 135 public String expandedString(MethodContext methodContext) { 136 return this.rawString(); 138 } 139 } 140 | Popular Tags |