001: /*
002: * $Id: EntityConditionList.java,v 1.3 2003/11/07 00:33:56 jonesde 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: */
025: package org.ofbiz.entity.condition;
026:
027: import java.util.Iterator;
028: import java.util.List;
029:
030: import org.ofbiz.entity.GenericEntity;
031: import org.ofbiz.entity.GenericModelException;
032: import org.ofbiz.entity.model.ModelEntity;
033:
034: /**
035: * Encapsulates a list of EntityConditions to be used as a single EntityCondition combined as specified
036: *
037: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
038: * @version $Revision: 1.3 $
039: * @since 2.0
040: */
041: public class EntityConditionList extends EntityCondition {
042:
043: protected List conditionList;
044: protected EntityJoinOperator operator;
045:
046: protected EntityConditionList() {
047: }
048:
049: public EntityConditionList(List conditionList,
050: EntityJoinOperator operator) {
051: this .conditionList = conditionList;
052: this .operator = operator;
053: }
054:
055: public EntityOperator getOperator() {
056: return this .operator;
057: }
058:
059: public EntityCondition getCondition(int index) {
060: return (EntityCondition) this .conditionList.get(index);
061: }
062:
063: public int getConditionListSize() {
064: return this .conditionList.size();
065: }
066:
067: public Iterator getConditionIterator() {
068: return this .conditionList.iterator();
069: }
070:
071: public String makeWhereString(ModelEntity modelEntity,
072: List entityConditionParams) {
073: // if (Debug.verboseOn()) Debug.logVerbose("makeWhereString for entity " + modelEntity.getEntityName(), module);
074: StringBuffer whereStringBuffer = new StringBuffer();
075:
076: if (conditionList != null && conditionList.size() > 0) {
077: for (int i = 0; i < conditionList.size(); i++) {
078: EntityCondition condition = (EntityCondition) conditionList
079: .get(i);
080:
081: whereStringBuffer.append('(');
082: whereStringBuffer.append(condition.makeWhereString(
083: modelEntity, entityConditionParams));
084: whereStringBuffer.append(')');
085: if (i < conditionList.size() - 1) {
086: whereStringBuffer.append(' ');
087: whereStringBuffer.append(operator.getCode());
088: whereStringBuffer.append(' ');
089: }
090: }
091: }
092: return whereStringBuffer.toString();
093: }
094:
095: public void checkCondition(ModelEntity modelEntity)
096: throws GenericModelException {
097: // if (Debug.verboseOn()) Debug.logVerbose("checkCondition for entity " + modelEntity.getEntityName(), module);
098: if (conditionList == null || conditionList.size() == 0)
099: return;
100:
101: Iterator exprIter = conditionList.iterator();
102: while (exprIter.hasNext()) {
103: EntityCondition entityCondition = (EntityCondition) exprIter
104: .next();
105: entityCondition.checkCondition(modelEntity);
106: }
107: }
108:
109: public boolean entityMatches(GenericEntity entity) {
110: if (conditionList != null && conditionList.size() > 0) {
111: boolean matches = true;
112: EntityCondition condition = (EntityCondition) conditionList
113: .get(0);
114: EntityOperator.MatchResult result = operator.join(
115: condition, entity);
116: for (int i = 1; !result.shortCircuit
117: && i < conditionList.size(); i++) {
118: condition = (EntityCondition) conditionList.get(i);
119: result = operator.join(condition, entity);
120: }
121: return result.matches;
122: }
123: return false;
124: }
125: }
|