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