001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.rm.dsi;
020:
021: import java.util.logging.*;
022:
023: import org.openharmonise.commons.cache.*;
024: import org.openharmonise.commons.dsi.*;
025: import org.openharmonise.rm.metadata.Profile;
026:
027: /**
028: * Cache of <code>ColumnRef</code> objects used within Harmonise.
029: * Column references are stored with a cache key composed of the class
030: * name, column name and a boolean of whether the column referfence refers
031: * to a historical table or not.
032: *
033: * <p>Note: Care should be used in dealing with <code>ColumnRef</code> objects
034: * taken from this cache as they are mutable and therefore there is a risk
035: * associated to any changes you might make to an object taken from
036: * the cache. For example, it is possible to reset the table name associated
037: * to the column reference to accomodate an alias in a select statement
038: * maybe, however this could possibly break further use.</p>
039: *
040: * @author mike
041: * @version $Revision: 1.2 $
042: *
043: */
044: public class ColumnRefCache extends AbstractCache {
045:
046: /**
047: * Name of this cache
048: */
049: private static final String CACHE_NAME = "columnref";
050:
051: /**
052: * Singleton instance of this cache
053: */
054: private static ColumnRefCache m_instance = null;
055:
056: /**
057: * Logger for this class
058: */
059: private static final Logger m_logger = Logger
060: .getLogger(ColumnRefCache.class.getName());
061:
062: /**
063: * Creates new instance of a column reference cache
064: *
065: * @throws CacheException if an error occurs creating the cache
066: */
067: private ColumnRefCache() throws CacheException {
068: super (CACHE_NAME);
069: }
070:
071: /**
072: * Returns the singleton instance of the cache
073: *
074: * @return
075: * @throws CacheException
076: */
077: static public ColumnRefCache getInstance() throws CacheException {
078: if (m_instance == null) {
079: m_instance = new ColumnRefCache();
080: }
081:
082: return m_instance;
083: }
084:
085: /**
086: * Returns a cached <code>ColumnRef</code> for the given column associated
087: * to the given <code>DataStoreObject</code>. If the <code>ColumnRef</code>
088: * is not in the cache it is retrieved from the object's <code>getInstanceColumnRef</code>
089: * method.
090: *
091: * <p>Note: this method calls the <code>getInstanceColumnRef</code> and
092: * therefore should not be used in the <code>getInstanceColumnRef</code>
093: * method of a <code>DataStoreObject</code> in order to avoid an
094: * ininite loop.</p>
095: *
096: * @param dsObj the data store object
097: * @param sColumn the database column name
098: * @param bIsHist <code>true</code> of the column is in the historical
099: * table, otherwise <code>false</code>
100: * @return the <code>ColumnRef</code> associated with the specified
101: * database column
102: * @throws CacheException if an error occurs obtaining the
103: * <code>ColumnRef</code>
104: */
105: public ColumnRef getColumnRef(DataStoreObject dsObj,
106: String sColumn, boolean bIsHist) throws CacheException {
107: String sCacheKey = null;
108:
109: //need to differentiate between profiles, i.e. document profiles, section profiles, etc..
110: if (dsObj instanceof Profile) {
111: sCacheKey = getCacheKey(dsObj.getDBTableName(), sColumn,
112: bIsHist);
113: } else {
114: sCacheKey = getCacheKey(dsObj.getClass(), sColumn, bIsHist);
115: }
116:
117: ColumnRef colref = (ColumnRef) getObject(sCacheKey);
118:
119: if (colref == null) {
120: try {
121: colref = dsObj.getInstanceColumnRef(sColumn, bIsHist);
122: } catch (DataStoreException e) {
123: throw new CacheException(
124: "Error occured getting column ref", e);
125: }
126:
127: super .addToCache(sCacheKey, colref);
128: }
129:
130: return colref;
131:
132: }
133:
134: /* (non-Javadoc)
135: * @see org.openharmonise.commons.cache.AbstractCache#getObject(java.lang.Object)
136: */
137: public Object getObject(final Object key) {
138:
139: Object cached_object = null;
140: try {
141: cached_object = super .getObject(key);
142: } catch (CacheException e) {
143: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
144: }
145: return cached_object;
146: }
147:
148: /**
149: * Returns a cached <code>ColumnRef</code> for the given column associated
150: * to the given class if there is one in the cache, otherwise a null is returned.
151: *
152: * @param clss the <code>Class</code> of a <code>DataStoreObject</code>
153: * @param sColumn the database column name
154: * @param bIsHist <code>true</code> of the column is in the historical
155: * table, otherwise <code>false</code>
156: * @return a cached <code>ColumnRef</code> for the specified database column
157: * @throws CacheException if any errors occur
158: */
159: public ColumnRef getColumnRef(Class clss, String sColumn,
160: boolean bIsHist) throws CacheException {
161:
162: String sCacheKey = getCacheKey(clss, sColumn, bIsHist);
163: ColumnRef colref = (ColumnRef) getObject(sCacheKey);
164:
165: return colref;
166: }
167:
168: /**
169: * Adds the given <code>ColumnRef</code> to this cache for the given
170: * column associated to the given class.
171: *
172: * @param clss the class of the data store object associated to given the <code>ColumnRef</code>
173: * @param sColumn the database column name
174: * @param bIsHist <code>true</code> of the column is in the historical
175: * table, otherwise <code>false</code>
176: * @param colref the column reference to cache
177: */
178: public void addToCache(Class clss, String sColumn, boolean bIsHist,
179: ColumnRef colref) {
180: String sCacheKey = getCacheKey(clss, sColumn, bIsHist);
181: super .addToCache(sCacheKey, colref);
182: }
183:
184: /* (non-Javadoc)
185: * @see org.openharmonise.commons.cache.AbstractCache#getCacheableObject(java.lang.Object)
186: */
187: protected Object getCacheableObject(Object key) throws Exception {
188:
189: return null;
190: }
191:
192: /**
193: * Returns a unique key to be used in the cache composed of the
194: * combination of class, column and historical boolean.
195: *
196: * @param clss the class name
197: * @param sColumn the column name
198: * @param bIsHist <code>true</code> if the column is in the historical table, otherwise <code>false</code>
199: * @return a unique key for the column reference
200: */
201: protected String getCacheKey(Class clss, String sColumn,
202: boolean bIsHist) {
203: StringBuffer strbuf = new StringBuffer();
204: strbuf.append(clss.getName()).append(sColumn).append(
205: String.valueOf(bIsHist));
206: return strbuf.toString();
207: }
208:
209: /**
210: * Returns a unique key to be used in the cache composed the combination
211: * of class, column and historical boolean.
212: *
213: * @param clss the class name
214: * @param sColumn the column name
215: * @param bIsHist <code>true</code> if the column is in the historical table, otherwise <code>false</code>
216: * @return a unique key for the column reference
217: */
218: protected String getCacheKey(String sTable, String sColumn,
219: boolean bIsHist) {
220: StringBuffer strbuf = new StringBuffer();
221: strbuf.append(sTable).append(sColumn).append(
222: String.valueOf(bIsHist));
223: return strbuf.toString();
224: }
225:
226: }
|