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.Iterator;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.ofbiz.base.util.Debug;
025: import org.ofbiz.entity.GenericDelegator;
026: import org.ofbiz.entity.GenericEntityException;
027: import org.ofbiz.entity.GenericValue;
028: import org.ofbiz.entity.condition.EntityCondition;
029: import org.ofbiz.entity.model.DynamicViewEntity;
030: import org.ofbiz.entity.model.ModelEntity;
031: import org.ofbiz.entity.model.ModelField;
032: import org.ofbiz.entity.model.ModelViewEntity;
033: import org.ofbiz.entity.util.EntityFindOptions;
034: import org.ofbiz.entity.util.EntityListIterator;
035:
036: /**
037: * @author Steve Fowler
038: *
039: *
040: *
041: */
042: public class EntityHelper {
043:
044: public static final String module = EntityHelper.class.getName();
045:
046: /**
047: * Create a dynamic view entity using the specified entity as the source. This can be used
048: * as a starting point when building a dynamic view with most fields from a single entity.
049: * All fields from the specified entity will be added to the view entity. Other entities
050: * and fields can be added to the returned dynamic view
051: * @param modelEntiy Primary Entity to be used to create the dynamic view entity.
052: * @return a dynamic view entity that is based on the specified modelEntity
053: */
054: public static DynamicViewEntity createDynamicViewEntity(
055: GenericDelegator delegator, String entityName) {
056: try {
057: ModelEntity modelEntity = delegator.getModelReader()
058: .getModelEntity(entityName);
059: return createDynamicViewEntity(delegator, modelEntity);
060: } catch (Exception e) {
061: Debug.logError(e, module);
062: throw new IllegalArgumentException(
063: "Error finding Entity with name " + entityName
064: + " for createDynamicViewEntity");
065: }
066: }
067:
068: /**
069: * Create a dynamic view entity using the specified entity as the source. This can be used
070: * as a starting point when building a dynamic view with most fields from a single entity.
071: * All fields from the specified entity will be added to the view entity. Other entities
072: * and fields can be added to the returned dynamic view
073: * @param modelEntiy Primary Entity to be used to create the dynamic view entity.
074: * @return a dynamic view entity that is based on the specified modelEntity
075: */
076: public static DynamicViewEntity createDynamicViewEntity(
077: GenericDelegator delegator, ModelEntity modelEntity) {
078: String entityName = modelEntity.getEntityName();
079: DynamicViewEntity dve = new DynamicViewEntity();
080: dve.addMemberEntity(entityName, entityName);
081:
082: Iterator modelFieldIter = modelEntity.getFieldsIterator();
083: while (modelFieldIter.hasNext()) {
084: ModelField modelField = (ModelField) modelFieldIter.next();
085: dve.addAlias(entityName, modelField.getName(), null, null,
086: Boolean.valueOf(modelField.getIsPk()), null, null);
087: }
088:
089: return dve;
090: }
091:
092: public static List findByCondition(GenericDelegator delegator,
093: DynamicViewEntity dve, EntityCondition condition,
094: List orderBy) throws GenericEntityException {
095: ModelViewEntity mve = dve.makeModelViewEntity(delegator);
096: List fields = mve.getAllFieldNames();
097: EntityListIterator eliOne = delegator
098: .findListIteratorByCondition(dve, condition, null,
099: fields, orderBy, null);
100: List valueList = eliOne.getCompleteList();
101: eliOne.close();
102: return valueList;
103: }
104:
105: public static EntityListIterator findIteratorByCondition(
106: GenericDelegator delegator, DynamicViewEntity dve,
107: EntityCondition condition, List orderBy)
108: throws GenericEntityException {
109: return findIteratorByCondition(delegator, dve, condition, null,
110: orderBy);
111: }
112:
113: public static EntityListIterator findIteratorByCondition(
114: GenericDelegator delegator, DynamicViewEntity dve,
115: EntityCondition condition, List selectFields, List orderBy)
116: throws GenericEntityException {
117: EntityListIterator eliOne = delegator
118: .findListIteratorByCondition(
119: dve,
120: condition,
121: null,
122: selectFields,
123: orderBy,
124: new EntityFindOptions(
125: true,
126: EntityFindOptions.TYPE_SCROLL_INSENSITIVE,
127: EntityFindOptions.CONCUR_READ_ONLY,
128: true));
129: return eliOne;
130: }
131:
132: public static String getEntityOperator(int operatorId) {
133: switch (operatorId) {
134: case 1:
135: return "equals";
136: case 2:
137: return "notEqual";
138: case 3:
139: return "lessThan";
140: case 4:
141: return "greaterThan";
142: case 5:
143: return "lessThanEqualTo";
144: case 6:
145: return "greaterThanEqualTo";
146: case 7:
147: return "in";
148: case 8:
149: return "between";
150: case 9:
151: return "not";
152: case 10:
153: return "and";
154: case 11:
155: return "or";
156: case 12:
157: return "like";
158: case 13:
159: return "not-in";
160: default:
161: throw new IllegalArgumentException(
162: "Unknown operator with Id:" + operatorId);
163: }
164: }
165:
166: public static List findByClause(GenericDelegator delegator,
167: String mainEntityName, List entityClauses, Map fields,
168: List orderBy) {
169: throw new RuntimeException(
170: "EntityHelper.findByClause is not implemented");
171: }
172:
173: public static GenericValue getPrimaryGVFromDynamicGV(
174: GenericDelegator delegator, GenericValue dynamicGV,
175: String primaryEntityName) {
176: if (dynamicGV == null)
177: return null;
178:
179: if (primaryEntityName.equals(dynamicGV.getEntityName()))
180: return dynamicGV;
181:
182: GenericValue retValue = delegator.makeValidValue(
183: primaryEntityName, dynamicGV.getAllFields());
184: return retValue;
185: }
186:
187: public static List getPrimaryGVLFromDynamicGVL(
188: GenericDelegator delegator, List dynamicGVL,
189: String primaryEntityName) {
190: if (dynamicGVL == null)
191: return null;
192:
193: Iterator dgvI = dynamicGVL.iterator();
194: List list = new ArrayList();
195:
196: while (dgvI.hasNext()) {
197: GenericValue gv = (GenericValue) dgvI.next();
198: list.add(getPrimaryGVFromDynamicGV(delegator, gv,
199: primaryEntityName));
200:
201: }
202: return list;
203: }
204:
205: }
|