1 24 package org.ofbiz.minilang.method.conditional; 25 26 import java.util.*; 27 import org.w3c.dom.*; 28 import org.ofbiz.base.util.*; 29 import org.ofbiz.minilang.*; 30 import org.ofbiz.minilang.method.*; 31 32 39 public class EmptyCondition implements Conditional { 40 41 public static final String module = EmptyCondition.class.getName(); 42 43 SimpleMethod simpleMethod; 44 45 ContextAccessor mapAcsr; 46 ContextAccessor fieldAcsr; 47 48 public EmptyCondition(Element element, SimpleMethod simpleMethod) { 49 this.simpleMethod = simpleMethod; 50 51 this.mapAcsr = new ContextAccessor(element.getAttribute("map-name")); 52 this.fieldAcsr = new ContextAccessor(element.getAttribute("field-name")); 53 } 54 55 public boolean checkCondition(MethodContext methodContext) { 56 boolean runSubOps = false; 58 Object fieldVal = getFieldVal(methodContext); 59 60 if (fieldVal == null) { 61 runSubOps = true; 62 } else { 63 if (fieldVal instanceof String ) { 64 String fieldStr = (String ) fieldVal; 65 66 if (fieldStr.length() == 0) { 67 runSubOps = true; 68 } 69 } else if (fieldVal instanceof Collection) { 70 Collection fieldCol = (Collection) fieldVal; 71 72 if (fieldCol.size() == 0) { 73 runSubOps = true; 74 } 75 } else if (fieldVal instanceof Map) { 76 Map fieldMap = (Map) fieldVal; 77 78 if (fieldMap.size() == 0) { 79 runSubOps = true; 80 } 81 } 82 } 83 84 return runSubOps; 85 } 86 87 protected Object getFieldVal(MethodContext methodContext) { 88 Object fieldVal = null; 89 if (!mapAcsr.isEmpty()) { 90 Map fromMap = (Map) mapAcsr.get(methodContext); 91 if (fromMap == null) { 92 if (Debug.infoOn()) Debug.logInfo("Map not found with name " + mapAcsr + ", running operations", module); 93 } else { 94 fieldVal = fieldAcsr.get(fromMap, methodContext); 95 } 96 } else { 97 fieldVal = fieldAcsr.get(methodContext); 99 } 100 return fieldVal; 101 } 102 103 public void prettyPrint(StringBuffer messageBuffer, MethodContext methodContext) { 104 messageBuffer.append("empty["); 105 if (!this.mapAcsr.isEmpty()) { 106 messageBuffer.append(this.mapAcsr); 107 messageBuffer.append("."); 108 } 109 messageBuffer.append(this.fieldAcsr); 110 if (methodContext != null) { 111 messageBuffer.append("="); 112 messageBuffer.append(getFieldVal(methodContext)); 113 } 114 messageBuffer.append("]"); 115 } 116 } 117 | Popular Tags |