1 24 package org.ofbiz.minilang.method.ifops; 25 26 import java.util.*; 27 28 import org.w3c.dom.*; 29 import org.ofbiz.base.util.*; 30 import org.ofbiz.minilang.*; 31 import org.ofbiz.minilang.method.*; 32 33 40 public class IfEmpty extends MethodOperation { 41 42 public static final String module = IfEmpty.class.getName(); 43 44 List subOps = new LinkedList(); 45 List elseSubOps = null; 46 47 ContextAccessor mapAcsr; 48 ContextAccessor fieldAcsr; 49 50 public IfEmpty(Element element, SimpleMethod simpleMethod) { 51 super(element, simpleMethod); 52 this.mapAcsr = new ContextAccessor(element.getAttribute("map-name")); 53 this.fieldAcsr = new ContextAccessor(element.getAttribute("field-name")); 54 55 SimpleMethod.readOperations(element, subOps, simpleMethod); 56 57 Element elseElement = UtilXml.firstChildElement(element, "else"); 58 if (elseElement != null) { 59 elseSubOps = new LinkedList(); 60 SimpleMethod.readOperations(elseElement, elseSubOps, simpleMethod); 61 } 62 } 63 64 public boolean exec(MethodContext methodContext) { 65 69 boolean runSubOps = false; 71 Object fieldVal = null; 72 73 if (!mapAcsr.isEmpty()) { 74 Map fromMap = (Map) mapAcsr.get(methodContext); 75 if (fromMap == null) { 76 if (Debug.infoOn()) Debug.logInfo("Map not found with name " + mapAcsr + ", running operations", module); 77 } else { 78 fieldVal = fieldAcsr.get(fromMap, methodContext); 79 } 80 } else { 81 fieldVal = fieldAcsr.get(methodContext); 83 } 84 85 runSubOps = ObjectType.isEmpty(fieldVal); 86 87 if (runSubOps) { 88 return SimpleMethod.runSubOps(subOps, methodContext); 89 } else { 90 if (elseSubOps != null) { 91 return SimpleMethod.runSubOps(elseSubOps, methodContext); 92 } else { 93 return true; 94 } 95 } 96 } 97 98 public String rawString() { 99 return "<if-empty field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>"; 100 } 101 public String expandedString(MethodContext methodContext) { 102 return this.rawString(); 104 } 105 } 106 | Popular Tags |