/* * Copyright (C) 2005-2009 Alfresco Software Limited. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * As a special exception to the terms and conditions of version 2.0 of * the GPL, you may redistribute this Program in connection with Free/Libre * and Open Source Software ("FLOSS") applications as described in Alfresco's * FLOSS exception. You should have recieved a copy of the text describing * the FLOSS exception, and it is also available here: * http://www.alfresco.com/legal/licensing" */ package org.alfresco.repo.domain.propval; import java.io.Serializable; import java.util.Date; import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.repo.cache.SimpleCache; import org.alfresco.repo.cache.lookup.EntityLookupCache; import org.alfresco.repo.cache.lookup.EntityLookupCache.EntityLookupCallbackDAOAdaptor; import org.alfresco.repo.domain.CrcHelper; import org.alfresco.repo.domain.propval.PropertyValueEntity.PersistedType; import org.alfresco.util.Pair; /** * Abstract implementation for Property Value DAO. *

* This provides basic services such as caching, but defers to the underlying implementation * for CRUD operations. * * @author Derek Hulley * @since 3.3 */ public abstract class AbstractPropertyValueDAOImpl implements PropertyValueDAO { private static final String CACHE_REGION_PROPERTY_CLASS = "PropertyClass"; private static final String CACHE_REGION_PROPERTY_STRING_VALUE = "PropertyStringValue"; private static final String CACHE_REGION_PROPERTY_DOUBLE_VALUE = "PropertyDoubleValue"; private static final String CACHE_REGION_PROPERTY_DATE_VALUE = "PropertyDateValue"; private static final String CACHE_REGION_PROPERTY_VALUE = "PropertyValue"; protected PropertyTypeConverter converter; private final PropertyClassCallbackDAO propertyClassDaoCallback; private final PropertyStringValueCallbackDAO propertyStringValueCallback; private final PropertyDoubleValueCallbackDAO propertyDoubleValueCallback; private final PropertyDateValueCallbackDAO propertyDateValueCallback; private final PropertyValueCallbackDAO propertyValueCallback; /** * Cache for the property class:
* KEY: ID
* VALUE: Java class
* VALUE KEY: Java class name
*/ private EntityLookupCache, String> propertyClassCache; /** * Cache for the property string value:
* KEY: ID
* VALUE: The full string
* VALUE KEY: Short string-crc pair ({@link CrcHelper#getStringCrcPair(String, int, boolean, boolean)})
*/ private EntityLookupCache> propertyStringValueCache; /** * Cache for the property double value:
* KEY: ID
* VALUE: The Double instance
* VALUE KEY: The value itself
*/ private EntityLookupCache propertyDoubleValueCache; /** * Cache for the property date value:
* KEY: ID
* VALUE: The Date instance
* VALUE KEY: The date-only date (i.e. everything below day is zeroed)
*/ private EntityLookupCache propertyDateValueCache; /** * Cache for the property value:
* KEY: ID
* VALUE: The Serializable instance
* VALUE KEY: A value key based on the persisted type
*/ private EntityLookupCache propertyValueCache; /** * Default constructor. *

* This sets up the DAO accessors to bypass any caching to handle the case where the caches are not * supplied in the setters. */ public AbstractPropertyValueDAOImpl() { this.propertyClassDaoCallback = new PropertyClassCallbackDAO(); this.propertyStringValueCallback = new PropertyStringValueCallbackDAO(); this.propertyDoubleValueCallback = new PropertyDoubleValueCallbackDAO(); this.propertyDateValueCallback = new PropertyDateValueCallbackDAO(); this.propertyValueCallback = new PropertyValueCallbackDAO(); this.propertyClassCache = new EntityLookupCache, String>(propertyClassDaoCallback); this.propertyStringValueCache = new EntityLookupCache>(propertyStringValueCallback); this.propertyDoubleValueCache = new EntityLookupCache(propertyDoubleValueCallback); this.propertyDateValueCache = new EntityLookupCache(propertyDateValueCallback); this.propertyValueCache = new EntityLookupCache(propertyValueCallback); } /** * @param converter the converter that translates between external and persisted values */ public void setConverter(PropertyTypeConverter converter) { this.converter = converter; } /** * Set the cache to use for alf_prop_class lookups (optional). * * @param propertyClassCache the cache of IDs to property classes */ public void setPropertyClassCache(SimpleCache propertyClassCache) { this.propertyClassCache = new EntityLookupCache, String>( propertyClassCache, CACHE_REGION_PROPERTY_CLASS, propertyClassDaoCallback); } /** * Set the cache to use for alf_prop_string_value lookups (optional). * * @param propertyStringValueCache the cache of IDs to property string values */ public void setPropertyStringValueCache(SimpleCache propertyStringValueCache) { this.propertyStringValueCache = new EntityLookupCache>( propertyStringValueCache, CACHE_REGION_PROPERTY_STRING_VALUE, propertyStringValueCallback); } /** * Set the cache to use for alf_prop_double_value lookups (optional). * * @param propertyDoubleValueCache the cache of IDs to property values */ public void setPropertyDoubleValueCache(SimpleCache propertyDoubleValueCache) { this.propertyDoubleValueCache = new EntityLookupCache( propertyDoubleValueCache, CACHE_REGION_PROPERTY_DOUBLE_VALUE, propertyDoubleValueCallback); } /** * Set the cache to use for alf_prop_date_value lookups (optional). * * @param propertyDateValueCache the cache of IDs to property values */ public void setPropertyDateValueCache(SimpleCache propertyDateValueCache) { this.propertyDateValueCache = new EntityLookupCache( propertyDateValueCache, CACHE_REGION_PROPERTY_DATE_VALUE, propertyDateValueCallback); } /** * Set the cache to use for alf_prop_value lookups (optional). * * @param propertyValueCache the cache of IDs to property values */ public void setPropertyValueCache(SimpleCache propertyValueCache) { this.propertyValueCache = new EntityLookupCache( propertyValueCache, CACHE_REGION_PROPERTY_VALUE, propertyValueCallback); } //================================ // 'alf_prop_class' accessors //================================ public Pair> getPropertyClassById(Long id) { if (id == null) { throw new IllegalArgumentException("Cannot look up entity by null ID."); } Pair> entityPair = propertyClassCache.getByKey(id); if (entityPair == null) { throw new AlfrescoRuntimeException("No property class exists for ID " + id); } return entityPair; } public Pair> getPropertyClass(Class value) { if (value == null) { throw new IllegalArgumentException("Property class cannot be null"); } Pair> entityPair = propertyClassCache.getByValue(value); return entityPair; } public Pair> getOrCreatePropertyClass(Class value) { if (value == null) { throw new IllegalArgumentException("Property class cannot be null"); } Pair> entityPair = propertyClassCache.getOrCreateByValue(value); return entityPair; } /** * Callback for alf_prop_class DAO. */ private class PropertyClassCallbackDAO extends EntityLookupCallbackDAOAdaptor, String> { private final Pair> convertEntityToPair(PropertyClassEntity entity) { if (entity == null) { return null; } else { return entity.getEntityPair(); } } public String getValueKey(Class value) { return value.getName(); } public Pair> createValue(Class value) { PropertyClassEntity entity = createClass(value); return convertEntityToPair(entity); } public Pair> findByKey(Long key) { PropertyClassEntity entity = findClassById(key); return convertEntityToPair(entity); } public Pair> findByValue(Class value) { PropertyClassEntity entity = findClassByValue(value); return convertEntityToPair(entity); } } protected abstract PropertyClassEntity findClassById(Long id); protected abstract PropertyClassEntity findClassByValue(Class value); protected abstract PropertyClassEntity createClass(Class value); //================================ // 'alf_prop_string_value' accessors //================================ public Pair getPropertyStringValueById(Long id) { if (id == null) { throw new IllegalArgumentException("Cannot look up entity by null ID."); } Pair entityPair = propertyStringValueCache.getByKey(id); if (entityPair == null) { throw new AlfrescoRuntimeException("No property string value exists for ID " + id); } return entityPair; } public Pair getPropertyStringValue(String value) { if (value == null) { throw new IllegalArgumentException("Persisted string values cannot be null"); } Pair entityPair = propertyStringValueCache.getByValue(value); return entityPair; } public Pair getOrCreatePropertyStringValue(String value) { if (value == null) { throw new IllegalArgumentException("Persisted string values cannot be null"); } Pair entityPair = propertyStringValueCache.getOrCreateByValue(value); return entityPair; } /** * Callback for alf_prop_string_value DAO. */ private class PropertyStringValueCallbackDAO extends EntityLookupCallbackDAOAdaptor> { private final Pair convertEntityToPair(PropertyStringValueEntity entity) { if (entity == null) { return null; } else { return entity.getEntityPair(); } } public Pair getValueKey(String value) { return CrcHelper.getStringCrcPair(value, 128, true, true); } public Pair createValue(String value) { PropertyStringValueEntity entity = createStringValue(value); return convertEntityToPair(entity); } public Pair findByKey(Long key) { PropertyStringValueEntity entity = findStringValueById(key); return convertEntityToPair(entity); } public Pair findByValue(String value) { PropertyStringValueEntity entity = findStringValueByValue(value); return convertEntityToPair(entity); } } protected abstract PropertyStringValueEntity findStringValueById(Long id); protected abstract PropertyStringValueEntity findStringValueByValue(String value); protected abstract PropertyStringValueEntity createStringValue(String value); //================================ // 'alf_prop_double_value' accessors //================================ public Pair getPropertyDoubleValueById(Long id) { if (id == null) { throw new IllegalArgumentException("Cannot look up entity by null ID."); } Pair entityPair = propertyDoubleValueCache.getByKey(id); if (entityPair == null) { throw new AlfrescoRuntimeException("No property double value exists for ID " + id); } return entityPair; } public Pair getPropertyDoubleValue(Double value) { if (value == null) { throw new IllegalArgumentException("Persisted double values cannot be null"); } Pair entityPair = propertyDoubleValueCache.getByValue(value); return entityPair; } public Pair getOrCreatePropertyDoubleValue(Double value) { if (value == null) { throw new IllegalArgumentException("Persisted double values cannot be null"); } Pair entityPair = propertyDoubleValueCache.getOrCreateByValue(value); return (Pair) entityPair; } /** * Callback for alf_prop_double_value DAO. */ private class PropertyDoubleValueCallbackDAO extends EntityLookupCallbackDAOAdaptor { private final Pair convertEntityToPair(PropertyDoubleValueEntity entity) { if (entity == null) { return null; } else { return entity.getEntityPair(); } } public Double getValueKey(Double value) { return value; } public Pair createValue(Double value) { PropertyDoubleValueEntity entity = createDoubleValue(value); return convertEntityToPair(entity); } public Pair findByKey(Long key) { PropertyDoubleValueEntity entity = findDoubleValueById(key); return convertEntityToPair(entity); } public Pair findByValue(Double value) { PropertyDoubleValueEntity entity = findDoubleValueByValue(value); return convertEntityToPair(entity); } } protected abstract PropertyDoubleValueEntity findDoubleValueById(Long id); protected abstract PropertyDoubleValueEntity findDoubleValueByValue(Double value); protected abstract PropertyDoubleValueEntity createDoubleValue(Double value); //================================ // 'alf_prop_date_value' accessors //================================ public Pair getPropertyDateValueById(Long id) { if (id == null) { throw new IllegalArgumentException("Cannot look up entity by null ID."); } Pair entityPair = propertyDateValueCache.getByKey(id); if (entityPair == null) { throw new AlfrescoRuntimeException("No property date value exists for ID " + id); } return entityPair; } public Pair getPropertyDateValue(Date value) { if (value == null) { throw new IllegalArgumentException("Persisted date values cannot be null"); } value = PropertyDateValueEntity.truncateDate(value); Pair entityPair = propertyDateValueCache.getByValue(value); return entityPair; } public Pair getOrCreatePropertyDateValue(Date value) { if (value == null) { throw new IllegalArgumentException("Persisted double values cannot be null"); } value = PropertyDateValueEntity.truncateDate(value); Pair entityPair = propertyDateValueCache.getOrCreateByValue(value); return (Pair) entityPair; } /** * Callback for alf_prop_date_value DAO. */ private class PropertyDateValueCallbackDAO extends EntityLookupCallbackDAOAdaptor { private final Pair convertEntityToPair(PropertyDateValueEntity entity) { if (entity == null) { return null; } else { return entity.getEntityPair(); } } /** * {@inheritDoc} *

* The value will already have been truncated to have day-accuracy. */ public Date getValueKey(Date value) { return PropertyDateValueEntity.truncateDate(value); } public Pair createValue(Date value) { PropertyDateValueEntity entity = createDateValue(value); return convertEntityToPair(entity); } public Pair findByKey(Long key) { PropertyDateValueEntity entity = findDateValueById(key); return convertEntityToPair(entity); } public Pair findByValue(Date value) { PropertyDateValueEntity entity = findDateValueByValue(value); return convertEntityToPair(entity); } } protected abstract PropertyDateValueEntity findDateValueById(Long id); /** * @param value a date with day-accuracy */ protected abstract PropertyDateValueEntity findDateValueByValue(Date value); /** * @param value a date with day-accuracy */ protected abstract PropertyDateValueEntity createDateValue(Date value); //================================ // 'alf_prop_value' accessors //================================ public Pair getPropertyValueById(Long id) { if (id == null) { throw new IllegalArgumentException("Cannot look up entity by null ID."); } Pair entityPair = propertyValueCache.getByKey(id); if (entityPair == null) { throw new AlfrescoRuntimeException("No property value exists for ID " + id); } return entityPair; } public Pair getPropertyValue(Serializable value) { Pair entityPair = propertyValueCache.getByValue(value); return entityPair; } public Pair getOrCreatePropertyValue(Serializable value) { Pair entityPair = propertyValueCache.getOrCreateByValue(value); return (Pair) entityPair; } /** * Callback for alf_prop_value DAO. */ private class PropertyValueCallbackDAO extends EntityLookupCallbackDAOAdaptor { private final Pair convertEntityToPair(PropertyValueEntity entity) { if (entity == null) { return null; } Long entityId = entity.getId(); Serializable entityValue = entity.getPersistedValue(); // Dig out the class to convert the value to i.e. the actual type of the value Long actualTypeId = entity.getActualTypeId(); Class actualType = getPropertyClassById(actualTypeId).getSecond(); // Convert it Serializable actualValue = (Serializable) converter.convert(actualType, entityValue); // Done return new Pair(entityId, actualValue); } public Serializable getValueKey(Serializable value) { PersistedType persistedType = PropertyValueEntity.getPersistedTypeEnum(value); // We don't return keys for pure Serializable instances if (persistedType == PersistedType.SERIALIZABLE) { // It will be Serialized, so no key return null; } else if (value instanceof String) { return CrcHelper.getStringCrcPair((String)value, 128, true, true); } else { // We've dodged Serializable and String; everything else is OK as a key. return value; } } public Pair createValue(Serializable value) { PropertyValueEntity entity = createPropertyValue(value); return convertEntityToPair(entity); } public Pair findByKey(Long key) { PropertyValueEntity entity = findPropertyValueById(key); return convertEntityToPair(entity); } public Pair findByValue(Serializable value) { PropertyValueEntity entity = findPropertyValueByValue(value); return convertEntityToPair(entity); } } protected abstract PropertyValueEntity findPropertyValueById(Long id); protected abstract PropertyValueEntity findPropertyValueByValue(Serializable value); protected abstract PropertyValueEntity createPropertyValue(Serializable value); }