KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: EntityCache.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
29 import org.ofbiz.base.util.Debug;
30 import org.ofbiz.base.util.cache.UtilCache;
31 import org.ofbiz.base.util.cache.CacheLine;
32 import org.ofbiz.entity.GenericEntity;
33 import org.ofbiz.entity.GenericPK;
34 import org.ofbiz.entity.condition.EntityCondition;
35
36 public class EntityCache extends AbstractCache {
37     public static final String JavaDoc module = EntityCache.class.getName();
38
39     public EntityCache(String JavaDoc delegatorName) {
40         super(delegatorName, "entity");
41     }
42
43     public GenericEntity get(GenericPK pk) {
44         UtilCache entityCache = getCache(pk.getEntityName());
45         if (entityCache == null) return null;
46         return (GenericEntity) entityCache.get(pk);
47     }
48
49     public GenericEntity put(GenericEntity entity) {
50         if (entity == null) return null;
51         return put(entity.getPrimaryKey(), entity);
52     }
53
54     public GenericEntity put(GenericPK pk, GenericEntity entity) {
55         if (pk.getModelEntity().getNeverCache()) {
56             Debug.logWarning("Tried to put a value of the " + pk.getEntityName() + " entity in the BY PRIMARY KEY cache but this entity has never-cache set to true, not caching.", module);
57             return null;
58         }
59
60         if (entity == null) {
61             entity = GenericEntity.NULL_ENTITY;
62         } else {
63             // before going into the cache, make this value immutable
64
entity.setImmutable();
65         }
66         UtilCache entityCache = getOrCreateCache(pk.getEntityName());
67         return (GenericEntity)entityCache.put(pk, entity);
68     }
69
70     public void remove(String JavaDoc entityName, EntityCondition condition) {
71         UtilCache entityCache = getCache(entityName);
72         if (entityCache == null) return;
73         Iterator JavaDoc it = entityCache.getCacheLineValues().iterator();
74         while (it.hasNext()) {
75             CacheLine line = (CacheLine) it.next();
76             if (entityCache.hasExpired(line)) continue;
77             GenericEntity entity = (GenericEntity) line.getValue();
78             if (entity == null) continue;
79             if (condition.entityMatches(entity)) it.remove();
80         }
81     }
82
83     public GenericEntity remove(GenericEntity entity) {
84         return remove(entity.getPrimaryKey());
85     }
86
87     public GenericEntity remove(GenericPK pk) {
88         UtilCache entityCache = getCache(pk.getEntityName());
89         if (Debug.verboseOn()) Debug.logVerbose("Removing from EntityCache with PK [" + pk + "], will remove from this cache: " + (entityCache == null ? "[No cache found to remove from]" : entityCache.getName()), module);
90         if (entityCache == null) return null;
91         GenericEntity retVal = (GenericEntity) entityCache.remove(pk);
92         if (Debug.verboseOn()) Debug.logVerbose("Removing from EntityCache with PK [" + pk + "], found this in the cache: " + retVal, module);
93         return retVal;
94     }
95 }
96
Popular Tags