KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.StringTokenizer JavaDoc;
36
37 import java.sql.Connection JavaDoc;
38 import java.sql.SQLException JavaDoc;
39 import java.sql.Statement JavaDoc;
40 import java.sql.PreparedStatement JavaDoc;
41 import java.sql.CallableStatement JavaDoc;
42 import java.sql.ResultSet JavaDoc;
43
44 import com.knowgate.debug.DebugFile;
45 import com.knowgate.jdc.JDCConnection;
46 import com.knowgate.dataobjs.DB;
47 import com.knowgate.dataobjs.DBPersist;
48
49 import com.knowgate.misc.Gadgets;
50 import com.knowgate.dataobjs.DBSubset;
51
52 /**
53  * <p>Security Role Groups</p>
54  * @author Sergio Montoro Ten
55  * @version 3.0
56  */

57 public final class ACLGroup extends DBPersist {
58
59   /**
60    * Default constructor.
61    */

62   public ACLGroup() {
63     super(DB.k_acl_groups, "ACLGroup");
64   }
65
66   /**
67    * Construct object and set gu_acl_group field.
68    * Object is not fully loaded from database.
69    * @param sGroupUId Group Unique Identifier
70    */

71   public ACLGroup(String JavaDoc sGroupUId) {
72     super(DB.k_acl_groups, "ACLGroup");
73     put(DB.gu_acl_group, sGroupUId);
74   }
75
76   // ----------------------------------------------------------
77

78   public boolean store(JDCConnection oConn) throws SQLException JavaDoc {
79
80     if (!AllVals.containsKey(DB.gu_acl_group)) {
81       put(DB.gu_acl_group, Gadgets.generateUUID());
82     }
83
84     return super.store(oConn);
85   } // store
86

87   // ----------------------------------------------------------
88

89   /**
90    * <p>Add User to Group.</p>
91    * <p>Insert new register at k_x_group_user table.</p>
92    * @param oConn Database Connection
93    * @param sIdUser User Unique Identifier.
94    * @throws SQLException May throw a primary key constraint violation is user already belongs to group.
95    */

96   public int addACLUser(JDCConnection oConn, String JavaDoc sIdUser) throws SQLException JavaDoc {
97      Statement JavaDoc oStmt;
98      int iRetVal;
99
100      if (DebugFile.trace) {
101         DebugFile.writeln("Begin ACLGroup.addACLUser(Connection], " + sIdUser + ")");
102         DebugFile.incIdent();
103      }
104
105      oStmt = oConn.createStatement();
106
107      if (DebugFile.trace)
108        DebugFile.writeln("Statement.executeUpdate(INSERT INTO " + DB.k_x_group_user + "(" + DB.gu_acl_group + "," + DB.gu_user + ") VALUES('" + getStringNull(DB.gu_acl_group, "null") + "','" + sIdUser + "')");
109
110      iRetVal = oStmt.executeUpdate("INSERT INTO " + DB.k_x_group_user + "(" + DB.gu_acl_group + "," + DB.gu_user + ") VALUES('" + getString(DB.gu_acl_group) + "','" + sIdUser + "')");
111      oStmt.close();
112
113      if (DebugFile.trace) {
114         DebugFile.decIdent();
115         DebugFile.writeln("End ACLGroup.addACLUser() : " + String.valueOf(iRetVal));
116      }
117
118      return iRetVal;
119   } // addACLUser
120

121   // ----------------------------------------------------------
122

123   /**
124    * <p>Add Users to Group.</p>
125    * <p>Insert new registers at k_x_group_user table.</p>
126    * @param oConn Database Connection
127    * @param sUserList A string of comma delimited User GUIDs that must be added to this ACLGroup.
128    * @throws SQLException May throw a primary key constraint violation is user already belongs to group.
129    */

130   public int addACLUsers(JDCConnection oConn, String JavaDoc sUserList) throws SQLException JavaDoc {
131
132      if (DebugFile.trace) {
133         DebugFile.writeln("Begin ACLGroup.addACLUsers(Connection], " + sUserList + ")");
134         DebugFile.incIdent();
135      }
136
137      Statement JavaDoc oStmt;
138      int iRetVal = 0;
139      StringTokenizer JavaDoc oStrTok = new StringTokenizer JavaDoc(sUserList, ",");
140      String JavaDoc sIdUser;
141
142      oStmt = oConn.createStatement();
143
144      while (oStrTok.hasMoreElements()) {
145        sIdUser = oStrTok.nextToken();
146
147        if (DebugFile.trace)
148          DebugFile.writeln("Statement.executeUpdate(INSERT INTO " + DB.k_x_group_user + "(" + DB.gu_acl_group + "," + DB.gu_user + ") VALUES('" + getStringNull(DB.gu_acl_group, "null") + "','" + sIdUser + "')");
149
150        iRetVal += oStmt.executeUpdate("INSERT INTO " + DB.k_x_group_user + "(" + DB.gu_acl_group + "," + DB.gu_user + ") VALUES('" + getString(DB.gu_acl_group) + "','" + sIdUser + "')");
151        oStmt.close();
152      } // wend
153

154      if (DebugFile.trace) {
155         DebugFile.decIdent();
156         DebugFile.writeln("End ACLGroup.addACLUsers() : " + String.valueOf(iRetVal));
157      }
158
159      return iRetVal;
160   } // addACLUser
161

162   // ----------------------------------------------------------
163

164   /**
165    * <p>Remove User from Group.</p>
166    * <p>remove register from k_x_group_user table.</p>
167    * @param oConn Database Connection
168    * @param sIdUser User Unique Identifier.
169    * @throws SQLException
170    */

171
172   public int removeACLUser(JDCConnection oConn, String JavaDoc sIdUser) throws SQLException JavaDoc {
173
174     if (DebugFile.trace) {
175        DebugFile.writeln("Begin ACLGroup.removeACLUser(Connection], " + sIdUser + ")");
176        DebugFile.incIdent();
177     }
178
179      int iRetVal;
180      Statement JavaDoc oStmt = oConn.createStatement();
181
182      if (DebugFile.trace)
183        DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_x_group_user + " WHERE " + DB.gu_user + "='" + sIdUser + "' AND " + DB.gu_acl_group + "='" + getStringNull(DB.gu_acl_group, "null") + "'");
184
185      iRetVal = oStmt.executeUpdate("DELETE FROM " + DB.k_x_group_user + " WHERE " + DB.gu_user + "='" + sIdUser + "' AND " + DB.gu_acl_group + "='" + getString(DB.gu_acl_group) + "'");
186      oStmt.close();
187
188      if (DebugFile.trace) {
189         DebugFile.decIdent();
190         DebugFile.writeln("End ACLGroup.removeACLUser() : " + String.valueOf(iRetVal));
191      }
192
193      return iRetVal;
194   } // removeACLUser
195

