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 import org.ofbiz.minilang.operation.*; 34 35 42 public class IfCompare extends MethodOperation { 43 44 public static final String module = IfCompare.class.getName(); 45 46 List subOps = new LinkedList(); 47 List elseSubOps = null; 48 49 ContextAccessor mapAcsr; 50 ContextAccessor fieldAcsr; 51 String value; 52 53 String operator; 54 String type; 55 String format; 56 57 public IfCompare(Element element, SimpleMethod simpleMethod) { 58 super(element, simpleMethod); 59 this.mapAcsr = new ContextAccessor(element.getAttribute("map-name")); 60 this.fieldAcsr = new ContextAccessor(element.getAttribute("field-name")); 61 this.value = element.getAttribute("value"); 62 63 this.operator = element.getAttribute("operator"); 64 this.type = element.getAttribute("type"); 65 this.format = element.getAttribute("format"); 66 67 SimpleMethod.readOperations(element, subOps, simpleMethod); 68 Element elseElement = UtilXml.firstChildElement(element, "else"); 69 if (elseElement != null) { 70 elseSubOps = new LinkedList(); 71 SimpleMethod.readOperations(elseElement, elseSubOps, simpleMethod); 72 } 73 } 74 75 public boolean exec(MethodContext methodContext) { 76 79 String value = methodContext.expandString(this.value); 80 String operator = methodContext.expandString(this.operator); 81 String type = methodContext.expandString(this.type); 82 String format = methodContext.expandString(this.format); 83 84 Object fieldVal = null; 85 if (!mapAcsr.isEmpty()) { 86 Map fromMap = (Map) mapAcsr.get(methodContext); 87 if (fromMap == null) { 88 if (Debug.infoOn()) Debug.logInfo("Map not found with name " + mapAcsr + ", using empty string for comparison", module); 89 } else { 90 fieldVal = fieldAcsr.get(fromMap, methodContext); 91 } 92 } else { 93 fieldVal = fieldAcsr.get(methodContext); 95 } 96 97 if (fieldVal == null) { 99 fieldVal = ""; 100 } 101 102 List messages = new LinkedList(); 103 Boolean resultBool = BaseCompare.doRealCompare(fieldVal, value, operator, type, format, messages, null, methodContext.getLoader()); 104 if (messages.size() > 0) { 105 messages.add(0, "Error with comparison in if-compare between field [" + mapAcsr.toString() + "." + fieldAcsr.toString() + "] with value [" + fieldVal + "] and value [" + value + "] with operator [" + operator + "] and type [" + type + "]: "); 106 if (methodContext.getMethodType() == MethodContext.EVENT) { 107 StringBuffer fullString = new StringBuffer (); 108 109 Iterator miter = messages.iterator(); 110 while (miter.hasNext()) { 111 fullString.append((String ) miter.next()); 112 } 113 Debug.logWarning(fullString.toString(), module); 114 115 methodContext.putEnv(simpleMethod.getEventErrorMessageName(), fullString.toString()); 116 methodContext.putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode()); 117 } else if (methodContext.getMethodType() == MethodContext.SERVICE) { 118 methodContext.putEnv(simpleMethod.getServiceErrorMessageListName(), messages); 119 methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode()); 120 } 121 return false; 122 } 123 124 if (resultBool != null && resultBool.booleanValue()) { 125 return SimpleMethod.runSubOps(subOps, methodContext); 126 } else { 127 if (elseSubOps != null) { 128 return SimpleMethod.runSubOps(elseSubOps, methodContext); 129 } else { 130 return true; 131 } 132 } 133 } 134 135 public String rawString() { 136 return "<if-compare field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\" value=\"" + value + "\"/>"; 138 } 139 public String expandedString(MethodContext methodContext) { 140 return this.rawString(); 142 } 143 } 144 | Popular Tags |