001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.entity.datasource;
019:
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.LinkedList;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.Set;
026:
027: import org.ofbiz.base.util.Debug;
028: import org.ofbiz.entity.GenericEntityException;
029: import org.ofbiz.entity.GenericPK;
030: import org.ofbiz.entity.GenericValue;
031: import org.ofbiz.entity.condition.EntityCondition;
032: import org.ofbiz.entity.model.ModelEntity;
033: import org.ofbiz.entity.model.ModelRelation;
034: import org.ofbiz.entity.util.EntityFindOptions;
035: import org.ofbiz.entity.util.EntityListIterator;
036:
037: /**
038: * Generic Entity Helper Class
039: *
040: */
041: public class GenericHelperDAO implements GenericHelper {
042:
043: public static final String module = GenericHelperDAO.class
044: .getName();
045:
046: protected GenericDAO genericDAO;
047: protected String helperName;
048:
049: public GenericHelperDAO(String helperName) {
050: this .helperName = helperName;
051: genericDAO = GenericDAO.getGenericDAO(helperName);
052: }
053:
054: public String getHelperName() {
055: return helperName;
056: }
057:
058: /** Creates a Entity in the form of a GenericValue and write it to the database
059: *@return GenericValue instance containing the new instance
060: */
061: public GenericValue create(GenericValue value)
062: throws GenericEntityException {
063: if (value == null) {
064: return null;
065: }
066: int retVal = genericDAO.insert(value);
067: if (Debug.verboseOn())
068: Debug.logVerbose("Insert Return Value : " + retVal, module);
069: return value;
070: }
071:
072: /** Find a Generic Entity by its Primary Key
073: *@param primaryKey The primary key to find by.
074: *@return The GenericValue corresponding to the primaryKey
075: */
076: public GenericValue findByPrimaryKey(GenericPK primaryKey)
077: throws GenericEntityException {
078: if (primaryKey == null) {
079: return null;
080: }
081: GenericValue genericValue = GenericValue.create(primaryKey);
082:
083: genericDAO.select(genericValue);
084: return genericValue;
085: }
086:
087: /** Find a Generic Entity by its Primary Key and only returns the values requested by the passed keys (names)
088: *@param primaryKey The primary key to find by.
089: *@param keys The keys, or names, of the values to retrieve; only these values will be retrieved
090: *@return The GenericValue corresponding to the primaryKey
091: */
092: public GenericValue findByPrimaryKeyPartial(GenericPK primaryKey,
093: Set keys) throws GenericEntityException {
094: if (primaryKey == null) {
095: return null;
096: }
097: GenericValue genericValue = GenericValue.create(primaryKey);
098:
099: genericDAO.partialSelect(genericValue, keys);
100: return genericValue;
101: }
102:
103: /** Find a number of Generic Value objects by their Primary Keys, all at once
104: * This is done here for the DAO GenericHelper; for a client-server helper it
105: * would be done on the server side to reduce network round trips.
106: *@param primaryKeys A List of primary keys to find by.
107: *@return List of GenericValue objects corresponding to the passed primaryKey objects
108: */
109: public List findAllByPrimaryKeys(List primaryKeys)
110: throws GenericEntityException {
111: if (primaryKeys == null)
112: return null;
113: List results = new LinkedList();
114:
115: Iterator pkiter = primaryKeys.iterator();
116:
117: while (pkiter.hasNext()) {
118: GenericPK primaryKey = (GenericPK) pkiter.next();
119: GenericValue result = this .findByPrimaryKey(primaryKey);
120:
121: if (result != null)
122: results.add(result);
123: }
124: return results;
125: }
126:
127: /** Remove a Generic Entity corresponding to the primaryKey
128: *@param primaryKey The primary key of the entity to remove.
129: *@return int representing number of rows effected by this operation
130: */
131: public int removeByPrimaryKey(GenericPK primaryKey)
132: throws GenericEntityException {
133: if (primaryKey == null)
134: return 0;
135: if (Debug.verboseOn())
136: Debug.logVerbose("Removing GenericPK: "
137: + primaryKey.toString(), module);
138: return genericDAO.delete(primaryKey);
139: }
140:
141: /** Finds GenericValues by the conditions specified in the EntityCondition object, the the EntityCondition javadoc for more details.
142: *@param modelEntity The ModelEntity of the Entity as defined in the entity XML file
143: *@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)
144: *@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)
145: *@param fieldsToSelect The fields of the named entity to get from the database; if empty or null all fields will be retreived
146: *@param orderBy The fields of the named entity to order the query by; optionally add a " ASC" for ascending or " DESC" for descending
147: *@param findOptions An instance of EntityFindOptions that specifies advanced query options. See the EntityFindOptions JavaDoc for more details.
148: *@return EntityListIterator representing the result of the query: NOTE THAT THIS MUST BE CLOSED WHEN YOU ARE
149: * DONE WITH IT, AND DON'T LEAVE IT OPEN TOO LONG BEACUSE IT WILL MAINTAIN A DATABASE CONNECTION.
150: */
151: public EntityListIterator findListIteratorByCondition(
152: ModelEntity modelEntity,
153: EntityCondition whereEntityCondition,
154: EntityCondition havingEntityCondition,
155: Collection fieldsToSelect, List orderBy,
156: EntityFindOptions findOptions)
157: throws GenericEntityException {
158: return genericDAO.selectListIteratorByCondition(modelEntity,
159: whereEntityCondition, havingEntityCondition,
160: fieldsToSelect, orderBy, findOptions);
161: }
162:
163: public List findByMultiRelation(GenericValue value,
164: ModelRelation modelRelationOne, ModelEntity modelEntityOne,
165: ModelRelation modelRelationTwo, ModelEntity modelEntityTwo,
166: List orderBy) throws GenericEntityException {
167: return genericDAO.selectByMultiRelation(value,
168: modelRelationOne, modelEntityOne, modelRelationTwo,
169: modelEntityTwo, orderBy);
170: }
171:
172: public long findCountByCondition(ModelEntity modelEntity,
173: EntityCondition whereEntityCondition,
174: EntityCondition havingEntityCondition,
175: EntityFindOptions findOptions)
176: throws GenericEntityException {
177: return genericDAO.selectCountByCondition(modelEntity,
178: whereEntityCondition, havingEntityCondition,
179: findOptions);
180: }
181:
182: /** Removes/deletes Generic Entity records found by all the specified condition
183: *@param modelEntity The ModelEntity of the Entity as defined in the entity XML file
184: *@param condition The condition that restricts the list of removed values
185: *@return int representing number of rows effected by this operation
186: */
187: public int removeByCondition(ModelEntity modelEntity,
188: EntityCondition condition) throws GenericEntityException {
189: if (modelEntity == null || condition == null) {
190: return 0;
191: }
192: return genericDAO.deleteByCondition(modelEntity, condition);
193: }
194:
195: /** Store the Entity from the GenericValue to the persistent store
196: *@param value GenericValue instance containing the entity
197: *@return int representing number of rows effected by this operation
198: */
199: public int store(GenericValue value) throws GenericEntityException {
200: if (value == null) {
201: return 0;
202: }
203: return genericDAO.update(value);
204: }
205:
206: /** Updates a group of values in a single pass.
207: *@param modelEntity The ModelEntity of the Entity as defined in the entity XML file
208: *@param fieldsToSet The fields of the named entity to set in the database
209: *@param condition The condition that restricts the list of removed values
210: *@return int representing number of rows effected by this operation
211: *@throws GenericEntityException
212: */
213: public int storeByCondition(ModelEntity modelEntity,
214: Map fieldsToSet, EntityCondition condition)
215: throws GenericEntityException {
216: if (modelEntity == null || condition == null) {
217: return 0;
218: }
219: return genericDAO.updateByCondition(modelEntity, fieldsToSet,
220: condition);
221: }
222:
223: /** Check the datasource to make sure the entity definitions are correct, optionally adding missing entities or fields on the server
224: *@param modelEntities Map of entityName names and ModelEntity values
225: *@param messages List to put any result messages in
226: *@param addMissing Flag indicating whether or not to add missing entities and fields on the server
227: */
228: public void checkDataSource(Map modelEntities, List messages,
229: boolean addMissing) throws GenericEntityException {
230: genericDAO.checkDb(modelEntities, messages, addMissing);
231: }
232: }
|