KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2003 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.CallableStatement JavaDoc;
37 import java.sql.ResultSet JavaDoc;
38 import java.sql.ResultSetMetaData JavaDoc;
39 import java.sql.PreparedStatement JavaDoc;
40 import java.sql.Statement JavaDoc;
41
42 import com.knowgate.debug.DebugFile;
43 import com.knowgate.jdc.JDCConnection;
44 import com.knowgate.dataobjs.DB;
45 import com.knowgate.dataobjs.DBSubset;
46
47 import com.knowgate.hipergate.Category;
48 import com.knowgate.hipergate.Product;
49
50 /**
51  * <p>NewsGroup</p>
52  * @author Sergio Montoro Ten
53  * @version 2.0
54  */

55 public class NewsGroup extends Category {
56
57   /**
58    * Create empty newsgroup
59    */

60   public NewsGroup() {
61     super(DB.k_newsgroups,"NewsGroup");
62   }
63
64   // ----------------------------------------------------------
65

66   /**
67    * Create NewsGroup and set its Category GUID
68    * @param sIdNewsGroup GUID of NewsGroup/Category
69    * @throws SQLException
70    */

71   public NewsGroup(String JavaDoc sIdNewsGroup) throws SQLException JavaDoc {
72     super(DB.k_newsgroups,"NewsGroup");
73
74     put(DB.gu_category, sIdNewsGroup);
75     put(DB.gu_newsgrp , sIdNewsGroup);
76   }
77
78   // ----------------------------------------------------------
79

80   /**
81    * <p>Create newsGroup and load properties from Database</p>
82    * Both field sets from k_categories and k_newsgroups are loaded into
83    * internal properties collection upon load.
84    * @param oConn Database Conenction
85    * @param sIdNewsGroup GUID of newsGroup to be loaded
86    * @throws SQLException
87    */

88   public NewsGroup(JDCConnection oConn, String JavaDoc sIdNewsGroup) throws SQLException JavaDoc {
89     super(DB.k_newsgroups,"NewsGroup");
90
91     load (oConn, new Object JavaDoc[]{sIdNewsGroup});
92   }
93
94   // ----------------------------------------------------------
95

96   /**
97    * <p>Count messages for this NewsGroup</p>
98    * @param oConn Database connection
99    * @return Message Count
100    * @throws SQLException If NewsGroup does not exist
101    */

102   public int countMessages(JDCConnection oConn) throws SQLException JavaDoc {
103     int iRetVal;
104
105
106     if (DebugFile.trace) {
107       DebugFile.writeln("Begin NewsGroup.countMessages([Connection])");
108       DebugFile.incIdent();
109
110     }
111
112     if (DebugFile.trace)
113       DebugFile.writeln("Connection.prepareStatement(SELECT COUNT(*) FROM " + DB.k_x_cat_objs + " WHERE " + DB.gu_category + "='" + getStringNull(DB.gu_newsgrp, "null") + "' AND " + DB.id_class + "=" + String.valueOf(NewsMessage.ClassId) + ")");
114
115     PreparedStatement JavaDoc oStmt = oConn.prepareStatement("SELECT COUNT(*) FROM " + DB.k_x_cat_objs + " WHERE " + DB.gu_category + "=? AND " + DB.id_class + "=" + String.valueOf(NewsMessage.ClassId));
116
117     oStmt.setString(1, getString(DB.gu_newsgrp));
118
119     ResultSet JavaDoc oRSet = oStmt.executeQuery();
120
121     if (oRSet.next()) {
122       iRetVal = Integer.parseInt(oRSet.getObject(1).toString());
123       oRSet.close();
124       oStmt.close();
125     }
126     else {
127       oRSet.close();
128       oStmt.close();
129       throw new SQLException JavaDoc("NewsGroup " + getString(DB.gu_newsgrp) + " not found", "42000");
130     }
131
132     if (DebugFile.trace) {
133       DebugFile.decIdent();
134       DebugFile.writeln("End NewsGroup.countMessages() : " + String.valueOf(iRetVal));
135     }
136
137     return iRetVal;
138   } // countMessages
139

140   // ----------------------------------------------------------
141

142   /**
143    * <p>Count messages for this NewsGroup in a given status</p>
144    * @param oConn Database connection
145    * @param iMsgStatus One of { NewsMessage.STATUS_VALIDATED, NewsMessage.STATUS_PENDING, NewsMessage.STATUS_DISCARDED, NewsMessage.STATUS_EXPIRED }
146    * @return Message Count
147    * @throws SQLException If NewsGroup does not exist
148    */

149   public int countMessages(JDCConnection oConn, short iMsgStatus) throws SQLException JavaDoc {
150     int iRetVal;
151
152     if (DebugFile.trace) {
153       DebugFile.writeln("Begin NewsGroup.countMessages([Connection], " + String.valueOf(iMsgStatus) + ")");
154       DebugFile.incIdent();
155     }
156
157     if (DebugFile.trace)
158       DebugFile.writeln("Connection.prepareStatement(SELECT COUNT(x." + DB.gu_object + ") FROM " + DB.k_x_cat_objs + " x, " + DB.k_newsmsgs + " m WHERE m." + DB.gu_msg + "=" + "x." + DB.gu_object + " AND m." + DB.id_status + "=" + String.valueOf(iMsgStatus) + " AND x." + DB.gu_category + "='" + getStringNull(DB.gu_newsgrp, "null") + "' AND x." + DB.id_class + "=" + String.valueOf(NewsMessage.ClassId) + ")");
159
160     PreparedStatement JavaDoc oStmt = oConn.prepareStatement("SELECT COUNT(x." + DB.gu_object + ") FROM " + DB.k_x_cat_objs + " x, " + DB.k_newsmsgs + " m WHERE m." + DB.gu_msg + "=" + "x." + DB.gu_object + " AND m." + DB.id_status + "=?" + " AND x." + DB.gu_category + "=? AND x." + DB.id_class + "=" + String.valueOf(NewsMessage.ClassId));
161
162     oStmt.setShort (1, iMsgStatus);
163     oStmt.setString(2, getString(DB.gu_newsgrp));
164
165     ResultSet JavaDoc oRSet = oStmt.executeQuery();
166
167     if (oRSet.next()) {
168       iRetVal = Integer.parseInt(oRSet.getObject(1).toString());
169       oRSet.close();
170       oStmt.close();
171     }
172     else {
173       oRSet.close();
174       oStmt.close();
175       throw new SQLException JavaDoc("NewsGroup " + getString(DB.gu_newsgrp) + " not found", "42000");
176     }
177
178     if (DebugFile.trace) {
179       DebugFile.decIdent();
180       DebugFile.writeln("End NewsGroup.countMessages() : " + String.valueOf(iRetVal));
181     }
182
183     return iRetVal;
184   } // countMessages
185

186   // ----------------------------------------------------------
187

188   /**
189    * <p>Load NewsGroup from database</p>
190    * Both field sets from k_categories and k_newsgroups are loaded into
191    * internal properties collection upon load.
192    * @param oConn Database Conenction
193    * @param PKVals A single element array containing the GUID of NewsGroup to be
194    * loaded. For example: oNewsGrpObj.load(oConnection, new object[]{"123456789012345678901234567890AB"});
195    * @return <b>true</b> if NewsGroup was successfully loaded, <b>false</b> if
196    * Newsgroup GUID was not found at k_newsgropus o k_categories tables.
197    * @throws SQLException
198    */

199   public boolean load(JDCConnection oConn, Object JavaDoc[] PKVals) throws SQLException JavaDoc {
200     boolean bRetVal;
201     PreparedStatement JavaDoc oStmt;
202     ResultSet JavaDoc oRSet;
203     ResultSetMetaData JavaDoc oMDat;
204     int iColCount;
205     String JavaDoc sColName;
206     String JavaDoc sSQL;
207
208     if (DebugFile.trace) {
209       DebugFile.writeln("Begin NewsGroup.load([Connection], ...)");
210       DebugFile.incIdent();
211       DebugFile.writeln("gu_newsgrp=" + (String JavaDoc) PKVals[0]);
212     }
213
214     clear();
215
216     sSQL = "SELECT * FROM " + DB.k_categories + " WHERE " + DB.gu_category + "=?";
217
218     if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(" + sSQL + ")");
219
220     oStmt = oConn.prepareStatement(sSQL, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
221
222     oStmt.setString(1, (String JavaDoc) PKVals[0]);
223
224     oRSet = oStmt.executeQuery();
225
226     bRetVal = oRSet.next();
227
228     if (bRetVal) {
229       oMDat = oRSet.getMetaData();
230       iColCount = oMDat.getColumnCount();
231
232       for (int c=1; c<=iColCount; c++) {
233         sColName = oMDat.getColumnName(c).toLowerCase();
234         if (!sColName.equalsIgnoreCase(DB.dt_created));
235           put(sColName, oRSet.getObject(c));
236       } // next
237
oMDat = null;
238     } // fi (bRetVal)
239

240     oRSet.close();
241     oStmt.close();
242
243     if (bRetVal) {
244       sSQL = "SELECT * FROM " + DB.k_newsgroups + " WHERE " + DB.gu_newsgrp + "=?";
245       if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(" + sSQL + ")");
246       oStmt = oConn.prepareStatement(sSQL, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
247       oStmt.setString(1, (String JavaDoc) PKVals[0]);
248       oRSet = oStmt.executeQuery();
249       bRetVal = oRSet.next();
250       if (bRetVal) {
251         oMDat = oRSet.getMetaData();
252         iColCount = oMDat.getColumnCount();
253
254         for (int c=1; c<=iColCount; c++) {
255           sColName = oMDat.getColumnName(c).toLowerCase();
256           if (!sColName.equalsIgnoreCase(DB.dt_created));
257             put(sColName, oRSet.getObject(c));
258         } // next
259
oMDat = null;
260       }
261       oRSet.close();
262       oStmt.close();
263     }
264
265     if (DebugFile.trace) {
266       DebugFile.decIdent();
267       DebugFile.writeln("End NewsGroup.load() : " + String.valueOf(bRetVal));
268     }
269
270     return bRetVal;
271   } // load
272

273   // ----------------------------------------------------------
274

275   /**
276    * <p>Get ACLUsers subscribed to this NewsGroup</p>
277    * @param oConn JDBC Database Connection
278    * @return A DBSubset with the following columns:<br>
279    * <table>
280    * <tr><td><b>gu_user</b></td><td><b>tx_email</b></td><td><b>id_msg_type</b></td><td><b>tp_subscrip</b></td></tr>
281    * <tr><td>ACLUser GUID</td><td><b>ACLUser main e-mail</b></td><td>Message Format {TXT | HTM}</td><td>Message Grouping {GROUP_NONE | GROUP_DIGEST}</td></tr>
282    * </table>
283    * @throws SQLException
284    */

285   public DBSubset subscribers (JDCConnection oConn) throws SQLException JavaDoc {
286
287     if (DebugFile.trace) {
288       DebugFile.writeln("Begin NewsGroup.subscribers ([Connection])");
289       DebugFile.incIdent();
290       DebugFile.writeln("gu_newsgrp=" + getStringNull(DB.gu_newsgrp, "null"));
291     }
292
293     DBSubset oSubs = new DBSubset (DB.k_newsgroup_subscriptions,
294                                    DB.gu_user + "," + DB.tx_email + "," + DB.id_msg_type + "," + DB.tp_subscrip,
295                                    DB.gu_newsgrp + "=? AND " + DB.id_status + "=" + String.valueOf(Subscription.ACTIVE), 100);
296
297     oSubs.load(oConn, new Object JavaDoc[] {getString(DB.gu_newsgrp)});
298
299     if (DebugFile.trace) {
300       DebugFile.decIdent();
301       DebugFile.writeln("End NewsGroup.subscribers() : " + String.valueOf(oSubs.getRowCount()));
302     }
303
304     return oSubs;
305   } // subscribers
306

307   // ----------------------------------------------------------
308

309   /**
310    * <p>Get whether or not a user is subcribed this news group</p>
311    * @param oConn JDBC Database Connection
312    * @param sUserId User GUID
313    * @return <b>true</b> if user is subscribed to this news group and he is active (k_newsgroup_subscriptions.id_status=1),
314    * <b>false</b> if user is not subscribed or if he is subscribed but unactive (k_newsgroup_subscriptions.id_status=0)
315    * @throws SQLException
316    */

317   public boolean isSubscriber (JDCConnection oConn, String JavaDoc sUserId)
318     throws SQLException JavaDoc {
319
320     PreparedStatement JavaDoc oStmt = oConn.prepareStatement("SELECT " + DB.tx_email + " FROM " + DB.k_newsgroup_subscriptions + " WHERE " + DB.gu_newsgrp + "=? AND " + DB.gu_user + "=? AND " + DB.id_status + "=" + String.valueOf(Subscription.ACTIVE), ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
321
322     oStmt.setString(1, getString(DB.gu_newsgrp));
323     oStmt.setString(2, sUserId);
324
325     ResultSet JavaDoc oRSet = oStmt.executeQuery();
326
327     boolean bSubscriber = oRSet.next();
328
329     oRSet.close();
330     oStmt.close();
331
332     return bSubscriber;
333   }
334
335   // ----------------------------------------------------------
336

337   /**
338    * <p>Delete this NewsGroup and all its messages</p>
339    * @param oConn JDBC Database Connection
340    * @return
341    * @throws SQLException
342    */

343
344   public boolean delete(JDCConnection oConn) throws SQLException JavaDoc {
345     return NewsGroup.delete(oConn,getString(DB.gu_newsgrp));
346   }
347
348   // **********************************************************
349
// Static Methods
350

351   /**
352    * <p>Store Newsgroup</p>
353    * @param oConn atabase Connection
354    * @param iDomain Identifier of Domain to with the NewsGroup will belong.
355    * @param sWorkArea GUID of WorkArea to with the NewsGroup will belong.
356    * @param sCategoryId Category GUID (newsgroups are subregisters of categories)
357    * @param sParentId GUID of Parent Group (groups, as categories, are hierarchical)
358    * @param sCategoryName Category name (k_categories.nm_category)
359    * @param iIsActive 1 if group is activem, 0 if it is inactive.
360    * @param iDocStatus Initial Document Status. One of { Newsgroup.FREE, Newsgroup.MODERATED }
361    * @param sOwner GUID of User owner of this NewsGroup
362    * @param sIcon1 Closed Folder Icon
363    * @param sIcon2 Opened Folder Icon
364    * @return GUID of newly created NewsGroup
365    * @throws SQLException
366    */

367   public static String JavaDoc store(JDCConnection oConn, int iDomain, String JavaDoc sWorkArea, String JavaDoc sCategoryId, String JavaDoc sParentId, String JavaDoc sCategoryName, short iIsActive, int iDocStatus, String JavaDoc sOwner, String JavaDoc sIcon1, String JavaDoc sIcon2 ) throws SQLException JavaDoc {
368     String JavaDoc sCatId = Category.store(oConn, sCategoryId, sParentId, sCategoryName, iIsActive, iDocStatus, sOwner, sIcon1, sIcon2);
369
370     NewsGroup oGrp = new NewsGroup(sCatId);
371     oGrp.put(DB.id_domain, iDomain);
372     oGrp.put(DB.gu_workarea, sWorkArea);
373     oGrp.put(DB.bo_binaries, (short)0);
374
375     oGrp.store(oConn);
376
377     return sCatId;
378   } // store
379

380   /**
381    * <p>Delete NewsGroup and all its messages.</p>
382    * Delete all files attached to messages contained in group and then call
383    * k_sp_del_newsgroup stored procedure.<br>
384    * @param oConn Database Connection
385    * @param sNewsGroupGUID GUID of NewsGroup to be deleted.
386    * @throws SQLException
387    * @see com.knowgate.hipergate.Product#delete(JDCConnection)
388    */

389   public static boolean delete(JDCConnection oConn, String JavaDoc sNewsGroupGUID) throws SQLException JavaDoc {
390     Statement JavaDoc oStmt;
391     ResultSet JavaDoc oRSet;
392     String JavaDoc sProductId;
393     Product oProd;
394     String JavaDoc sSQL;
395
396     if (DebugFile.trace) {
397       DebugFile.writeln("Begin NewsGroup.delete([Connection], " + sNewsGroupGUID + ")");
398       DebugFile.incIdent();
399     }
400
401     // Borrar los archivos adjuntos
402
sSQL = "SELECT " + DB.gu_product + "," + DB.gu_msg + " FROM " + DB.k_newsmsgs + " WHERE " + DB.gu_product + " IS NOT NULL AND " + DB.gu_msg + " IN (SELECT " + DB.gu_object + " FROM " + DB.k_x_cat_objs + " WHERE " + DB.gu_category + "='" + sNewsGroupGUID + "')";
403     oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
404     if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(" + sSQL + ")");
405     oRSet = oStmt.executeQuery(sSQL);
406     while (oRSet.next()) {
407       sProductId = oRSet.getString(1);
408
409       if (DebugFile.trace) DebugFile.writeln("ResultSet.updateString gu_product = " + sProductId + " to NULL");
410
411       oRSet.updateString(1, null);
412
413       if (DebugFile.trace) DebugFile.writeln("ResultSet.updateRow();");
414
415       oRSet.updateRow();
416
417       if (DebugFile.trace) DebugFile.writeln("new Product([Connection], " + sProductId + ")");
418
419       oProd = new Product(oConn, sProductId);
420
421       oProd.delete(oConn);
422     } // wend
423
oRSet.close();
424     oStmt.close();
425
426     // Borrar los mensajes y la categoría subyacente
427
CallableStatement JavaDoc oCall;
428
429     if (DebugFile.trace) DebugFile.writeln("Connection.prepareCall({ call k_sp_del_newsgroup('" + sNewsGroupGUID + "') })");
430
431     oCall = oConn.prepareCall("{ call k_sp_del_newsgroup('" + sNewsGroupGUID + "') }");
432
433     if (DebugFile.trace) DebugFile.writeln("CallableStatement.execute({ call k_sp_del_newsgroup('" + sNewsGroupGUID + "') })");
434     oCall.execute();
435     oCall.close();
436
437     return true;
438   } // delete
439

440   // **********************************************************
441
// Constantes Publicas
442

443    public static final short ClassId = 30;
444
445    public static final short FREE = 0;
446    public static final short MODERATED = 1;
447
448 } // NewsGroup
449
Popular Tags