KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > datasource > GenericHelperDAO


1 /*
2  * $Id: GenericHelperDAO.java 5831 2005-09-26 06:52:24Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.entity.datasource;
26
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Set JavaDoc;
33
34 import org.ofbiz.base.util.Debug;
35 import org.ofbiz.entity.GenericEntityException;
36 import org.ofbiz.entity.GenericPK;
37 import org.ofbiz.entity.GenericValue;
38 import org.ofbiz.entity.condition.EntityCondition;
39 import org.ofbiz.entity.model.ModelEntity;
40 import org.ofbiz.entity.model.ModelRelation;
41 import org.ofbiz.entity.util.EntityFindOptions;
42 import org.ofbiz.entity.util.EntityListIterator;
43
44 /**
45  * Generic Entity Helper Class
46  *
47  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
48  * @author <a HREF='mailto:chris_maurer@altavista.com'>Chris Maurer</a>
49  * @version $Rev: 5831 $
50  * @since 2.0
51  */

52 public class GenericHelperDAO implements GenericHelper {
53
54     public static final String JavaDoc module = GenericHelperDAO.class.getName();
55
56     protected GenericDAO genericDAO;
57     protected String JavaDoc helperName;
58
59     public GenericHelperDAO(String JavaDoc helperName) {
60         this.helperName = helperName;
61         genericDAO = GenericDAO.getGenericDAO(helperName);
62     }
63
64     public String JavaDoc getHelperName() {
65         return helperName;
66     }
67
68     /** Creates a Entity in the form of a GenericValue and write it to the database
69      *@return GenericValue instance containing the new instance
70      */

71     public GenericValue create(GenericValue value) throws GenericEntityException {
72         if (value == null) {
73             return null;
74         }
75         int retVal = genericDAO.insert(value);
76         if (Debug.verboseOn()) Debug.logVerbose("Insert Return Value : " + retVal, module);
77         return value;
78     }
79
80     /** Find a Generic Entity by its Primary Key
81      *@param primaryKey The primary key to find by.
82      *@return The GenericValue corresponding to the primaryKey
83      */

84     public GenericValue findByPrimaryKey(GenericPK primaryKey) throws GenericEntityException {
85         if (primaryKey == null) {
86             return null;
87         }
88         GenericValue genericValue = GenericValue.create(primaryKey);
89
90         genericDAO.select(genericValue);
91         return genericValue;
92     }
93
94     /** Find a Generic Entity by its Primary Key and only returns the values requested by the passed keys (names)
95      *@param primaryKey The primary key to find by.
96      *@param keys The keys, or names, of the values to retrieve; only these values will be retrieved
97      *@return The GenericValue corresponding to the primaryKey
98      */

99     public GenericValue findByPrimaryKeyPartial(GenericPK primaryKey, Set JavaDoc keys) throws GenericEntityException {
100         if (primaryKey == null) {
101             return null;
102         }
103         GenericValue genericValue = GenericValue.create(primaryKey);
104
105         genericDAO.partialSelect(genericValue, keys);
106         return genericValue;
107     }
108
109     /** Find a number of Generic Value objects by their Primary Keys, all at once
110      * This is done here for the DAO GenericHelper; for a client-server helper it
111      * would be done on the server side to reduce network round trips.
112      *@param primaryKeys A List of primary keys to find by.
113      *@return List of GenericValue objects corresponding to the passed primaryKey objects
114      */

115     public List JavaDoc findAllByPrimaryKeys(List JavaDoc primaryKeys) throws GenericEntityException {
116         if (primaryKeys == null) return null;
117         List JavaDoc results = new LinkedList JavaDoc();
118
119         Iterator JavaDoc pkiter = primaryKeys.iterator();
120
121         while (pkiter.hasNext()) {
122             GenericPK primaryKey = (GenericPK) pkiter.next();
123             GenericValue result = this.findByPrimaryKey(primaryKey);
124
125             if (result != null) results.add(result);
126         }
127         return results;
128     }
129
130     /** Remove a Generic Entity corresponding to the primaryKey
131      *@param primaryKey The primary key of the entity to remove.
132      *@return int representing number of rows effected by this operation
133      */

134     public int removeByPrimaryKey(GenericPK primaryKey) throws GenericEntityException {
135         if (primaryKey == null) return 0;
136         if (Debug.verboseOn()) Debug.logVerbose("Removing GenericPK: " + primaryKey.toString(), module);
137         return genericDAO.delete(primaryKey);
138     }
139
140     /** Finds GenericValues by the conditions specified in the EntityCondition object, the the EntityCondition javadoc for more details.
141      *@param modelEntity The ModelEntity of the Entity as defined in the entity XML file
142      *@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)
143      *@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)
144      *@param fieldsToSelect The fields of the named entity to get from the database; if empty or null all fields will be retreived
145      *@param orderBy The fields of the named entity to order the query by; optionally add a " ASC" for ascending or " DESC" for descending
146      *@param findOptions An instance of EntityFindOptions that specifies advanced query options. See the EntityFindOptions JavaDoc for more details.
147      *@return EntityListIterator representing the result of the query: NOTE THAT THIS MUST BE CLOSED WHEN YOU ARE
148      * DONE WITH IT, AND DON'T LEAVE IT OPEN TOO LONG BEACUSE IT WILL MAINTAIN A DATABASE CONNECTION.
149      */

150     public EntityListIterator findListIteratorByCondition(ModelEntity modelEntity, EntityCondition whereEntityCondition,
151         EntityCondition havingEntityCondition, Collection JavaDoc fieldsToSelect, List JavaDoc orderBy, EntityFindOptions findOptions)
152         throws GenericEntityException {
153         return genericDAO.selectListIteratorByCondition(modelEntity, whereEntityCondition, havingEntityCondition, fieldsToSelect, orderBy, findOptions);
154     }
155
156     public List JavaDoc findByMultiRelation(GenericValue value, ModelRelation modelRelationOne, ModelEntity modelEntityOne,
157         ModelRelation modelRelationTwo, ModelEntity modelEntityTwo, List JavaDoc orderBy) throws GenericEntityException {
158         return genericDAO.selectByMultiRelation(value, modelRelationOne, modelEntityOne, modelRelationTwo, modelEntityTwo, orderBy);
159     }
160
161     public long findCountByCondition(ModelEntity modelEntity, EntityCondition whereEntityCondition, EntityCondition havingEntityCondition) throws GenericEntityException {
162         return genericDAO.selectCountByCondition(modelEntity, whereEntityCondition, havingEntityCondition);
163     }
164
165     /** Removes/deletes Generic Entity records found by all the specified condition
166      *@param modelEntity The ModelEntity of the Entity as defined in the entity XML file
167      *@param condition The condition that restricts the list of removed values
168      *@return int representing number of rows effected by this operation
169      */

170     public int removeByCondition(ModelEntity modelEntity, EntityCondition condition) throws GenericEntityException {
171         if (modelEntity == null || condition == null) {
172             return 0;
173         }
174         return genericDAO.deleteByCondition(modelEntity, condition);
175     }
176
177     /** Store the Entity from the GenericValue to the persistent store
178      *@param value GenericValue instance containing the entity
179      *@return int representing number of rows effected by this operation
180      */

181     public int store(GenericValue value) throws GenericEntityException {
182         if (value == null) {
183             return 0;
184         }
185         return genericDAO.update(value);
186     }
187
188     /** Updates a group of values in a single pass.
189      *@param modelEntity The ModelEntity of the Entity as defined in the entity XML file
190      *@param fieldsToSet The fields of the named entity to set in the database
191      *@param condition The condition that restricts the list of removed values
192      *@return int representing number of rows effected by this operation
193      *@throws GenericEntityException
194      */

195     public int storeByCondition(ModelEntity modelEntity, Map JavaDoc fieldsToSet, EntityCondition condition) throws GenericEntityException {
196         if (modelEntity == null || condition == null) {
197             return 0;
198         }
199         return genericDAO.updateByCondition(modelEntity, fieldsToSet, condition);
200     }
201
202     /** Check the datasource to make sure the entity definitions are correct, optionally adding missing entities or fields on the server
203      *@param modelEntities Map of entityName names and ModelEntity values
204      *@param messages List to put any result messages in
205      *@param addMissing Flag indicating whether or not to add missing entities and fields on the server
206      */

207     public void checkDataSource(Map JavaDoc modelEntities, List JavaDoc messages, boolean addMissing) throws GenericEntityException {
208         genericDAO.checkDb(modelEntities, messages, addMissing);
209     }
210 }
211
Popular Tags