196   // ----------------------------------------------------------
197

198   /**
199    * <p>Remove all users from this group.</p>
200    * <p>Delete registers from k_x_group_user</p>
201    * @param oConn Database connection
202    * @throws SQLException
203    */

204   public int clearACLUsers(JDCConnection oConn) throws SQLException JavaDoc {
205
206     if (DebugFile.trace) {
207       DebugFile.writeln("Begin ACLGroup.clearACLUsers([Connection])");
208       DebugFile.incIdent();
209     }
210
211      int iRetVal;
212
213      Statement JavaDoc oStmt = oConn.createStatement();
214
215      if (DebugFile.trace) DebugFile.writeln("DELETE FROM " + DB.k_x_group_user + " WHERE " + DB.gu_acl_group + "='" + getStringNull(DB.gu_acl_group, "null") + "'");
216
217      iRetVal = oStmt.executeUpdate("DELETE FROM " + DB.k_x_group_user + " WHERE " + DB.gu_acl_group + "='" + getString(DB.gu_acl_group) + "'");
218
219      oStmt.close();
220
221      if (DebugFile.trace) {
222        DebugFile.decIdent();
223        DebugFile.writeln("End ACLGroup.clearACLUsers() : " + String.valueOf(iRetVal));
224      }
225
226      return iRetVal;
227   } // clearACLUsers
228

229   /**
230    * Get users that belong to this group
231    * @param oConn JDCConnection
232    * @return DBSubset with all the columns from k_users table
233    * @throws SQLException
234    * @since 3.0
235    */

236   public DBSubset getACLUsers(JDCConnection oConn) throws SQLException JavaDoc {
237     Object JavaDoc aGroup[] = { get(DB.gu_acl_group) };
238     DBSubset oUsers = new DBSubset(DB.k_x_group_user+" x,"+DB.k_users+" u",
239                                    "u."+DB.gu_user+",u."+DB.dt_created+",u."+DB.id_domain+",u."+
240                                    DB.tx_nickname+",u."+DB.tx_pwd+",u."+DB.tx_pwd_sign+",u."+
241                                    DB.bo_change_pwd+",u."+DB.bo_searchable+",u."+
242                                    DB.bo_active+",u."+DB.len_quota+",u."+DB.max_quota+",u."+
243                                    DB.tp_account+",u."+DB.id_account+",u."+
244                                    DB.dt_last_update+",u."+DB.dt_last_visit+",u."+
245                                    DB.dt_cancel+",u."+DB.tx_main_email+",u."+
246                                    DB.tx_alt_email+",u."+DB.nm_user+",u."+
247                                    DB.tx_surname1+",u."+DB.tx_surname2+",u."+
248                                    DB.tx_challenge+",u."+DB.tx_reply+",u."+
249                                    DB.dt_pwd_expires+",u."+DB.gu_category+",u."+
250                                    DB.gu_workarea+",u."+DB.nm_company+",u."+
251                                    DB.de_title+",u."+DB.id_gender+",u."+DB.dt_birth+",u."+
252                                    DB.ny_age+",u."+DB.marital_status+",u."+
253                                    DB.tx_education+",u."+DB.icq_id+",u."+
254                                    DB.sn_passport+",u."+DB.tp_passport+",u."+
255                                    DB.tx_comments,
256                                    "x."+DB.gu_acl_group + "=? AND "+
257                                    "x."+DB.gu_user+"=u."+DB.k_users,10);
258
259     oUsers.load (oConn, aGroup);
260     return oUsers;
261   } // getACLUsers
262

