Commit cd86edce authored by Matt Tucker's avatar Matt Tucker Committed by matt

Database logic cleanup.

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@6101 b35dd754-fafc-0310-a699-88a17e54d16e
parent 68e1b101
...@@ -80,12 +80,10 @@ public class RosterItemProvider { ...@@ -80,12 +80,10 @@ public class RosterItemProvider {
* (for example, a RosterItem that directly accesses the backend storage, or one that is an * (for example, a RosterItem that directly accesses the backend storage, or one that is an
* object in an object database).<p> * object in an object database).<p>
* *
* If you don't want roster items edited through wildfire, throw * @param username the username of the user/chatbot that owns the roster item.
* UnsupportedOperationException. * @param item the settings for the roster item to create.
* * @return the new roster item.
* @param username the username of the user/chatbot that owns the roster item * @throws UserAlreadyExistsException if a roster item with the username already exists.
* @param item the settings for the roster item to create
* @return The created roster item
*/ */
public RosterItem createItem(String username, RosterItem item) public RosterItem createItem(String username, RosterItem item)
throws UserAlreadyExistsException throws UserAlreadyExistsException
...@@ -113,10 +111,7 @@ public class RosterItemProvider { ...@@ -113,10 +111,7 @@ public class RosterItemProvider {
throw new UserAlreadyExistsException(item.getJid().toBareJID()); throw new UserAlreadyExistsException(item.getJid().toBareJID());
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
return item; return item;
} }
...@@ -154,16 +149,12 @@ public class RosterItemProvider { ...@@ -154,16 +149,12 @@ public class RosterItemProvider {
pstmt.executeUpdate(); pstmt.executeUpdate();
insertGroups(rosterID, item.getGroups().iterator(), con); insertGroups(rosterID, item.getGroups().iterator(), con);
} }
catch (SQLException e) { catch (SQLException e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
} }
...@@ -200,10 +191,7 @@ public class RosterItemProvider { ...@@ -200,10 +191,7 @@ public class RosterItemProvider {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
} }
...@@ -217,11 +205,12 @@ public class RosterItemProvider { ...@@ -217,11 +205,12 @@ public class RosterItemProvider {
List<String> answer = new ArrayList<String>(); List<String> answer = new ArrayList<String>();
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_USERNAMES); pstmt = con.prepareStatement(LOAD_USERNAMES);
pstmt.setString(1, jid); pstmt.setString(1, jid);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
answer.add(rs.getString(1)); answer.add(rs.getString(1));
} }
...@@ -230,10 +219,7 @@ public class RosterItemProvider { ...@@ -230,10 +219,7 @@ public class RosterItemProvider {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
return answer.iterator(); return answer.iterator();
} }
...@@ -248,11 +234,12 @@ public class RosterItemProvider { ...@@ -248,11 +234,12 @@ public class RosterItemProvider {
int count = 0; int count = 0;
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try { try {
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(COUNT_ROSTER_ITEMS); pstmt = con.prepareStatement(COUNT_ROSTER_ITEMS);
pstmt.setString(1, username); pstmt.setString(1, username);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
if (rs.next()) { if (rs.next()) {
count = rs.getInt(1); count = rs.getInt(1);
} }
...@@ -261,10 +248,7 @@ public class RosterItemProvider { ...@@ -261,10 +248,7 @@ public class RosterItemProvider {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
return count; return count;
} }
...@@ -283,12 +267,13 @@ public class RosterItemProvider { ...@@ -283,12 +267,13 @@ public class RosterItemProvider {
LinkedList<RosterItem> itemList = new LinkedList<RosterItem>(); LinkedList<RosterItem> itemList = new LinkedList<RosterItem>();
Connection con = null; Connection con = null;
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
ResultSet rs = null;
try { try {
// Load all the contacts in the roster // Load all the contacts in the roster
con = DbConnectionManager.getConnection(); con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_ROSTER); pstmt = con.prepareStatement(LOAD_ROSTER);
pstmt.setString(1, username); pstmt.setString(1, username);
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
// Create a new RosterItem (ie. user contact) from the stored information // Create a new RosterItem (ie. user contact) from the stored information
RosterItem item = new RosterItem(rs.getLong(2), RosterItem item = new RosterItem(rs.getLong(2),
...@@ -302,10 +287,10 @@ public class RosterItemProvider { ...@@ -302,10 +287,10 @@ public class RosterItemProvider {
itemList.add(item); itemList.add(item);
} }
// Close the statement and result set // Close the statement and result set
pstmt.close();
rs.close(); rs.close();
pstmt.close();
// Set null to pstmt to be sure that it's not closed twice. It seems that // Set null to pstmt to be sure that it's not closed twice. It seems that
// Sybase driver is raising an error when trying to close an alrady closed statement. // Sybase driver is raising an error when trying to close an already closed statement.
pstmt = null; pstmt = null;
// Load the groups for the loaded contact // Load the groups for the loaded contact
...@@ -316,21 +301,13 @@ public class RosterItemProvider { ...@@ -316,21 +301,13 @@ public class RosterItemProvider {
while (rs.next()) { while (rs.next()) {
item.getGroups().add(rs.getString(1)); item.getGroups().add(rs.getString(1));
} }
// Close the result set
rs.close();
// Close the prepared statement
pstmt.close();
pstmt = null;
} }
} }
catch (SQLException e) { catch (SQLException e) {
Log.error(LocaleUtils.getLocalizedString("admin.error"), e); Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
} }
finally { finally {
try { if (pstmt != null) { pstmt.close(); } } DbConnectionManager.closeConnection(rs, pstmt, con);
catch (Exception e) { Log.error(e); }
try { if (con != null) { con.close(); } }
catch (Exception e) { Log.error(e); }
} }
return itemList.iterator(); return itemList.iterator();
} }
...@@ -338,19 +315,20 @@ public class RosterItemProvider { ...@@ -338,19 +315,20 @@ public class RosterItemProvider {
/** /**
* Insert the groups into the given roster item. * Insert the groups into the given roster item.
* *
* @param rosterID The roster ID of the item the groups belong to * @param rosterID the roster ID of the item the groups belong to
* @param iter An iterator over the group names to insert * @param iter an iterator over the group names to insert
* @param con the database connection to use for the operation.
* @throws SQLException if an SQL exception occurs.
*/ */
private void insertGroups(long rosterID, Iterator<String> iter, Connection con) throws SQLException private void insertGroups(long rosterID, Iterator<String> iter, Connection con) throws SQLException
{ {
PreparedStatement pstmt = null; PreparedStatement pstmt = null;
String groupName = null;
try { try {
pstmt = con.prepareStatement(CREATE_ROSTER_ITEM_GROUPS); pstmt = con.prepareStatement(CREATE_ROSTER_ITEM_GROUPS);
pstmt.setLong(1, rosterID); pstmt.setLong(1, rosterID);
for (int i = 0; iter.hasNext(); i++) { for (int i = 0; iter.hasNext(); i++) {
pstmt.setInt(2, i); pstmt.setInt(2, i);
groupName = iter.next(); String groupName = iter.next();
pstmt.setString(3, groupName); pstmt.setString(3, groupName);
try { try {
pstmt.executeUpdate(); pstmt.executeUpdate();
...@@ -361,14 +339,7 @@ public class RosterItemProvider { ...@@ -361,14 +339,7 @@ public class RosterItemProvider {
} }
} }
finally { finally {
try { DbConnectionManager.closeStatement(pstmt);
if (pstmt != null) {
pstmt.close();
}
}
catch (Exception e) {
Log.error("Error inserting group: " + groupName + " for rosterID: " + rosterID, e);
}
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment