KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > VCardManager


1 /**
2  * $RCSfile: VCardManager.java,v $
3  * $Revision: 1.2 $
4  * $Date: 2005/04/28 03:26:06 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

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 JavaDoc;
22
23 /**
24  * Manages VCard information for users.
25  *
26  * @author Matt Tucker
27  */

28 public class VCardManager {
29
30     private static final String JavaDoc LOAD_PROPERTIES =
31         "SELECT name, propValue FROM jiveVCard WHERE username=?";
32     private static final String JavaDoc DELETE_PROPERTY =
33         "DELETE FROM jiveVCard WHERE username=? AND name=?";
34     private static final String JavaDoc UPDATE_PROPERTY =
35         "UPDATE jiveVCard SET propValue=? WHERE name=? AND username=?";
36     private static final String JavaDoc 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     /**
54      * Obtains the user's vCard information for a given vcard property name.
55      * Advanced user systems can use vCard
56      * information to link to user directory information or store other
57      * relevant user information.
58      *
59      * @param name The name of the vcard property to retrieve
60      * @return The vCard value found
61      */

62     public String JavaDoc getVCardProperty(String JavaDoc username, String JavaDoc name) {
63         Map<String JavaDoc,String JavaDoc> properties = (Map<String JavaDoc,String JavaDoc>)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     /**
72      * Sets the user's vCard information. Advanced user systems can use vCard
73      * information to link to user directory information or store other
74      * relevant user information.
75      *
76      * @param name The name of the vcard property
77      * @param value The value of the vcard property
78      */

79     public void setVCardProperty(String JavaDoc username, String JavaDoc name, String JavaDoc value) {
80         Map<String JavaDoc,String JavaDoc> properties = (Map<String JavaDoc,String JavaDoc>)vcardCache.get(username);
81         if (properties == null) {
82             properties = loadPropertiesFromDb(username);
83             vcardCache.put(username, properties);
84         }
85         // See if we need to update a property value or insert a new one.
86
if (properties.containsKey(name)) {
87             // Only update the value in the database if the property value
88
// has changed.
89
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     /**
101      * Deletes a given vCard property from the user account.
102      *
103      * @param name The name of the vcard property to remove
104      */

105     public void deleteVCardProperty(String JavaDoc username, String JavaDoc name) {
106         Map<String JavaDoc,String JavaDoc> properties = (Map<String JavaDoc,String JavaDoc>)vcardCache.get(username);
107         if (properties == null) {
108             properties = loadPropertiesFromDb(username);
109             vcardCache.put(username, properties);
110         }
111         if (properties.remove(name) != null) {
112             // Delete the property from the DB if it was present in memory
113
deletePropertyFromDb(username, name);
114         }
115     }
116
117     /**
118      * Obtain an iterator for all vcard property names.
119      *
120      * @return the iterator over all vcard property names.
121      */

122     public Collection<String JavaDoc> getVCardPropertyNames(String JavaDoc username) {
123         Map<String JavaDoc,String JavaDoc> properties = (Map<String JavaDoc,String JavaDoc>)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 JavaDoc,String JavaDoc> loadPropertiesFromDb(String JavaDoc username) {
132         synchronized (username.intern()) {
133             Map<String JavaDoc,String JavaDoc> properties = new Hashtable<String JavaDoc,String JavaDoc>();
134             java.sql.Connection JavaDoc 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 JavaDoc e) { Log.error(e); }
151                 try { if (con != null) { con.close(); } }
152                 catch (Exception JavaDoc e) { Log.error(e); }
153             }
154             return properties;
155         }
156     }
157
158     private void insertPropertyIntoDb(String JavaDoc username, String JavaDoc name, String JavaDoc 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 JavaDoc e) { Log.error(e); }
176             try { if (con != null) { con.close(); } }
177             catch (Exception JavaDoc e) { Log.error(e); }
178         }
179     }
180
181     private void updatePropertyInDb(String JavaDoc username, String JavaDoc name, String JavaDoc 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 JavaDoc e) { Log.error(e); }
198             try { if (con != null) { con.close(); } }
199             catch (Exception JavaDoc e) { Log.error(e); }
200         }
201     }
202
203     private void deletePropertyFromDb(String JavaDoc username, String JavaDoc 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 JavaDoc e) { Log.error(e); }
219             try { if (con != null) { con.close(); } }
220             catch (Exception JavaDoc e) { Log.error(e); }
221         }
222     }
223 }
Popular Tags