1 11 12 package org.jivesoftware.messenger.vcard; 13 14 import org.dom4j.Element; 15 import org.dom4j.io.SAXReader; 16 import org.jivesoftware.database.DbConnectionManager; 17 import org.jivesoftware.util.Log; 18 import org.jivesoftware.util.AlreadyExistsException; 19 import org.jivesoftware.util.NotFoundException; 20 21 import java.sql.PreparedStatement ; 22 import java.sql.ResultSet ; 23 import java.sql.Connection ; 24 import java.sql.SQLException ; 25 import java.io.StringReader ; 26 27 33 public class DefaultVCardProvider implements VCardProvider { 34 35 private static final String LOAD_PROPERTIES = 36 "SELECT value FROM jiveVCard WHERE username=?"; 37 private static final String DELETE_PROPERTIES = 38 "DELETE FROM jiveVCard WHERE username=?"; 39 private static final String UPDATE_PROPERTIES = 40 "UPDATE jiveVCard SET value=? WHERE username=?"; 41 private static final String INSERT_PROPERTY = 42 "INSERT INTO jiveVCard (username, value) VALUES (?, ?)"; 43 44 private SAXReader saxReader = new SAXReader(); 45 46 47 public Element loadVCard(String username) { 48 synchronized (username.intern()) { 49 Element vCardElement = null; 50 java.sql.Connection con = null; 51 PreparedStatement pstmt = null; 52 try { 53 con = DbConnectionManager.getConnection(); 54 pstmt = con.prepareStatement(LOAD_PROPERTIES); 55 pstmt.setString(1, username); 56 ResultSet rs = pstmt.executeQuery(); 57 while (rs.next()) { 58 vCardElement = 59 saxReader.read(new StringReader (rs.getString(1))).getRootElement(); 60 } 61 } 62 catch (Exception e) { 63 Log.error(e); 64 } 65 finally { 66 try { if (pstmt != null) { pstmt.close(); } } 67 catch (Exception e) { Log.error(e); } 68 try { if (con != null) { con.close(); } } 69 catch (Exception e) { Log.error(e); } 70 } 71 return vCardElement; 72 } 73 } 74 75 public void createVCard(String username, Element vCardElement) throws AlreadyExistsException { 76 if (loadVCard(username) != null) { 77 throw new AlreadyExistsException("Username " + username + " already has a vCard"); 79 } 80 81 Connection con = null; 82 PreparedStatement pstmt = null; 83 try { 84 con = DbConnectionManager.getConnection(); 85 pstmt = con.prepareStatement(INSERT_PROPERTY); 86 pstmt.setString(1, username); 87 pstmt.setString(2, vCardElement.asXML()); 88 pstmt.executeUpdate(); 89 } 90 catch (SQLException e) { 91 Log.error(e); 92 } 93 finally { 94 try { if (pstmt != null) { pstmt.close(); } } 95 catch (Exception e) { Log.error(e); } 96 try { if (con != null) { con.close(); } } 97 catch (Exception e) { Log.error(e); } 98 } 99 } 100 101 public void updateVCard(String username, Element vCardElement) throws NotFoundException { 102 if (loadVCard(username) == null) { 103 throw new NotFoundException("Username " + username + " does not have a vCard"); 105 } 106 Connection con = null; 107 PreparedStatement pstmt = null; 108 try { 109 con = DbConnectionManager.getConnection(); 110 pstmt = con.prepareStatement(UPDATE_PROPERTIES); 111 pstmt.setString(1, vCardElement.asXML()); 112 pstmt.setString(2, username); 113 pstmt.executeUpdate(); 114 } 115 catch (SQLException e) { 116 Log.error(e); 117 } 118 finally { 119 try { if (pstmt != null) { pstmt.close(); } } 120 catch (Exception e) { Log.error(e); } 121 try { if (con != null) { con.close(); } } 122 catch (Exception e) { Log.error(e); } 123 } 124 } 125 126 public void deleteVCard(String username) { 127 Connection con = null; 128 PreparedStatement pstmt = null; 129 try { 130 con = DbConnectionManager.getConnection(); 131 pstmt = con.prepareStatement(DELETE_PROPERTIES); 132 pstmt.setString(1, username); 133 pstmt.executeUpdate(); 134 } 135 catch (SQLException e) { 136 Log.error(e); 137 } 138 finally { 139 try { if (pstmt != null) { pstmt.close(); } } 140 catch (Exception e) { Log.error(e); } 141 try { if (con != null) { con.close(); } } 142 catch (Exception e) { Log.error(e); } 143 } 144 } 145 146 public boolean isReadOnly() { 147 return false; 148 } 149 } 150 | Popular Tags |