1 24 package org.ofbiz.minilang.method.entityops; 25 26 import java.util.Map ; 27 28 import org.ofbiz.base.util.Debug; 29 import org.ofbiz.base.util.GeneralException; 30 import org.ofbiz.base.util.UtilXml; 31 import org.ofbiz.base.util.collections.FlexibleMapAccessor; 32 import org.ofbiz.base.util.string.FlexibleStringExpander; 33 import org.ofbiz.entity.GenericDelegator; 34 import org.ofbiz.entity.condition.EntityCondition; 35 import org.ofbiz.entity.finder.EntityFinderUtil.Condition; 36 import org.ofbiz.entity.finder.EntityFinderUtil.ConditionExpr; 37 import org.ofbiz.entity.finder.EntityFinderUtil.ConditionList; 38 import org.ofbiz.entity.finder.EntityFinderUtil.ConditionObject; 39 import org.ofbiz.minilang.SimpleMethod; 40 import org.ofbiz.minilang.method.MethodContext; 41 import org.ofbiz.minilang.method.MethodOperation; 42 import org.w3c.dom.Element ; 43 44 51 public class EntityCount extends MethodOperation { 52 53 public static final String module = EntityCount.class.getName(); 54 55 protected FlexibleStringExpander entityNameExdr; 56 protected FlexibleStringExpander delegatorNameExdr; 57 protected Condition whereCondition; 58 protected Condition havingCondition; 59 protected FlexibleMapAccessor countAcsr; 60 61 public EntityCount(Element element, SimpleMethod simpleMethod) { 62 super(element, simpleMethod); 63 this.entityNameExdr = new FlexibleStringExpander(element.getAttribute("entity-name")); 64 this.delegatorNameExdr = new FlexibleStringExpander(element.getAttribute("delegator-name")); 65 this.countAcsr = new FlexibleMapAccessor(element.getAttribute("count-name")); 66 67 Element conditionExprElement = UtilXml.firstChildElement(element, "condition-expr"); 69 Element conditionListElement = UtilXml.firstChildElement(element, "condition-list"); 70 Element conditionObjectElement = UtilXml.firstChildElement(element, "condition-object"); 71 if (conditionExprElement != null && conditionListElement != null) { 72 throw new IllegalArgumentException ("In entity find by condition element, cannot have condition-expr and condition-list sub-elements"); 73 } 74 if (conditionExprElement != null) { 75 this.whereCondition = new ConditionExpr(conditionExprElement); 76 } else if (conditionListElement != null) { 77 this.whereCondition = new ConditionList(conditionListElement); 78 } else if (conditionObjectElement != null) { 79 this.whereCondition = new ConditionObject(conditionObjectElement); 80 } 81 82 Element havingConditionListElement = UtilXml.firstChildElement(element, "having-condition-list"); 84 if (havingConditionListElement != null) { 85 this.havingCondition = new ConditionList(havingConditionListElement); 86 } 87 } 88 89 public boolean exec(MethodContext methodContext) { 90 try { 91 Map context = methodContext.getEnvMap(); 92 GenericDelegator delegator = methodContext.getDelegator(); 93 String entityName = this.entityNameExdr.expandString(context); 94 String delegatorName = this.delegatorNameExdr.expandString(context); 95 96 if (delegatorName != null && delegatorName.length() > 0) { 97 delegator = GenericDelegator.getGenericDelegator(delegatorName); 98 } 99 100 EntityCondition whereEntityCondition = null; 102 if (this.whereCondition != null) { 103 whereEntityCondition = this.whereCondition.createCondition(context, entityName, delegator); 104 } 105 106 EntityCondition havingEntityCondition = null; 108 if (this.havingCondition != null) { 109 havingEntityCondition = this.havingCondition.createCondition(context, entityName, delegator); 110 } 111 112 long count = delegator.findCountByCondition(entityName, whereEntityCondition, havingEntityCondition); 113 114 this.countAcsr.put(context, new Long (count)); 115 } catch (GeneralException e) { 116 Debug.logError(e, module); 117 String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process: " + e.getMessage(); 118 119 if (methodContext.getMethodType() == MethodContext.EVENT) { 120 methodContext.putEnv(simpleMethod.getEventErrorMessageName(), errMsg); 121 methodContext.putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode()); 122 } else if (methodContext.getMethodType() == MethodContext.SERVICE) { 123 methodContext.putEnv(simpleMethod.getServiceErrorMessageName(), errMsg); 124 methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode()); 125 } 126 return false; 127 } 128 return true; 129 } 130 131 public String rawString() { 132 return "<entity-count/>"; 134 } 135 public String expandedString(MethodContext methodContext) { 136 return this.rawString(); 138 } 139 } 140 141 | Popular Tags |