001: /*
002: Copyright (C) 2004 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.forums;
034:
035: import java.sql.SQLException;
036: import java.sql.Statement;
037: import java.sql.ResultSet;
038:
039: import com.knowgate.debug.DebugFile;
040: import com.knowgate.jdc.JDCConnection;
041: import com.knowgate.dataobjs.DB;
042: import com.knowgate.dataobjs.DBPersist;
043:
044: /**
045: * <p>NewsGroup Subscription</p>
046: * <p>Handles subscriptions of users from k_users table to newsgroups from k_newsgroups table</p>
047: * @author Sergio Montoro Ten
048: * @version 2.0
049: */
050:
051: public class Subscription extends DBPersist {
052:
053: /**
054: * Default constructor
055: */
056: public Subscription() {
057: super (DB.k_newsgroup_subscriptions, "Subscription");
058: }
059:
060: public Subscription(JDCConnection oConn, String sNewsGroupId,
061: String sUserId) throws SQLException {
062:
063: super (DB.k_newsgroup_subscriptions, "Subscription");
064:
065: load(oConn, new Object[] { sNewsGroupId, sUserId });
066: }
067:
068: //----------------------------------------------------------------------------
069:
070: /**
071: * <p>Subscribe ACLUser to a NewsGroup</p>
072: * <p>Newly created subscriptions are activated by default.</p>
073: * @param oConn JDBC Database Connection
074: * @param sNewsGroupId NewsGroup GUID
075: * @param sUserId ACLUser GUID
076: * @param sMessageFormat Message Format {TXT | HTM}
077: * @param sUserId iMessagesGrouping Message Grouping { GROUP_NONE | GROUP_DIGEST }
078: * @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.
079: * @throws SQLException
080: */
081: public static boolean subscribe(JDCConnection oConn,
082: String sNewsGroupId, String sUserId, String sMessageFormat,
083: short iMessagesGrouping) throws SQLException {
084:
085: if (DebugFile.trace) {
086: DebugFile
087: .writeln("Begin Subscription.subscribe ([Connection], "
088: + sNewsGroupId + "," + sUserId + ")");
089: DebugFile.incIdent();
090: }
091:
092: String sTxEmail;
093:
094: Statement oStmt = oConn
095: .createStatement(ResultSet.TYPE_FORWARD_ONLY,
096: ResultSet.CONCUR_READ_ONLY);
097:
098: if (DebugFile.trace)
099: DebugFile.writeln("Statement.executeQuery (SELECT "
100: + DB.tx_main_email + " FROM " + DB.k_users
101: + " WHERE " + DB.gu_user + "='" + sUserId + "')");
102:
103: ResultSet oRSet = oStmt.executeQuery("SELECT "
104: + DB.tx_main_email + " FROM " + DB.k_users + " WHERE "
105: + DB.gu_user + "='" + sUserId + "'");
106:
107: if (oRSet.next())
108: sTxEmail = oRSet.getString(1);
109: else
110: sTxEmail = null;
111:
112: oRSet.close();
113: oStmt.close();
114:
115: if (null != sTxEmail) {
116: Subscription oUsrSubs = new Subscription();
117:
118: oUsrSubs.put(DB.gu_newsgrp, sNewsGroupId);
119: oUsrSubs.put(DB.gu_user, sUserId);
120: oUsrSubs.put(DB.id_msg_type, sMessageFormat);
121: oUsrSubs.put(DB.tp_subscrip, iMessagesGrouping);
122: oUsrSubs.put(DB.tx_email, sTxEmail);
123:
124: oUsrSubs.store(oConn);
125: } // fi (sTxEmail)
126:
127: if (DebugFile.trace) {
128: DebugFile.decIdent();
129: DebugFile.writeln("End Subscription.subscribe() : "
130: + (null != sTxEmail ? sTxEmail : "false"));
131: }
132:
133: return (null != sTxEmail);
134: } // Subscribe
135:
136: //----------------------------------------------------------------------------
137:
138: /**
139: * <p>Subscribe ACLUser to a NewsGroup</p>
140: * <p>Message format is TXT with no grouping by default.</p>
141: * @param oConn JDBC Database Connection
142: * @param sNewsGroupId NewsGroup GUID
143: * @param sUserId ACLUser GUID
144: * @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.
145: * @throws SQLException
146: */
147: public static boolean subscribe(JDCConnection oConn,
148: String sNewsGroupId, String sUserId) throws SQLException {
149:
150: return subscribe(oConn, sNewsGroupId, sUserId, "TXT",
151: Subscription.GROUP_NONE);
152: }
153:
154: //----------------------------------------------------------------------------
155:
156: /**
157: * <p>Unsubscribe ACLUser to a NewsGroup</p>
158: * <p>The ACLUser is removed from table k_newsgroup_subscriptions for given NewsGroup.<br>
159: * If you want to remove an e-mail directly first call ACLUser.getIdFromEmail() method.<br></p>
160: * @param oConn JDBC Database Connection
161: * @param sNewsGroupId NewsGroup GUID
162: * @param sUserId ACLUser GUID
163: * @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.
164: * @throws SQLException
165: */
166: public static boolean unsubscribe(JDCConnection oConn,
167: String sNewsGroupId, String sUserId) throws SQLException {
168:
169: if (DebugFile.trace) {
170: DebugFile
171: .writeln("Begin Subscription.unsubscribe ([Connection], "
172: + sNewsGroupId + "," + sUserId + ")");
173: DebugFile.incIdent();
174: }
175:
176: Statement oStmt = oConn.createStatement();
177:
178: if (DebugFile.trace)
179: DebugFile.writeln("Statement.executeUpdate (DELETE FROM "
180: + DB.k_newsgroup_subscriptions + " WHERE "
181: + DB.gu_newsgrp + "='" + sNewsGroupId + "' AND "
182: + DB.gu_user + "='" + sUserId + "')");
183:
184: int iAffected = oStmt.executeUpdate("DELETE FROM "
185: + DB.k_newsgroup_subscriptions + " WHERE "
186: + DB.gu_newsgrp + "='" + sNewsGroupId + "' AND "
187: + DB.gu_user + "='" + sUserId + "'");
188:
189: oStmt.close();
190:
191: if (DebugFile.trace) {
192: DebugFile.decIdent();
193: DebugFile.writeln("End Subscription.unsubscribe() : "
194: + (iAffected != 0 ? "true" : "false"));
195: }
196:
197: return (iAffected != 0);
198: } // Unsubscribe
199:
200: //----------------------------------------------------------------------------
201:
202: /**
203: * <p>Activate a Subscription</p>
204: * <p>Set k_newsgroup_subscriptions.id_status=1</p>
205: * @param oConn JDBC Database Connection
206: * @param sNewsGroupId Newsgroup GUID
207: * @param sUserId ACLUser GUID
208: * @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.
209: * @throws SQLException
210: */
211: public static boolean activate(JDCConnection oConn,
212: String sNewsGroupId, String sUserId) throws SQLException {
213:
214: if (DebugFile.trace) {
215: DebugFile
216: .writeln("Begin Subscription.activate ([Connection], "
217: + sNewsGroupId + "," + sUserId + ")");
218: DebugFile.incIdent();
219: }
220:
221: Statement oStmt = oConn.createStatement();
222:
223: if (DebugFile.trace)
224: DebugFile.writeln("Statement.executeUpdate(UPDATE "
225: + DB.k_newsgroup_subscriptions + " SET "
226: + DB.id_status + "=" + String.valueOf(ACTIVE)
227: + " WHERE " + DB.gu_newsgrp + "='" + sNewsGroupId
228: + "' AND " + DB.gu_user + "='" + sUserId + "')");
229:
230: int iAffected = oStmt.executeUpdate("UPDATE "
231: + DB.k_newsgroup_subscriptions + " SET " + DB.id_status
232: + "=" + String.valueOf(ACTIVE) + " WHERE "
233: + DB.gu_newsgrp + "='" + sNewsGroupId + "' AND "
234: + DB.gu_user + "='" + sUserId + "'");
235:
236: oStmt.close();
237:
238: if (DebugFile.trace) {
239: DebugFile.decIdent();
240: DebugFile.writeln("End Subscription.activate() : "
241: + (iAffected != 0 ? "true" : "false"));
242: }
243:
244: return (iAffected != 0);
245: } // activate
246:
247: //----------------------------------------------------------------------------
248:
249: /**
250: * <p>Deactivate a Subscription</p>
251: * <p>Set k_newsgroup_subscriptions.id_status=0</p>
252: * @param oConn JDBC Database Connection
253: * @param sNewsGroupId Newsgroup GUID
254: * @param sUserId ACLUser GUID
255: * @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.
256: * @throws SQLException
257: */
258: public static boolean deactivate(JDCConnection oConn,
259: String sNewsGroupId, String sUserId) throws SQLException {
260:
261: if (DebugFile.trace) {
262: DebugFile
263: .writeln("Begin Subscription.deactivate ([Connection], "
264: + sNewsGroupId + "," + sUserId + ")");
265: DebugFile.incIdent();
266: }
267:
268: Statement oStmt = oConn.createStatement();
269:
270: if (DebugFile.trace)
271: DebugFile.writeln("Statement.executeUpdate(UPDATE "
272: + DB.k_newsgroup_subscriptions + " SET "
273: + DB.id_status + "=" + String.valueOf(UNACTIVE)
274: + " WHERE " + DB.gu_newsgrp + "='" + sNewsGroupId
275: + "' AND " + DB.gu_user + "='" + sUserId + "')");
276:
277: int iAffected = oStmt.executeUpdate("UPDATE "
278: + DB.k_newsgroup_subscriptions + " SET " + DB.id_status
279: + "=" + String.valueOf(UNACTIVE) + " WHERE "
280: + DB.gu_newsgrp + "='" + sNewsGroupId + "' AND "
281: + DB.gu_user + "='" + sUserId + "'");
282:
283: oStmt.close();
284:
285: if (DebugFile.trace) {
286: DebugFile.decIdent();
287: DebugFile.writeln("End Subscription.deactivate() : "
288: + (iAffected != 0 ? "true" : "false"));
289: }
290:
291: return (iAffected != 0);
292: } // deactivate
293:
294: //----------------------------------------------------------------------------
295:
296: public static final short ACTIVE = 1;
297: public static final short UNACTIVE = 0;
298:
299: public static final short GROUP_NONE = 1;
300: public static final short GROUP_DIGEST = 2;
301: }
|