001: /*
002: * $Id: GenericValue.java,v 1.4 2003/12/05 20:09:55 ajzeneski Exp $
003: *
004: * Copyright (c) 2001 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;
026:
027: import java.util.HashMap;
028: import java.util.Hashtable;
029: import java.util.List;
030: import java.util.Map;
031:
032: import org.ofbiz.base.util.UtilValidate;
033: import org.ofbiz.entity.model.ModelEntity;
034: import org.ofbiz.entity.util.EntityUtil;
035:
036: /**
037: * Generic Entity Value Object - Handles persisntence for any defined entity.
038: *
039: *@author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
040: *@author Eric Pabst
041: *@version $Revision: 1.4 $
042: *@since 1.0
043: */
044: public class GenericValue extends GenericEntity {
045:
046: /** Hashtable to cache various related entity collections */
047: public transient Map relatedCache = null;
048:
049: /** Hashtable to cache various related cardinality one entity collections */
050: public transient Map relatedOneCache = null;
051:
052: /** This Map will contain the original field values from the database iff
053: * this GenericValue came from the database. If it was made manually it will
054: * no have this Map, ie it will be null to not take up memory.
055: */
056: protected Map originalDbValues = null;
057:
058: /** Creates new GenericValue */
059: public GenericValue(ModelEntity modelEntity) {
060: super (modelEntity);
061: }
062:
063: /** Creates new GenericValue from existing Map */
064: public GenericValue(ModelEntity modelEntity, Map fields) {
065: super (modelEntity, fields);
066: }
067:
068: /** Creates new GenericValue from existing GenericValue */
069: public GenericValue(GenericValue value) {
070: super (value);
071: }
072:
073: /** Creates new GenericValue from existing GenericValue */
074: public GenericValue(GenericPK primaryKey) {
075: super (primaryKey);
076: }
077:
078: public void synchronizedWithDatasource() {
079: super .synchronizedWithDatasource();
080: this .copyOriginalDbValues();
081: }
082:
083: public GenericValue create() throws GenericEntityException {
084: return this .getDelegator().create(this );
085: }
086:
087: public void store() throws GenericEntityException {
088: this .getDelegator().store(this );
089: }
090:
091: public void remove() throws GenericEntityException {
092: this .getDelegator().removeValue(this );
093: }
094:
095: public void refresh() throws GenericEntityException {
096: this .getDelegator().refresh(this );
097: }
098:
099: public boolean originalDbValuesAvailable() {
100: return this .originalDbValues != null ? true : false;
101: }
102:
103: public Object getOriginalDbValue(String name) {
104: if (getModelEntity().getField(name) == null) {
105: throw new IllegalArgumentException("[GenericEntity.get] \""
106: + name + "\" is not a field of " + entityName);
107: }
108: return originalDbValues.get(name);
109: }
110:
111: /** This should only be called by the Entity Engine once a GenericValue has
112: * been read from the database so that we have a copy of the original field
113: * values from the Db.
114: */
115: public void copyOriginalDbValues() {
116: this .originalDbValues = new HashMap(this .fields);
117: }
118:
119: /** Get the named Related Entity for the GenericValue from the persistent store
120: *@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
121: *@return List of GenericValue instances as specified in the relation definition
122: */
123: public List getRelated(String relationName)
124: throws GenericEntityException {
125: return this .getDelegator().getRelated(relationName, this );
126: }
127:
128: /** Get the named Related Entity for the GenericValue from the persistent store
129: *@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
130: * @param orderBy The fields of the named entity to order the query by; may be null;
131: * optionally add a " ASC" for ascending or " DESC" for descending
132: *@return List of GenericValue instances as specified in the relation definition
133: */
134: public List getRelated(String relationName, List orderBy)
135: throws GenericEntityException {
136: return this .getDelegator().getRelated(relationName, null,
137: orderBy, this );
138: }
139:
140: /** Get the named Related Entity for the GenericValue from the persistent store
141: *@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
142: * @param byAndFields the fields that must equal in order to keep; may be null
143: * @param orderBy The fields of the named entity to order the query by; may be null;
144: * optionally add a " ASC" for ascending or " DESC" for descending
145: *@return List of GenericValue instances as specified in the relation definition
146: */
147: public List getRelated(String relationName, Map byAndFields,
148: List orderBy) throws GenericEntityException {
149: return this .getDelegator().getRelated(relationName,
150: byAndFields, orderBy, this );
151: }
152:
153: /** Get the named Related Entity for the GenericValue from the persistent
154: * store, looking first in the global generic cache (for the moment this isn't true, is same as EmbeddedCache variant)
155: *@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
156: *@return List of GenericValue instances as specified in the relation definition
157: */
158: public List getRelatedCache(String relationName)
159: throws GenericEntityException {
160: return this .getDelegator().getRelatedCache(relationName, this );
161: }
162:
163: /**
164: * Get the named Related Entity for the GenericValue from the persistent store across another Relation.
165: * Helps to get related Values in a multi-to-multi relationship.
166: * @param relationNameOne String containing the relation name which is the
167: * combination of relation.title and relation.rel-entity-name as
168: * specified in the entity XML definition file, for first relation
169: * @param relationNameTwo String containing the relation name for second relation
170: * @param orderBy The fields of the named entity to order the query by; may be null;
171: * optionally add a " ASC" for ascending or " DESC" for descending
172: * @return List of GenericValue instances as specified in the relation definition
173: */
174: public List getRelatedMulti(String relationNameOne,
175: String relationNameTwo, List orderBy)
176: throws GenericEntityException {
177: return this .getDelegator().getMultiRelation(this ,
178: relationNameOne, relationNameTwo, orderBy);
179: }
180:
181: /**
182: * Get the named Related Entity for the GenericValue from the persistent store across another Relation.
183: * Helps to get related Values in a multi-to-multi relationship.
184: * @param relationNameOne String containing the relation name which is the
185: * combination of relation.title and relation.rel-entity-name as
186: * specified in the entity XML definition file, for first relation
187: * @param relationNameTwo String containing the relation name for second relation
188: * @return List of GenericValue instances as specified in the relation definition
189: */
190: public List getRelatedMulti(String relationNameOne,
191: String relationNameTwo) throws GenericEntityException {
192: return this .getDelegator().getMultiRelation(this ,
193: relationNameOne, relationNameTwo, null);
194: }
195:
196: /** Get the named Related Entity for the GenericValue from the persistent
197: * store, looking first in the global generic cache (for the moment this isn't true, is same as EmbeddedCache variant)
198: *@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
199: * @param byAndFields the fields that must equal in order to keep; may be null
200: * @param orderBy The fields of the named entity to order the query by; may be null;
201: * optionally add a " ASC" for ascending or " DESC" for descending
202: *@return List of GenericValue instances as specified in the relation definition
203: */
204: public List getRelatedCache(String relationName, Map byAndFields,
205: List orderBy) throws GenericEntityException {
206: List col = getRelatedCache(relationName);
207:
208: if (byAndFields != null)
209: col = EntityUtil.filterByAnd(col, byAndFields);
210: if (UtilValidate.isNotEmpty(orderBy))
211: col = EntityUtil.orderBy(col, orderBy);
212: return col;
213: }
214:
215: /** Get the named Related Entity for the GenericValue from the persistent
216: * store, looking first in the global generic cache (for the moment this isn't true, is same as EmbeddedCache variant)
217: *@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
218: * @param orderBy The fields of the named entity to order the query by; may be null;
219: * optionally add a " ASC" for ascending or " DESC" for descending
220: *@return List of GenericValue instances as specified in the relation definition
221: */
222: public List getRelatedCache(String relationName, List orderBy)
223: throws GenericEntityException {
224: return this .getRelatedCache(relationName, null, orderBy);
225: }
226:
227: /** Get the named Related Entity for the GenericValue from the persistent
228: * store, looking first in a cache associated with this entity which is
229: * destroyed with this ValueObject when no longer used.
230: *@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
231: *@return List of GenericValue instances as specified in the relation definition
232: */
233: public List getRelatedEmbeddedCache(String relationName)
234: throws GenericEntityException {
235: if (relatedCache == null)
236: relatedCache = new Hashtable();
237: List col = (List) relatedCache.get(relationName);
238:
239: if (col == null) {
240: col = getRelated(relationName);
241: relatedCache.put(relationName, col);
242: }
243: return col;
244: }
245:
246: /** Get the named Related Entity for the GenericValue from the persistent
247: * store, looking first in a cache associated with this entity which is
248: * destroyed with this ValueObject when no longer used.
249: *@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
250: * @param byAndFields the fields that must equal in order to keep; may be null
251: * @param orderBy The fields of the named entity to order the query by; may be null;
252: * optionally add a " ASC" for ascending or " DESC" for descending
253: *@return List of GenericValue instances as specified in the relation definition
254: */
255: public List getRelatedEmbeddedCache(String relationName,
256: Map byAndFields, List orderBy)
257: throws GenericEntityException {
258: List col = getRelatedEmbeddedCache(relationName);
259:
260: if (byAndFields != null)
261: col = EntityUtil.filterByAnd(col, byAndFields);
262: if (UtilValidate.isNotEmpty(orderBy))
263: col = EntityUtil.orderBy(col, orderBy);
264: return col;
265: }
266:
267: /** Get the named Related Entity for the GenericValue from the persistent store
268: *@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
269: *@return List of GenericValue instances as specified in the relation definition
270: */
271: public GenericValue getRelatedOne(String relationName)
272: throws GenericEntityException {
273: return this .getDelegator().getRelatedOne(relationName, this );
274: }
275:
276: /** Get the named Related Entity for the GenericValue from the persistent
277: * store, looking first in the global generic cache (for the moment this isn't true, is same as EmbeddedCache variant)
278: *@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
279: *@return List of GenericValue instances as specified in the relation definition
280: */
281: public GenericValue getRelatedOneCache(String relationName)
282: throws GenericEntityException {
283: return this .getDelegator().getRelatedOneCache(relationName,
284: this );
285: }
286:
287: /** Get the named Related Entity for the GenericValue from the persistent
288: * store, looking first in a cache associated with this entity which is
289: * destroyed with this ValueObject when no longer used.
290: *@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
291: *@return List of GenericValue instances as specified in the relation definition
292: */
293: public GenericValue getRelatedOneEmbeddedCache(String relationName)
294: throws GenericEntityException {
295: if (relatedOneCache == null)
296: relatedOneCache = new Hashtable();
297: GenericValue value = (GenericValue) relatedOneCache
298: .get(relationName);
299:
300: if (value == null) {
301: value = getRelatedOne(relationName);
302: if (value != null)
303: relatedOneCache.put(relationName, value);
304: }
305: return value;
306: }
307:
308: /** Get the named Related Entity for the GenericValue from the persistent store and filter it
309: *@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
310: *@param fields the fields that must equal in order to keep
311: *@return List of GenericValue instances as specified in the relation definition
312: */
313: public List getRelatedByAnd(String relationName, Map fields)
314: throws GenericEntityException {
315: return this .getDelegator().getRelatedByAnd(relationName,
316: fields, this );
317: }
318:
319: /** Get the named Related Entity for the GenericValue from the persistent
320: * store and filter it, looking first in the global generic cache (for the moment this isn't true, is same as EmbeddedCache variant)
321: *@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
322: *@param fields the fields that must equal in order to keep
323: *@return List of GenericValue instances as specified in the relation definition
324: */
325: public List getRelatedByAndCache(String relationName, Map fields)
326: throws GenericEntityException {
327: return EntityUtil.filterByAnd(this .getDelegator()
328: .getRelatedCache(relationName, this ), fields);
329: }
330:
331: /** Get the named Related Entity for the GenericValue from the persistent
332: * store and filter it, looking first in a cache associated with this entity which is
333: * destroyed with this ValueObject when no longer used.
334: *@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
335: *@param fields the fields that must equal in order to keep
336: *@return List of GenericValue instances as specified in the relation definition
337: */
338: public List getRelatedByAndEmbeddedCache(String relationName,
339: Map fields) throws GenericEntityException {
340: return EntityUtil.filterByAnd(
341: getRelatedEmbeddedCache(relationName), fields);
342: }
343:
344: /** Get the named Related Entity for the GenericValue from the persistent store and order it
345: *@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
346: *@param orderBy the order that they should be returned
347: *@return List of GenericValue instances as specified in the relation definition
348: */
349: public List getRelatedOrderBy(String relationName, List orderBy)
350: throws GenericEntityException {
351: return this .getDelegator().getRelatedOrderBy(relationName,
352: orderBy, this );
353: }
354:
355: /** Get the named Related Entity for the GenericValue from the persistent
356: * store and order it, looking first in the global generic cache (for the moment this isn't true, is same as EmbeddedCache variant)
357: *@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
358: *@param orderBy the order that they should be returned
359: *@return List of GenericValue instances as specified in the relation definition
360: */
361: public List getRelatedOrderByCache(String relationName, List orderBy)
362: throws GenericEntityException {
363: return EntityUtil.orderBy(this .getDelegator().getRelatedCache(
364: relationName, this ), orderBy);
365: }
366:
367: /** Get the named Related Entity for the GenericValue from the persistent
368: * store and order it, looking first in a cache associated with this entity which is
369: * destroyed with this ValueObject when no longer used.
370: *@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
371: *@param orderBy the order that they should be returned
372: *@return List of GenericValue instances as specified in the relation definition
373: */
374: public List getRelatedOrderByEmbeddedCache(String relationName,
375: List orderBy) throws GenericEntityException {
376: return EntityUtil.orderBy(
377: getRelatedEmbeddedCache(relationName), orderBy);
378: }
379:
380: /** Remove the named Related Entity for the GenericValue from the persistent store
381: *@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
382: */
383: public void removeRelated(String relationName)
384: throws GenericEntityException {
385: this .getDelegator().removeRelated(relationName, this );
386: }
387:
388: /** Get a dummy primary key for the named Related Entity for the GenericValue
389: * @param relationName String containing the relation name which is the
390: * combination of relation.title and relation.rel-entity-name as
391: * specified in the entity XML definition file
392: * @return GenericPK containing a possibly incomplete PrimaryKey object representing the related entity or entities
393: */
394: public GenericPK getRelatedDummyPK(String relationName)
395: throws GenericEntityException {
396: return this .getDelegator().getRelatedDummyPK(relationName,
397: null, this );
398: }
399:
400: /** Get a dummy primary key for the named Related Entity for the GenericValue
401: * @param relationName String containing the relation name which is the
402: * combination of relation.title and relation.rel-entity-name as
403: * specified in the entity XML definition file
404: * @param byAndFields the fields that must equal in order to keep; may be null
405: * @return GenericPK containing a possibly incomplete PrimaryKey object representing the related entity or entities
406: */
407: public GenericPK getRelatedDummyPK(String relationName,
408: Map byAndFields) throws GenericEntityException {
409: return this .getDelegator().getRelatedDummyPK(relationName,
410: byAndFields, this );
411: }
412:
413: /** Clones this GenericValue, this is a shallow clone & uses the default shallow HashMap clone
414: *@return Object that is a clone of this GenericValue
415: */
416: public Object clone() {
417: GenericValue newEntity = new GenericValue(this);
418:
419: newEntity.setDelegator(internalDelegator);
420: return newEntity;
421: }
422: }
|