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.List;
021: import java.util.Map;
022:
023: import org.ofbiz.base.util.Debug;
024: import org.ofbiz.entity.GenericDelegator;
025: import org.ofbiz.entity.GenericEntityException;
026: import org.ofbiz.entity.condition.EntityCondition;
027: import org.ofbiz.entity.condition.EntityFieldMap;
028: import org.ofbiz.entity.condition.EntityOperator;
029: import org.ofbiz.minilang.SimpleMethod;
030: import org.ofbiz.minilang.method.ContextAccessor;
031: import org.ofbiz.minilang.method.MethodContext;
032: import org.ofbiz.minilang.method.MethodOperation;
033: import org.w3c.dom.Element;
034:
035: /**
036: * Uses the delegator to find entity values by anding the map fields
037: */
038: public class FindByAnd extends MethodOperation {
039:
040: public static final String module = FindByAnd.class.getName();
041:
042: ContextAccessor listAcsr;
043: String entityName;
044: ContextAccessor mapAcsr;
045: ContextAccessor orderByListAcsr;
046: String delegatorName;
047: String useCacheStr;
048: String useIteratorStr;
049:
050: public FindByAnd(Element element, SimpleMethod simpleMethod) {
051: super (element, simpleMethod);
052: listAcsr = new ContextAccessor(element
053: .getAttribute("list-name"));
054: entityName = element.getAttribute("entity-name");
055: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
056: orderByListAcsr = new ContextAccessor(element
057: .getAttribute("order-by-list-name"));
058: delegatorName = element.getAttribute("delegator-name");
059:
060: useCacheStr = element.getAttribute("use-cache");
061: useIteratorStr = element.getAttribute("use-iterator");
062: }
063:
064: public boolean exec(MethodContext methodContext) {
065: String entityName = methodContext.expandString(this .entityName);
066: String delegatorName = methodContext
067: .expandString(this .delegatorName);
068: String useCacheStr = methodContext
069: .expandString(this .useCacheStr);
070: String useIteratorStr = methodContext
071: .expandString(this .useIteratorStr);
072:
073: boolean useCache = "true".equals(useCacheStr);
074: boolean useIterator = "true".equals(useIteratorStr);
075:
076: List orderByNames = null;
077: if (!orderByListAcsr.isEmpty()) {
078: orderByNames = (List) orderByListAcsr.get(methodContext);
079: }
080:
081: GenericDelegator delegator = methodContext.getDelegator();
082: if (delegatorName != null && delegatorName.length() > 0) {
083: delegator = GenericDelegator
084: .getGenericDelegator(delegatorName);
085: }
086:
087: try {
088: if (useIterator) {
089: EntityCondition whereCond = null;
090: if (!mapAcsr.isEmpty()) {
091: whereCond = new EntityFieldMap((Map) mapAcsr
092: .get(methodContext), EntityOperator.AND);
093: }
094: listAcsr.put(methodContext, delegator
095: .findListIteratorByCondition(entityName,
096: whereCond, null, null, orderByNames,
097: null));
098: } else {
099: if (useCache) {
100: listAcsr.put(methodContext, delegator
101: .findByAndCache(entityName, (Map) mapAcsr
102: .get(methodContext), orderByNames));
103: } else {
104: listAcsr.put(methodContext, delegator.findByAnd(
105: entityName, (Map) mapAcsr
106: .get(methodContext), orderByNames));
107: }
108: }
109: } catch (GenericEntityException e) {
110: Debug.logError(e, module);
111: String errMsg = "ERROR: Could not complete the "
112: + simpleMethod.getShortDescription()
113: + " process [problem finding the " + entityName
114: + " entity: " + e.getMessage() + "]";
115:
116: if (methodContext.getMethodType() == MethodContext.EVENT) {
117: methodContext.putEnv(simpleMethod
118: .getEventErrorMessageName(), errMsg);
119: methodContext.putEnv(simpleMethod
120: .getEventResponseCodeName(), simpleMethod
121: .getDefaultErrorCode());
122: } else if (methodContext.getMethodType() == MethodContext.SERVICE) {
123: methodContext.putEnv(simpleMethod
124: .getServiceErrorMessageName(), errMsg);
125: methodContext.putEnv(simpleMethod
126: .getServiceResponseMessageName(), simpleMethod
127: .getDefaultErrorCode());
128: }
129: return false;
130: }
131: return true;
132: }
133:
134: public String rawString() {
135: // TODO: something more than the empty tag
136: return "<find-by-and/>";
137: }
138:
139: public String expandedString(MethodContext methodContext) {
140: // TODO: something more than a stub/dummy
141: return this.rawString();
142: }
143: }
|