1 11 12 package org.jivesoftware.messenger.roster; 13 14 import org.jivesoftware.messenger.user.UserAlreadyExistsException; 15 import org.jivesoftware.messenger.user.UserNotFoundException; 16 import org.jivesoftware.database.DbConnectionManager; 17 import org.jivesoftware.database.SequenceManager; 18 import org.jivesoftware.util.JiveConstants; 19 import org.jivesoftware.util.Log; 20 import org.jivesoftware.util.LocaleUtils; 21 import org.xmpp.packet.JID; 22 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.ArrayList ; 26 import java.util.LinkedList ; 27 import java.sql.Connection ; 28 import java.sql.PreparedStatement ; 29 import java.sql.SQLException ; 30 import java.sql.ResultSet ; 31 32 43 public class RosterItemProvider { 44 45 private static final String CREATE_ROSTER_ITEM = 46 "INSERT INTO jiveRoster (username, rosterID, jid, sub, ask, recv, nick) " + 47 "VALUES (?, ?, ?, ?, ?, ?, ?)"; 48 private static final String UPDATE_ROSTER_ITEM = 49 "UPDATE jiveRoster SET sub=?, ask=?, recv=?, nick=? WHERE rosterID=?"; 50 private static final String DELETE_ROSTER_ITEM_GROUPS = 51 "DELETE FROM jiveRosterGroups WHERE rosterID=?"; 52 private static final String CREATE_ROSTER_ITEM_GROUPS = 53 "INSERT INTO jiveRosterGroups (rosterID, rank, groupName) VALUES (?, ?, ?)"; 54 private static final String DELETE_ROSTER_ITEM = 55 "DELETE FROM jiveRoster WHERE rosterID=?"; 56 private static final String LOAD_USERNAMES = 57 "SELECT DISTINCT username from jiveRoster WHERE jid=?"; 58 private static final String COUNT_ROSTER_ITEMS = 59 "SELECT COUNT(rosterID) FROM jiveRoster WHERE username=?"; 60 private static final String LOAD_ROSTER = 61 "SELECT jid, rosterID, sub, ask, recv, nick FROM jiveRoster WHERE username=?"; 62 private static final String LOAD_ROSTER_ITEM_GROUPS = 63 "SELECT groupName FROM jiveRosterGroups WHERE rosterID=? ORDER BY rank"; 64 65 66 private static RosterItemProvider instance = new RosterItemProvider(); 67 68 public static RosterItemProvider getInstance() { 69 return instance; 70 } 71 72 87 public RosterItem createItem(String username, RosterItem item) 88 throws UserAlreadyExistsException 89 { 90 Connection con = null; 91 PreparedStatement pstmt = null; 92 try { 93 con = DbConnectionManager.getConnection(); 94 95 long rosterID = SequenceManager.nextID(JiveConstants.ROSTER); 96 pstmt = con.prepareStatement(CREATE_ROSTER_ITEM); 97 pstmt.setString(1, username); 98 pstmt.setLong(2, rosterID); 99 pstmt.setString(3, item.getJid().toBareJID()); 100 pstmt.setInt(4, item.getSubStatus().getValue()); 101 pstmt.setInt(5, item.getAskStatus().getValue()); 102 pstmt.setInt(6, item.getRecvStatus().getValue()); 103 pstmt.setString(7, item.getNickname()); 104 pstmt.executeUpdate(); 105 106 item.setID(rosterID); 107 insertGroups(rosterID, item.getGroups().iterator(), pstmt, con); 108 } 109 catch (SQLException e) { 110 throw new UserAlreadyExistsException(item.getJid().toBareJID()); 111 } 112 finally { 113 try { if (pstmt != null) { pstmt.close(); } } 114 catch (Exception e) { Log.error(e); } 115 try { if (con != null) { con.close(); } } 116 catch (Exception e) { Log.error(e); } 117 } 118 return item; 119 } 120 121 130 public void updateItem(String username, RosterItem item) throws UserNotFoundException { 131 Connection con = null; 132 PreparedStatement pstmt = null; 133 long rosterID = item.getID(); 134 try { 135 con = DbConnectionManager.getConnection(); 136 pstmt = con.prepareStatement(UPDATE_ROSTER_ITEM); 138 pstmt.setInt(1, item.getSubStatus().getValue()); 139 pstmt.setInt(2, item.getAskStatus().getValue()); 140 pstmt.setInt(3, item.getRecvStatus().getValue()); 141 pstmt.setString(4, item.getNickname()); 142 pstmt.setLong(5, rosterID); 143 pstmt.executeUpdate(); 144 145 pstmt = con.prepareStatement(DELETE_ROSTER_ITEM_GROUPS); 147 pstmt.setLong(1, rosterID); 148 pstmt.executeUpdate(); 149 150 insertGroups(rosterID, item.getGroups().iterator(), pstmt, con); 151 152 } 153 catch (SQLException e) { 154 Log.error(LocaleUtils.getLocalizedString("admin.error"), e); 155 } 156 finally { 157 try { if (pstmt != null) { pstmt.close(); } } 158 catch (Exception e) { Log.error(e); } 159 try { if (con != null) { con.close(); } } 160 catch (Exception e) { Log.error(e); } 161 } 162 } 163 164 172 public void deleteItem(String username, long rosterItemID) { 173 Connection con = null; 175 PreparedStatement pstmt = null; 176 try { 177 con = DbConnectionManager.getConnection(); 178 pstmt = con.prepareStatement(DELETE_ROSTER_ITEM_GROUPS); 180 181 pstmt.setLong(1, rosterItemID); 182 pstmt.executeUpdate(); 183 184 pstmt = con.prepareStatement(DELETE_ROSTER_ITEM); 186 187 pstmt.setLong(1, rosterItemID); 188 pstmt.executeUpdate(); 189 } 190 catch (SQLException e) { 191 Log.error(LocaleUtils.getLocalizedString("admin.error"), e); 192 } 193 finally { 194 try { if (pstmt != null) { pstmt.close(); } } 195 catch (Exception e) { Log.error(e); } 196 try { if (con != null) { con.close(); } } 197 catch (Exception e) { Log.error(e); } 198 } 199 } 200 201 207 public Iterator <String > getUsernames(String jid) { 208 List <String > answer = new ArrayList <String >(); 209 Connection con = null; 210 PreparedStatement pstmt = null; 211 try { 212 con = DbConnectionManager.getConnection(); 213 pstmt = con.prepareStatement(LOAD_USERNAMES); 214 pstmt.setString(1, jid); 215 ResultSet rs = pstmt.executeQuery(); 216 while (rs.next()) { 217 answer.add(rs.getString(1)); 218 } 219 } 220 catch (SQLException e) { 221 Log.error(LocaleUtils.getLocalizedString("admin.error"), e); 222 } 223 finally { 224 try { if (pstmt != null) { pstmt.close(); } } 225 catch (Exception e) { Log.error(e); } 226 try { if (con != null) { con.close(); } } 227 catch (Exception e) { Log.error(e); } 228 } 229 return answer.iterator(); 230 } 231 232 238 public int getItemCount(String username) { 239 int count = 0; 240 Connection con = null; 241 PreparedStatement pstmt = null; 242 try { 243 con = DbConnectionManager.getConnection(); 244 pstmt = con.prepareStatement(COUNT_ROSTER_ITEMS); 245 pstmt.setString(1, username); 246 ResultSet rs = pstmt.executeQuery(); 247 if (rs.next()) { 248 count = rs.getInt(1); 249 } 250 } 251 catch (SQLException e) { 252 Log.error(LocaleUtils.getLocalizedString("admin.error"), e); 253 } 254 finally { 255 try { if (pstmt != null) { pstmt.close(); } } 256 catch (Exception e) { Log.error(e); } 257 try { if (con != null) { con.close(); } } 258 catch (Exception e) { Log.error(e); } 259 } 260 return count; 261 } 262 263 273 public Iterator getItems(String username) { 274 LinkedList itemList = new LinkedList (); 275 Connection con = null; 276 PreparedStatement pstmt = null; 277 try { 278 con = DbConnectionManager.getConnection(); 279 pstmt = con.prepareStatement(LOAD_ROSTER); 280 pstmt.setString(1, username); 281 ResultSet rs = pstmt.executeQuery(); 282 while (rs.next()) { 285 RosterItem item = new RosterItem(rs.getLong(2), 286 new JID(rs.getString(1)), 287 RosterItem.SubType.getTypeFromInt(rs.getInt(3)), 288 RosterItem.AskType.getTypeFromInt(rs.getInt(4)), 289 RosterItem.RecvType.getTypeFromInt(rs.getInt(5)), 290 rs.getString(6), 291 null); 292 Connection con2 = DbConnectionManager.getConnection(); 293 PreparedStatement gstmt = null; 294 ResultSet gs = null; 295 try { 296 gstmt = con2.prepareStatement(LOAD_ROSTER_ITEM_GROUPS); 297 gstmt.setLong(1, item.getID()); 298 gs = gstmt.executeQuery(); 299 while (gs.next()) { 300 item.getGroups().add(gs.getString(1)); 301 } 302 itemList.add(item); 303 } 304 finally { 305 try { if (gstmt != null) { gstmt.close(); } } 306 catch (Exception e) { Log.error(e); } 307 try { if (con2 != null) { con2.close(); } } 308 catch (Exception e) { Log.error(e); } 309 } 310 } 311 } 312 catch (SQLException e) { 313 Log.error(LocaleUtils.getLocalizedString("admin.error"), e); 314 } 315 finally { 316 try { if (pstmt != null) { pstmt.close(); } } 317 catch (Exception e) { Log.error(e); } 318 try { if (con != null) { con.close(); } } 319 catch (Exception e) { Log.error(e); } 320 } 321 return itemList.iterator(); 322 } 323 324 330 private void insertGroups(long rosterID, Iterator iter, PreparedStatement pstmt, 331 Connection con) throws SQLException 332 { 333 try { 334 pstmt = con.prepareStatement(CREATE_ROSTER_ITEM_GROUPS); 335 pstmt.setLong(1, rosterID); 336 for (int i = 0; iter.hasNext(); i++) { 337 pstmt.setInt(2, i); 338 pstmt.setString(3, (String )iter.next()); 339 try { 340 pstmt.executeUpdate(); 341 } 342 catch (SQLException e) { 343 Log.error(e); 344 } 345 } 346 } 347 finally { 348 try { 349 if (pstmt != null) { 350 pstmt.close(); 351 } 352 } 353 catch (Exception e) { 354 Log.error(e); 355 } 356 } 357 } 358 } 359 | Popular Tags |