KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > GenericValue


1 /*
2  * $Id: GenericValue.java 5462 2005-08-05 18:35:48Z 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;
26
27
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import javolution.lang.Reusable;
33 import javolution.realtime.ObjectFactory;
34 import javolution.util.FastMap;
35
36 import org.ofbiz.base.util.Debug;
37 import org.ofbiz.base.util.UtilMisc;
38 import org.ofbiz.base.util.UtilValidate;
39 import org.ofbiz.entity.model.ModelEntity;
40 import org.ofbiz.entity.model.ModelKeyMap;
41 import org.ofbiz.entity.model.ModelRelation;
42 import org.ofbiz.entity.util.EntityUtil;
43
44
45 /**
46  * Generic Entity Value Object - Handles persisntence for any defined entity.
47  *
48  *@author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
49  *@author Eric Pabst
50  *@version $Rev: 5462 $
51  *@since 1.0
52  */

53 public class GenericValue extends GenericEntity implements Reusable {
54
55     public static final GenericValue NULL_VALUE = new NullGenericValue();
56
57     protected static final ObjectFactory genericValueFactory = new ObjectFactory() {
58         protected Object JavaDoc create() {
59             return new GenericValue();
60         }
61     };
62     
63     /** Hashtable to cache various related entity collections */
64     public transient Map JavaDoc relatedCache = null;
65
66     /** Hashtable to cache various related cardinality one entity collections */
67     public transient Map JavaDoc relatedOneCache = null;
68
69     /** This Map will contain the original field values from the database iff
70      * this GenericValue came from the database. If it was made manually it will
71      * no have this Map, ie it will be null to not take up memory.
72      */

73     protected Map JavaDoc originalDbValues = null;
74
75     protected GenericValue() { }
76
77     /** Creates new GenericValue */
78     public static GenericValue create(ModelEntity modelEntity) {
79         GenericValue newValue = (GenericValue) genericValueFactory.object();
80         newValue.init(modelEntity);
81         return newValue;
82     }
83
84     /** Creates new GenericValue from existing Map */
85     public static GenericValue create(ModelEntity modelEntity, Map JavaDoc fields) {
86         GenericValue newValue = (GenericValue) genericValueFactory.object();
87         newValue.init(modelEntity, fields);
88         return newValue;
89     }
90
91     /** Creates new GenericValue from existing GenericValue */
92     public static GenericValue create(GenericValue value) {
93         GenericValue newValue = (GenericValue) genericValueFactory.object();
94         newValue.init(value);
95         return newValue;
96     }
97
98     /** Creates new GenericValue from existing GenericValue */
99     public static GenericValue create(GenericPK primaryKey) {
100         GenericValue newValue = (GenericValue) genericValueFactory.object();
101         newValue.init(primaryKey);
102         return newValue;
103     }
104     
105     public void reset() {
106         // from GenericEntity
107
super.reset();
108
109         // from GenericValue
110
this.relatedCache = null;
111         this.relatedOneCache = null;
112         this.originalDbValues = null;
113     }
114
115     public void synchronizedWithDatasource() {
116         super.synchronizedWithDatasource();
117         this.copyOriginalDbValues();
118     }
119
120     public GenericValue create() throws GenericEntityException {
121         return this.getDelegator().create(this);
122     }
123
124     public void store() throws GenericEntityException {
125         this.getDelegator().store(this);
126     }
127
128     public void remove() throws GenericEntityException {
129         this.getDelegator().removeValue(this);
130     }
131
132     public void refresh() throws GenericEntityException {
133         this.getDelegator().refresh(this);
134     }
135
136     public void refreshFromCache() throws GenericEntityException {
137         this.getDelegator().refreshFromCache(this);
138     }
139
140     public boolean originalDbValuesAvailable() {
141         return this.originalDbValues != null ? true : false;
142     }
143
144     public Object JavaDoc getOriginalDbValue(String JavaDoc name) {
145         if (getModelEntity().getField(name) == null) {
146             throw new IllegalArgumentException JavaDoc("[GenericEntity.get] \"" + name + "\" is not a field of " + entityName);
147         }
148         if (originalDbValues == null) return null;
149         return originalDbValues.get(name);
150     }
151
152     /** This should only be called by the Entity Engine once a GenericValue has
153      * been read from the database so that we have a copy of the original field
154      * values from the Db.
155      */

156     public void copyOriginalDbValues() {
157         this.originalDbValues = FastMap.newInstance();
158         this.originalDbValues.putAll(this.fields);
159     }
160
161     /** Get the named Related Entity for the GenericValue from the persistent store
162      *@param relationName String containing the relation name which is the combination of relation.title and relation.rel-entity-name as specified in the entity XML definition file
163      *@return List of GenericValue instances as specified in the relation definition
164      */

165     public List JavaDoc getRelated(String JavaDoc relationName) throws GenericEntityException {
166         return this.getDelegator().getRelated(relationName, this);
167     }
168
169     /** Get the named Related Entity for the GenericValue from the persistent store
170      *@param relationName String containing the relation name which is the combination of relation.title and relation.rel-entity-name as specified in the entity XML definition file
171      * @param orderBy The fields of the named entity to order the query by; may be null;
172      * optionally add a " ASC" for ascending or " DESC" for descending
173      *@return List of GenericValue instances as specified in the relation definition
174      */

175     public List JavaDoc getRelated(String JavaDoc relationName, List JavaDoc orderBy) throws GenericEntityException {
176         return this.getDelegator().getRelated(relationName, null, orderBy, this);
177     }
178
179     /** Get the named Related Entity for the GenericValue from the persistent store
180      *@param relationName String containing the relation name which is the combination of relation.title and relation.rel-entity-name as specified in the entity XML definition file
181      * @param byAndFields the fields that must equal in order to keep; may be null
182      * @param orderBy The fields of the named entity to order the query by; may be null;
183      * optionally add a " ASC" for ascending or " DESC" for descending
184      *@return List of GenericValue instances as specified in the relation definition
185      */

186     public List JavaDoc getRelated(String JavaDoc relationName, Map JavaDoc byAndFields, List JavaDoc orderBy) throws GenericEntityException {
187         return this.getDelegator().getRelated(relationName, byAndFields, orderBy, this);
188     }
189
190     /** Get the named Related Entity for the GenericValue from the persistent
191      * store, looking first in the global generic cache (for the moment this isn't true, is same as EmbeddedCache variant)
192      *@param relationName String containing the relation name which is the combination of relation.title and relation.rel-entity-name as specified in the entity XML definition file
193      *@return List of GenericValue instances as specified in the relation definition
194      */

195     public List JavaDoc getRelatedCache(String JavaDoc relationName) throws GenericEntityException {
196         return this.getDelegator().getRelatedCache(relationName, this);
197     }
198
199     /**
200      * Get the named Related Entity for the GenericValue from the persistent store across another Relation.
201      * Helps to get related Values in a multi-to-multi relationship.
202      * @param relationNameOne String containing the relation name which is the
203      * combination of relation.title and relation.rel-entity-name as
204      * specified in the entity XML definition file, for first relation
205      * @param relationNameTwo String containing the relation name for second relation
206      * @param orderBy The fields of the named entity to order the query by; may be null;
207      * optionally add a " ASC" for ascending or " DESC" for descending
208      * @return List of GenericValue instances as specified in the relation definition
209      */

210     public List JavaDoc getRelatedMulti(String JavaDoc relationNameOne, String JavaDoc relationNameTwo, List JavaDoc orderBy) throws GenericEntityException {
211         return this.getDelegator().getMultiRelation(this, relationNameOne, relationNameTwo, orderBy);
212     }
213
214     /**
215      * Get the named Related Entity for the GenericValue from the persistent store across another Relation.
216      * Helps to get related Values in a multi-to-multi relationship.
217      * @param relationNameOne String containing the relation name which is the
218      * combination of relation.title and relation.rel-entity-name as
219      * specified in the entity XML definition file, for first relation
220      * @param relationNameTwo String containing the relation name for second relation
221      * @return List of GenericValue instances as specified in the relation definition
222      */

223     public List JavaDoc getRelatedMulti(String JavaDoc relationNameOne, String JavaDoc relationNameTwo) throws GenericEntityException {
224         return this.getDelegator().getMultiRelation(this, relationNameOne, relationNameTwo, null);
225     }
226
227     /** Get the named Related Entity for the GenericValue from the persistent
228      * store, looking first in the global generic cache (for the moment this isn't true, is same as EmbeddedCache variant)
229      *@param relationName String containing the relation name which is the combination of relation.title and relation.rel-entity-name as specified in the entity XML definition file
230      * @param byAndFields the fields that must equal in order to keep; may be null
231      * @param orderBy The fields of the named entity to order the query by; may be null;
232      * optionally add a " ASC" for ascending or " DESC" for descending
233      *@return List of GenericValue instances as specified in the relation definition
234      */

235     public List JavaDoc getRelatedCache(String JavaDoc relationName, Map JavaDoc byAndFields, List JavaDoc orderBy) throws GenericEntityException {
236         List JavaDoc col = getRelatedCache(relationName);
237
238         if (byAndFields != null) col = EntityUtil.filterByAnd(col, byAndFields);
239         if (UtilValidate.isNotEmpty(orderBy)) col = EntityUtil.orderBy(col, orderBy);
240         return col;
241     }
242
243     /** Get the named Related Entity for the GenericValue from the persistent
244      * store, looking first in the global generic cache (for the moment this isn't true, is same as EmbeddedCache variant)
245      *@param relationName String containing the relation name which is the combination of relation.title and relation.rel-entity-name as specified in the entity XML definition file
246      * @param orderBy The fields of the named entity to order the query by; may be null;
247      * optionally add a " ASC" for ascending or " DESC" for descending
248      *@return List of GenericValue instances as specified in the relation definition
249      */

250     public List JavaDoc getRelatedCache(String JavaDoc relationName, List JavaDoc orderBy) throws GenericEntityException {
251         return this.getRelatedCache(relationName, null, orderBy);
252     }
253
254     /** Get the named Related Entity for the GenericValue from the persistent
255      * store, looking first in a cache associated with this entity which is
256      * destroyed with this ValueObject when no longer used.
257      *@param relationName String containing the relation name which is the combination of relation.title and relation.rel-entity-name as specified in the entity XML definition file
258      *@return List of GenericValue instances as specified in the relation definition
259      */

260     public List JavaDoc getRelatedEmbeddedCache(String JavaDoc relationName) throws GenericEntityException {
261         if (relatedCache == null) relatedCache = FastMap.newInstance();
262         List JavaDoc col = (List JavaDoc) relatedCache.get(relationName);
263
264         if (col == null) {
265             col = getRelated(relationName);
266             relatedCache.put(relationName, col);
267         }
268         return col;
269     }
270
271     /** Get the named Related Entity for the GenericValue from the persistent
272      * store, looking first in a cache associated with this entity which is
273      * destroyed with this ValueObject when no longer used.
274      *@param relationName String containing the relation name which is the combination of relation.title and relation.rel-entity-name as specified in the entity XML definition file
275      * @param byAndFields the fields that must equal in order to keep; may be null
276      * @param orderBy The fields of the named entity to order the query by; may be null;
277      * optionally add a " ASC" for ascending or " DESC" for descending
278      *@return List of GenericValue instances as specified in the relation definition
279      */

280     public List JavaDoc getRelatedEmbeddedCache(String JavaDoc relationName, Map JavaDoc byAndFields, List JavaDoc orderBy) throws GenericEntityException {
281         List JavaDoc col = getRelatedEmbeddedCache(relationName);
282
283         if (byAndFields != null) col = EntityUtil.filterByAnd(col, byAndFields);
284         if (UtilValidate.isNotEmpty(orderBy)) col = EntityUtil.orderBy(col, orderBy);
285         return col;
286     }
287
288     public void removeRelatedEmbeddedCache(String JavaDoc relationName) {
289         if (relatedCache == null) return;
290         relatedCache.remove(relationName);
291     }
292
293     public void storeRelatedEmbeddedCache(String JavaDoc relationName, List JavaDoc col) {
294         if (relatedCache == null) relatedCache = FastMap.newInstance();
295         relatedCache.put(relationName, col);
296     }
297
298     public void storeRelatedEmbeddedCache(String JavaDoc relationName, GenericValue value) {
299         if (relatedCache == null) relatedCache = FastMap.newInstance();
300         relatedCache.put(relationName, UtilMisc.toList(value));
301     }
302
303     public void clearEmbeddedCache() {
304         relatedCache.clear();
305     }
306
307     /** Get the named Related Entity for the GenericValue from the persistent store
308      *@param relationName String containing the relation name which is the combination of relation.title and relation.rel-entity-name as specified in the entity XML definition file
309      *@return List of GenericValue instances as specified in the relation definition
310      */

311     public GenericValue getRelatedOne(String JavaDoc relationName) throws GenericEntityException {
312         return this.getDelegator().getRelatedOne(relationName, this);
313     }
314
315     /** Get the named Related Entity for the GenericValue from the persistent
316      * store, looking first in the global generic cache (for the moment this isn't true, is same as EmbeddedCache variant)
317      *@param relationName String containing the relation name which is the combination of relation.title and relation.rel-entity-name as specified in the entity XML definition file
318      *@return List of GenericValue instances as specified in the relation definition
319      */

320     public GenericValue getRelatedOneCache(String JavaDoc relationName) throws GenericEntityException {
321         return this.getDelegator().getRelatedOneCache(relationName, this);
322     }
323
324     /** Get the named Related Entity for the GenericValue from the persistent
325      * store, looking first in a cache associated with this entity which is
326      * destroyed with this ValueObject when no longer used.
327      *@param relationName String containing the relation name which is the combination of relation.title and relation.rel-entity-name as specified in the entity XML definition file
328      *@return List of GenericValue instances as specified in the relation definition
329      */

330     public GenericValue getRelatedOneEmbeddedCache(String JavaDoc relationName) throws GenericEntityException {
331         if (relatedOneCache == null) relatedOneCache = FastMap.newInstance();
332         GenericValue value = (GenericValue) relatedOneCache.get(relationName);
333
334         if (value == null) {
335             value = getRelatedOne(relationName);
336             if (value != null) relatedOneCache.put(relationName, value);
337         }
338         return value;
339     }
340
341     /** Get the named Related Entity for the GenericValue from the persistent store and filter it
342      *@param relationName String containing the relation name which is the combination of relation.title and relation.rel-entity-name as specified in the entity XML definition file
343      *@param fields the fields that must equal in order to keep
344      *@return List of GenericValue instances as specified in the relation definition
345      */

346     public List JavaDoc getRelatedByAnd(String JavaDoc relationName, Map JavaDoc fields) throws GenericEntityException {
347         return this.getDelegator().getRelatedByAnd(relationName, fields, this);
348     }
349
350     /** Get the named Related Entity for the GenericValue from the persistent
351      * store and filter it, looking first in the global generic cache (for the moment this isn't true, is same as EmbeddedCache variant)
352      *@param relationName String containing the relation name which is the combination of relation.title and relation.rel-entity-name as specified in the entity XML definition file
353      *@param fields the fields that must equal in order to keep
354      *@return List of GenericValue instances as specified in the relation definition
355      */

356     public List JavaDoc getRelatedByAndCache(String JavaDoc relationName, Map JavaDoc fields) throws GenericEntityException {
357         return EntityUtil.filterByAnd(this.getDelegator().getRelatedCache(relationName, this), fields);
358     }
359
360     /** Get the named Related Entity for the GenericValue from the persistent
361      * store and filter it, looking first in a cache associated with this entity which is
362      * destroyed with this ValueObject when no longer used.
363      *@param relationName String containing the relation name which is the combination of relation.title and relation.rel-entity-name as specified in the entity XML definition file
364      *@param fields the fields that must equal in order to keep
365      *@return List of GenericValue instances as specified in the relation definition
366      */

367     public List JavaDoc getRelatedByAndEmbeddedCache(String JavaDoc relationName, Map JavaDoc fields) throws GenericEntityException {
368         return EntityUtil.filterByAnd(getRelatedEmbeddedCache(relationName), fields);
369     }
370
371     /** Get the named Related Entity for the GenericValue from the persistent store and order it
372      *@param relationName String containing the relation name which is the combination of relation.title and relation.rel-entity-name as specified in the entity XML definition file
373      *@param orderBy the order that they should be returned
374      *@return List of GenericValue instances as specified in the relation definition
375      */

376     public List JavaDoc getRelatedOrderBy(String JavaDoc relationName, List JavaDoc orderBy) throws GenericEntityException {
377         return this.getDelegator().getRelatedOrderBy(relationName, orderBy, this);
378     }
379
380     /** Get the named Related Entity for the GenericValue from the persistent
381      * store and order it, looking first in the global generic cache (for the moment this isn't true, is same as EmbeddedCache variant)
382      *@param relationName String containing the relation name which is the combination of relation.title and relation.rel-entity-name as specified in the entity XML definition file
383      *@param orderBy the order that they should be returned
384      *@return List of GenericValue instances as specified in the relation definition
385      */

386     public List JavaDoc getRelatedOrderByCache(String JavaDoc relationName, List JavaDoc orderBy) throws GenericEntityException {
387         return EntityUtil.orderBy(this.getDelegator().getRelatedCache(relationName, this), orderBy);
388     }
389
390     /** Get the named Related Entity for the GenericValue from the persistent
391      * store and order it, looking first in a cache associated with this entity which is
392      * destroyed with this ValueObject when no longer used.
393      *@param relationName String containing the relation name which is the combination of relation.title and relation.rel-entity-name as specified in the entity XML definition file
394      *@param orderBy the order that they should be returned
395      *@return List of GenericValue instances as specified in the relation definition
396      */

397     public List JavaDoc getRelatedOrderByEmbeddedCache(String JavaDoc relationName, List JavaDoc orderBy) throws GenericEntityException {
398         return EntityUtil.orderBy(getRelatedEmbeddedCache(relationName), orderBy);
399     }
400
401     /** Remove the named Related Entity for the GenericValue from the persistent store
402      *@param relationName String containing the relation name which is the combination of relation.title and relation.rel-entity-name as specified in the entity XML definition file
403      */

404     public void removeRelated(String JavaDoc relationName) throws GenericEntityException {
405         this.getDelegator().removeRelated(relationName, this);
406     }
407
408     /** Get a dummy primary key for the named Related Entity for the GenericValue
409      * @param relationName String containing the relation name which is the
410      * combination of relation.title and relation.rel-entity-name as
411      * specified in the entity XML definition file
412      * @return GenericPK containing a possibly incomplete PrimaryKey object representing the related entity or entities
413      */

414     public GenericPK getRelatedDummyPK(String JavaDoc relationName) throws GenericEntityException {
415         return this.getDelegator().getRelatedDummyPK(relationName, null, this);
416     }
417
418     /** Get a dummy primary key for the named Related Entity for the GenericValue
419      * @param relationName String containing the relation name which is the
420      * combination of relation.title and relation.rel-entity-name as
421      * specified in the entity XML definition file
422      * @param byAndFields the fields that must equal in order to keep; may be null
423      * @return GenericPK containing a possibly incomplete PrimaryKey object representing the related entity or entities
424      */

425     public GenericPK getRelatedDummyPK(String JavaDoc relationName, Map JavaDoc byAndFields) throws GenericEntityException {
426         return this.getDelegator().getRelatedDummyPK(relationName, byAndFields, this);
427     }
428
429     /**
430      * Checks to see if all foreign key records exist in the database. Will create a dummy value for
431      * those missing when specified.
432      *
433      * @param insertDummy Create a dummy record using the provided fields
434      * @return true if all FKs exist (or when all missing are created)
435      * @throws GenericEntityException
436      */

437     public boolean checkFks(boolean insertDummy) throws GenericEntityException {
438         ModelEntity model = this.getModelEntity();
439         Iterator JavaDoc relItr = model.getRelationsIterator();
440         while (relItr.hasNext()) {
441             ModelRelation relation = (ModelRelation) relItr.next();
442             if ("one".equalsIgnoreCase(relation.getType())) {
443                 // see if the related value exists
444
Map JavaDoc fields = FastMap.newInstance();
445                 for (int i = 0; i < relation.getKeyMapsSize(); i++) {
446                     ModelKeyMap keyMap = relation.getKeyMap(i);
447                     fields.put(keyMap.getRelFieldName(), this.get(keyMap.getFieldName()));
448                 }
449                 long count = this.getDelegator().findCountByAnd(relation.getRelEntityName(), fields);
450                 if (count == 0) {
451                     if (insertDummy) {
452                         // create the new related value (dummy)
453
GenericValue newValue = this.getDelegator().makeValue(relation.getRelEntityName(), null);
454                         Iterator JavaDoc keyMapIter = relation.getKeyMapsIterator();
455                         boolean allFieldsSet = true;
456                         while (keyMapIter.hasNext()) {
457                             ModelKeyMap mkm = (ModelKeyMap) keyMapIter.next();
458                             if (this.get(mkm.getFieldName()) != null) {
459                                 newValue.set(mkm.getRelFieldName(), this.get(mkm.getFieldName()));
460                                 if (Debug.infoOn()) Debug.logInfo("Set [" + mkm.getRelFieldName() + "] to - " + this.get(mkm.getFieldName()), module);
461                             } else {
462                                 allFieldsSet = false;
463                             }
464                         }
465                         if (allFieldsSet) {
466                             if (Debug.infoOn()) Debug.logInfo("Creating place holder value : " + newValue, module);
467
468                             // check the FKs for the newly created entity
469
newValue.checkFks(true);
470                             newValue.create();
471                         }
472                     } else {
473                         return false;
474                     }
475                 }
476             }
477         }
478         return true;
479     }
480
481     /** Clones this GenericValue, this is a shallow clone & uses the default shallow HashMap clone
482      *@return Object that is a clone of this GenericValue
483      */

484     public Object JavaDoc clone() {
485         GenericValue newEntity = GenericValue.create(this);
486         newEntity.setDelegator(internalDelegator);
487         return newEntity;
488     }
489
490     protected static class NullGenericValue extends GenericValue implements NULL { };
491 }
492
Popular Tags