001: /**
002: * @author garethc
003: * 22/10/2002 11:40:10
004: */package vqwiki.db;
005:
006: import vqwiki.AbstractNotify;
007: import vqwiki.Environment;
008:
009: import java.sql.Connection;
010: import java.sql.PreparedStatement;
011: import java.sql.ResultSet;
012: import java.util.ArrayList;
013: import java.util.Collection;
014: import java.util.Properties;
015: import java.io.InputStream;
016: import java.io.IOException;
017:
018: import org.apache.log4j.Logger;
019:
020: public class DatabaseNotify extends AbstractNotify {
021:
022: private static final Logger logger = Logger
023: .getLogger(DatabaseNotify.class);
024: private static Properties statements;
025:
026: /**
027: *
028: */
029: public DatabaseNotify(String virtualWiki, String topicName) {
030: super ();
031: this .virtualWiki = virtualWiki;
032: this .topicName = topicName;
033: }
034:
035: /**
036: *
037: */
038: public void addMember(String userName) throws Exception {
039: Connection conn = null;
040: try {
041: conn = DatabaseConnection.getConnection();
042: PreparedStatement stmt = conn
043: .prepareStatement(getStatement("STATEMENT_ADD_MEMBER"));
044: stmt.setString(1, this .topicName);
045: stmt.setString(2, userName);
046: stmt.setString(3, this .virtualWiki);
047: stmt.execute();
048: stmt.close();
049: } finally {
050: DatabaseConnection.closeConnection(conn);
051: }
052: }
053:
054: /**
055: *
056: */
057: public void removeMember(String userName) throws Exception {
058: Connection conn = null;
059: try {
060: conn = DatabaseConnection.getConnection();
061: PreparedStatement stmt = conn
062: .prepareStatement(getStatement("STATEMENT_REMOVE_MEMBER"));
063: stmt.setString(1, this .topicName);
064: stmt.setString(2, userName);
065: stmt.setString(3, this .virtualWiki);
066: stmt.execute();
067: stmt.close();
068: } finally {
069: DatabaseConnection.closeConnection(conn);
070: }
071: }
072:
073: /**
074: *
075: */
076: public boolean isMember(String userName) throws Exception {
077: Connection conn = null;
078: int count = 0;
079: try {
080: conn = DatabaseConnection.getConnection();
081: PreparedStatement stmt = conn
082: .prepareStatement(getStatement("STATEMENT_IS_MEMBER"));
083: stmt.setString(1, this .topicName);
084: stmt.setString(2, userName);
085: stmt.setString(3, this .virtualWiki);
086: ResultSet rs = stmt.executeQuery();
087: rs.next();
088: count = rs.getInt(1);
089: rs.close();
090: stmt.close();
091: } finally {
092: DatabaseConnection.closeConnection(conn);
093: }
094: return count > 0;
095: }
096:
097: /**
098: *
099: */
100: public Collection getMembers() throws Exception {
101: Collection all = new ArrayList();
102: Connection conn = null;
103: try {
104: conn = DatabaseConnection.getConnection();
105: PreparedStatement stmt = conn
106: .prepareStatement(getStatement("STATEMENT_GET_MEMBERS"));
107: stmt.setString(1, this .topicName);
108: stmt.setString(2, this .virtualWiki);
109: ResultSet rs = stmt.executeQuery();
110: while (rs.next()) {
111: String username;
112: if (Environment.getInstance().isMySQL()) {
113: username = rs.getString("user");
114: } else {
115: username = rs.getString("wikiuser");
116: }
117: all.add(username);
118: }
119: rs.close();
120: stmt.close();
121: } finally {
122: DatabaseConnection.closeConnection(conn);
123: }
124: return all;
125: }
126:
127: /**
128: *
129: */
130: public static Collection getAllNotifications(String virtualWiki)
131: throws Exception {
132: Collection all = new ArrayList();
133: Connection conn = null;
134: try {
135: conn = DatabaseConnection.getConnection();
136: PreparedStatement stmt = conn
137: .prepareStatement(getStatement("STATEMENT_ALL_NOTIFICATIONS"));
138: stmt.setString(1, virtualWiki);
139: ResultSet rs = stmt.executeQuery();
140: while (rs.next()) {
141: DatabaseNotify notify = new DatabaseNotify(rs
142: .getString("virtualwiki"), rs
143: .getString("topic"));
144: all.add(notify);
145: }
146: rs.close();
147: stmt.close();
148: } finally {
149: DatabaseConnection.closeConnection(conn);
150: }
151: return all;
152: }
153:
154: /**
155: *
156: */
157: private static String getStatement(String key) {
158: if (statements == null) {
159: String statementResource, databaseType;
160: databaseType = Environment.getInstance().getDatabaseType();
161: statementResource = "/notify_" + databaseType
162: + ".sql.properties";
163: try {
164: InputStream in = DatabaseNotify.class
165: .getResourceAsStream(statementResource);
166: if (in == null) {
167: logger.error("statement file not found: "
168: + statementResource);
169: return null;
170: }
171: statements = new Properties();
172: statements.load(in);
173: in.close();
174: } catch (IOException e) {
175: logger.error("Error getting statements from file", e);
176: return null;
177: }
178: }
179: String statement = statements.getProperty(key);
180: logger.debug("statement for " + key + " = " + statement);
181: return statement.trim();
182: }
183: }
|