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:
020: package org.openharmonise.rm.tasks;
021:
022: import java.sql.*;
023: import java.sql.ResultSet;
024: import java.util.*;
025: import java.util.logging.*;
026: import java.util.logging.Level;
027:
028: import org.openharmonise.commons.cache.*;
029: import org.openharmonise.commons.dsi.*;
030: import org.openharmonise.commons.dsi.dml.*;
031: import org.openharmonise.rm.DataAccessException;
032: import org.openharmonise.rm.dsi.DataStoreInterfaceFactory;
033:
034: /**
035: * This class provides a cache for <code>Task</code> objects.
036: *
037: * @author Michael Bell
038: * @version $Revision: 1.3 $
039: *
040: */
041: public class TaskCache extends AbstractCache {
042:
043: /**
044: * Logger for this class
045: */
046: private static final Logger m_logger = Logger
047: .getLogger(TaskCache.class.getName());
048:
049: /* (non-Javadoc)
050: * @see org.openharmonise.commons.cache.AbstractCache#clearCache()
051: */
052: public void clearCache() {
053: super .clearCache();
054:
055: try {
056: initialise();
057: } catch (CacheException e) {
058: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
059: }
060: }
061:
062: private static TaskCache m_instance = null;
063: private static final String CACHE_NAME = "Task";
064: private AbstractDataStoreInterface m_dsi = null;
065:
066: private TaskCache() throws CacheException {
067: super (CACHE_NAME);
068: initialise();
069: }
070:
071: public static synchronized TaskCache getInstance()
072: throws CacheException {
073: if (m_instance == null) {
074: m_instance = new TaskCache();
075: }
076:
077: return m_instance;
078: }
079:
080: public Task getTask(int nId) throws CacheException {
081: return (Task) super .getObject(String.valueOf(nId));
082: }
083:
084: protected Object getCacheableObject(Object key)
085: throws java.lang.Exception {
086: Task tsk = new Task(m_dsi, Integer.parseInt((String) key));
087:
088: return tsk;
089: }
090:
091: private void initialise() throws CacheException {
092: ResultSet rs = null;
093: try {
094: m_dsi = DataStoreInterfaceFactory.getDataStoreInterface();
095:
096: SelectStatement select = new SelectStatement();
097: Task tsk = new Task();
098:
099: select.addSelectColumn(tsk.getInstanceColumnRef(
100: Task.ATTRIB_ID, false));
101:
102: rs = m_dsi.execute(select);
103:
104: while (rs.next()) {
105: this .getObject(rs.getString(1));
106: }
107: } catch (Exception e) {
108: throw new CacheException(e);
109: } finally {
110: if (rs != null) {
111: try {
112: rs.close();
113: } catch (SQLException e) {
114: throw new CacheException(e);
115: }
116: }
117: }
118: }
119:
120: public List getPendingTasks() throws CacheException {
121: Iterator keyIter = getCacheKeys().iterator();
122: Vector pends = new Vector();
123:
124: while (keyIter.hasNext() == true) {
125: Task tsk = (Task) getObject(keyIter.next());
126:
127: try {
128: if (tsk.isPending() == true) {
129: pends.add(tsk);
130: }
131: } catch (DataAccessException e) {
132: throw new CacheException(e);
133: }
134: }
135:
136: return pends;
137: }
138: }
|