1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.sys.batch.dataaccess.impl;
17
18 import java.sql.PreparedStatement;
19 import java.sql.ResultSet;
20 import java.sql.SQLException;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.kuali.ole.sys.batch.dataaccess.PreparedStatementCachingDao;
27 import org.kuali.rice.core.framework.persistence.jdbc.dao.PlatformAwareDaoBaseJdbc;
28
29 public abstract class AbstractPreparedStatementCachingDaoJdbc extends PlatformAwareDaoBaseJdbc implements PreparedStatementCachingDao {
30 protected static final String RETRIEVE_PREFIX = "retrieve-";
31 protected static final String INSERT_PREFIX = "insert-";
32 protected static final String UPDATE_PREFIX = "update-";
33
34 protected abstract class JdbcWrapper<T> {
35 protected abstract void populateStatement(PreparedStatement preparedStatement) throws SQLException;
36
37 void update(Class<T> type, PreparedStatement preparedStatement) {
38 try {
39 populateStatement(preparedStatement);
40 preparedStatement.executeUpdate();
41 }
42 catch (SQLException e) {
43 throw new RuntimeException("AbstractUpdatingPreparedStatementCachingDaoJdbc.UpdatingJdbcWrapper encountered exception during getObject method for type: " + type, e);
44 }
45 }
46 }
47
48 protected abstract class RetrievingJdbcWrapper<T> extends JdbcWrapper {
49 protected abstract T extractResult(ResultSet resultSet) throws SQLException;
50
51 public T get(Class<T> type) {
52 T value = null;
53 PreparedStatement statement = preparedStatementCache.get(RETRIEVE_PREFIX + type);
54 try {
55 populateStatement(statement);
56 ResultSet resultSet = statement.executeQuery();
57 if (resultSet.next()) {
58 value = extractResult(resultSet);
59 if (resultSet.next()) {
60 throw new RuntimeException("More that one row returned when selecting by primary key in AbstractRetrievingPreparedStatementCachingDaoJdbc.RetrievingJdbcWrapper for: " + type);
61 }
62 }
63 resultSet.close();
64 }
65 catch (SQLException e) {
66 throw new RuntimeException("AbstractRetrievingPreparedStatementCachingDaoJdbc.RetrievingJdbcWrapper encountered exception during getObject method for type: " + type, e);
67 }
68 return (T) value;
69 }
70 }
71
72
73
74
75 protected abstract class RetrievingListJdbcWrapper<T> extends JdbcWrapper {
76 protected abstract T extractResult(ResultSet resultSet) throws SQLException;
77
78 public List<T> get(Class<T> type) {
79 List<T> resultList = new ArrayList<T>();
80 PreparedStatement statement = preparedStatementCache.get(RETRIEVE_PREFIX + type);
81 try {
82 populateStatement(statement);
83 ResultSet resultSet = statement.executeQuery();
84 while (resultSet.next()) {
85 resultList.add(extractResult(resultSet));
86 }
87 resultSet.close();
88 }
89 catch (SQLException e) {
90 throw new RuntimeException("AbstractRetrievingPreparedStatementCachingDaoJdbc.RetrievingListJdbcWrapper encountered exception during getObject method for type: " + type, e);
91 }
92 return resultList;
93 }
94 }
95
96 protected abstract class InsertingJdbcWrapper<T> extends JdbcWrapper {
97 public void execute(Class<T> type) {
98 update(type, preparedStatementCache.get(INSERT_PREFIX + type));
99 }
100 }
101
102 protected abstract class UpdatingJdbcWrapper<T> extends JdbcWrapper {
103 public void execute(Class<T> type) {
104 update(type, preparedStatementCache.get(UPDATE_PREFIX + type));
105 }
106 }
107
108 protected Map<String, PreparedStatement> preparedStatementCache;
109
110 protected abstract Map<String, String> getSql();
111
112 public void initialize() {
113 preparedStatementCache = new HashMap<String, PreparedStatement>();
114 try {
115 for (String statementKey : getSql().keySet()) {
116 preparedStatementCache.put(statementKey, getConnection().prepareStatement(getSql().get(statementKey)));
117 }
118 }
119 catch (SQLException e) {
120 throw new RuntimeException("Caught exception preparing statements in CachingDaoJdbc initialize method", e);
121 }
122 }
123
124 public void destroy() {
125 try {
126 for (PreparedStatement preparedStatement : preparedStatementCache.values()) {
127 preparedStatement.close();
128 }
129 preparedStatementCache = null;
130 }
131 catch (SQLException e) {
132 throw new RuntimeException("Caught exception closing statements in CachingDaoJdbc destroy method", e);
133 }
134 }
135
136 }