1 11 12 package org.jivesoftware.messenger; 13 14 import org.jivesoftware.database.DbConnectionManager; 15 import org.jivesoftware.util.Log; 16 import org.jivesoftware.util.Cache; 17 import org.jivesoftware.util.CacheManager; 18 19 import java.util.*; 20 import java.sql.*; 21 import java.sql.Connection ; 22 23 28 public class VCardManager { 29 30 private static final String LOAD_PROPERTIES = 31 "SELECT name, propValue FROM jiveVCard WHERE username=?"; 32 private static final String DELETE_PROPERTY = 33 "DELETE FROM jiveVCard WHERE username=? AND name=?"; 34 private static final String UPDATE_PROPERTY = 35 "UPDATE jiveVCard SET propValue=? WHERE name=? AND username=?"; 36 private static final String INSERT_PROPERTY = 37 "INSERT INTO jiveVCard (username, name, propValue) VALUES (?, ?, ?)"; 38 39 40 private static VCardManager instance = new VCardManager(); 41 42 public static VCardManager getInstance() { 43 return instance; 44 } 45 46 private Cache vcardCache; 47 48 private VCardManager() { 49 CacheManager.initializeCache("vcardCache", 512 * 1024); 50 vcardCache = CacheManager.getCache("vcardCache"); 51 } 52 53 62 public String getVCardProperty(String username, String name) { 63 Map<String ,String > properties = (Map<String ,String >)vcardCache.get(username); 64 if (properties == null) { 65 properties = loadPropertiesFromDb(username); 66 vcardCache.put(username, properties); 67 } 68 return properties.get(name); 69 } 70 71 79 public void setVCardProperty(String username, String name, String value) { 80 Map<String ,String > properties = (Map<String ,String >)vcardCache.get(username); 81 if (properties == null) { 82 properties = loadPropertiesFromDb(username); 83 vcardCache.put(username, properties); 84 } 85 if (properties.containsKey(name)) { 87 if (!(value.equals(properties.get(name)))) { 90 properties.put(name, value); 91 updatePropertyInDb(username, name, value); 92 } 93 } 94 else { 95 properties.put(name, value); 96 insertPropertyIntoDb(username, name, value); 97 } 98 } 99 100 105 public void deleteVCardProperty(String username, String name) { 106 Map<String ,String > properties = (Map<String ,String >)vcardCache.get(username); 107 if (properties == null) { 108 properties = loadPropertiesFromDb(username); 109 vcardCache.put(username, properties); 110 } 111 if (properties.remove(name) != null) { 112 deletePropertyFromDb(username, name); 114 } 115 } 116 117 122 public Collection<String > getVCardPropertyNames(String username) { 123 Map<String ,String > properties = (Map<String ,String >)vcardCache.get(username); 124 if (properties == null) { 125 properties = loadPropertiesFromDb(username); 126 vcardCache.put(username, properties); 127 } 128 return Collections.unmodifiableCollection(properties.keySet()); 129 } 130 131 private Map<String ,String > loadPropertiesFromDb(String username) { 132 synchronized (username.intern()) { 133 Map<String ,String > properties = new Hashtable<String ,String >(); 134 java.sql.Connection con = null; 135 PreparedStatement pstmt = null; 136 try { 137 con = DbConnectionManager.getConnection(); 138 pstmt = con.prepareStatement(LOAD_PROPERTIES); 139 pstmt.setString(1, username); 140 ResultSet rs = pstmt.executeQuery(); 141 while (rs.next()) { 142 properties.put(rs.getString(1), rs.getString(2)); 143 } 144 } 145 catch (SQLException e) { 146 Log.error(e); 147 } 148 finally { 149 try { if (pstmt != null) { pstmt.close(); } } 150 catch (Exception e) { Log.error(e); } 151 try { if (con != null) { con.close(); } } 152 catch (Exception e) { Log.error(e); } 153 } 154 return properties; 155 } 156 } 157 158 private void insertPropertyIntoDb(String username, String name, String value) { 159 Connection con = null; 160 PreparedStatement pstmt = null; 161 162 try { 163 con = DbConnectionManager.getConnection(); 164 pstmt = con.prepareStatement(INSERT_PROPERTY); 165 pstmt.setString(1, username); 166 pstmt.setString(2, name); 167 pstmt.setString(3, value); 168 pstmt.executeUpdate(); 169 } 170 catch (SQLException e) { 171 Log.error(e); 172 } 173 finally { 174 try { if (pstmt != null) { pstmt.close(); } } 175 catch (Exception e) { Log.error(e); } 176 try { if (con != null) { con.close(); } } 177 catch (Exception e) { Log.error(e); } 178 } 179 } 180 181 private void updatePropertyInDb(String username, String name, String value) { 182 Connection con = null; 183 PreparedStatement pstmt = null; 184 try { 185 con = DbConnectionManager.getConnection(); 186 pstmt = con.prepareStatement(UPDATE_PROPERTY); 187 pstmt.setString(1, value); 188 pstmt.setString(2, name); 189 pstmt.setString(3, username); 190 pstmt.executeUpdate(); 191 } 192 catch (SQLException e) { 193 Log.error(e); 194 } 195 finally { 196 try { if (pstmt != null) { pstmt.close(); } } 197 catch (Exception e) { Log.error(e); } 198 try { if (con != null) { con.close(); } } 199 catch (Exception e) { Log.error(e); } 200 } 201 } 202 203 private void deletePropertyFromDb(String username, String name) { 204 Connection con = null; 205 PreparedStatement pstmt = null; 206 try { 207 con = DbConnectionManager.getConnection(); 208 pstmt = con.prepareStatement(DELETE_PROPERTY); 209 pstmt.setString(1, username); 210 pstmt.setString(2, name); 211 pstmt.executeUpdate(); 212 } 213 catch (SQLException e) { 214 Log.error(e); 215 } 216 finally { 217 try { if (pstmt != null) { pstmt.close(); } } 218 catch (Exception e) { Log.error(e); } 219 try { if (con != null) { con.close(); } } 220 catch (Exception e) { Log.error(e); } 221 } 222 } 223 } | Popular Tags |