001: /*
002: *
003: * Copyright (c) 2004 SourceTap - www.sourcetap.com
004: *
005: * The contents of this file are subject to the SourceTap Public License
006: * ("License"); You may not use this file except in compliance with the
007: * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
010: * the specific language governing rights and limitations under the License.
011: *
012: * The above copyright notice and this permission notice shall be included
013: * in all copies or substantial portions of the Software.
014: *
015: */
016:
017: package com.sourcetap.sfa.util;
018:
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.ofbiz.base.util.UtilMisc;
026: import org.ofbiz.entity.GenericDelegator;
027: import org.ofbiz.entity.GenericEntityException;
028: import org.ofbiz.entity.condition.EntityComparisonOperator;
029: import org.ofbiz.entity.condition.EntityCondition;
030: import org.ofbiz.entity.condition.EntityConditionList;
031: import org.ofbiz.entity.condition.EntityExpr;
032: import org.ofbiz.entity.condition.EntityOperator;
033: import org.ofbiz.entity.model.DynamicViewEntity;
034: import org.ofbiz.entity.model.ModelKeyMap;
035: import org.ofbiz.entity.model.ModelViewEntity.ModelMemberEntity;
036: import org.ofbiz.entity.util.EntityFindOptions;
037: import org.ofbiz.entity.util.EntityListIterator;
038:
039: /**
040: * @author Steve Fowler
041: *
042: *
043: *
044: */
045: public class QueryInfo {
046: protected String primaryEntityName;
047: protected Map secondaryEntityNames = new HashMap();
048: protected Map secondaryAttributes = new HashMap();
049: protected DynamicViewEntity dve = null;
050: protected List entityConditions = new ArrayList();
051: protected List orderBy = null;
052: protected GenericDelegator delegator;
053: protected List selectFields = null;
054: protected String saveResultListId = "";
055:
056: public QueryInfo(GenericDelegator delegatorIn, String primaryEntity) {
057: primaryEntityName = primaryEntity;
058: delegator = delegatorIn;
059: }
060:
061: public void addCondition(EntityCondition newCondition) {
062: entityConditions.add(newCondition);
063: }
064:
065: public void addJoin(String firstEntity, String secondEntity,
066: Boolean relOptional, String firstField, String secondField) {
067: addJoin(firstEntity, firstEntity, secondEntity, secondEntity,
068: relOptional, firstField, secondField);
069: }
070:
071: public void addJoin(String firstEntity, String secondEntity,
072: String secondEntityAlias, Boolean relOptional,
073: String firstField, String secondField) {
074: addJoin(firstEntity, firstEntity, secondEntity,
075: secondEntityAlias, relOptional, firstField, secondField);
076: }
077:
078: public void addJoin(String firstEntity, String firstEntityAlias,
079: String secondEntity, String secondEntityAlias,
080: Boolean relOptional, String firstField, String secondField) {
081: addJoin(firstEntity, firstEntity, secondEntity,
082: secondEntityAlias, relOptional, firstField, firstField,
083: secondField, secondField);
084: }
085:
086: public void addJoin(String firstEntity, String firstEntityAlias,
087: String secondEntity, String secondEntityAlias,
088: Boolean relOptional, String firstField,
089: String firstFieldAlias, String secondField,
090: String secondFieldAlias) {
091: checkEntity(firstEntity, firstEntityAlias);
092: checkEntity(secondEntity, secondEntityAlias);
093:
094: if (!firstField.equals(firstFieldAlias))
095: addAlias(firstEntityAlias, firstField, firstFieldAlias);
096:
097: if (!secondField.equals(secondFieldAlias))
098: addAlias(secondEntityAlias, secondField, secondFieldAlias);
099:
100: dve.addViewLink(firstEntityAlias, secondEntityAlias,
101: relOptional, UtilMisc.toList(new ModelKeyMap(
102: firstFieldAlias, secondFieldAlias)));
103: }
104:
105: public void addJoin(String firstEntity, String secondEntity,
106: Boolean relOptional, List keyMaps) {
107: addJoin(firstEntity, firstEntity, secondEntity, secondEntity,
108: relOptional, keyMaps);
109: }
110:
111: public void addJoin(String firstEntity, String firstEntityAlias,
112: String secondEntity, String secondEntityAlias,
113: Boolean relOptional, List keyMaps) {
114: checkEntity(firstEntity, firstEntityAlias);
115: checkEntity(secondEntity, secondEntityAlias);
116: dve.addViewLink(firstEntityAlias, secondEntityAlias,
117: relOptional, keyMaps);
118: }
119:
120: public void checkEntity(String entityName, String entityAlias) {
121: if (!entityName.equals(primaryEntityName)
122: && !secondaryEntityNames.containsKey(entityAlias)) {
123: if (dve == null) {
124: dve = EntityHelper.createDynamicViewEntity(delegator,
125: primaryEntityName);
126: }
127: dve.addMemberEntity(entityAlias, entityName);
128: secondaryEntityNames.put(entityAlias, entityName);
129: }
130: }
131:
132: public void checkAttribute(String entityAlias, String attributeName) {
133: checkAttribute(entityAlias, attributeName, attributeName);
134: }
135:
136: public void checkAttribute(String entityAlias,
137: String attributeName, String attributeAlias) {
138: checkEntity(entityAlias, entityAlias);
139: if (!entityAlias.equals(primaryEntityName)
140: && !secondaryAttributes.containsKey(entityAlias + "."
141: + attributeAlias)) {
142: dve.addAlias(entityAlias, attributeAlias, attributeName,
143: null, null, null, null);
144:
145: secondaryAttributes.put(entityAlias + "." + attributeAlias,
146: attributeName);
147: }
148: }
149:
150: public void addCondition(String entityAlias, String attributeAlias,
151: EntityComparisonOperator operator, Object value) {
152: addCondition(entityAlias, attributeAlias, attributeAlias,
153: operator, value);
154: }
155:
156: public void addCondition(String entityAlias, String attributeAlias,
157: String attributeName, EntityComparisonOperator operator,
158: Object value) {
159: checkAttribute(entityAlias, attributeName, attributeAlias);
160: entityConditions.add(new EntityExpr(attributeAlias, operator,
161: value));
162: }
163:
164: public void addAlias(String entityAlias, String attributeName,
165: String aliasName) {
166: checkEntity(entityAlias, entityAlias);
167: dve.addAlias(entityAlias, aliasName, attributeName, null, null,
168: null, null);
169: secondaryAttributes.put(entityAlias + "." + aliasName,
170: attributeName);
171: }
172:
173: public List executeQuery() throws GenericEntityException {
174: if (isDynamic()) {
175: return EntityHelper.findByCondition(delegator, dve,
176: new EntityConditionList(entityConditions,
177: EntityOperator.AND), orderBy);
178: } else {
179: return delegator.findByCondition(primaryEntityName,
180: new EntityConditionList(entityConditions,
181: EntityOperator.AND), null, orderBy);
182: }
183: }
184:
185: public EntityListIterator getQueryIterator()
186: throws GenericEntityException {
187: if (isDynamic()) {
188: return EntityHelper.findIteratorByCondition(delegator, dve,
189: new EntityConditionList(entityConditions,
190: EntityOperator.AND), selectFields, orderBy);
191: } else {
192: return delegator.findListIteratorByCondition(
193: primaryEntityName, new EntityConditionList(
194: entityConditions, EntityOperator.AND),
195: null, selectFields, orderBy, new EntityFindOptions(
196: true,
197: EntityFindOptions.TYPE_SCROLL_INSENSITIVE,
198: EntityFindOptions.CONCUR_READ_ONLY, true));
199: }
200: }
201:
202: public EntityConditionList getEntityConditions() {
203: return new EntityConditionList(entityConditions,
204: EntityOperator.AND);
205: }
206:
207: public boolean isDynamic() {
208: return (dve != null);
209: }
210:
211: public DynamicViewEntity getDynamicViewEntity() {
212: return dve;
213: }
214:
215: public String getPrimaryEntity() {
216: return primaryEntityName;
217: }
218:
219: public GenericDelegator getDelegator() {
220: return delegator;
221: }
222:
223: public void setOrderBy(List orderBy_) {
224: orderBy = orderBy_;
225: }
226:
227: public List getOrderBy() {
228: return orderBy;
229: }
230:
231: public List getEntities() {
232: if (dve == null)
233: return UtilMisc.toList(primaryEntityName);
234:
235: ArrayList entityList = new ArrayList();
236: Iterator listIter = dve.getModelMemberEntitiesEntryIter();
237: while (listIter.hasNext()) {
238: Map.Entry entry = (Map.Entry) listIter.next();
239:
240: ModelMemberEntity modelMemberEntity = (ModelMemberEntity) entry
241: .getValue();
242: entityList.add(modelMemberEntity.getEntityName());
243: }
244:
245: return entityList;
246: }
247:
248: public List getSelectFields() {
249: return selectFields;
250: }
251:
252: public void setSelectFields(List fieldsToSelect) {
253: selectFields = fieldsToSelect;
254: }
255:
256: public void addSelectFields(String fieldName) {
257: if (selectFields == null)
258: selectFields = new ArrayList();
259:
260: selectFields.add(fieldName);
261: }
262:
263: /**
264: * @return Returns the saveResultListId.
265: */
266: public String getSaveResultListId() {
267: return saveResultListId;
268: }
269:
270: /**
271: * @param saveResultListName The saveResultListId to set.
272: */
273: public void setSaveResultListId(String saveResultListId) {
274: this.saveResultListId = saveResultListId;
275: }
276:
277: }
|