1 32 33 package com.knowgate.acl; 34 35 import java.io.IOException ; 36 37 import java.sql.SQLException ; 38 import java.sql.CallableStatement ; 39 import java.sql.PreparedStatement ; 40 import java.sql.Statement ; 41 import java.sql.ResultSet ; 42 43 import com.knowgate.debug.DebugFile; 44 import com.knowgate.jdc.JDCConnection; 45 import com.knowgate.dataobjs.DB; 46 import com.knowgate.dataobjs.DBBind; 47 import com.knowgate.dataobjs.DBPersist; 48 import com.knowgate.dataobjs.DBSubset; 49 50 import com.knowgate.workareas.WorkArea; 51 import com.knowgate.hipergate.Category; 52 53 59 60 public final class ACLDomain extends DBPersist { 61 62 65 public ACLDomain() { 66 super(DB.k_domains, "ACLDomain"); 67 } 68 69 71 77 public ACLDomain(JDCConnection oConn, int iIdDomain) throws SQLException { 78 super(DB.k_domains, "ACLDomain"); 79 80 Object aDom[] = { new Integer (iIdDomain) }; 81 82 load (oConn,aDom); 83 } 84 85 87 90 public boolean store(JDCConnection oConn) throws SQLException { 91 92 if (!AllVals.containsKey(DB.id_domain)) { 93 put(DB.id_domain, DBBind.nextVal(oConn, "seq_" + DB.k_domains)); 94 } 95 96 return super.store(oConn); 97 } 99 101 104 public boolean delete(JDCConnection oConn) throws SQLException { 105 try { 106 return ACLDomain.delete(oConn, getInt(DB.id_domain)); 107 } catch (IOException ioe) { 108 throw new SQLException ("IOException " + ioe.getMessage()); 109 } 110 } 111 112 114 130 public static boolean delete(JDCConnection oConn, int iDomainId) throws SQLException ,IOException { 131 PreparedStatement oStmt; 132 DBSubset oWrks, oCats, oGrps, oUsrs; 133 int iWrks, iCats, iGrps, iUsrs; 134 int iAffected; 135 136 if (DebugFile.trace) { 137 DebugFile.writeln("Begin ACLDomain.delete([Connection], " + String.valueOf(iDomainId) + ")"); 138 DebugFile.incIdent(); 139 } 140 141 if (DBBind.exists(oConn, DB.k_workareas,"U")) { 143 oWrks = new DBSubset(DB.k_workareas, DB.gu_workarea, DB.id_domain + "=" + String.valueOf(iDomainId), 8); 144 iWrks = oWrks.load(oConn); 145 146 for (int w=0; w<iWrks; w++) 147 WorkArea.delete(oConn, oWrks.getString(0,w)); 148 149 oWrks = null; 150 } 152 if (DBBind.exists(oConn, DB.k_thesauri, "U")) { 154 155 if (DebugFile.trace) 157 DebugFile.writeln("Connection.prepareStatement(DELETE FROM " + DB.k_thesauri + " WHERE " + DB.bo_mainterm + "=0 AND " + DB.gu_rootterm + " IN (SELECT " + DB.gu_rootterm + " FROM " + DB.k_thesauri_root + " WHERE " + DB.id_domain + "=" + String.valueOf(iDomainId) + "))"); 158 159 oStmt = oConn.prepareStatement("DELETE FROM " + DB.k_thesauri + " WHERE " + DB.bo_mainterm + "=0 AND " + DB.gu_rootterm + " IN (SELECT " + DB.gu_rootterm + " FROM " + DB.k_thesauri_root + " WHERE " + DB.id_domain + "=?)"); 160 oStmt.setInt(1, iDomainId); 161 oStmt.executeUpdate(); 162 oStmt.close(); 163 164 if (DebugFile.trace) 166 DebugFile.writeln("Connection.prepareStatement(DELETE FROM " + DB.k_thesauri + " WHERE " + DB.gu_rootterm + " IN (SELECT " + DB.gu_rootterm + " FROM " + DB.k_thesauri_root + " WHERE " + DB.id_domain + "=" + String.valueOf(iDomainId) + "))"); 167 168 oStmt = oConn.prepareStatement("DELETE FROM " + DB.k_thesauri + " WHERE " + DB.gu_rootterm + " IN (SELECT " + DB.gu_rootterm + " FROM " + DB.k_thesauri_root + " WHERE " + DB.id_domain + "=?)"); 169 oStmt.setInt(1, iDomainId); 170 oStmt.executeUpdate(); 171 oStmt.close(); 172 173 if (DebugFile.trace) 175 DebugFile.writeln("Connection.prepareStatement(DELETE FROM " + DB.k_thesauri_root + " WHERE " + DB.id_domain + "=?)"); 176 177 oStmt = oConn.prepareStatement("DELETE FROM " + DB.k_thesauri_root + " WHERE " + DB.id_domain + "=?"); 178 oStmt.setInt(1, iDomainId); 179 oStmt.executeUpdate(); 180 oStmt.close(); 181 } 182 183 if (DBBind.exists(oConn, DB.k_categories,"U")) { 185 186 oCats = new DBSubset(DB.k_categories + " c," + DB.k_users + " u" , "c." + DB.gu_category, 187 "u."+ DB.id_domain + "=" + String.valueOf(iDomainId) + " AND c." + DB.gu_owner + "=u." + DB.gu_user, 8); 188 189 iCats = oCats.load(oConn); 190 191 for (int c=0; c<iCats; c++) 192 Category.delete(oConn, oCats.getString(0,c)); 193 194 oCats = null; 195 } 197 oStmt = oConn.prepareStatement("UPDATE " + DB.k_domains + " SET " + DB.gu_owner + "=NULL," + DB.gu_admins + "=NULL WHERE " + DB.id_domain + "=?"); 199 oStmt.setInt(1, iDomainId); 200 oStmt.executeUpdate(); 201 oStmt.close(); 202 203 if (DBBind.exists(oConn, DB.k_acl_groups,"U")) { 205 oGrps = new DBSubset(DB.k_acl_groups, DB.gu_acl_group, DB.id_domain + "=" + String.valueOf(iDomainId), 8); 206 iGrps = oGrps.load(oConn); 207 208 for (int g=0; g<iGrps; g++) 209 ACLGroup.delete(oConn, oGrps.getString(0,g)); 210 211 oGrps = null; 212 } 214 if (DBBind.exists(oConn, DB.k_users,"U")) { 216 217 oUsrs = new DBSubset(DB.k_users, DB.gu_user, DB.id_domain + "=" + String.valueOf(iDomainId), 8); 218 iUsrs = oUsrs.load(oConn); 219 220 for (int g=0; g<iUsrs; g++) 221 ACLUser.delete(oConn, oUsrs.getString(0,g)); 222 223 oUsrs = null; 224 } 226 if (DebugFile.trace) 227 DebugFile.writeln("Connection.prepareStatement(DELETE FROM " + DB.k_domains + " WHERE " + DB.id_domain + "=" + String.valueOf(iDomainId)); 228 229 oStmt = oConn.prepareStatement("DELETE FROM " + DB.k_domains + " WHERE " + DB.id_domain + "=?"); 230 oStmt.setInt(1, iDomainId); 231 iAffected = oStmt.executeUpdate(); 232 oStmt.close(); 233 oStmt = null; 234 235 if (DebugFile.trace) { 236 DebugFile.decIdent(); 237 DebugFile.writeln("End ACLDomain.delete() : " + String.valueOf(iAffected>0 ? true : false)); 238 } 239 240 return iAffected>0 ? true : false; 241 } 243 245 252 public static Integer forWorkArea(JDCConnection oConn, String sWorkAreaId) 253 throws SQLException { 254 255 Integer iDom; 256 PreparedStatement oStmt = oConn.prepareStatement("SELECT "+DB.id_domain+" FROM "+DB.k_workareas+" WHERE "+DB.gu_workarea+"=?", 257 ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 258 oStmt.setString(1, sWorkAreaId); 259 ResultSet oRSet = oStmt.executeQuery(); 260 if (oRSet.next()) 261 iDom = new Integer (oRSet.getObject(1).toString()); 262 else 263 iDom = null; 264 oRSet.close(); 265 oStmt.close(); 266 return iDom; 267 } 269 271 279 public static int getIdFromName(JDCConnection oConn, String sDomainNm) throws SQLException { 280 CallableStatement oCall; 281 PreparedStatement oStmt; 282 ResultSet oRSet; 283 int iDomainId; 284 285 switch (oConn.getDataBaseProduct()) { 286 287 case JDCConnection.DBMS_MYSQL: 288 case JDCConnection.DBMS_MSSQL: 289 case JDCConnection.DBMS_ORACLE: 290 oCall = oConn.prepareCall("{call k_get_domain_id (?,?)}"); 291 oCall.setString(1, sDomainNm); 292 oCall.registerOutParameter(2, java.sql.Types.INTEGER); 293 oCall.execute(); 294 iDomainId = oCall.getInt(2); 295 oCall.close(); 296 oCall = null; 297 break; 298 299 default: 300 oStmt = oConn.prepareStatement("SELECT " + DB.id_domain + " FROM " + DB.k_domains + " WHERE " + DB.nm_domain + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 301 oStmt.setString(1, sDomainNm); 302 oRSet = oStmt.executeQuery(); 303 if (oRSet.next()) 304 iDomainId = oRSet.getInt(1); 305 else 306 iDomainId = 0; 307 oRSet.close(); 308 oStmt.close(); 309 } 311 return iDomainId; 312 } 313 314 316 private static void printUsage() { 317 System.out.println(""); 318 System.out.println("Usage:"); 319 System.out.println("ACLDomain list emails id_domain"); 320 } 321 322 public static void main(String [] argv) 323 throws SQLException , NumberFormatException { 324 325 if (argv.length!=3) { 326 printUsage(); 327 } 328 else if (!argv[1].equalsIgnoreCase("list") || !argv[1].equalsIgnoreCase("emails")) { 329 printUsage(); 330 } 331 else { 332 DBBind oDBB = new DBBind(); 333 JDCConnection oCon = oDBB.getConnection("ACLDomain_main"); 334 Statement oStm = oCon.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 335 ResultSet oRst = oStm.executeQuery("SELECT " + DB.tx_main_email + " FROM " + DB.k_users + " WHERE " + DB.id_domain + "=" + argv[2] + " AND " + DB.bo_active + "<>0"); 336 337 while (oRst.next()) { 338 System.out.println(oRst.getString(1)); 339 } 341 oRst.close(); 342 oStm.close(); 343 oCon.close("ACLDomain_main"); 344 345 oDBB.connectionPool().close(); 346 oDBB = null; 347 } 348 } 350 352 public static final short ClassId = 1; 353 354 } | Popular Tags |