001: /*
002: * $Id: FindByAnd.java,v 1.1 2003/08/17 06:06:11 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.minilang.method.entityops;
025:
026: import java.util.List;
027: import java.util.Map;
028:
029: import org.ofbiz.base.util.Debug;
030: import org.ofbiz.entity.GenericDelegator;
031: import org.ofbiz.entity.GenericEntityException;
032: import org.ofbiz.entity.condition.EntityCondition;
033: import org.ofbiz.entity.condition.EntityFieldMap;
034: import org.ofbiz.entity.condition.EntityOperator;
035: import org.ofbiz.minilang.SimpleMethod;
036: import org.ofbiz.minilang.method.ContextAccessor;
037: import org.ofbiz.minilang.method.MethodContext;
038: import org.ofbiz.minilang.method.MethodOperation;
039: import org.w3c.dom.Element;
040:
041: /**
042: * Uses the delegator to find entity values by anding the map fields
043: *
044: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
045: * @version $Revision: 1.1 $
046: * @since 2.0
047: */
048: public class FindByAnd extends MethodOperation {
049:
050: public static final String module = FindByAnd.class.getName();
051:
052: ContextAccessor listAcsr;
053: String entityName;
054: ContextAccessor mapAcsr;
055: ContextAccessor orderByListAcsr;
056: String delegatorName;
057: String useCacheStr;
058: String useIteratorStr;
059:
060: public FindByAnd(Element element, SimpleMethod simpleMethod) {
061: super (element, simpleMethod);
062: listAcsr = new ContextAccessor(element
063: .getAttribute("list-name"));
064: entityName = element.getAttribute("entity-name");
065: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
066: orderByListAcsr = new ContextAccessor(element
067: .getAttribute("order-by-list-name"));
068: delegatorName = element.getAttribute("delegator-name");
069:
070: useCacheStr = element.getAttribute("use-cache");
071: useIteratorStr = element.getAttribute("use-iterator");
072: }
073:
074: public boolean exec(MethodContext methodContext) {
075: String entityName = methodContext.expandString(this .entityName);
076: String delegatorName = methodContext
077: .expandString(this .delegatorName);
078: String useCacheStr = methodContext
079: .expandString(this .useCacheStr);
080: String useIteratorStr = methodContext
081: .expandString(this .useIteratorStr);
082:
083: boolean useCache = "true".equals(useCacheStr);
084: boolean useIterator = "true".equals(useIteratorStr);
085:
086: List orderByNames = null;
087: if (!orderByListAcsr.isEmpty()) {
088: orderByNames = (List) orderByListAcsr.get(methodContext);
089: }
090:
091: GenericDelegator delegator = methodContext.getDelegator();
092: if (delegatorName != null && delegatorName.length() > 0) {
093: delegator = GenericDelegator
094: .getGenericDelegator(delegatorName);
095: }
096:
097: try {
098: if (useIterator) {
099: EntityCondition whereCond = null;
100: if (!mapAcsr.isEmpty()) {
101: whereCond = new EntityFieldMap((Map) mapAcsr
102: .get(methodContext), EntityOperator.AND);
103: }
104: listAcsr.put(methodContext, delegator
105: .findListIteratorByCondition(entityName,
106: whereCond, null, null, orderByNames,
107: null));
108: } else {
109: if (useCache) {
110: listAcsr.put(methodContext, delegator
111: .findByAndCache(entityName, (Map) mapAcsr
112: .get(methodContext), orderByNames));
113: } else {
114: listAcsr.put(methodContext, delegator.findByAnd(
115: entityName, (Map) mapAcsr
116: .get(methodContext), orderByNames));
117: }
118: }
119: } catch (GenericEntityException e) {
120: Debug.logError(e, module);
121: String errMsg = "ERROR: Could not complete the "
122: + simpleMethod.getShortDescription()
123: + " process [problem finding the " + entityName
124: + " entity: " + e.getMessage() + "]";
125:
126: if (methodContext.getMethodType() == MethodContext.EVENT) {
127: methodContext.putEnv(simpleMethod
128: .getEventErrorMessageName(), errMsg);
129: methodContext.putEnv(simpleMethod
130: .getEventResponseCodeName(), simpleMethod
131: .getDefaultErrorCode());
132: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
133: methodContext.putEnv(simpleMethod
134: .getServiceErrorMessageName(), errMsg);
135: methodContext.putEnv(simpleMethod
136: .getServiceResponseMessageName(), simpleMethod
137: .getDefaultErrorCode());
138: }
139: return false;
140: }
141: return true;
142: }
143: }
|