001: /*
002: * $Id: EntityFieldMap.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.ArrayList;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.Map;
031:
032: import org.ofbiz.entity.GenericEntity;
033: import org.ofbiz.entity.GenericModelException;
034: import org.ofbiz.entity.jdbc.SqlJdbcUtil;
035: import org.ofbiz.entity.model.ModelEntity;
036: import org.ofbiz.entity.model.ModelField;
037:
038: /**
039: * Encapsulates simple expressions used for specifying queries
040: *
041: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
042: * @version $Revision: 1.3 $
043: * @since 2.0
044: */
045: public class EntityFieldMap extends EntityCondition {
046:
047: protected Map fieldMap;
048: protected EntityOperator operator;
049:
050: protected EntityFieldMap() {
051: }
052:
053: public EntityFieldMap(Map fieldMap, EntityOperator operator) {
054: this .fieldMap = fieldMap;
055: this .operator = operator;
056: }
057:
058: public EntityOperator getOperator() {
059: return this .operator;
060: }
061:
062: public Object getField(String name) {
063: return this .fieldMap.get(name);
064: }
065:
066: public boolean containsField(String name) {
067: return this .fieldMap.containsKey(name);
068: }
069:
070: public Iterator getFieldKeyIterator() {
071: return this .fieldMap.keySet().iterator();
072: }
073:
074: public Iterator getFieldEntryIterator() {
075: return this .fieldMap.entrySet().iterator();
076: }
077:
078: public String makeWhereString(ModelEntity modelEntity,
079: List entityConditionParams) {
080: // if (Debug.verboseOn()) Debug.logVerbose("makeWhereString for entity " + modelEntity.getEntityName(), module);
081: List whereFields = new ArrayList();
082:
083: if (fieldMap != null && fieldMap.size() > 0) {
084: if (modelEntity == null) {
085: Iterator iter = fieldMap.keySet().iterator();
086: while (iter.hasNext()) {
087: String fieldName = (String) iter.next();
088: whereFields.add(fieldName);
089: }
090: } else {
091: for (int fi = 0; fi < modelEntity.getFieldsSize(); fi++) {
092: ModelField curField = modelEntity.getField(fi);
093:
094: if (fieldMap.containsKey(curField.getName())) {
095: whereFields.add(curField);
096: }
097: }
098: }
099: }
100: return SqlJdbcUtil.makeWhereStringFromFields(whereFields,
101: fieldMap, operator.getCode(), entityConditionParams);
102: }
103:
104: public boolean entityMatches(GenericEntity entity) {
105: Iterator iter = fieldMap.entrySet().iterator();
106: while (iter.hasNext()) {
107: Map.Entry entry = (Map.Entry) iter.next();
108: String fieldName = (String) entry.getKey();
109: Object value = entity.get(fieldName);
110: if (value == null) {
111: if (entry.getValue() != null) {
112: return false;
113: }
114: } else {
115: if (!value.equals(entry.getValue())) {
116: return false;
117: }
118: }
119: }
120: return true;
121: }
122:
123: public void checkCondition(ModelEntity modelEntity)
124: throws GenericModelException {
125: // if (Debug.verboseOn()) Debug.logVerbose("checkCondition for entity " + modelEntity.getEntityName(), module);
126: // make sure that all fields in the Map are valid
127: if (fieldMap != null
128: && !modelEntity.areFields(fieldMap.keySet())) {
129: throw new GenericModelException(
130: "At least one of the passed fields is not valid: "
131: + fieldMap.keySet().toString());
132: }
133: }
134: }
|