KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: AbstractEntityConditionCache.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.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.ofbiz.base.util.Debug;
33 import org.ofbiz.base.util.UtilMisc;
34 import org.ofbiz.base.util.cache.UtilCache;
35 import org.ofbiz.entity.GenericEntity;
36 import org.ofbiz.entity.GenericPK;
37 import org.ofbiz.entity.GenericValue;
38 import org.ofbiz.entity.condition.EntityCondition;
39 import org.ofbiz.entity.model.ModelEntity;
40
41 public abstract class AbstractEntityConditionCache extends AbstractCache {
42
43     public static final String JavaDoc module = AbstractEntityConditionCache.class.getName();
44
45     protected AbstractEntityConditionCache(String JavaDoc delegatorName, String JavaDoc id) {
46         super(delegatorName, id);
47     }
48
49     protected Object JavaDoc get(String JavaDoc entityName, EntityCondition condition, Object JavaDoc key) {
50         Map JavaDoc conditionCache = getConditionCache(entityName, condition);
51         if (conditionCache == null) return null;
52         // the following line was synchronized, but for pretty good safety and better performance, only syncrhnizing the put; synchronized (conditionCache) {
53
return conditionCache.get(key);
54     }
55
56     protected Object JavaDoc put(String JavaDoc entityName, EntityCondition condition, Object JavaDoc key, Object JavaDoc value) {
57         ModelEntity entity = this.getDelegator().getModelEntity(entityName);
58         if (entity.getNeverCache()) {
59             Debug.logWarning("Tried to put a value of the " + entityName + " entity in the cache but this entity has never-cache set to true, not caching.", module);
60             return null;
61         }
62         
63         Map JavaDoc conditionCache = getOrCreateConditionCache(entityName, condition);
64         synchronized (conditionCache) {
65             return conditionCache.put(key, value);
66         }
67     }
68
69     public void remove(String JavaDoc entityName, EntityCondition condition) {
70         UtilCache cache = getCache(entityName);
71         if (cache == null) return;
72         cache.remove(condition);
73     }
74
75     protected Object JavaDoc remove(String JavaDoc entityName, EntityCondition condition, Object JavaDoc key) {
76         Map JavaDoc conditionCache = getConditionCache(entityName, condition);
77         if (conditionCache == null) return null;
78         synchronized (conditionCache) {
79             return conditionCache.remove(key);
80          }
81     }
82
83     public static final EntityCondition getConditionKey(EntityCondition condition) {
84         return condition != null ? condition : null;
85     }
86
87     public static final EntityCondition getFrozenConditionKey(EntityCondition condition) {
88         EntityCondition frozenCondition = condition != null ? condition.freeze() : null;
89         // This is no longer needed, fixed issue with unequal conditions after freezing
90
//if (condition != null) {
91
// if (!condition.equals(frozenCondition)) {
92
// Debug.logWarning("Frozen condition does not equal condition:\n -=-=-=-Original=" + condition + "\n -=-=-=-Frozen=" + frozenCondition, module);
93
// Debug.logWarning("Frozen condition not equal info: condition class=" + condition.getClass().getName() + "; frozenCondition class=" + frozenCondition.getClass().getName(), module);
94
// }
95
//}
96
return frozenCondition;
97     }
98
99     protected Map JavaDoc getConditionCache(String JavaDoc entityName, EntityCondition condition) {
100         UtilCache cache = getCache(entityName);
101         if (cache == null) return null;
102         return (Map JavaDoc) cache.get(getConditionKey(condition));
103     }
104
105     protected Map JavaDoc getOrCreateConditionCache(String JavaDoc entityName, EntityCondition condition) {
106         UtilCache utilCache = getOrCreateCache(entityName);
107         Object JavaDoc conditionKey = getConditionKey(condition);
108         Map JavaDoc conditionCache = (Map JavaDoc) utilCache.get(conditionKey);
109         if (conditionCache == null) {
110             conditionCache = new HashMap JavaDoc();
111             utilCache.put(conditionKey, conditionCache);
112         }
113         return conditionCache;
114     }
115
116     protected static final boolean isNull(Map JavaDoc value) {
117         return value == null || value == GenericEntity.NULL_ENTITY || value == GenericValue.NULL_VALUE;
118     }
119
120     protected ModelEntity getModelCheckValid(GenericEntity oldEntity, GenericEntity newEntity) {
121         ModelEntity model;
122         if (!isNull(newEntity)) {
123             model = newEntity.getModelEntity();
124             String JavaDoc entityName = model.getEntityName();
125             if (oldEntity != null && !entityName.equals(oldEntity.getEntityName())) {
126                 throw new IllegalArgumentException JavaDoc("internal error: storeHook called with 2 different entities(old=" + oldEntity.getEntityName() + ", new=" + entityName + ")");
127             }
128         } else {
129             if (!isNull(oldEntity)) {
130                 model = oldEntity.getModelEntity();
131             } else {
132                 throw new IllegalArgumentException JavaDoc("internal error: storeHook called with 2 null arguments");
133             }
134         }
135         return model;
136     }
137
138     public void storeHook(GenericEntity newEntity) {
139         storeHook(null, newEntity);
140     }
141
142     // if oldValue == null, then this is a new entity
143
// if newValue == null, then
144
public void storeHook(GenericEntity oldEntity, GenericEntity newEntity) {
145         storeHook(false, oldEntity, newEntity);
146     }
147
148     // if oldValue == null, then this is a new entity
149
// if newValue == null, then
150
public void storeHook(GenericPK oldPK, GenericEntity newEntity) {
151         storeHook(true, oldPK, newEntity);
152     }
153
154     protected List JavaDoc convert(boolean isPK, String JavaDoc targetEntityName, GenericEntity entity) {
155         if (isNull(entity)) return null;
156         if (isPK) {
157             return entity.getModelEntity().convertToViewValues(targetEntityName, (GenericPK) entity);
158         } else {
159             return entity.getModelEntity().convertToViewValues(targetEntityName, (GenericEntity) entity);
160         }
161     }
162
163     public synchronized void storeHook(boolean isPK, GenericEntity oldEntity, GenericEntity newEntity) {
164         ModelEntity model = getModelCheckValid(oldEntity, newEntity);
165         String JavaDoc entityName = model.getEntityName();
166         // for info about cache clearing
167
if (newEntity == null) {
168             //Debug.logInfo("In storeHook calling sub-storeHook for entity name [" + entityName + "] for the oldEntity: " + oldEntity, module);
169
}
170         storeHook(entityName, isPK, UtilMisc.toList(oldEntity), UtilMisc.toList(newEntity));
171         Iterator JavaDoc it = model.getViewConvertorsIterator();
172         while (it.hasNext()) {
173             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
174             String JavaDoc targetEntityName = (String JavaDoc) entry.getKey();
175             storeHook(targetEntityName, isPK, convert(isPK, targetEntityName, oldEntity), convert(false, targetEntityName, newEntity));
176         }
177     }
178
179     protected void storeHook(String JavaDoc entityName, boolean isPK, List JavaDoc oldValues, List JavaDoc newValues) {
180         UtilCache entityCache = null;
181         synchronized (UtilCache.utilCacheTable) {
182             entityCache = (UtilCache) UtilCache.utilCacheTable.get(getCacheName(entityName));
183         }
184         // for info about cache clearing
185
if (newValues == null || newValues.size() == 0 || newValues.get(0) == null) {
186             //Debug.logInfo("In storeHook (cache clear) for entity name [" + entityName + "], got entity cache with name: " + (entityCache == null ? "[No cache found to remove from]" : entityCache.getName()), module);
187
}
188         if (entityCache == null) {
189             return;
190         }
191         Iterator JavaDoc cacheKeyIter = entityCache.getCacheLineKeys().iterator();
192         while (cacheKeyIter.hasNext()) {
193             EntityCondition condition = (EntityCondition) cacheKeyIter.next();
194             //Debug.logInfo("In storeHook entityName [" + entityName + "] checking against condition: " + condition, module);
195
boolean shouldRemove = false;
196             if (condition == null) {
197                 shouldRemove = true;
198             } else if (oldValues == null) {
199                 Iterator JavaDoc newValueIter = newValues.iterator();
200                 while (newValueIter.hasNext() && !shouldRemove) {
201                     Map JavaDoc newValue = (Map JavaDoc) newValueIter.next();
202                     shouldRemove |= condition.mapMatches(getDelegator(), newValue);
203                 }
204             } else {
205                 boolean oldMatched = false;
206                 Iterator JavaDoc oldValueIter = oldValues.iterator();
207                 while (oldValueIter.hasNext() && !shouldRemove) {
208                     Map JavaDoc oldValue = (Map JavaDoc) oldValueIter.next();
209                     if (condition.mapMatches(getDelegator(), oldValue)) {
210                         oldMatched = true;
211                         //Debug.logInfo("In storeHook, oldMatched for entityName [" + entityName + "]; shouldRemove is false", module);
212
if (newValues != null) {
213                             Iterator JavaDoc newValueIter = newValues.iterator();
214                             while (newValueIter.hasNext() && !shouldRemove) {
215                                 Map JavaDoc newValue = (Map JavaDoc) newValueIter.next();
216                                 shouldRemove |= isNull(newValue) || condition.mapMatches(getDelegator(), newValue);
217                                 //Debug.logInfo("In storeHook, for entityName [" + entityName + "] shouldRemove is now " + shouldRemove, module);
218
}
219                         } else {
220                             shouldRemove = true;
221                         }
222                     }
223                 }
224                 // QUESTION: what is this? why would we do this?
225
if (!oldMatched && isPK) {
226                     //Debug.logInfo("In storeHook, for entityName [" + entityName + "] oldMatched is false and isPK is true, so setting shouldRemove to true (will remove from cache)", module);
227
shouldRemove = true;
228                 }
229             }
230             if (shouldRemove) {
231                 if (Debug.verboseOn()) Debug.logVerbose("In storeHook, matched condition, removing from cache for entityName [" + entityName + "] in cache with name [" + entityCache.getName() + "] entry with condition: " + condition, module);
232                 // doesn't work anymore since this is a copy of the cache keySet, can call remove directly though with a concurrent mod exception: cacheKeyIter.remove();
233
entityCache.remove(condition);
234             }
235         }
236     }
237 }
238
Popular Tags