001: /*
002: * $Id: GenericHelperDAO.java,v 1.4 2004/02/03 08:14:41 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.datasource;
026:
027: import java.util.Collection;
028: import java.util.Iterator;
029: import java.util.LinkedList;
030: import java.util.List;
031: import java.util.Map;
032: import java.util.Set;
033:
034: import org.ofbiz.base.util.Debug;
035: import org.ofbiz.entity.GenericEntityException;
036: import org.ofbiz.entity.GenericPK;
037: import org.ofbiz.entity.GenericValue;
038: import org.ofbiz.entity.condition.EntityCondition;
039: import org.ofbiz.entity.model.ModelEntity;
040: import org.ofbiz.entity.model.ModelRelation;
041: import org.ofbiz.entity.util.EntityFindOptions;
042: import org.ofbiz.entity.util.EntityListIterator;
043:
044: /**
045: * Generic Entity Helper Class
046: *
047: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
048: * @author <a href='mailto:chris_maurer@altavista.com'>Chris Maurer</a>
049: * @version $Revision: 1.4 $
050: * @since 2.0
051: */
052: public class GenericHelperDAO implements GenericHelper {
053:
054: public static final String module = GenericHelperDAO.class
055: .getName();
056:
057: protected GenericDAO genericDAO;
058: protected String helperName;
059:
060: public GenericHelperDAO(String helperName) {
061: this .helperName = helperName;
062: genericDAO = GenericDAO.getGenericDAO(helperName);
063: }
064:
065: public String getHelperName() {
066: return helperName;
067: }
068:
069: /** Creates a Entity in the form of a GenericValue and write it to the database
070: *@return GenericValue instance containing the new instance
071: */
072: public GenericValue create(GenericValue value)
073: throws GenericEntityException {
074: if (value == null) {
075: return null;
076: }
077: int retVal = genericDAO.insert(value);
078: if (Debug.verboseOn())
079: Debug.logVerbose("Insert Return Value : " + retVal, module);
080: return value;
081: }
082:
083: /** Find a Generic Entity by its Primary Key
084: *@param primaryKey The primary key to find by.
085: *@return The GenericValue corresponding to the primaryKey
086: */
087: public GenericValue findByPrimaryKey(GenericPK primaryKey)
088: throws GenericEntityException {
089: if (primaryKey == null) {
090: return null;
091: }
092: GenericValue genericValue = new GenericValue(primaryKey);
093:
094: genericDAO.select(genericValue);
095: return genericValue;
096: }
097:
098: /** Find a Generic Entity by its Primary Key and only returns the values requested by the passed keys (names)
099: *@param primaryKey The primary key to find by.
100: *@param keys The keys, or names, of the values to retrieve; only these values will be retrieved
101: *@return The GenericValue corresponding to the primaryKey
102: */
103: public GenericValue findByPrimaryKeyPartial(GenericPK primaryKey,
104: Set keys) throws GenericEntityException {
105: if (primaryKey == null) {
106: return null;
107: }
108: GenericValue genericValue = new GenericValue(primaryKey);
109:
110: genericDAO.partialSelect(genericValue, keys);
111: return genericValue;
112: }
113:
114: /** Find a number of Generic Value objects by their Primary Keys, all at once
115: * This is done here for the DAO GenericHelper; for a client-server helper it
116: * would be done on the server side to reduce network round trips.
117: *@param primaryKeys A List of primary keys to find by.
118: *@return List of GenericValue objects corresponding to the passed primaryKey objects
119: */
120: public List findAllByPrimaryKeys(List primaryKeys)
121: throws GenericEntityException {
122: if (primaryKeys == null)
123: return null;
124: List results = new LinkedList();
125:
126: Iterator pkiter = primaryKeys.iterator();
127:
128: while (pkiter.hasNext()) {
129: GenericPK primaryKey = (GenericPK) pkiter.next();
130: GenericValue result = this .findByPrimaryKey(primaryKey);
131:
132: if (result != null)
133: results.add(result);
134: }
135: return results;
136: }
137:
138: /** Remove a Generic Entity corresponding to the primaryKey
139: *@param primaryKey The primary key of the entity to remove.
140: *@return int representing number of rows effected by this operation
141: */
142: public int removeByPrimaryKey(GenericPK primaryKey)
143: throws GenericEntityException {
144: if (primaryKey == null)
145: return 0;
146: if (Debug.verboseOn())
147: Debug.logVerbose("Removing GenericPK: "
148: + primaryKey.toString(), module);
149: return genericDAO.delete(primaryKey);
150: }
151:
152: /** Finds Generic Entity records by all of the specified fields (ie: combined using AND)
153: *@param modelEntity The ModelEntity of the Entity as defined in the entity XML file
154: *@param fields The fields of the named entity to query by with their corresponging values
155: *@param orderBy The fields of the named entity to order the query by; optionally add a " ASC" for ascending or " DESC" for descending
156: *@return List of GenericValue instances that match the query
157: */
158: public List findByAnd(ModelEntity modelEntity, Map fields,
159: List orderBy) throws GenericEntityException {
160: return genericDAO.selectByAnd(modelEntity, fields, orderBy);
161: }
162:
163: /** Finds Generic Entity records by all of the specified fields (ie: combined using OR)
164: *@param modelEntity The ModelEntity of the Entity as defined in the entity XML file
165: *@param fields The fields of the named entity to query by with their corresponging values
166: *@param orderBy The fields of the named entity to order the query by; optionally add a " ASC" for ascending or " DESC" for descending
167: *@return List of GenericValue instances that match the query
168: */
169: public List findByOr(ModelEntity modelEntity, Map fields,
170: List orderBy) throws GenericEntityException {
171: return genericDAO.selectByOr(modelEntity, fields, orderBy);
172: }
173:
174: /** Finds GenericValues by the conditions specified in the EntityCondition object, the the EntityCondition javadoc for more details.
175: *@param modelEntity The ModelEntity of the Entity as defined in the entity XML file
176: *@param entityCondition The EntityCondition object that specifies how to constrain this query
177: *@param fieldsToSelect The fields of the named entity to get from the database; if empty or null all fields will be retreived
178: *@param orderBy The fields of the named entity to order the query by; optionally add a " ASC" for ascending or " DESC" for descending
179: *@return List of GenericValue objects representing the result
180: */
181: public List findByCondition(ModelEntity modelEntity,
182: EntityCondition entityCondition, Collection fieldsToSelect,
183: List orderBy) throws GenericEntityException {
184: return genericDAO.selectByCondition(modelEntity,
185: entityCondition, fieldsToSelect, orderBy);
186: }
187:
188: /** Finds GenericValues by the conditions specified in the EntityCondition object, the the EntityCondition javadoc for more details.
189: *@param modelEntity The ModelEntity of the Entity as defined in the entity XML file
190: *@param whereEntityCondition The EntityCondition object that specifies how to constrain this query before any groupings are done (if this is a view entity with group-by aliases)
191: *@param havingEntityCondition The EntityCondition object that specifies how to constrain this query after any groupings are done (if this is a view entity with group-by aliases)
192: *@param fieldsToSelect The fields of the named entity to get from the database; if empty or null all fields will be retreived
193: *@param orderBy The fields of the named entity to order the query by; optionally add a " ASC" for ascending or " DESC" for descending
194: *@param findOptions An instance of EntityFindOptions that specifies advanced query options. See the EntityFindOptions JavaDoc for more details.
195: *@return EntityListIterator representing the result of the query: NOTE THAT THIS MUST BE CLOSED WHEN YOU ARE
196: * DONE WITH IT, AND DON'T LEAVE IT OPEN TOO LONG BEACUSE IT WILL MAINTAIN A DATABASE CONNECTION.
197: */
198: public EntityListIterator findListIteratorByCondition(
199: ModelEntity modelEntity,
200: EntityCondition whereEntityCondition,
201: EntityCondition havingEntityCondition,
202: Collection fieldsToSelect, List orderBy,
203: EntityFindOptions findOptions)
204: throws GenericEntityException {
205: return genericDAO.selectListIteratorByCondition(modelEntity,
206: whereEntityCondition, havingEntityCondition,
207: fieldsToSelect, orderBy, findOptions);
208: }
209:
210: public List findByMultiRelation(GenericValue value,
211: ModelRelation modelRelationOne, ModelEntity modelEntityOne,
212: ModelRelation modelRelationTwo, ModelEntity modelEntityTwo,
213: List orderBy) throws GenericEntityException {
214: return genericDAO.selectByMultiRelation(value,
215: modelRelationOne, modelEntityOne, modelRelationTwo,
216: modelEntityTwo, orderBy);
217: }
218:
219: public long findCountByCondition(ModelEntity modelEntity,
220: EntityCondition whereEntityCondition,
221: EntityCondition havingEntityCondition)
222: throws GenericEntityException {
223: return genericDAO.selectCountByCondition(modelEntity,
224: whereEntityCondition, havingEntityCondition);
225: }
226:
227: /** Removes/deletes Generic Entity records found by all of the specified fields (ie: combined using AND)
228: *@param modelEntity The ModelEntity of the Entity as defined in the entity XML file
229: *@param fields The fields of the named entity to query by with their corresponging values
230: *@return int representing number of rows effected by this operation
231: */
232: public int removeByAnd(ModelEntity modelEntity, Map fields)
233: throws GenericEntityException {
234: if (modelEntity == null || fields == null) {
235: return 0;
236: }
237: return genericDAO.deleteByAnd(modelEntity, fields);
238: }
239:
240: /** Store the Entity from the GenericValue to the persistent store
241: *@param value GenericValue instance containing the entity
242: *@return int representing number of rows effected by this operation
243: */
244: public int store(GenericValue value) throws GenericEntityException {
245: if (value == null) {
246: return 0;
247: }
248: return genericDAO.update(value);
249: }
250:
251: /** Store the Entities from the List GenericValue instances to the persistent store.
252: * This is different than the normal store method in that the store method only does
253: * an update, while the storeAll method checks to see if each entity exists, then
254: * either does an insert or an update as appropriate.
255: * These updates all happen in one transaction, so they will either all succeed or all fail,
256: * if the data source supports transactions. This is just like to othersToStore feature
257: * of the GenericEntity on a create or store.
258: *@param values List of GenericValue instances containing the entities to store
259: *@return int representing number of rows effected by this operation
260: */
261: public int storeAll(List values) throws GenericEntityException {
262: return genericDAO.storeAll(values);
263: }
264:
265: /** Remove the Entities from the List from the persistent store.
266: * <br>The List contains GenericEntity objects, can be either GenericPK or GenericValue.
267: * <br>If a certain entity contains a complete primary key, the entity in the datasource corresponding
268: * to that primary key will be removed, this is like a removeByPrimary Key.
269: * <br>On the other hand, if a certain entity is an incomplete or non primary key,
270: * if will behave like the removeByAnd method.
271: * <br>These updates all happen in one transaction, so they will either all succeed or all fail,
272: * if the data source supports transactions.
273: *@param dummyPKs List of GenericEntity instances containing the entities or by and fields to remove
274: *@return int representing number of rows effected by this operation
275: */
276: public int removeAll(List dummyPKs) throws GenericEntityException {
277: return genericDAO.deleteAll(dummyPKs);
278: }
279:
280: /** Check the datasource to make sure the entity definitions are correct, optionally adding missing entities or fields on the server
281: *@param modelEntities Map of entityName names and ModelEntity values
282: *@param messages List to put any result messages in
283: *@param addMissing Flag indicating whether or not to add missing entities and fields on the server
284: */
285: public void checkDataSource(Map modelEntities, List messages,
286: boolean addMissing) throws GenericEntityException {
287: genericDAO.checkDb(modelEntities, messages, addMissing);
288: }
289: }
|