KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > tasks > TaskCache


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19
20 package org.openharmonise.rm.tasks;
21
22 import java.sql.*;
23 import java.sql.ResultSet JavaDoc;
24 import java.util.*;
25 import java.util.logging.*;
26 import java.util.logging.Level JavaDoc;
27
28 import org.openharmonise.commons.cache.*;
29 import org.openharmonise.commons.dsi.*;
30 import org.openharmonise.commons.dsi.dml.*;
31 import org.openharmonise.rm.DataAccessException;
32 import org.openharmonise.rm.dsi.DataStoreInterfaceFactory;
33
34
35
36 /**
37  * This class provides a cache for <code>Task</code> objects.
38  *
39  * @author Michael Bell
40  * @version $Revision: 1.3 $
41  *
42  */

43 public class TaskCache extends AbstractCache {
44     
45     /**
46      * Logger for this class
47      */

48     private static final Logger m_logger = Logger.getLogger(TaskCache.class.getName());
49     
50     /* (non-Javadoc)
51      * @see org.openharmonise.commons.cache.AbstractCache#clearCache()
52      */

53     public void clearCache() {
54         super.clearCache();
55         
56         try {
57             initialise();
58         } catch (CacheException e) {
59             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
60         }
61     }
62     private static TaskCache m_instance = null;
63     private static final String JavaDoc CACHE_NAME = "Task";
64     private AbstractDataStoreInterface m_dsi = null;
65
66     private TaskCache() throws CacheException {
67         super(CACHE_NAME);
68         initialise();
69     }
70
71     public static synchronized TaskCache getInstance()
72                                               throws CacheException {
73         if (m_instance == null) {
74             m_instance = new TaskCache();
75         }
76
77         return m_instance;
78     }
79     
80     public Task getTask(int nId) throws CacheException {
81         return (Task) super.getObject(String.valueOf(nId));
82     }
83
84     protected Object JavaDoc getCacheableObject(Object JavaDoc key) throws java.lang.Exception JavaDoc {
85         Task tsk = new Task(m_dsi, Integer.parseInt((String JavaDoc) key));
86
87         return tsk;
88     }
89
90     private void initialise() throws CacheException {
91         ResultSet JavaDoc rs = null;
92         try {
93             m_dsi = DataStoreInterfaceFactory.getDataStoreInterface();
94
95             SelectStatement select = new SelectStatement();
96             Task tsk = new Task();
97
98             select.addSelectColumn(tsk.getInstanceColumnRef(Task.ATTRIB_ID, false));
99
100             rs = m_dsi.execute(select);
101
102             while (rs.next()) {
103                 this.getObject(rs.getString(1));
104             }
105         } catch (Exception JavaDoc e) {
106             throw new CacheException(e);
107         } finally {
108             if(rs != null) {
109                 try {
110                     rs.close();
111                 } catch (SQLException e) {
112                     throw new CacheException(e);
113                 }
114             }
115         }
116     }
117
118     public List getPendingTasks() throws CacheException {
119         Iterator keyIter = getCacheKeys().iterator();
120         Vector pends = new Vector();
121
122         while (keyIter.hasNext() == true) {
123             Task tsk = (Task) getObject(keyIter.next());
124
125             try {
126                 if (tsk.isPending() == true) {
127                     pends.add(tsk);
128                 }
129             } catch (DataAccessException e) {
130                 throw new CacheException(e);
131             }
132         }
133
134         return pends;
135     }
136 }
Popular Tags