KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > cache > EntityListCache


1 /*
2  * $Id: EntityListCache.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.cache;
26
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.ofbiz.entity.condition.EntityCondition;
32 import org.ofbiz.entity.util.EntityUtil;
33
34 public class EntityListCache extends AbstractEntityConditionCache {
35
36     public static final String JavaDoc module = EntityListCache.class.getName();
37
38     public EntityListCache(String JavaDoc delegatorName) {
39         super(delegatorName, "entity-list");
40     }
41
42     public List JavaDoc get(String JavaDoc entityName, EntityCondition condition) {
43         return this.get(entityName, condition, null);
44     }
45
46     public List JavaDoc get(String JavaDoc entityName, EntityCondition condition, List JavaDoc orderBy) {
47         Map JavaDoc conditionCache = getConditionCache(entityName, condition);
48         if (conditionCache == null) return null;
49         Object JavaDoc orderByKey = getOrderByKey(orderBy);
50         List JavaDoc valueList = (List JavaDoc) conditionCache.get(orderByKey);
51         if (valueList == null) {
52             // the valueList was not found for the given ordering, so grab the first one and order it in memory
53
Iterator JavaDoc it = conditionCache.values().iterator();
54             if (it.hasNext()) valueList = (List JavaDoc) it.next();
55             
56             synchronized (conditionCache) {
57                 if (valueList != null) {
58                     valueList = EntityUtil.orderBy(valueList, orderBy);
59                     conditionCache.put(orderByKey, valueList);
60                 }
61             }
62         }
63         return valueList;
64     }
65
66     public void put(String JavaDoc entityName, EntityCondition condition, List JavaDoc entities) {
67         this.put(entityName, condition, null, entities);
68     }
69
70     public List JavaDoc put(String JavaDoc entityName, EntityCondition condition, List JavaDoc orderBy, List JavaDoc entities) {
71         return (List JavaDoc) super.put(entityName, getFrozenConditionKey(condition), getOrderByKey(orderBy), entities);
72     }
73
74     public List JavaDoc remove(String JavaDoc entityName, EntityCondition condition, List JavaDoc orderBy) {
75         return (List JavaDoc) super.remove(entityName, condition, getOrderByKey(orderBy));
76     }
77
78     public static final Object JavaDoc getOrderByKey(List JavaDoc orderBy) {
79         return orderBy != null ? (Object JavaDoc) orderBy : "{null}";
80     }
81 }
82
Popular Tags