001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.minilang.method.entityops;
019:
020: import java.util.Map;
021:
022: import org.ofbiz.base.util.Debug;
023: import org.ofbiz.base.util.GeneralException;
024: import org.ofbiz.base.util.UtilXml;
025: import org.ofbiz.base.util.collections.FlexibleMapAccessor;
026: import org.ofbiz.base.util.string.FlexibleStringExpander;
027: import org.ofbiz.entity.GenericDelegator;
028: import org.ofbiz.entity.condition.EntityCondition;
029: import org.ofbiz.entity.finder.EntityFinderUtil.Condition;
030: import org.ofbiz.entity.finder.EntityFinderUtil.ConditionExpr;
031: import org.ofbiz.entity.finder.EntityFinderUtil.ConditionList;
032: import org.ofbiz.entity.finder.EntityFinderUtil.ConditionObject;
033: import org.ofbiz.minilang.SimpleMethod;
034: import org.ofbiz.minilang.method.MethodContext;
035: import org.ofbiz.minilang.method.MethodOperation;
036: import org.w3c.dom.Element;
037:
038: /**
039: * Uses the delegator to find entity values by a condition
040: */
041: public class EntityCount extends MethodOperation {
042:
043: public static final String module = EntityCount.class.getName();
044:
045: protected FlexibleStringExpander entityNameExdr;
046: protected FlexibleStringExpander delegatorNameExdr;
047: protected Condition whereCondition;
048: protected Condition havingCondition;
049: protected FlexibleMapAccessor countAcsr;
050:
051: public EntityCount(Element element, SimpleMethod simpleMethod) {
052: super (element, simpleMethod);
053: this .entityNameExdr = new FlexibleStringExpander(element
054: .getAttribute("entity-name"));
055: this .delegatorNameExdr = new FlexibleStringExpander(element
056: .getAttribute("delegator-name"));
057: this .countAcsr = new FlexibleMapAccessor(element
058: .getAttribute("count-name"));
059:
060: // process condition-expr | condition-list
061: Element conditionExprElement = UtilXml.firstChildElement(
062: element, "condition-expr");
063: Element conditionListElement = UtilXml.firstChildElement(
064: element, "condition-list");
065: Element conditionObjectElement = UtilXml.firstChildElement(
066: element, "condition-object");
067: if (conditionExprElement != null
068: && conditionListElement != null) {
069: throw new IllegalArgumentException(
070: "In entity find by condition element, cannot have condition-expr and condition-list sub-elements");
071: }
072: if (conditionExprElement != null) {
073: this .whereCondition = new ConditionExpr(
074: conditionExprElement);
075: } else if (conditionListElement != null) {
076: this .whereCondition = new ConditionList(
077: conditionListElement);
078: } else if (conditionObjectElement != null) {
079: this .whereCondition = new ConditionObject(
080: conditionObjectElement);
081: }
082:
083: // process having-condition-list
084: Element havingConditionListElement = UtilXml.firstChildElement(
085: element, "having-condition-list");
086: if (havingConditionListElement != null) {
087: this .havingCondition = new ConditionList(
088: havingConditionListElement);
089: }
090: }
091:
092: public boolean exec(MethodContext methodContext) {
093: try {
094: Map context = methodContext.getEnvMap();
095: GenericDelegator delegator = methodContext.getDelegator();
096: String entityName = this .entityNameExdr
097: .expandString(context);
098: String delegatorName = this .delegatorNameExdr
099: .expandString(context);
100:
101: if (delegatorName != null && delegatorName.length() > 0) {
102: delegator = GenericDelegator
103: .getGenericDelegator(delegatorName);
104: }
105:
106: // create whereEntityCondition from whereCondition
107: EntityCondition whereEntityCondition = null;
108: if (this .whereCondition != null) {
109: whereEntityCondition = this .whereCondition
110: .createCondition(context, entityName, delegator);
111: }
112:
113: // create havingEntityCondition from havingCondition
114: EntityCondition havingEntityCondition = null;
115: if (this .havingCondition != null) {
116: havingEntityCondition = this .havingCondition
117: .createCondition(context, entityName, delegator);
118: }
119:
120: long count = delegator.findCountByCondition(entityName,
121: whereEntityCondition, havingEntityCondition);
122:
123: this .countAcsr.put(context, new Long(count));
124: } catch (GeneralException e) {
125: Debug.logError(e, module);
126: String errMsg = "ERROR: Could not complete the "
127: + simpleMethod.getShortDescription() + " process: "
128: + e.getMessage();
129:
130: if (methodContext.getMethodType() == MethodContext.EVENT) {
131: methodContext.putEnv(simpleMethod
132: .getEventErrorMessageName(), errMsg);
133: methodContext.putEnv(simpleMethod
134: .getEventResponseCodeName(), simpleMethod
135: .getDefaultErrorCode());
136: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
137: methodContext.putEnv(simpleMethod
138: .getServiceErrorMessageName(), errMsg);
139: methodContext.putEnv(simpleMethod
140: .getServiceResponseMessageName(), simpleMethod
141: .getDefaultErrorCode());
142: }
143: return false;
144: }
145: return true;
146: }
147:
148: public String rawString() {
149: // TODO: something more than the empty tag
150: return "<entity-count/>";
151: }
152:
153: public String expandedString(MethodContext methodContext) {
154: // TODO: something more than a stub/dummy
155: return this.rawString();
156: }
157: }
|