View Javadoc

1   /*
2    * Copyright 2007 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 1.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl1.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.useroptions.dao.impl;
17  
18  import java.sql.Connection;
19  import java.sql.PreparedStatement;
20  import java.sql.ResultSet;
21  import java.sql.SQLException;
22  
23  import javax.sql.DataSource;
24  
25  import org.apache.log4j.Logger;
26  import org.kuali.rice.kew.useroptions.dao.ReloadActionListDAO;
27  
28  /**
29   * Implementation of ReloadActionListDAO.  This needs to be straight JDBC since it ignores SQLException in one case.
30   * 
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   *
33   */
34  public class ReloadActionListDaoJdbcImpl implements ReloadActionListDAO {
35  	
36  	private static final String RELOAD_ACTION_LIST = "RELOAD_ACTION_LIST";
37  
38  	private static final Logger LOG = Logger.getLogger(ReloadActionListDaoJdbcImpl.class);
39  	
40  	DataSource nonTxDataSource;
41  	
42  	/**
43  	 * This constructs a ReloadActionListDaoJdbcImpl
44  	 * 
45  	 */
46  	public ReloadActionListDaoJdbcImpl(DataSource nonTxDataSource) {
47  		this.nonTxDataSource = nonTxDataSource; 
48  	}
49  
50  	/**
51  	 * @see org.kuali.rice.kew.useroptions.dao.ReloadActionListDAO#setReloadActionListFlag(java.lang.String)
52  	 */
53  	public void setReloadActionListFlag(String userId) {
54  		Connection conn = null;
55  		PreparedStatement preparedStatement = null;
56  		
57  		try {
58  			conn = nonTxDataSource.getConnection();
59  			
60  			preparedStatement = conn.prepareStatement("insert into KREW_USR_OPTN_T (PRSN_OPTN_ID, PRNCPL_ID, VAL, VER_NBR) VALUES (?, ?, ?, ?)");
61  			preparedStatement.setString(1, RELOAD_ACTION_LIST);
62  			preparedStatement.setString(2, userId);
63  			preparedStatement.setString(3, "true");
64  			preparedStatement.setInt(4, 1);
65  			int result = preparedStatement.executeUpdate();
66  			
67  		} catch (SQLException e) {
68  			// this is normal if the preference is already set.
69  		} finally {
70  			if (preparedStatement != null) {
71  				try {
72  					preparedStatement.close();
73  				} catch (SQLException e) {
74  					LOG.error("couldn't close PreparedStatement", e);
75  				}
76  			}
77  			if (conn != null) {
78  				try {
79  					conn.close();
80  				} catch (SQLException e) {
81  					LOG.error("couldn't close Connection", e);
82  				}
83  			}
84  		}
85  	}
86  
87  	
88  	/**
89  	 * @see org.kuali.rice.kew.useroptions.dao.ReloadActionListDAO#checkAndResetReloadActionListFlag(java.lang.String)
90  	 */
91  	public boolean checkAndResetReloadActionListFlag(String userId) {
92  		Connection conn = null;
93  		PreparedStatement preparedStatement = null;
94  		int updatedCount = 0;
95  		
96  		try {
97  			conn = nonTxDataSource.getConnection();
98  			
99  			preparedStatement = conn.prepareStatement("delete from KREW_USR_OPTN_T where PRSN_OPTN_ID = ? and PRNCPL_ID = ?");
100 			preparedStatement.setString(1, RELOAD_ACTION_LIST);
101 			preparedStatement.setString(2, userId);
102 			updatedCount = preparedStatement.executeUpdate();
103 			
104 		} catch (SQLException e) {
105 			LOG.error("unable to delete RELOAD_ACTION_LIST preference", e);
106 		} finally {
107 			if (preparedStatement != null) {
108 				try {
109 					preparedStatement.close();
110 				} catch (SQLException e) {
111 					LOG.error("couldn't close PreparedStatement", e);
112 				}
113 			}
114 			if (conn != null) {
115 				try {
116 					conn.close();
117 				} catch (SQLException e) {
118 					LOG.error("couldn't close Connection", e);
119 				}
120 			}
121 		}
122 		
123 		return updatedCount == 1;
124 	}
125 	
126 	
127 }