001: /*
002: Copyright (C) 2003 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.acl;
034:
035: import java.io.IOException;
036:
037: import java.sql.SQLException;
038: import java.sql.CallableStatement;
039: import java.sql.PreparedStatement;
040: import java.sql.Statement;
041: import java.sql.ResultSet;
042:
043: import com.knowgate.debug.DebugFile;
044: import com.knowgate.jdc.JDCConnection;
045: import com.knowgate.dataobjs.DB;
046: import com.knowgate.dataobjs.DBBind;
047: import com.knowgate.dataobjs.DBPersist;
048: import com.knowgate.dataobjs.DBSubset;
049:
050: import com.knowgate.workareas.WorkArea;
051: import com.knowgate.hipergate.Category;
052:
053: /**
054: *
055: * <p>Security Domain Management Functions</p>
056: * @author Sergio Montoro Ten
057: * @version 2.1
058: */
059:
060: public final class ACLDomain extends DBPersist {
061:
062: /**
063: * Default constructor
064: */
065: public ACLDomain() {
066: super (DB.k_domains, "ACLDomain");
067: }
068:
069: // ----------------------------------------------------------
070:
071: /**
072: * <p>Constructs ACLDomain and load attributes from k_domains table</p>
073: * @param oConn Database Connection
074: * @param iIdDomain Domain Identifier (id_domain field at k_domains table)
075: * @throws SQLException
076: */
077: public ACLDomain(JDCConnection oConn, int iIdDomain)
078: throws SQLException {
079: super (DB.k_domains, "ACLDomain");
080:
081: Object aDom[] = { new Integer(iIdDomain) };
082:
083: load(oConn, aDom);
084: }
085:
086: // ----------------------------------------------------------
087:
088: /**
089: * @see DBPersist#store(JDCConnection)
090: */
091: public boolean store(JDCConnection oConn) throws SQLException {
092:
093: if (!AllVals.containsKey(DB.id_domain)) {
094: put(DB.id_domain, DBBind.nextVal(oConn, "seq_"
095: + DB.k_domains));
096: }
097:
098: return super .store(oConn);
099: } // store
100:
101: // ----------------------------------------------------------
102:
103: /**
104: * @see ACLDomain#delete(JDCConnection,int)
105: */
106: public boolean delete(JDCConnection oConn) throws SQLException {
107: try {
108: return ACLDomain.delete(oConn, getInt(DB.id_domain));
109: } catch (IOException ioe) {
110: throw new SQLException("IOException " + ioe.getMessage());
111: }
112: }
113:
114: // ----------------------------------------------------------
115:
116: /**
117: * <p>Fully delete a domain and ALL its associated data</p>
118: * <p>This method will perform the following actions<br>
119: * 1. Delete WorkAreas from this domain<br>
120: * 2. Delete categories owned by users of this domain<br>
121: * 3. Delete Security groups from domain<br>
122: * 4. Delete domain users<br>
123: * @param oConn Database Connection
124: * @param iDomainId Domain Identifier
125: * @return
126: * @throws SQLException
127: * @throws IOException
128: * @see com.knowgate.acl.ACLUser#delete(JDCConnection,String)
129: * @see com.knowgate.hipergate.Category#delete(JDCConnection,String)
130: * @see com.knowgate.workareas.WorkArea#delete(JDCConnection,String)
131: */
132: public static boolean delete(JDCConnection oConn, int iDomainId)
133: throws SQLException, IOException {
134: PreparedStatement oStmt;
135: DBSubset oWrks, oCats, oGrps, oUsrs;
136: int iWrks, iCats, iGrps, iUsrs;
137: int iAffected;
138:
139: if (DebugFile.trace) {
140: DebugFile.writeln("Begin ACLDomain.delete([Connection], "
141: + String.valueOf(iDomainId) + ")");
142: DebugFile.incIdent();
143: }
144:
145: // Delete workareas associated with domain
146: if (DBBind.exists(oConn, DB.k_workareas, "U")) {
147: oWrks = new DBSubset(DB.k_workareas, DB.gu_workarea,
148: DB.id_domain + "=" + String.valueOf(iDomainId), 8);
149: iWrks = oWrks.load(oConn);
150:
151: for (int w = 0; w < iWrks; w++)
152: WorkArea.delete(oConn, oWrks.getString(0, w));
153:
154: oWrks = null;
155: } // fi(exists(DB.k_workareas))
156:
157: // Delete thesauri entries from this domain
158: if (DBBind.exists(oConn, DB.k_thesauri, "U")) {
159:
160: // Delete synonyms first
161: if (DebugFile.trace)
162: DebugFile
163: .writeln("Connection.prepareStatement(DELETE FROM "
164: + DB.k_thesauri
165: + " WHERE "
166: + DB.bo_mainterm
167: + "=0 AND "
168: + DB.gu_rootterm
169: + " IN (SELECT "
170: + DB.gu_rootterm
171: + " FROM "
172: + DB.k_thesauri_root
173: + " WHERE "
174: + DB.id_domain
175: + "="
176: + String.valueOf(iDomainId) + "))");
177:
178: oStmt = oConn.prepareStatement("DELETE FROM "
179: + DB.k_thesauri + " WHERE " + DB.bo_mainterm
180: + "=0 AND " + DB.gu_rootterm + " IN (SELECT "
181: + DB.gu_rootterm + " FROM " + DB.k_thesauri_root
182: + " WHERE " + DB.id_domain + "=?)");
183: oStmt.setInt(1, iDomainId);
184: oStmt.executeUpdate();
185: oStmt.close();
186:
187: // Then delete rest of main terms
188: if (DebugFile.trace)
189: DebugFile
190: .writeln("Connection.prepareStatement(DELETE FROM "
191: + DB.k_thesauri
192: + " WHERE "
193: + DB.gu_rootterm
194: + " IN (SELECT "
195: + DB.gu_rootterm
196: + " FROM "
197: + DB.k_thesauri_root
198: + " WHERE "
199: + DB.id_domain
200: + "="
201: + String.valueOf(iDomainId) + "))");
202:
203: oStmt = oConn.prepareStatement("DELETE FROM "
204: + DB.k_thesauri + " WHERE " + DB.gu_rootterm
205: + " IN (SELECT " + DB.gu_rootterm + " FROM "
206: + DB.k_thesauri_root + " WHERE " + DB.id_domain
207: + "=?)");
208: oStmt.setInt(1, iDomainId);
209: oStmt.executeUpdate();
210: oStmt.close();
211:
212: // Delete root terms
213: if (DebugFile.trace)
214: DebugFile
215: .writeln("Connection.prepareStatement(DELETE FROM "
216: + DB.k_thesauri_root
217: + " WHERE "
218: + DB.id_domain + "=?)");
219:
220: oStmt = oConn.prepareStatement("DELETE FROM "
221: + DB.k_thesauri_root + " WHERE " + DB.id_domain
222: + "=?");
223: oStmt.setInt(1, iDomainId);
224: oStmt.executeUpdate();
225: oStmt.close();
226: }
227:
228: // Delete all categories owned by users of this domain
229: if (DBBind.exists(oConn, DB.k_categories, "U")) {
230:
231: oCats = new DBSubset(DB.k_categories + " c," + DB.k_users
232: + " u", "c." + DB.gu_category, "u." + DB.id_domain
233: + "=" + String.valueOf(iDomainId) + " AND c."
234: + DB.gu_owner + "=u." + DB.gu_user, 8);
235:
236: iCats = oCats.load(oConn);
237:
238: for (int c = 0; c < iCats; c++)
239: Category.delete(oConn, oCats.getString(0, c));
240:
241: oCats = null;
242: } // fi(exists(DB.k_categories))
243:
244: // Prevent foreign key violations on deleting administrator user and groups
245: oStmt = oConn.prepareStatement("UPDATE " + DB.k_domains
246: + " SET " + DB.gu_owner + "=NULL," + DB.gu_admins
247: + "=NULL WHERE " + DB.id_domain + "=?");
248: oStmt.setInt(1, iDomainId);
249: oStmt.executeUpdate();
250: oStmt.close();
251:
252: // Delete security groups
253: if (DBBind.exists(oConn, DB.k_acl_groups, "U")) {
254: oGrps = new DBSubset(DB.k_acl_groups, DB.gu_acl_group,
255: DB.id_domain + "=" + String.valueOf(iDomainId), 8);
256: iGrps = oGrps.load(oConn);
257:
258: for (int g = 0; g < iGrps; g++)
259: ACLGroup.delete(oConn, oGrps.getString(0, g));
260:
261: oGrps = null;
262: } // fi(exists(DB.k_acl_groups))
263:
264: // Delete user
265: if (DBBind.exists(oConn, DB.k_users, "U")) {
266:
267: oUsrs = new DBSubset(DB.k_users, DB.gu_user, DB.id_domain
268: + "=" + String.valueOf(iDomainId), 8);
269: iUsrs = oUsrs.load(oConn);
270:
271: for (int g = 0; g < iUsrs; g++)
272: ACLUser.delete(oConn, oUsrs.getString(0, g));
273:
274: oUsrs = null;
275: } // fi(exists(DB.k_users))
276:
277: if (DebugFile.trace)
278: DebugFile
279: .writeln("Connection.prepareStatement(DELETE FROM "
280: + DB.k_domains + " WHERE " + DB.id_domain
281: + "=" + String.valueOf(iDomainId));
282:
283: oStmt = oConn.prepareStatement("DELETE FROM " + DB.k_domains
284: + " WHERE " + DB.id_domain + "=?");
285: oStmt.setInt(1, iDomainId);
286: iAffected = oStmt.executeUpdate();
287: oStmt.close();
288: oStmt = null;
289:
290: if (DebugFile.trace) {
291: DebugFile.decIdent();
292: DebugFile.writeln("End ACLDomain.delete() : "
293: + String.valueOf(iAffected > 0 ? true : false));
294: }
295:
296: return iAffected > 0 ? true : false;
297: } // delete()
298:
299: // ----------------------------------------------------------
300:
301: /**
302: * Get Domain to which a given WorkArea belongs
303: * @param oConn JDCConnection
304: * @param sWorkAreaId String WorkArea GUID
305: * @return Integer Domain Id. or <b>null</b> if no WorkArea with such GUID was found
306: * @throws SQLException
307: */
308: public static Integer forWorkArea(JDCConnection oConn,
309: String sWorkAreaId) throws SQLException {
310:
311: Integer iDom;
312: PreparedStatement oStmt = oConn.prepareStatement("SELECT "
313: + DB.id_domain + " FROM " + DB.k_workareas + " WHERE "
314: + DB.gu_workarea + "=?", ResultSet.TYPE_FORWARD_ONLY,
315: ResultSet.CONCUR_READ_ONLY);
316: oStmt.setString(1, sWorkAreaId);
317: ResultSet oRSet = oStmt.executeQuery();
318: if (oRSet.next())
319: iDom = new Integer(oRSet.getObject(1).toString());
320: else
321: iDom = null;
322: oRSet.close();
323: oStmt.close();
324: return iDom;
325: } // forWorkArea
326:
327: // ----------------------------------------------------------
328:
329: /**
330: * <p>Gets domain identifier given its name</p>
331: * <p>Calls k_get_domain_id stored procedure and gets id_domaingiven nm_domain
332: * @param oConn Database Connection
333: * @param sDomainNm Domain name (nm_domain from k_domains table)
334: * @return Domain Identifier
335: * @throws SQLException
336: */
337: public static int getIdFromName(JDCConnection oConn,
338: String sDomainNm) throws SQLException {
339: CallableStatement oCall;
340: PreparedStatement oStmt;
341: ResultSet oRSet;
342: int iDomainId;
343:
344: switch (oConn.getDataBaseProduct()) {
345:
346: case JDCConnection.DBMS_MYSQL:
347: case JDCConnection.DBMS_MSSQL:
348: case JDCConnection.DBMS_ORACLE:
349: oCall = oConn.prepareCall("{call k_get_domain_id (?,?)}");
350: oCall.setString(1, sDomainNm);
351: oCall.registerOutParameter(2, java.sql.Types.INTEGER);
352: oCall.execute();
353: iDomainId = oCall.getInt(2);
354: oCall.close();
355: oCall = null;
356: break;
357:
358: default:
359: oStmt = oConn.prepareStatement("SELECT " + DB.id_domain
360: + " FROM " + DB.k_domains + " WHERE "
361: + DB.nm_domain + "=?", ResultSet.TYPE_FORWARD_ONLY,
362: ResultSet.CONCUR_READ_ONLY);
363: oStmt.setString(1, sDomainNm);
364: oRSet = oStmt.executeQuery();
365: if (oRSet.next())
366: iDomainId = oRSet.getInt(1);
367: else
368: iDomainId = 0;
369: oRSet.close();
370: oStmt.close();
371: } // end switch()
372:
373: return iDomainId;
374: }
375:
376: // ---------------------------------------------------------------------------
377:
378: private static void printUsage() {
379: System.out.println("");
380: System.out.println("Usage:");
381: System.out.println("ACLDomain list emails id_domain");
382: }
383:
384: public static void main(String[] argv) throws SQLException,
385: NumberFormatException {
386:
387: if (argv.length != 3) {
388: printUsage();
389: } else if (!argv[1].equalsIgnoreCase("list")
390: || !argv[1].equalsIgnoreCase("emails")) {
391: printUsage();
392: } else {
393: DBBind oDBB = new DBBind();
394: JDCConnection oCon = oDBB.getConnection("ACLDomain_main");
395: Statement oStm = oCon.createStatement(
396: ResultSet.TYPE_FORWARD_ONLY,
397: ResultSet.CONCUR_READ_ONLY);
398: ResultSet oRst = oStm.executeQuery("SELECT "
399: + DB.tx_main_email + " FROM " + DB.k_users
400: + " WHERE " + DB.id_domain + "=" + argv[2]
401: + " AND " + DB.bo_active + "<>0");
402:
403: while (oRst.next()) {
404: System.out.println(oRst.getString(1));
405: } // wend
406:
407: oRst.close();
408: oStm.close();
409: oCon.close("ACLDomain_main");
410:
411: oDBB.connectionPool().close();
412: oDBB = null;
413: }
414: } // main
415:
416: // ---------------------------------------------------------------------------
417:
418: public static final short ClassId = 1;
419:
420: } // ACLDomain
|