1 32 33 package com.knowgate.forums; 34 35 import java.sql.SQLException ; 36 import java.sql.CallableStatement ; 37 import java.sql.ResultSet ; 38 import java.sql.ResultSetMetaData ; 39 import java.sql.PreparedStatement ; 40 import java.sql.Statement ; 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 55 public class NewsGroup extends Category { 56 57 60 public NewsGroup() { 61 super(DB.k_newsgroups,"NewsGroup"); 62 } 63 64 66 71 public NewsGroup(String sIdNewsGroup) throws SQLException { 72 super(DB.k_newsgroups,"NewsGroup"); 73 74 put(DB.gu_category, sIdNewsGroup); 75 put(DB.gu_newsgrp , sIdNewsGroup); 76 } 77 78 80 88 public NewsGroup(JDCConnection oConn, String sIdNewsGroup) throws SQLException { 89 super(DB.k_newsgroups,"NewsGroup"); 90 91 load (oConn, new Object []{sIdNewsGroup}); 92 } 93 94 96 102 public int countMessages(JDCConnection oConn) throws SQLException { 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 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 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 ("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 } 140 142 149 public int countMessages(JDCConnection oConn, short iMsgStatus) throws SQLException { 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 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 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 ("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 } 186 188 199 public boolean load(JDCConnection oConn, Object [] PKVals) throws SQLException { 200 boolean bRetVal; 201 PreparedStatement oStmt; 202 ResultSet oRSet; 203 ResultSetMetaData oMDat; 204 int iColCount; 205 String sColName; 206 String sSQL; 207 208 if (DebugFile.trace) { 209 DebugFile.writeln("Begin NewsGroup.load([Connection], ...)"); 210 DebugFile.incIdent(); 211 DebugFile.writeln("gu_newsgrp=" + (String ) 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 ) 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 } oMDat = null; 238 } 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 ) 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 } 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 } 273 275 285 public DBSubset subscribers (JDCConnection oConn) throws SQLException { 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 [] {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 } 307 309 317 public boolean isSubscriber (JDCConnection oConn, String sUserId) 318 throws SQLException { 319 320 PreparedStatement 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 oRSet = oStmt.executeQuery(); 326 327 boolean bSubscriber = oRSet.next(); 328 329 oRSet.close(); 330 oStmt.close(); 331 332 return bSubscriber; 333 } 334 335 337 343 344 public boolean delete(JDCConnection oConn) throws SQLException { 345 return NewsGroup.delete(oConn,getString(DB.gu_newsgrp)); 346 } 347 348 351 367 public static String store(JDCConnection oConn, int iDomain, String sWorkArea, String sCategoryId, String sParentId, String sCategoryName, short iIsActive, int iDocStatus, String sOwner, String sIcon1, String sIcon2 ) throws SQLException { 368 String 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 } 380 389 public static boolean delete(JDCConnection oConn, String sNewsGroupGUID) throws SQLException { 390 Statement oStmt; 391 ResultSet oRSet; 392 String sProductId; 393 Product oProd; 394 String sSQL; 395 396 if (DebugFile.trace) { 397 DebugFile.writeln("Begin NewsGroup.delete([Connection], " + sNewsGroupGUID + ")"); 398 DebugFile.incIdent(); 399 } 400 401 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 } oRSet.close(); 424 oStmt.close(); 425 426 CallableStatement 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 } 440 443 public static final short ClassId = 30; 444 445 public static final short FREE = 0; 446 public static final short MODERATED = 1; 447 448 } | Popular Tags |