KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > vcard > DefaultVCardProvider


1 /**
2  * $RCSfile: DefaultVCardProvider.java,v $
3  * $Revision: 1.1 $
4  * $Date: 2005/07/20 03:20:39 $
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.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 JavaDoc;
22 import java.sql.ResultSet JavaDoc;
23 import java.sql.Connection JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.io.StringReader JavaDoc;
26
27 /**
28  * Default implementation of the VCardProvider interface, which reads and writes data
29  * from the <tt>jiveVCard</tt> database table.
30  *
31  * @author Gaston Dombiak
32  */

33 public class DefaultVCardProvider implements VCardProvider {
34
35     private static final String JavaDoc LOAD_PROPERTIES =
36         "SELECT value FROM jiveVCard WHERE username=?";
37     private static final String JavaDoc DELETE_PROPERTIES =
38         "DELETE FROM jiveVCard WHERE username=?";
39     private static final String JavaDoc UPDATE_PROPERTIES =
40         "UPDATE jiveVCard SET value=? WHERE username=?";
41     private static final String JavaDoc INSERT_PROPERTY =
42         "INSERT INTO jiveVCard (username, value) VALUES (?, ?)";
43
44     private SAXReader saxReader = new SAXReader();
45
46
47     public Element loadVCard(String JavaDoc username) {
48         synchronized (username.intern()) {
49             Element vCardElement = null;
50             java.sql.Connection JavaDoc con = null;
51             PreparedStatement JavaDoc pstmt = null;
52             try {
53                 con = DbConnectionManager.getConnection();
54                 pstmt = con.prepareStatement(LOAD_PROPERTIES);
55                 pstmt.setString(1, username);
56                 ResultSet JavaDoc rs = pstmt.executeQuery();
57                 while (rs.next()) {
58                     vCardElement =
59                             saxReader.read(new StringReader JavaDoc(rs.getString(1))).getRootElement();
60                 }
61             }
62             catch (Exception JavaDoc e) {
63                 Log.error(e);
64             }
65             finally {
66                 try { if (pstmt != null) { pstmt.close(); } }
67                 catch (Exception JavaDoc e) { Log.error(e); }
68                 try { if (con != null) { con.close(); } }
69                 catch (Exception JavaDoc e) { Log.error(e); }
70             }
71             return vCardElement;
72         }
73     }
74
75     public void createVCard(String JavaDoc username, Element vCardElement) throws AlreadyExistsException {
76         if (loadVCard(username) != null) {
77             // The user already has a vCard
78
throw new AlreadyExistsException("Username " + username + " already has a vCard");
79         }
80
81         Connection JavaDoc con = null;
82         PreparedStatement JavaDoc 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 JavaDoc e) {
91             Log.error(e);
92         }
93         finally {
94             try { if (pstmt != null) { pstmt.close(); } }
95             catch (Exception JavaDoc e) { Log.error(e); }
96             try { if (con != null) { con.close(); } }
97             catch (Exception JavaDoc e) { Log.error(e); }
98         }
99     }
100
101     public void updateVCard(String JavaDoc username, Element vCardElement) throws NotFoundException {
102         if (loadVCard(username) == null) {
103             // The user already has a vCard
104
throw new NotFoundException("Username " + username + " does not have a vCard");
105         }
106         Connection JavaDoc con = null;
107         PreparedStatement JavaDoc 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 JavaDoc e) {
116             Log.error(e);
117         }
118         finally {
119             try { if (pstmt != null) { pstmt.close(); } }
120             catch (Exception JavaDoc e) { Log.error(e); }
121             try { if (con != null) { con.close(); } }
122             catch (Exception JavaDoc e) { Log.error(e); }
123         }
124     }
125
126     public void deleteVCard(String JavaDoc username) {
127         Connection JavaDoc con = null;
128         PreparedStatement JavaDoc 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 JavaDoc e) {
136             Log.error(e);
137         }
138         finally {
139             try { if (pstmt != null) { pstmt.close(); } }
140             catch (Exception JavaDoc e) { Log.error(e); }
141             try { if (con != null) { con.close(); } }
142             catch (Exception JavaDoc e) { Log.error(e); }
143         }
144     }
145
146     public boolean isReadOnly() {
147         return false;
148     }
149 }
150
Popular Tags