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 IfNotEmpty extends MethodOperation { 41 42 public static final String module = IfNotEmpty.class.getName(); 43 44 List subOps = new LinkedList(); 45 List elseSubOps = null; 46 47 ContextAccessor mapAcsr; 48 ContextAccessor fieldAcsr; 49 50 public IfNotEmpty(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 59 if (elseElement != null) { 60 elseSubOps = new LinkedList(); 61 SimpleMethod.readOperations(elseElement, elseSubOps, simpleMethod); 62 } 63 } 64 65 public boolean exec(MethodContext methodContext) { 66 70 Object fieldVal = null; 71 72 if (!mapAcsr.isEmpty()) { 73 Map fromMap = (Map) mapAcsr.get(methodContext); 74 if (fromMap == null) { 75 if (Debug.verboseOn()) Debug.logVerbose("Map not found with name " + mapAcsr + ", not running operations", module); 76 } else { 77 fieldVal = fieldAcsr.get(fromMap, methodContext); 78 } 79 } else { 80 fieldVal = fieldAcsr.get(methodContext); 82 } 83 84 if (fieldVal == null) { 85 if (Debug.verboseOn()) Debug.logVerbose("Field value not found with name " + fieldAcsr + " in Map with name " + mapAcsr + ", not running operations", module); 86 } 87 88 boolean runSubOps = !ObjectType.isEmpty(fieldVal); 90 91 if (runSubOps) { 92 return SimpleMethod.runSubOps(subOps, methodContext); 94 } else { 95 if (elseSubOps != null) { 96 return SimpleMethod.runSubOps(elseSubOps, methodContext); 98 } else { 99 return true; 101 } 102 } 103 } 104 105 public String rawString() { 106 return "<if-not-empty field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>"; 108 } 109 public String expandedString(MethodContext methodContext) { 110 return this.rawString(); 112 } 113 } 114 | Popular Tags |