View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.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/ecl2.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  	@Override
54  	public void setReloadActionListFlag(String userId) {
55  		Connection conn = null;
56  		PreparedStatement preparedStatement = null;
57  		
58  		try {
59  			conn = nonTxDataSource.getConnection();
60  			
61  			preparedStatement = conn.prepareStatement("insert into KREW_USR_OPTN_T (PRSN_OPTN_ID, PRNCPL_ID, VAL, VER_NBR) VALUES (?, ?, ?, ?)");
62  			preparedStatement.setString(1, RELOAD_ACTION_LIST);
63  			preparedStatement.setString(2, userId);
64  			preparedStatement.setString(3, "true");
65  			preparedStatement.setInt(4, 1);
66  			int result = preparedStatement.executeUpdate();
67  			
68  		} catch (SQLException e) {
69  			// this is normal if the preferences is already set.
70  		} finally {
71  			if (preparedStatement != null) {
72  				try {
73  					preparedStatement.close();
74  				} catch (SQLException e) {
75  					LOG.error("couldn't close PreparedStatement", e);
76  				}
77  			}
78  			if (conn != null) {
79  				try {
80  					conn.close();
81  				} catch (SQLException e) {
82  					LOG.error("couldn't close Connection", e);
83  				}
84  			}
85  		}
86  	}
87  
88  	
89  	/**
90  	 * @see org.kuali.rice.kew.useroptions.dao.ReloadActionListDAO#checkAndResetReloadActionListFlag(java.lang.String)
91  	 */
92  	@Override
93  	public boolean checkAndResetReloadActionListFlag(String userId) {
94  		Connection conn = null;
95  		PreparedStatement preparedStatement = null;
96  		int updatedCount = 0;
97  		
98  		try {
99  			conn = nonTxDataSource.getConnection();
100 			
101 			preparedStatement = conn.prepareStatement("delete from KREW_USR_OPTN_T where PRSN_OPTN_ID = ? and PRNCPL_ID = ?");
102 			preparedStatement.setString(1, RELOAD_ACTION_LIST);
103 			preparedStatement.setString(2, userId);
104 			updatedCount = preparedStatement.executeUpdate();
105 			
106 		} catch (SQLException e) {
107 			LOG.error("unable to delete RELOAD_ACTION_LIST preferences for " + userId, e);
108 		} finally {
109 			if (preparedStatement != null) {
110 				try {
111 					preparedStatement.close();
112 				} catch (SQLException e) {
113 					LOG.error("couldn't close PreparedStatement", e);
114 				}
115 			}
116 			if (conn != null) {
117 				try {
118 					conn.close();
119 				} catch (SQLException e) {
120 					LOG.error("couldn't close Connection", e);
121 				}
122 			}
123 		}
124 		
125 		return updatedCount == 1;
126 	}
127 	
128 	
129 }