KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > acl > ACLDomain


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.acl;
34
35 import java.io.IOException JavaDoc;
36
37 import java.sql.SQLException JavaDoc;
38 import java.sql.CallableStatement JavaDoc;
39 import java.sql.PreparedStatement JavaDoc;
40 import java.sql.Statement JavaDoc;
41 import java.sql.ResultSet JavaDoc;
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 /**
54  *
55  * <p>Security Domain Management Functions</p>
56  * @author Sergio Montoro Ten
57  * @version 2.1
58  */

59
60 public final class ACLDomain extends DBPersist {
61
62   /**
63    * Default constructor
64    */

65   public ACLDomain() {
66     super(DB.k_domains, "ACLDomain");
67   }
68
69   // ----------------------------------------------------------
70

71   /**
72    * <p>Constructs ACLDomain and load attributes from k_domains table</p>
73    * @param oConn Database Connection
74    * @param iIdDomain Domain Identifier (id_domain field at k_domains table)
75    * @throws SQLException
76    */

77   public ACLDomain(JDCConnection oConn, int iIdDomain) throws SQLException JavaDoc {
78     super(DB.k_domains, "ACLDomain");
79
80     Object JavaDoc aDom[] = { new Integer JavaDoc(iIdDomain) };
81
82     load (oConn,aDom);
83   }
84
85   // ----------------------------------------------------------
86

87   /**
88    * @see DBPersist#store(JDCConnection)
89    */

90   public boolean store(JDCConnection oConn) throws SQLException JavaDoc {
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   } // store
98

99   // ----------------------------------------------------------
100

101   /**
102    * @see ACLDomain#delete(JDCConnection,int)
103    */

104   public boolean delete(JDCConnection oConn) throws SQLException JavaDoc {
105     try {
106       return ACLDomain.delete(oConn, getInt(DB.id_domain));
107     } catch (IOException JavaDoc ioe) {
108       throw new SQLException JavaDoc ("IOException " + ioe.getMessage());
109     }
110   }
111
112   // ----------------------------------------------------------
113

114   /**
115    * <p>Fully delete a domain and ALL its associated data</p>
116    * <p>This method will perform the following actions<br>
117    * 1. Delete WorkAreas from this domain<br>
118    * 2. Delete categories owned by users of this domain<br>
119    * 3. Delete Security groups from domain<br>
120    * 4. Delete domain users<br>
121    * @param oConn Database Connection
122    * @param iDomainId Domain Identifier
123    * @return
124    * @throws SQLException
125    * @throws IOException
126    * @see com.knowgate.acl.ACLUser#delete(JDCConnection,String)
127    * @see com.knowgate.hipergate.Category#delete(JDCConnection,String)
128    * @see com.knowgate.workareas.WorkArea#delete(JDCConnection,String)
129    */

130   public static boolean delete(JDCConnection oConn, int iDomainId) throws SQLException JavaDoc,IOException JavaDoc {
131     PreparedStatement JavaDoc 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     // Delete workareas associated with domain
142
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     } // fi(exists(DB.k_workareas))
151

152     // Delete thesauri entries from this domain
153
if (DBBind.exists(oConn, DB.k_thesauri, "U")) {
154
155       // Delete synonyms first
156
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       // Then delete rest of main terms
165
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       // Delete root terms
174
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     // Delete all categories owned by users of this domain
184
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     } // fi(exists(DB.k_categories))
196

197     // Prevent foreign key violations on deleting administrator user and groups
198
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     // Delete security groups
204
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     } // fi(exists(DB.k_acl_groups))
213

214     // Delete user
215
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     } // fi(exists(DB.k_users))
225

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   } // delete()
242

243   // ----------------------------------------------------------
244

245   /**
246    * Get Domain to which a given WorkArea belongs
247    * @param oConn JDCConnection
248    * @param sWorkAreaId String WorkArea GUID
249    * @return Integer Domain Id. or <b>null</b> if no WorkArea with such GUID was found
250    * @throws SQLException
251    */

252   public static Integer JavaDoc forWorkArea(JDCConnection oConn, String JavaDoc sWorkAreaId)
253       throws SQLException JavaDoc {
254
255     Integer JavaDoc iDom;
256     PreparedStatement JavaDoc 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 JavaDoc oRSet = oStmt.executeQuery();
260     if (oRSet.next())
261       iDom = new Integer JavaDoc(oRSet.getObject(1).toString());
262     else
263       iDom = null;
264     oRSet.close();
265     oStmt.close();
266     return iDom;
267   } // forWorkArea
268

269   // ----------------------------------------------------------
270

271   /**
272    * <p>Gets domain identifier given its name</p>
273    * <p>Calls k_get_domain_id stored procedure and gets id_domaingiven nm_domain
274    * @param oConn Database Connection
275    * @param sDomainNm Domain name (nm_domain from k_domains table)
276    * @return Domain Identifier
277    * @throws SQLException
278    */

279   public static int getIdFromName(JDCConnection oConn, String JavaDoc sDomainNm) throws SQLException JavaDoc {
280     CallableStatement JavaDoc oCall;
281     PreparedStatement JavaDoc oStmt;
282     ResultSet JavaDoc 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     } // end switch()
310

311     return iDomainId;
312   }
313
314   // ---------------------------------------------------------------------------
315

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 JavaDoc[] argv)
323       throws SQLException JavaDoc, NumberFormatException JavaDoc {
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 JavaDoc oStm = oCon.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
335       ResultSet JavaDoc 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       } // wend
340

341       oRst.close();
342       oStm.close();
343       oCon.close("ACLDomain_main");
344
345       oDBB.connectionPool().close();
346       oDBB = null;
347     }
348   } // main
349

350   // ---------------------------------------------------------------------------
351

352   public static final short ClassId = 1;
353
354 } // ACLDomain
355
Popular Tags