001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.entity.cache;
019:
020: import java.util.Iterator;
021:
022: import org.ofbiz.base.util.Debug;
023: import org.ofbiz.base.util.cache.UtilCache;
024: import org.ofbiz.base.util.cache.CacheLine;
025: import org.ofbiz.entity.GenericEntity;
026: import org.ofbiz.entity.GenericPK;
027: import org.ofbiz.entity.condition.EntityCondition;
028:
029: public class EntityCache extends AbstractCache {
030: public static final String module = EntityCache.class.getName();
031:
032: public EntityCache(String delegatorName) {
033: super (delegatorName, "entity");
034: }
035:
036: public GenericEntity get(GenericPK pk) {
037: UtilCache entityCache = getCache(pk.getEntityName());
038: if (entityCache == null)
039: return null;
040: return (GenericEntity) entityCache.get(pk);
041: }
042:
043: public GenericEntity put(GenericEntity entity) {
044: if (entity == null)
045: return null;
046: return put(entity.getPrimaryKey(), entity);
047: }
048:
049: public GenericEntity put(GenericPK pk, GenericEntity entity) {
050: if (pk.getModelEntity().getNeverCache()) {
051: Debug
052: .logWarning(
053: "Tried to put a value of the "
054: + pk.getEntityName()
055: + " entity in the BY PRIMARY KEY cache but this entity has never-cache set to true, not caching.",
056: module);
057: return null;
058: }
059:
060: if (entity == null) {
061: entity = GenericEntity.NULL_ENTITY;
062: } else {
063: // before going into the cache, make this value immutable
064: entity.setImmutable();
065: }
066: UtilCache entityCache = getOrCreateCache(pk.getEntityName());
067: return (GenericEntity) entityCache.put(pk, entity);
068: }
069:
070: public void remove(String entityName, EntityCondition condition) {
071: UtilCache entityCache = getCache(entityName);
072: if (entityCache == null)
073: return;
074: Iterator it = entityCache.getCacheLineValues().iterator();
075: while (it.hasNext()) {
076: CacheLine line = (CacheLine) it.next();
077: if (entityCache.hasExpired(line))
078: continue;
079: GenericEntity entity = (GenericEntity) line.getValue();
080: if (entity == null)
081: continue;
082: if (condition.entityMatches(entity))
083: it.remove();
084: }
085: }
086:
087: public GenericEntity remove(GenericEntity entity) {
088: return remove(entity.getPrimaryKey());
089: }
090:
091: public GenericEntity remove(GenericPK pk) {
092: UtilCache entityCache = getCache(pk.getEntityName());
093: if (Debug.verboseOn())
094: Debug
095: .logVerbose(
096: "Removing from EntityCache with PK ["
097: + pk
098: + "], will remove from this cache: "
099: + (entityCache == null ? "[No cache found to remove from]"
100: : entityCache.getName()),
101: module);
102: if (entityCache == null)
103: return null;
104: GenericEntity retVal = (GenericEntity) entityCache.remove(pk);
105: if (Debug.verboseOn())
106: Debug.logVerbose("Removing from EntityCache with PK [" + pk
107: + "], found this in the cache: " + retVal, module);
108: return retVal;
109: }
110: }
|