KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > forums > Subscription


1 /*
2   Copyright (C) 2004 Know Gate S.L. All rights reserved.
3                       C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.forums;
34
35 import java.sql.SQLException JavaDoc;
36 import java.sql.Statement JavaDoc;
37 import java.sql.ResultSet JavaDoc;
38
39 import com.knowgate.debug.DebugFile;
40 import com.knowgate.jdc.JDCConnection;
41 import com.knowgate.dataobjs.DB;
42 import com.knowgate.dataobjs.DBPersist;
43
44 /**
45  * <p>NewsGroup Subscription</p>
46  * <p>Handles subscriptions of users from k_users table to newsgroups from k_newsgroups table</p>
47  * @author Sergio Montoro Ten
48  * @version 2.0
49  */

50
51 public class Subscription extends DBPersist {
52
53   /**
54    * Default constructor
55    */

56   public Subscription() {
57     super(DB.k_newsgroup_subscriptions, "Subscription");
58   }
59
60   public Subscription(JDCConnection oConn, String JavaDoc sNewsGroupId, String JavaDoc sUserId)
61     throws SQLException JavaDoc {
62
63     super(DB.k_newsgroup_subscriptions, "Subscription");
64
65     load (oConn, new Object JavaDoc[]{sNewsGroupId, sUserId});
66   }
67
68   //----------------------------------------------------------------------------
69

70   /**
71    * <p>Subscribe ACLUser to a NewsGroup</p>
72    * <p>Newly created subscriptions are activated by default.</p>
73    * @param oConn JDBC Database Connection
74    * @param sNewsGroupId NewsGroup GUID
75    * @param sUserId ACLUser GUID
76    * @param sMessageFormat Message Format {TXT | HTM}
77    * @param sUserId iMessagesGrouping Message Grouping { GROUP_NONE | GROUP_DIGEST }
78    * @return <b>true</b> if ACLUser was successfully subscribed to NewsGroup, <b>false</b> if no ACLUser with such GUID was found at k_users table.
79    * @throws SQLException
80    */

81   public static boolean subscribe (JDCConnection oConn, String JavaDoc sNewsGroupId, String JavaDoc sUserId, String JavaDoc sMessageFormat, short iMessagesGrouping)
82     throws SQLException JavaDoc {
83
84     if (DebugFile.trace) {
85       DebugFile.writeln("Begin Subscription.subscribe ([Connection], " + sNewsGroupId + "," + sUserId + ")");
86       DebugFile.incIdent();
87     }
88
89     String JavaDoc sTxEmail;
90
91     Statement JavaDoc oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
92
93     if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery (SELECT " + DB.tx_main_email + " FROM " + DB.k_users + " WHERE " + DB.gu_user + "='" + sUserId + "')");
94
95     ResultSet JavaDoc oRSet = oStmt.executeQuery("SELECT " + DB.tx_main_email + " FROM " + DB.k_users + " WHERE " + DB.gu_user + "='" + sUserId + "'");
96
97     if (oRSet.next())
98       sTxEmail = oRSet.getString(1);
99     else
100       sTxEmail = null;
101
102     oRSet.close();
103     oStmt.close();
104
105     if (null!=sTxEmail) {
106       Subscription oUsrSubs = new Subscription();
107
108       oUsrSubs.put(DB.gu_newsgrp, sNewsGroupId);
109       oUsrSubs.put(DB.gu_user, sUserId);
110       oUsrSubs.put(DB.id_msg_type, sMessageFormat);
111       oUsrSubs.put(DB.tp_subscrip, iMessagesGrouping);
112       oUsrSubs.put(DB.tx_email, sTxEmail);
113
114       oUsrSubs.store(oConn);
115     } // fi (sTxEmail)
116

117     if (DebugFile.trace) {
118       DebugFile.decIdent();
119       DebugFile.writeln("End Subscription.subscribe() : " + (null!=sTxEmail ? sTxEmail : "false"));
120     }
121
122     return (null!=sTxEmail);
123   } // Subscribe
124

125   //----------------------------------------------------------------------------
126

127   /**
128    * <p>Subscribe ACLUser to a NewsGroup</p>
129    * <p>Message format is TXT with no grouping by default.</p>
130    * @param oConn JDBC Database Connection
131    * @param sNewsGroupId NewsGroup GUID
132    * @param sUserId ACLUser GUID
133    * @return <b>true</b> if ACLUser was successfully subscribed to NewsGroup, <b>false</b> if no ACLUser with such GUID was found at k_users table.
134    * @throws SQLException
135    */

136   public static boolean subscribe (JDCConnection oConn, String JavaDoc sNewsGroupId, String JavaDoc sUserId)
137     throws SQLException JavaDoc {
138
139     return subscribe (oConn, sNewsGroupId, sUserId, "TXT", Subscription.GROUP_NONE);
140   }
141
142   //----------------------------------------------------------------------------
143

144   /**
145    * <p>Unsubscribe ACLUser to a NewsGroup</p>
146    * <p>The ACLUser is removed from table k_newsgroup_subscriptions for given NewsGroup.<br>
147    * If you want to remove an e-mail directly first call ACLUser.getIdFromEmail() method.<br></p>
148    * @param oConn JDBC Database Connection
149    * @param sNewsGroupId NewsGroup GUID
150    * @param sUserId ACLUser GUID
151    * @return <b>true</b> if ACLUser was successfully unsubscribed from NewsGroup, <b>false</b> if no ACLUser with such GUID was found at k_newsgroup_subscriptions.
152    * @throws SQLException
153    */

154   public static boolean unsubscribe (JDCConnection oConn, String JavaDoc sNewsGroupId, String JavaDoc sUserId)
155     throws SQLException JavaDoc {
156
157     if (DebugFile.trace) {
158       DebugFile.writeln("Begin Subscription.unsubscribe ([Connection], " + sNewsGroupId + "," + sUserId + ")");
159       DebugFile.incIdent();
160     }
161
162     Statement JavaDoc oStmt = oConn.createStatement();
163
164     if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate (DELETE FROM " + DB.k_newsgroup_subscriptions + " WHERE " + DB.gu_newsgrp + "='" + sNewsGroupId + "' AND " + DB.gu_user + "='" + sUserId + "')");
165
166     int iAffected = oStmt.executeUpdate("DELETE FROM " + DB.k_newsgroup_subscriptions + " WHERE " + DB.gu_newsgrp + "='" + sNewsGroupId + "' AND " + DB.gu_user + "='" + sUserId + "'");
167
168     oStmt.close();
169
170     if (DebugFile.trace) {
171       DebugFile.decIdent();
172       DebugFile.writeln("End Subscription.unsubscribe() : " + (iAffected!=0 ? "true" : "false"));
173     }
174
175     return (iAffected!=0);
176   } // Unsubscribe
177

178   //----------------------------------------------------------------------------
179

180   /**
181    * <p>Activate a Subscription</p>
182    * <p>Set k_newsgroup_subscriptions.id_status=1</p>
183    * @param oConn JDBC Database Connection
184    * @param sNewsGroupId Newsgroup GUID
185    * @param sUserId ACLUser GUID
186    * @return <b>true</b> if subscription from ACLUser was successfully activated, <b>false</b> if no ACLUser with such GUID was found at k_newsgroup_subscriptions.
187    * @throws SQLException
188    */

189   public static boolean activate (JDCConnection oConn, String JavaDoc sNewsGroupId, String JavaDoc sUserId)
190     throws SQLException JavaDoc {
191
192     if (DebugFile.trace) {
193       DebugFile.writeln("Begin Subscription.activate ([Connection], " + sNewsGroupId + "," + sUserId + ")");
194       DebugFile.incIdent();
195     }
196
197     Statement JavaDoc oStmt = oConn.createStatement();
198
199     if (DebugFile.trace)
200       DebugFile.writeln("Statement.executeUpdate(UPDATE " + DB.k_newsgroup_subscriptions + " SET " + DB.id_status + "=" + String.valueOf(ACTIVE) + " WHERE " + DB.gu_newsgrp + "='" + sNewsGroupId + "' AND " + DB.gu_user + "='" + sUserId + "')");
201
202     int iAffected = oStmt.executeUpdate("UPDATE " + DB.k_newsgroup_subscriptions + " SET " + DB.id_status + "=" + String.valueOf(ACTIVE) + " WHERE " + DB.gu_newsgrp + "='" + sNewsGroupId + "' AND " + DB.gu_user + "='" + sUserId + "'");
203
204     oStmt.close();
205
206     if (DebugFile.trace) {
207       DebugFile.decIdent();
208       DebugFile.writeln("End Subscription.activate() : " + (iAffected!=0 ? "true" : "false"));
209     }
210
211     return (iAffected!=0);
212   } // activate
213

214   //----------------------------------------------------------------------------
215

216   /**
217    * <p>Deactivate a Subscription</p>
218    * <p>Set k_newsgroup_subscriptions.id_status=0</p>
219    * @param oConn JDBC Database Connection
220    * @param sNewsGroupId Newsgroup GUID
221    * @param sUserId ACLUser GUID
222    * @return <b>true</b> if subscription from ACLUser was successfully deactivated, <b>false</b> if no ACLUser with such GUID was found at k_newsgroup_subscriptions.
223    * @throws SQLException
224    */

225   public static boolean deactivate (JDCConnection oConn, String JavaDoc sNewsGroupId, String JavaDoc sUserId)
226     throws SQLException JavaDoc {
227
228     if (DebugFile.trace) {
229       DebugFile.writeln("Begin Subscription.deactivate ([Connection], " + sNewsGroupId + "," + sUserId + ")");
230       DebugFile.incIdent();
231     }
232
233     Statement JavaDoc oStmt = oConn.createStatement();
234
235     if (DebugFile.trace)
236       DebugFile.writeln("Statement.executeUpdate(UPDATE " + DB.k_newsgroup_subscriptions + " SET " + DB.id_status + "=" + String.valueOf(UNACTIVE) + " WHERE " + DB.gu_newsgrp + "='" + sNewsGroupId + "' AND " + DB.gu_user + "='" + sUserId + "')");
237
238     int iAffected = oStmt.executeUpdate("UPDATE " + DB.k_newsgroup_subscriptions + " SET " + DB.id_status + "=" + String.valueOf(UNACTIVE) + " WHERE " + DB.gu_newsgrp + "='" + sNewsGroupId + "' AND " + DB.gu_user + "='" + sUserId + "'");
239
240     oStmt.close();
241
242     if (DebugFile.trace) {
243       DebugFile.decIdent();
244       DebugFile.writeln("End Subscription.deactivate() : " + (iAffected!=0 ? "true" : "false"));
245     }
246
247     return (iAffected!=0);
248   } // deactivate
249

250   //----------------------------------------------------------------------------
251

252   public static final short ACTIVE = 1;
253   public static final short UNACTIVE = 0;
254
255   public static final short GROUP_NONE = 1;
256   public static final short GROUP_DIGEST = 2;
257 }
Popular Tags