1 25 package org.ofbiz.entity.cache; 26 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 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 module = AbstractEntityConditionCache.class.getName(); 44 45 protected AbstractEntityConditionCache(String delegatorName, String id) { 46 super(delegatorName, id); 47 } 48 49 protected Object get(String entityName, EntityCondition condition, Object key) { 50 Map conditionCache = getConditionCache(entityName, condition); 51 if (conditionCache == null) return null; 52 return conditionCache.get(key); 54 } 55 56 protected Object put(String entityName, EntityCondition condition, Object key, Object 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 conditionCache = getOrCreateConditionCache(entityName, condition); 64 synchronized (conditionCache) { 65 return conditionCache.put(key, value); 66 } 67 } 68 69 public void remove(String entityName, EntityCondition condition) { 70 UtilCache cache = getCache(entityName); 71 if (cache == null) return; 72 cache.remove(condition); 73 } 74 75 protected Object remove(String entityName, EntityCondition condition, Object key) { 76 Map 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 return frozenCondition; 97 } 98 99 protected Map getConditionCache(String entityName, EntityCondition condition) { 100 UtilCache cache = getCache(entityName); 101 if (cache == null) return null; 102 return (Map ) cache.get(getConditionKey(condition)); 103 } 104 105 protected Map getOrCreateConditionCache(String entityName, EntityCondition condition) { 106 UtilCache utilCache = getOrCreateCache(entityName); 107 Object conditionKey = getConditionKey(condition); 108 Map conditionCache = (Map ) utilCache.get(conditionKey); 109 if (conditionCache == null) { 110 conditionCache = new HashMap (); 111 utilCache.put(conditionKey, conditionCache); 112 } 113 return conditionCache; 114 } 115 116 protected static final boolean isNull(Map 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 entityName = model.getEntityName(); 125 if (oldEntity != null && !entityName.equals(oldEntity.getEntityName())) { 126 throw new IllegalArgumentException ("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 ("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 public void storeHook(GenericEntity oldEntity, GenericEntity newEntity) { 145 storeHook(false, oldEntity, newEntity); 146 } 147 148 public void storeHook(GenericPK oldPK, GenericEntity newEntity) { 151 storeHook(true, oldPK, newEntity); 152 } 153 154 protected List convert(boolean isPK, String 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 entityName = model.getEntityName(); 166 if (newEntity == null) { 168 } 170 storeHook(entityName, isPK, UtilMisc.toList(oldEntity), UtilMisc.toList(newEntity)); 171 Iterator it = model.getViewConvertorsIterator(); 172 while (it.hasNext()) { 173 Map.Entry entry = (Map.Entry ) it.next(); 174 String targetEntityName = (String ) entry.getKey(); 175 storeHook(targetEntityName, isPK, convert(isPK, targetEntityName, oldEntity), convert(false, targetEntityName, newEntity)); 176 } 177 } 178 179 protected void storeHook(String entityName, boolean isPK, List oldValues, List newValues) { 180 UtilCache entityCache = null; 181 synchronized (UtilCache.utilCacheTable) { 182 entityCache = (UtilCache) UtilCache.utilCacheTable.get(getCacheName(entityName)); 183 } 184 if (newValues == null || newValues.size() == 0 || newValues.get(0) == null) { 186 } 188 if (entityCache == null) { 189 return; 190 } 191 Iterator cacheKeyIter = entityCache.getCacheLineKeys().iterator(); 192 while (cacheKeyIter.hasNext()) { 193 EntityCondition condition = (EntityCondition) cacheKeyIter.next(); 194 boolean shouldRemove = false; 196 if (condition == null) { 197 shouldRemove = true; 198 } else if (oldValues == null) { 199 Iterator newValueIter = newValues.iterator(); 200 while (newValueIter.hasNext() && !shouldRemove) { 201 Map newValue = (Map ) newValueIter.next(); 202 shouldRemove |= condition.mapMatches(getDelegator(), newValue); 203 } 204 } else { 205 boolean oldMatched = false; 206 Iterator oldValueIter = oldValues.iterator(); 207 while (oldValueIter.hasNext() && !shouldRemove) { 208 Map oldValue = (Map ) oldValueIter.next(); 209 if (condition.mapMatches(getDelegator(), oldValue)) { 210 oldMatched = true; 211 if (newValues != null) { 213 Iterator newValueIter = newValues.iterator(); 214 while (newValueIter.hasNext() && !shouldRemove) { 215 Map newValue = (Map ) newValueIter.next(); 216 shouldRemove |= isNull(newValue) || condition.mapMatches(getDelegator(), newValue); 217 } 219 } else { 220 shouldRemove = true; 221 } 222 } 223 } 224 if (!oldMatched && isPK) { 226 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 entityCache.remove(condition); 234 } 235 } 236 } 237 } 238 | Popular Tags |