263   // **********************************************************
264
// Metodos Estaticos
265

266   /**
267    * <p>Get Group Unique Id. from its name.</p>
268    * <p>This method executes a SQL query with a ResultSet</p>
269    * @param oConn Connection Database Connection
270    * @param iDomainId int Domain Identifier to with Group belongs
271    * @param sGroupNm String Group Name
272    * @return Group Unique Identifier or <b>null</b> if no group with such name was found at given domain.
273    * @throws SQLException
274    * @since 3.0
275    */

276
277   public static String JavaDoc getIdFromName(Connection JavaDoc oConn, int iDomainId, String JavaDoc sGroupNm) throws SQLException JavaDoc {
278     String JavaDoc sRetVal;
279     PreparedStatement JavaDoc oStmt;
280     ResultSet JavaDoc oRSet;
281     oStmt = oConn.prepareStatement("SELECT " + DB.gu_acl_group + " FROM " + DB.k_acl_groups + " WHERE " + DB.id_domain + "=? AND " + DB.nm_acl_group + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
282     oStmt.setInt (1, iDomainId);
283     oStmt.setString(2, sGroupNm);
284     oRSet = oStmt.executeQuery();
285     if (oRSet.next())
286       sRetVal = oRSet.getString(1);
287     else
288       sRetVal = null;
289     oRSet.close();
290     oStmt.close();
291     return sRetVal;
292   }
293
294   /**
295    * <p>Get Group Unique Id. from its name.</p>
296    * <p>This method calls k_sp_get_group_id stored procedure.</p>
297    * @param oConn JDCConnection
298    * @param iDomainId int Domain Identifier to with Group belongs
299    * @param sGroupNm Group Name
300    * @return Group String Unique Identifier or <b>null</b> if no group with such name was found at given domain.
301    * @throws SQLException
302    */

303
304   public static String JavaDoc getIdFromName(JDCConnection oConn, int iDomainId, String JavaDoc sGroupNm) throws SQLException JavaDoc {
305      String JavaDoc sRetVal;
306
307      switch (oConn.getDataBaseProduct()) {
308
309        case JDCConnection.DBMS_MYSQL:
310        case JDCConnection.DBMS_MSSQL:
311        case JDCConnection.DBMS_ORACLE:
312          sRetVal = DBPersist.getUIdFromName(oConn, new Integer JavaDoc(iDomainId), sGroupNm, "k_sp_get_group_id");
313          break;
314        default:
315          sRetVal = getIdFromName((Connection JavaDoc) oConn, iDomainId, sGroupNm);
316      } // end switch
317

318      return sRetVal;
319   } // getIdFromName
320

321   // ----------------------------------------------------------
322

323   /**
324    * <p>Delete Group</p>
325    * <p>Call k_sp_del_group stored procedure</p>
326    * @param oConn Database Connection
327    * @param sGroupGUID Group Unique Identifier
328    * @throws SQLException
329    */

330   public static boolean delete(JDCConnection oConn, String JavaDoc sGroupGUID) throws SQLException JavaDoc {
331     boolean bRetVal;
332
333     if (DebugFile.trace) {
334       DebugFile.writeln("Begin ACLGroup.delete([Connection], " + sGroupGUID + ")");
335       DebugFile.incIdent();
336     }
337
338     switch (oConn.getDataBaseProduct()) {
339       case JDCConnection.DBMS_POSTGRESQL:
340         Statement JavaDoc oStmt = oConn.createStatement();
341         if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(SELECT k_sp_del_group ('" + sGroupGUID + "'))");
342         ResultSet JavaDoc oRSet = oStmt.executeQuery("SELECT k_sp_del_group ('" + sGroupGUID + "')");
343         oRSet.close();
344         oStmt.close();
345         bRetVal = true;
346         break;
347       default:
348         if (DebugFile.trace) DebugFile.writeln("Connection.prepareCall({ call k_sp_del_group ('" + sGroupGUID + "') })");
349         CallableStatement JavaDoc oCall = oConn.prepareCall("{ call k_sp_del_group ('" + sGroupGUID + "') }");
350         bRetVal = oCall.execute();
351         oCall.close();
352     }
353
354     if (DebugFile.trace) {
355       DebugFile.decIdent();
356       DebugFile.writeln("End ACLGroup.delete() : " + String.valueOf(bRetVal));
357     }
358
359     return bRetVal;
360   }
361
362   // **********************************************************
363
// Public Constants
364

365   public static final short ClassId = 3;
366
367 } // ACLGroup
368
Popular Tags