/* * Copyright (C) 2005-2010 Alfresco Software Limited. * * This file is part of Alfresco * * Alfresco is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Alfresco 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with Alfresco. If not, see . */ package org.alfresco.repo.domain.encoding; import org.alfresco.repo.cache.SimpleCache; import org.alfresco.util.Pair; import org.alfresco.repo.cache.lookup.EntityLookupCache; import org.alfresco.repo.cache.lookup.EntityLookupCache.EntityLookupCallbackDAOAdaptor; import org.springframework.extensions.surf.util.ParameterCheck; /** * Abstract implementation for Encoding DAO. *

* This provides basic services such as caching, but defers to the underlying implementation * for CRUD operations. * * @author Derek Hulley * @since 3.2 */ public abstract class AbstractEncodingDAOImpl implements EncodingDAO { private static final String CACHE_REGION_ENCODING = "Encoding"; /** * Cache for the Locale values:
* KEY: ID
* VALUE: String
* VALUE KEY: String
*/ private EntityLookupCache encodingEntityCache; /** * Set the cache that maintains the ID-Encoding mappings and vice-versa (bi-directional) * * @param encodingEntityCache the cache */ public void setEncodingEntityCache(SimpleCache encodingEntityCache) { this.encodingEntityCache = new EntityLookupCache( encodingEntityCache, CACHE_REGION_ENCODING, new EncodingEntityCallbackDAO()); } public Pair getEncoding(Long id) { return encodingEntityCache.getByKey(id); } public Pair getEncoding(String encoding) { encoding = encoding.toUpperCase(); ParameterCheck.mandatory("encoding", encoding); return encodingEntityCache.getByValue(encoding); } public Pair getOrCreateEncoding(String encoding) { encoding = encoding.toUpperCase(); ParameterCheck.mandatory("encoding", encoding); return encodingEntityCache.getOrCreateByValue(encoding); } /** * Callback for alf_encoding DAO */ private class EncodingEntityCallbackDAO extends EntityLookupCallbackDAOAdaptor { @Override public String getValueKey(String value) { return value; } public Pair findByKey(Long id) { EncodingEntity entity = getEncodingEntity(id); if (entity == null) { return null; } else { return new Pair(id, entity.getEncoding().toUpperCase()); } } @Override public Pair findByValue(String encoding) { EncodingEntity entity = getEncodingEntity(encoding); if (entity == null) { return null; } else { return new Pair(entity.getId(), encoding); } } public Pair createValue(String encoding) { EncodingEntity entity = createEncodingEntity(encoding); return new Pair(entity.getId(), encoding); } } /** * @param id the ID of the encoding entity * @return Return the entity or null if it doesn't exist */ protected abstract EncodingEntity getEncodingEntity(Long id); protected abstract EncodingEntity getEncodingEntity(String encoding); protected abstract EncodingEntity createEncodingEntity(String encoding); }