KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.math.BigDecimal JavaDoc;
37
38 import java.io.IOException JavaDoc;
39
40 import java.sql.Connection JavaDoc;
41 import java.sql.SQLException JavaDoc;
42 import java.sql.CallableStatement JavaDoc;
43 import java.sql.PreparedStatement JavaDoc;
44 import java.sql.Statement JavaDoc;
45 import java.sql.ResultSet JavaDoc;
46
47 import com.knowgate.debug.DebugFile;
48 import com.knowgate.jdc.JDCConnection;
49 import com.knowgate.dataobjs.DB;
50 import com.knowgate.dataobjs.DBBind;
51 import com.knowgate.dataobjs.DBPersist;
52 import com.knowgate.dataobjs.DBSubset;
53 import com.knowgate.hipergate.Category;
54
55 import com.knowgate.misc.Gadgets;
56
57 /**
58  * <p>Object mapping for k_users table registers</p>
59  * @author Sergio Montoro Ten
60  * @version 3.0
61  */

62
63 public final class ACLUser extends DBPersist {
64
65   /**
66    * Default constructor.
67    */

68   public ACLUser() {
69    super(DB.k_users, "ACLUser");
70   }
71
72   /**
73    * <p>Constructs ACLUser and set GUID</p>
74    * Does not load any fields from database.
75    * @param sUserGUID user Unique Identifier (gu_user field at k_users table)
76    * @throws SQLException
77    */

78
79   public ACLUser(String JavaDoc sUserGUID) throws SQLException JavaDoc {
80     super(DB.k_users,"ACLUser");
81
82     put(DB.gu_user, sUserGUID);
83   }
84
85   /**
86    * <p>Constructs ACLUser and load attributes from k_users table</p>
87    * @param oConn Database Connection
88    * @param sUserGUID user Unique Identifier (gu_user field at k_users table)
89    * @throws SQLException
90    */

91
92   public ACLUser(JDCConnection oConn, String JavaDoc sUserGUID) throws SQLException JavaDoc {
93     super(DB.k_users,"ACLUser");
94
95     Object JavaDoc aUser[] = { sUserGUID };
96
97     load (oConn, aUser);
98   }
99
100   // ----------------------------------------------------------
101

102   /**
103    * <p>Get Addresses associated with user at k_x_addr_user table.</p>
104    * @param oConn Database Connection
105    * @return A {@link DBSubset} with a 3 columns containing
106    * Address Unique Identifier (gu_address), Address Ordinal Position (ix_address)
107    * and Address Location Type (tp_location).
108    * @throws SQLException
109    * @see {@link Address}
110    */

111   public DBSubset getAddresses(JDCConnection oConn) throws SQLException JavaDoc {
112     Object JavaDoc aUser[] = { get(DB.gu_user) };
113     oAddresses = new DBSubset(DB.k_x_addr_user,DB.gu_address + "," + DB.ix_address + "," + DB.tp_location, DB.gu_user + "=?",10);
114
115     oAddresses.load (oConn, aUser);
116     return oAddresses;
117   }
118
119   // ----------------------------------------------------------
120

121   /**
122    * </p>Get security role groups to witch this user belongs looking a k_x_group_user table.</p>
123    * @param oConn Database Connection
124    * @return A {@link DBSubset} with a 1 column containing each group unique identifier (gu_acl_group).
125    * @throws SQLException
126    */

127   public DBSubset getGroups(JDCConnection oConn) throws SQLException JavaDoc {
128     Object JavaDoc aUser[] = { get(DB.gu_user) };
129     oGroups = new DBSubset(DB.k_x_group_user,DB.gu_acl_group,DB.gu_user + "=?",10);
130
131     oGroups.load (oConn, aUser);
132     return oGroups;
133   }
134
135   // ----------------------------------------------------------
136

137   /**
138    * <p>Add User to Groups.</p>
139    * <p>Insert new registers at k_x_group_user table.</p>
140    * @param oConn Database Connection
141    * @param sGroupList A string of comma delimited ACLGroup GUIDs to with this ACLUser must be added.
142    * @throws SQLException May throw a primary key constraint violation is user already belongs to group.
143    */

144   public int addToACLGroups(JDCConnection oConn, String JavaDoc sGroupList) throws SQLException JavaDoc {
145
146      if (DebugFile.trace) {
147         DebugFile.writeln("Begin ACLUser.addToACLGroups(Connection], " + sGroupList + ")");
148         DebugFile.incIdent();
149      }
150
151      Statement JavaDoc oStmt;
152      int iRetVal = 0;
153      StringTokenizer JavaDoc oStrTok = new StringTokenizer JavaDoc(sGroupList, ",");
154      String JavaDoc sIdGroup;
155
156      oStmt = oConn.createStatement();
157
158      while (oStrTok.hasMoreElements()) {
159        sIdGroup = oStrTok.nextToken();
160
161        if (DebugFile.trace)
162          DebugFile.writeln("Statement.executeUpdate(INSERT INTO " + DB.k_x_group_user + "(" + DB.gu_user + "," + DB.gu_acl_group + ") VALUES('" + getStringNull(DB.gu_user, "null") + "','" + sIdGroup + "')");
163
164        iRetVal += oStmt.executeUpdate("INSERT INTO " + DB.k_x_group_user + "(" + DB.gu_user + "," + DB.gu_acl_group + ") VALUES('" + getString(DB.gu_user) + "','" + sIdGroup + "')");
165      } // wend
166

167      oStmt.close();
168
169      if (DebugFile.trace) {
170         DebugFile.decIdent();
171         DebugFile.writeln("End ACLUser.addToACLGroups() : " + String.valueOf(iRetVal));
172      }
173
174      return iRetVal;
175   } // addToACLGroups
176

177   // ----------------------------------------------------------
178

179   /**
180    * <p>Find out is this user has administrator.</p>
181    * <p>A user may have administrator priviledges in two ways:<br>
182    * 1.- It can belong to the group gu_admins from k_domains table.<br>
183    * 2.- Its user identifier may be the one at gu_owner field of k_domains table.<br></p>
184    * <p>The domain owner is a special kind of administrator user that cannot be deleted from domain.</p>
185    * @param oConn Database Connection
186    * @return true is user has adminsitrator priviledges, false otherwise.
187    * @throws IllegalStateException if id_domain or gu_user is not set
188    * @throws SQLException
189    */

190   public boolean isDomainAdmin(JDCConnection oConn) throws SQLException JavaDoc, IllegalStateException JavaDoc {
191     PreparedStatement JavaDoc oStmt;
192     ResultSet JavaDoc oRSet;
193     boolean bAdmin;
194
195     if (DebugFile.trace) {
196        DebugFile.writeln("Begin ACLUser.sDomainAdmin(Connection])");
197        DebugFile.incIdent();
198     }
199
200     if (isNull(DB.id_domain)) {
201       if (DebugFile.trace) {
202         DebugFile.writeln("ERROR id_domain not set");
203         DebugFile.decIdent();
204       }
205
206       throw new IllegalStateException JavaDoc("ACLUSer.isDomainAdmin() Property id_domain is not set");
207     }
208
209     if (isNull(DB.gu_user)) {
210       if (DebugFile.trace) {
211         DebugFile.writeln("ERROR gu_user not set");
212         DebugFile.decIdent();
213       }
214
215       throw new IllegalStateException JavaDoc("ACLUSer.isDomainAdmin() Property gu_user is not set");
216     }
217
218     if (DebugFile.trace)
219        DebugFile.writeln("Connection.prepareStatement(SELECT NULL FROM " + DB.k_x_group_user + " x," + DB.k_domains + " d WHERE d." + DB.id_domain + "=" + String.valueOf(getInt(DB.id_domain)) + " AND x." + DB.gu_user + "='" + getString(DB.gu_user) + "'" + " AND x." + DB.gu_acl_group + "=d." + DB.gu_admins + ")");
220
221     int iDomainId;
222     if (isNull(DB.id_domain))
223       iDomainId = -1;
224     else
225       iDomainId = getInt(DB.id_domain);
226
227     oStmt = oConn.prepareStatement("SELECT NULL FROM " + DB.k_x_group_user + " x," + DB.k_domains + " d WHERE d." + DB.id_domain + "=?" + " AND x." + DB.gu_user + "=?" + " AND x." + DB.gu_acl_group + "=d." + DB.gu_admins, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
228     oStmt.setInt(1,iDomainId);
229     oStmt.setString(2,getStringNull(DB.gu_user,null));
230     oRSet = oStmt.executeQuery();
231     bAdmin = oRSet.next();
232     oRSet.close();
233     oStmt.close();
234
235     if (!bAdmin) {
236       if (DebugFile.trace)
237         DebugFile.writeln("Connection.prepareStatement(SELECT " + DB.gu_owner + " FROM " + DB.k_domains + " WHERE " + DB.id_domain + "=" + String.valueOf(getInt(DB.id_domain)) + ")");
238
239       oStmt = oConn.prepareStatement("SELECT " + DB.gu_owner + " FROM " + DB.k_domains + " WHERE " + DB.id_domain + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
240       oStmt.setInt(1,getInt(DB.id_domain));
241       oRSet = oStmt.executeQuery();
242
243       if (oRSet.next())
244         if (null!=oRSet.getObject(1))
245           bAdmin = oRSet.getString(1).equals(getString(DB.gu_user));
246         else
247           bAdmin = false;
248       else
249         bAdmin = false;
250
251       oRSet.close();
252       oStmt.close();
253     }
254
255     if (DebugFile.trace) {
256        DebugFile.decIdent();
257        DebugFile.writeln("End ACLUser.isDomainAdmin() : " + String.valueOf(bAdmin));
258     }
259
260     return bAdmin;
261   } // isDomainAdmin
262

263   // ----------------------------------------------------------
264

265   /**
266    * <p>Store ACLUser</p>
267    * If gu_user is not set then a new GUID is assigned.<br>
268    * If dt_last_update is not set then current system date is assigned.<br>
269    * If len_quota is not set then zero is assigned.<br>
270    * If max_quota is not set then 100Mb assigned.<br>
271    * Syntax for tx_main_email and tx_alt_email is verified if these fields are not null
272    * @param oConn Database Connection
273    * @throws SQLException
274    */

275   public boolean store(JDCConnection oConn) throws SQLException JavaDoc {
276     boolean bRetVal;
277     Object JavaDoc oDomainId;
278     String JavaDoc NmWorkArea;
279     PreparedStatement JavaDoc oStmt;
280     ResultSet JavaDoc oRSet;
281
282     if (DebugFile.trace) {
283        DebugFile.writeln("Begin ACLUser.store([Connection])");
284        DebugFile.incIdent();
285     }
286
287     if (!AllVals.containsKey(DB.gu_user))
288       put (DB.gu_user, Gadgets.generateUUID());
289
290     if (!AllVals.containsKey(DB.dt_last_update))
291       put (DB.dt_last_update, new java.sql.Timestamp JavaDoc(DBBind.getTime()));
292
293     if (!AllVals.containsKey(DB.len_quota))
294       put (DB.len_quota, new BigDecimal JavaDoc(0d));
295
296     if (!AllVals.containsKey(DB.max_quota))
297       put (DB.max_quota, new BigDecimal JavaDoc(104857600d));
298
299     if (AllVals.containsKey(DB.gu_workarea) && AllVals.containsKey(DB.id_domain)) {
300       if (!isNull(DB.gu_workarea)) {
301
302         if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT " + DB.id_domain + "," + DB.nm_workarea + " FROM " + DB.k_workareas + " WHERE " + DB.gu_workarea + "='" + getString(DB.gu_workarea) + "')");
303
304         oStmt = oConn.prepareStatement("SELECT " + DB.id_domain + "," + DB.nm_workarea + " FROM " + DB.k_workareas + " WHERE " + DB.gu_workarea + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
305         oStmt.setString(1, getString(DB.gu_workarea));
306         oRSet = oStmt.executeQuery();
307         if (oRSet.next()) {
308           oDomainId = oRSet.getObject(1);
309           NmWorkArea = oRSet.getString(2);
310         }
311         else {
312           oDomainId = null;
313           NmWorkArea = null;
314         }
315         oRSet.close();
316         oStmt.close();
317
318         if (null!=oDomainId) {
319           if (!oDomainId.toString().equals(get(DB.id_domain).toString()))
320             throw new SQLException JavaDoc("ACLUSer.store() Integrity constraint violation: WorkArea " + NmWorkArea + " must belong to Domain " + oDomainId.toString() + " as User does but it belongs to " + oDomainId.toString(), "23000");
321         } // fi (null!=oDomainId)
322

323       } // fi (null!=gu_workarea)
324
} // fi (containsKey(DB.gu_workarea) && containsKey(DB.id_domain))
325

326     if (!Gadgets.checkEMail(getStringNull(DB.tx_main_email,"nobody@nodomain.com"))) {
327       if (DebugFile.trace) DebugFile.decIdent();
328       throw new SQLException JavaDoc("ACLUSer.store() Check constraint violation: e-mail address "+getString(DB.tx_main_email)+" does not have a valid syntax", "23000");
329     }
330
331     if (!Gadgets.checkEMail(getStringNull(DB.tx_alt_email,"nobody@nodomain.com"))) {
332       if (DebugFile.trace) DebugFile.decIdent();
333       throw new SQLException JavaDoc("ACLUSer.store() Check constraint violation: e-mail address "+getString(DB.tx_alt_email)+" does not have a valid syntax", "23000");
334     }
335
336     bRetVal = super.store(oConn);
337
338     if (DebugFile.trace) {
339       DebugFile.decIdent();
340        DebugFile.writeln("End ACLUser.store() : " + String.valueOf(bRetVal));
341     }
342
343     return bRetVal;
344   } // store
345

346   // ----------------------------------------------------------
347

348   public boolean delete(JDCConnection oConn) throws SQLException JavaDoc {
349     try {
350       return ACLUser.delete(oConn, getString(DB.gu_user));
351     } catch (IOException JavaDoc ioe) {
352       throw new SQLException JavaDoc ("IOException " + ioe.getMessage());
353     }
354   } // delete
355

356   // ----------------------------------------------------------
357

358   /**
359    * <p>Remove user from all security role groups</p>
360    * @param oConn Database Connection
361    * @return Count of groups from witch user was removed.
362    * @throws SQLException
363    */

364   public int clearACLGroups(JDCConnection oConn) throws SQLException JavaDoc {
365
366     if (DebugFile.trace) {
367       DebugFile.writeln("Begin ACLUser.clearACLGroups([Connection])");
368       DebugFile.incIdent();
369     }
370
371      int iRetVal;
372
373      Statement JavaDoc oStmt = oConn.createStatement();
374      iRetVal = oStmt.executeUpdate("DELETE FROM " + DB.k_x_group_user + " WHERE " + DB.gu_user + "='" + getString(DB.gu_user) + "'");
375      oStmt.close();
376
377      if (DebugFile.trace) {
378         DebugFile.decIdent();
379         DebugFile.writeln("End ACLGroup.clearACLGroups() : " + String.valueOf(iRetVal));
380      }
381
382      return iRetVal;
383   } // clearACLGroups
384

385   // ----------------------------------------------------------
386

387   /**
388    * <p>Remove User from Group.</p>
389    * <p>remove register from k_x_group_user table.</p>
390    * @param oConn Database Connection
391    * @param sIdGroup Group Unique Identifier.
392    * @throws SQLException
393    */

394
395   public int removeFromACLGroup(JDCConnection oConn, String JavaDoc sIdGroup) throws SQLException JavaDoc {
396
397     if (DebugFile.trace) {
398        DebugFile.writeln("Begin ACLUser.removeFromACLGroup(Connection], " + sIdGroup + ")");
399        DebugFile.incIdent();
400     }
401
402      int iRetVal;
403      Statement JavaDoc oStmt = oConn.createStatement();
404
405      if (DebugFile.trace)
406        DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_x_group_user + " WHERE " + DB.gu_user + "='" + getStringNull(DB.gu_user, "null") + "' AND " + DB.gu_acl_group + "='" + sIdGroup + "'");
407
408      iRetVal = oStmt.executeUpdate("DELETE FROM " + DB.k_x_group_user + " WHERE " + DB.gu_user + "='" + getString(DB.gu_user) + "' AND " + DB.gu_acl_group + "='" + sIdGroup + "'");
409      oStmt.close();
410
411      if (DebugFile.trace) {
412         DebugFile.decIdent();
413         DebugFile.writeln("End ACLUser.removeFromACLGroup() : " + String.valueOf(iRetVal));
414      }
415
416      return iRetVal;
417   } // removeFromACLGroup
418

419   // ---------------------------------------------------------------------------
420

421   /**
422    * <p>Get GUID of user mail root category</p>
423    * The user mail root category is always named <i>DOMAIN</i>_<i>nickname</i>_mail
424    * at nm_category field of k_categories.<br>
425    * If there is no category named <i>DOMAIN</i>_<i>nickname</i>_mail but the
426    * user has his gu_category field set at k_users table then this method tries
427    * to create a new mail root category.
428    * @param oConn Database Connection
429    * @return a gu_category value from k_categories or <b>null</b> if this user
430    * does not have a mail root category and a new one could not be created
431    * @throws SQLException
432    */

433   public String JavaDoc getMailRoot (JDCConnection oConn) throws SQLException JavaDoc {
434     PreparedStatement JavaDoc oStmt;
435     CallableStatement JavaDoc oCall;
436     ResultSet JavaDoc oRSet;
437     String JavaDoc sRetVal;
438
439     if (DebugFile.trace) {
440        DebugFile.writeln("Begin ACLUser.getMailRoot(Connection])");
441        DebugFile.incIdent();
442     }
443
444     switch (oConn.getDataBaseProduct()) {
445
446       case JDCConnection.DBMS_POSTGRESQL:
447         if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT k_sp_get_user_mailroot('"+getStringNull(DB.gu_user,"null")+"'))");
448         oStmt = oConn.prepareStatement("SELECT k_sp_get_user_mailroot(?)");
449         oStmt.setString(1, getString(DB.gu_user));
450         oRSet = oStmt.executeQuery();
451         if (oRSet.next())
452           sRetVal = oRSet.getString(1);
453         else
454           sRetVal = null;
455         oRSet.close();
456         oStmt.close();
457         break;
458       default:
459         if (DebugFile.trace) DebugFile.writeln("Connection.prepareCall({ call k_sp_get_user_mailroot('"+getStringNull(DB.gu_user,"null")+"',?) }");
460         oCall = oConn.prepareCall("{ call k_sp_get_user_mailroot(?,?) }");
461         oCall.setString(1, getString(DB.gu_user));
462         oCall.registerOutParameter(2, java.sql.Types.CHAR);
463         oCall.execute();
464         sRetVal = oCall.getString(2);
465         oCall.close();
466         break;
467     }
468
469     if (sRetVal==null) {
470       if (DebugFile.trace) DebugFile.writeln("user mail root not found creating new one...");
471       ACLUser oMe = new ACLUser();
472       if (oMe.load(oConn, new Object JavaDoc[]{getString(DB.gu_user)})) {
473         if (!oMe.isNull(DB.gu_category)) {
474           ACLDomain oDom = new ACLDomain(oConn, oMe.getInt(DB.id_domain));
475
476           Statement JavaDoc oInsr = oConn.createStatement();
477           String JavaDoc sNewGUID = Gadgets.generateUUID();
478
479           String JavaDoc sSQL = "INSERT INTO "+DB.k_categories+" ("+DB.gu_category+","+DB.gu_owner+","+DB.nm_category+","+DB.bo_active+","+DB.dt_modified+","+DB.nm_icon+","+DB.id_doc_status+","+DB.nm_icon2+") VALUES ('"+sNewGUID+"','"+oMe.getString(DB.gu_user)+"','"+oDom.getString(DB.nm_domain)+"_"+oMe.getString(DB.tx_nickname)+"_mail',1,NULL,'myemailc_16x16.gif',1,'myemailo_16x16.gif')";
480           if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeUpdate("+sSQL+")");
481           oInsr.executeUpdate(sSQL);
482
483           String JavaDoc[] aLabels = new String JavaDoc[]{"en","es","fr","de","it","pt","ca","ja","cn","tw","fi","ru","pl","nl"};
484
485           for (int l=0; l<aLabels.length; l++)
486             oInsr.executeUpdate("INSERT INTO "+DB.k_cat_labels+" ("+DB.gu_category+","+DB.id_language+","+DB.tr_category+","+DB.url_category+") VALUES ('"+sNewGUID+"','"+aLabels[l]+"','e-mail',NULL)");
487
488           oInsr.executeUpdate("INSERT INTO "+DB.k_x_cat_user_acl+" ("+DB.gu_category+","+DB.gu_user+","+DB.acl_mask+") VALUES ('"+sNewGUID+"','"+oMe.getString(DB.gu_user)+"',2147483647)");
489
490           if (!oMe.getString(DB.gu_user).equals(oDom.getString(DB.gu_owner)))
491             oInsr.executeUpdate("INSERT INTO "+DB.k_x_cat_user_acl+" ("+DB.gu_category+","+DB.gu_user+","+DB.acl_mask+") VALUES ('"+sNewGUID+"','"+oDom.getString(DB.gu_owner)+"',2147483647)");
492
493           sSQL = "INSERT INTO "+DB.k_cat_tree+" ("+DB.gu_parent_cat+","+DB.gu_child_cat+") VALUES ('"+oMe.getString(DB.gu_category)+"','"+sNewGUID+"')";
494           if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeUpdate("+sSQL+")");
495           oInsr.executeUpdate(sSQL);
496
497           oInsr.close();
498           sRetVal = sNewGUID;
499         }
500         else {
501           if (DebugFile.trace) DebugFile.writeln("user " + getString(DB.gu_user) + " has not home category");
502         }
503       }
504       else {
505         if (DebugFile.trace) DebugFile.writeln("unable to load user " + getString(DB.gu_user));
506       }
507     }
508     else
509       sRetVal = sRetVal.trim();
510
511     if (DebugFile.trace) {
512        DebugFile.decIdent();
513        DebugFile.writeln("End ACLUser.getMailRoot() : " + String.valueOf(sRetVal));
514     }
515
516     return sRetVal;
517   } // getMailRoot
518

519   // ---------------------------------------------------------------------------
520

521   /**
522    * <p>Get GUID of user mail folder category</p>
523    * The Inbox category is where messages downloaded from the mail server are cached by default.
524    * The user mail inbox category is always named <i>DOMAIN</i>_<i>nickname</i>_inbox at nm_category field of k_categories.<br>
525    * If there is no category named <i>DOMAIN</i>_<i>nickname</i>_inbox but the
526    * user has a mail root category then this method tries to create a new mail
527    * inbox category under mail root.<BR>
528    * @param oConn Database Connection
529    * @param sFolderName One of { "inbox", "drafts", "deleted", "sent", "spam" }
530    * @return a gu_category value from k_categories or <b>null</b> if this user
531    * does not have a mail inbox category and a new one could not be created
532    * @throws SQLException
533    * @throws NullPointerException if sFolderName is <b>null</b> or empty string
534    */

535
536   public String JavaDoc getMailFolder (JDCConnection oConn, String JavaDoc sFolderName)
537     throws SQLException JavaDoc {
538     PreparedStatement JavaDoc oStmt;
539     CallableStatement JavaDoc oCall;
540     ResultSet JavaDoc oRSet;
541     String JavaDoc sRetVal;
542
543     if (DebugFile.trace) {
544        DebugFile.writeln("Begin ACLUser.getMailFolder([JDCConnection],"+sFolderName+")");
545        DebugFile.incIdent();
546     }
547
548     if (sFolderName==null) {
549       if (DebugFile.trace) DebugFile.decIdent();
550       throw new NullPointerException JavaDoc("ACLUser.getMailFolder() folder name cannot be null");
551     }
552     if (sFolderName.length()==0) {
553       if (DebugFile.trace) DebugFile.decIdent();
554       throw new NullPointerException JavaDoc("ACLUser.getMailFolder() folder name cannot be an empty string");
555     }
556
557     String JavaDoc sIcon1 = sFolderName.equalsIgnoreCase("inbox") ? "mailbox_16x16.gif" : "folderclosed_16x16.gif";
558     String JavaDoc sIcon2 = sFolderName.equalsIgnoreCase("inbox") ? "mailbox_16x16.gif" : "folderopen_16x16.gif";
559
560     switch (oConn.getDataBaseProduct()) {
561
562       case JDCConnection.DBMS_POSTGRESQL:
563         if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT k_sp_get_user_mailfolder('"+getStringNull(DB.gu_user,"null")+"','"+sFolderName+"')");
564
565         oStmt = oConn.prepareStatement("SELECT k_sp_get_user_mailfolder(?,?)");
566         oStmt.setString(1, getString(DB.gu_user));
567         oStmt.setString(2, sFolderName);
568         oRSet = oStmt.executeQuery();
569         if (oRSet.next())
570           sRetVal = oRSet.getString(1);
571         else
572           sRetVal = null;
573         oRSet.close();
574         oStmt.close();
575         break;
576       default:
577         if (DebugFile.trace) DebugFile.writeln("Connection.prepareCall({ call k_sp_get_user_mailfolder('"+getStringNull(DB.gu_user,"null")+"','"+sFolderName+"',?)})");
578         oCall = oConn.prepareCall("{ call k_sp_get_user_mailfolder(?,?,?) }");
579         oCall.setString(1, getString(DB.gu_user));
580         oCall.setString(2, sFolderName);
581         oCall.registerOutParameter(3, java.sql.Types.CHAR);
582         oCall.execute();
583         sRetVal = oCall.getString(3);
584         oCall.close();
585         break;
586     }
587
588     if (sRetVal==null) {
589       if (DebugFile.trace) DebugFile.writeln("Creating new mail folder " + sFolderName + " for user " + getStringNull(DB.gu_user,"null"));
590       ACLUser oMe = new ACLUser();
591       if (oMe.load(oConn, new Object JavaDoc[] {getString(DB.gu_user)})) {
592         if (!oMe.isNull(DB.gu_category)) {
593           String JavaDoc sGuMailRoot = getMailRoot(oConn);
594           if (sGuMailRoot!=null) {
595             ACLDomain oDom = new ACLDomain(oConn, oMe.getInt(DB.id_domain));
596
597             Statement JavaDoc oInsr = oConn.createStatement();
598             String JavaDoc sNewGUID = Gadgets.generateUUID();
599
600             String JavaDoc sSQL = "INSERT INTO "+DB.k_categories+" ("+DB.gu_category+","+DB.gu_owner+","+DB.nm_category+","+DB.bo_active+","+DB.dt_modified+","+DB.nm_icon+","+DB.id_doc_status+","+DB.nm_icon2+","+DB.len_size+") VALUES ('"+sNewGUID+"','"+oMe.getString(DB.gu_user)+"','"+oDom.getString(DB.nm_domain)+"_"+oMe.getString(DB.tx_nickname)+"_"+sFolderName+"',1,NULL,'"+sIcon1+"',1,'"+sIcon2+"',0)";
601
602             if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(" + sSQL + ")");
603
604             oInsr.executeUpdate(sSQL);
605
606             String JavaDoc[] aLabels = new String JavaDoc[]{"en","es","fr","de","it","pt","ca","ja","cn","tw","fi","ru","pl","nl","xx"};
607
608             for (int l=0; l<aLabels.length; l++) {
609               sSQL = "INSERT INTO "+DB.k_cat_labels+" ("+DB.gu_category+","+DB.id_language+","+DB.tr_category+","+DB.url_category+") VALUES ('"+sNewGUID+"','"+aLabels[l]+"','"+sFolderName.substring(0,1).toUpperCase()+sFolderName.substring(1).toLowerCase()+"',NULL)";
610               if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(" + sSQL + ")");
611               oInsr.executeUpdate(sSQL);
612             }
613
614             sSQL = "INSERT INTO "+DB.k_x_cat_user_acl+" ("+DB.gu_category+","+DB.gu_user+","+DB.acl_mask+") VALUES ('"+sNewGUID+"','"+oMe.getString(DB.gu_user)+"',2147483647)";
615             if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(" + sSQL + ")");
616             oInsr.executeUpdate(sSQL);
617
618             if (!oMe.getString(DB.gu_user).equals(oDom.getString(DB.gu_owner))) {
619               sSQL = "INSERT INTO "+DB.k_x_cat_user_acl+" ("+DB.gu_category+","+DB.gu_user+","+DB.acl_mask+") VALUES ('"+sNewGUID+"','"+oDom.getString(DB.gu_owner)+"',2147483647)";
620               if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(" + sSQL + ")");
621               oInsr.executeUpdate(sSQL);
622             }
623
624             sSQL = "INSERT INTO "+DB.k_cat_tree+" ("+DB.gu_parent_cat+","+DB.gu_child_cat+") VALUES ('"+sGuMailRoot+"','"+sNewGUID+"')";
625             if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(" + sSQL + ")");
626             oInsr.executeUpdate(sSQL);
627
628             oInsr.close();
629
630             Category oMailRoot = new Category(sGuMailRoot);
631             oMailRoot.expand(oConn);
632             sRetVal = sNewGUID;
633           }
634         }
635       }
636       else {
637         if (DebugFile.trace) DebugFile.writeln("user " + getStringNull(DB.gu_user,"null") + " does not exist");
638         throw new SQLException JavaDoc ("User "+getStringNull(DB.gu_user,"null")+" not found", "02000", 0);
639       }
640     }
641     else
642       sRetVal = sRetVal.trim();
643
644     if (DebugFile.trace) {
645        DebugFile.decIdent();
646        DebugFile.writeln("End ACLUser.getMailFolder() : " + String.valueOf(sRetVal));
647     }
648
649     return sRetVal;
650   } // getMailFolder
651

652   // ---------------------------------------------------------------------------
653

654   /**
655    * Get roles of this user for a given application and workarea
656    * @param oConn JDCConnection
657    * @param iIdApp int Id of application (from k_apps table)
658    * @param sGuWorkArea String Guid of WorkArea (from k_workareas table)
659    * @return int Any bitwise OR combination of { ACL.ROLE_ADMIN || ACL.ROLE_POWERUSER || ACL.ROLE_USER || ACL.ROLE_GUEST }
660    * @throws SQLException
661    * @since 3.0
662    */

663   public int getRolesForApplication(JDCConnection oConn, int iIdApp, String JavaDoc sGuWorkArea)
664     throws SQLException JavaDoc {
665
666     if (DebugFile.trace) {
667       DebugFile.writeln("Begin ACLUser.getRolesForApplication([JDCConnection],"+String.valueOf(iIdApp)+","+sGuWorkArea+")");
668       DebugFile.incIdent();
669     }
670
671     int iRolesBitMask = ACL.ROLE_NONE;
672     String JavaDoc sGuAdmins, sGuPowUsers, sGuUser, sGuGuest;
673     PreparedStatement JavaDoc oStmt = oConn.prepareStatement(
674       "SELECT "+DB.gu_admins+","+DB.gu_powusers+","+DB.gu_users+","+DB.gu_guests+
675       " FROM "+DB.k_x_app_workarea+" WHERE "+DB.id_app+"=? AND "+DB.gu_workarea+"=?",
676       ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
677     oStmt.setInt(1, iIdApp);
678     oStmt.setString(2, sGuWorkArea);
679     ResultSet JavaDoc oRSet = oStmt.executeQuery();
680     boolean bFound = oRSet.next();
681     if (bFound) {
682       sGuAdmins = oRSet.getString(1);
683       sGuPowUsers = oRSet.getString(2);
684       sGuUser = oRSet.getString(3);
685       sGuGuest = oRSet.getString(4);
686     } else {
687       sGuAdmins = sGuPowUsers = sGuUser = sGuGuest = null;
688     }
689     oRSet.close();
690     oStmt.close();
691     if (bFound) {
692       oStmt = oConn.prepareStatement(
693                 "SELECT NULL FROM "+DB.k_x_group_user+" WHERE "+DB.gu_acl_group+"=? AND "+DB.gu_user+"='"+getString(DB.gu_user)+"'",
694                 ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
695       if (null!=sGuAdmins) {
696         oStmt.setString(1, sGuAdmins);
697         oRSet = oStmt.executeQuery();
698         if (oRSet.next()) iRolesBitMask = ACL.ROLE_ADMIN;
699         oRSet.close();
700       }
701       if (null!=sGuPowUsers) {
702         oStmt.setString(1, sGuPowUsers);
703         oRSet = oStmt.executeQuery();
704         if (oRSet.next()) iRolesBitMask |= ACL.ROLE_POWERUSER;
705         oRSet.close();
706       }
707       if (null!=sGuUser) {
708         oStmt.setString(1, sGuUser);
709         oRSet = oStmt.executeQuery();
710         if (oRSet.next()) iRolesBitMask |= ACL.ROLE_USER;
711         oRSet.close();
712       }
713       if (null!=sGuGuest) {
714         oStmt.setString(1, sGuGuest);
715         oRSet = oStmt.executeQuery();
716         if (oRSet.next()) iRolesBitMask |= ACL.ROLE_GUEST;
717         oRSet.close();
718       }
719       oStmt.close();
720     } // fi (bFound)
721

722     if (DebugFile.trace) {
723        DebugFile.decIdent();
724        DebugFile.writeln("End ACLUser.getRolesForApplication() : " + String.valueOf(iRolesBitMask));
725     }
726
727     return iRolesBitMask;
728   } // getRolesForApplication
729

730   // ---------------------------------------------------------------------------
731

732   /**
733    * Find out if this user has administrator role over given application and workarea
734    * @param oConn JDCConnection
735    * @param iIdApp int Id of application (from k_apps table)
736    * @param sGuWorkArea String Guid of WorkArea (from k_workareas table)
737    * @return boolean
738    * @throws SQLException
739    * @since 3.0
740    */

741   public boolean isAdminForApplication(JDCConnection oConn, int iIdApp, String JavaDoc sGuWorkArea)
742     throws SQLException JavaDoc {
743     return ((getRolesForApplication(oConn, iIdApp, sGuWorkArea)&ACL.ROLE_ADMIN)!=0);
744   }
745
746   // ---------------------------------------------------------------------------
747

748   /**
749    * Find out if this user has power user role over given application and workarea
750    * @param oConn JDCConnection
751    * @param iIdApp int Id of application (from k_apps table)
752    * @param sGuWorkArea String Guid of WorkArea (from k_workareas table)
753    * @return boolean
754    * @throws SQLException
755    * @since 3.0
756    */

757   public boolean isPowerUserForApplication(JDCConnection oConn, int iIdApp, String JavaDoc sGuWorkArea)
758     throws SQLException JavaDoc {
759     return ((getRolesForApplication(oConn, iIdApp, sGuWorkArea)&ACL.ROLE_POWERUSER)!=0);
760   }
761
762   // ---------------------------------------------------------------------------
763

764   /**
765    * Find out if this user has plain user role over given application and workarea
766    * @param oConn JDCConnection
767    * @param iIdApp int Id of application (from k_apps table)
768    * @param sGuWorkArea String Guid of WorkArea (from k_workareas table)
769    * @return boolean
770    * @throws SQLException
771    * @since 3.0
772    */

773   public boolean isUserForApplication(JDCConnection oConn, int iIdApp, String JavaDoc sGuWorkArea)
774     throws SQLException JavaDoc {
775     return ((getRolesForApplication(oConn, iIdApp, sGuWorkArea)&ACL.ROLE_USER)!=0);
776   }
777
778   // ---------------------------------------------------------------------------
779

780   /**
781    * Find out if this user has administrator role in his default workarea over given application
782    * @param oConn JDCConnection
783    * @param iIdApp int Id of application (from k_apps table)
784    * @param sGuWorkArea String Guid of WorkArea (from k_workareas table)
785    * @return boolean
786    * @throws SQLException
787    * @since 3.0
788    */

789   public boolean isAdminForApplication(JDCConnection oConn, int iIdApp)
790     throws SQLException JavaDoc {
791     return ((getRolesForApplication(oConn, iIdApp, getString(DB.gu_workarea))&ACL.ROLE_ADMIN)!=0);
792   }
793
794   // ---------------------------------------------------------------------------
795

796   /**
797    * Find out if this user has power user role in his default workarea over given application
798    * @param oConn JDCConnection
799    * @param iIdApp int Id of application (from k_apps table)
800    * @param sGuWorkArea String Guid of WorkArea (from k_workareas table)
801    * @return boolean
802    * @throws SQLException
803    * @since 3.0
804    */

805   public boolean isPowerUserForApplication(JDCConnection oConn, int iIdApp)
806     throws SQLException JavaDoc {
807     return ((getRolesForApplication(oConn, iIdApp, getString(DB.gu_workarea))&ACL.ROLE_POWERUSER)!=0);
808   }
809
810   // ---------------------------------------------------------------------------
811

812   /**
813    * Find out if this user has plain user role in his default workarea over given application
814    * @param oConn JDCConnection
815    * @param iIdApp int Id of application (from k_apps table)
816    * @param sGuWorkArea String Guid of WorkArea (from k_workareas table)
817    * @return boolean
818    * @throws SQLException
819    * @since 3.0
820    */

821   public boolean isUserForApplication(JDCConnection oConn, int iIdApp)
822     throws SQLException JavaDoc {
823     return ((getRolesForApplication(oConn, iIdApp, getString(DB.gu_workarea))&ACL.ROLE_USER)!=0);
824   }
825
826   // ---------------------------------------------------------------------------
827

828   /**
829    * Find out if this user has guest role in his default workarea over given application
830    * @param oConn JDCConnection
831    * @param iIdApp int Id of application (from k_apps table)
832    * @param sGuWorkArea String Guid of WorkArea (from k_workareas table)
833    * @return boolean
834    * @throws SQLException
835    * @since 3.0
836    */

837   public boolean isGuestForApplication(JDCConnection oConn, int iIdApp)
838     throws SQLException JavaDoc {
839     return ((getRolesForApplication(oConn, iIdApp, getString(DB.gu_workarea))&ACL.ROLE_GUEST)!=0);
840   }
841
842   // ---------------------------------------------------------------------------
843

844   // ***************************************************************************
845
// Static Methods
846

847   /**
848    * <p>Get User Unique Id. from its main e-mail address.</p>
849    * <p>This method calls k_get_user_from_email stored procedure.</p>
850    * @param oConn Database Connection
851    * @param sUserEMail User main e-mail (tx_main_email from k_users table)
852    * @return User Unique Id. or <b>null</b> if no user was found with such e-mail.
853    * @throws SQLException
854    */

855   public static String JavaDoc getIdFromEmail(JDCConnection oConn, String JavaDoc sUserEMail) throws SQLException JavaDoc {
856     String JavaDoc sRetVal;
857     PreparedStatement JavaDoc oStmt;
858     ResultSet JavaDoc oRSet;
859
860     switch (oConn.getDataBaseProduct()) {
861
862       case JDCConnection.DBMS_MYSQL:
863       case JDCConnection.DBMS_MSSQL:
864       case JDCConnection.DBMS_ORACLE:
865         sRetVal = DBPersist.getUIdFromName(oConn, null, sUserEMail, "k_get_user_from_email");
866         break;
867
868       default:
869         if (DebugFile.trace)
870           DebugFile.writeln("Connection.prepareStatement(SELECT " + DB.gu_user + " FROM " + DB.k_users + " WHERE " + DB.tx_main_email + "='" + sUserEMail + "')");
871
872         oStmt = oConn.prepareStatement("SELECT " + DB.gu_user + " FROM " + DB.k_users + " WHERE " + DB.tx_main_email + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
873         oStmt.setString(1, sUserEMail);
874         oRSet = oStmt.executeQuery();
875         if (oRSet.next())
876           sRetVal = oRSet.getString(1);
877         else
878           sRetVal = null;
879         oRSet.close();
880         oStmt.close();
881         break;
882
883     } // end switch
884

885     return sRetVal;
886   } // getIdFromEmail
887

888   // ----------------------------------------------------------
889

890   /**
891    * <p>Get User main e-mail from its GUID.</p>
892    * @param oConn Database Connection
893    * @param sUserId User GUID (gu_user from k_users table)
894    * @return User tx_main_email or <b>null</b> if no user was found with such GUID.
895    * @throws SQLException
896    */

897
898   public static String JavaDoc getEmailFromId(JDCConnection oConn, String JavaDoc sUserId) throws SQLException JavaDoc {
899     String JavaDoc sRetVal;
900     PreparedStatement JavaDoc oStmt;
901     ResultSet JavaDoc oRSet;
902
903     if (DebugFile.trace)
904       DebugFile.writeln("Connection.prepareStatement(SELECT " + DB.tx_main_email + " FROM " + DB.k_users + " WHERE " + DB.gu_user + "='" + sUserId + "')");
905
906     oStmt = oConn.prepareStatement("SELECT " + DB.tx_main_email + " FROM " + DB.k_users + " WHERE " + DB.gu_user + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
907     oStmt.setString(1, sUserId);
908     oRSet = oStmt.executeQuery();
909     if (oRSet.next())
910       sRetVal = oRSet.getString(1);
911     else
912       sRetVal = null;
913     oRSet.close();
914     oStmt.close();
915
916     return sRetVal;
917   } // getEmailFromId
918

919   // ----------------------------------------------------------
920

921   /**
922    * <p>Get User Unique Id. from its nickname.</p>
923    * <p>This method executes a SQL query with a ResultSet</p>
924    * @param oConn Database Connection
925    * @param sUserEMail User nickname (tx_nickname from k_users table)
926    * @return User Unique Id. or <b>null</b> if no user was found with such e-mail.
927    * @throws SQLException
928    */

929
930   public static String JavaDoc getIdFromNick(Connection JavaDoc oConn, int iDomainId, String JavaDoc sUserNick)
931     throws SQLException JavaDoc {
932     String JavaDoc sRetVal;
933     PreparedStatement JavaDoc oStmt;
934     ResultSet JavaDoc oRSet;
935
936     oStmt = oConn.prepareStatement("SELECT " + DB.gu_user + " FROM " + DB.k_users + " WHERE " + DB.id_domain + "=? AND " + DB.tx_nickname + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
937     oStmt.setInt (1, iDomainId);
938     oStmt.setString(2, sUserNick);
939     oRSet = oStmt.executeQuery();
940     if (oRSet.next())
941       sRetVal = oRSet.getString(1);
942     else
943       sRetVal = null;
944     oRSet.close();
945     oStmt.close();
946     return sRetVal;
947   } // getIdFromNick
948

949   // ----------------------------------------------------------
950

951   /**
952    * <p>Get User Unique Id. from its nickname.</p>
953    * <p>This method calls k_get_user_from_nick stored procedure.</p>
954    * @param oConn Database Connection
955    * @param sUserEMail User nickname (tx_nickname from k_users table)
956    * @return User Unique Id. or <b>null</b> if no user was found with such e-mail.
957    * @throws SQLException
958    * @since 3.0
959    */

960
961   public static String JavaDoc getIdFromNick(JDCConnection oConn, int iDomainId, String JavaDoc sUserNick)
962     throws SQLException JavaDoc {
963     String JavaDoc sRetVal;
964
965     switch (oConn.getDataBaseProduct()) {
966       case JDCConnection.DBMS_MSSQL:
967       case JDCConnection.DBMS_ORACLE:
968         sRetVal = DBPersist.getUIdFromName(oConn, new Integer JavaDoc(iDomainId), sUserNick, "k_get_user_from_nick");
969         break;
970       default:
971         sRetVal = getIdFromNick((Connection JavaDoc) oConn, iDomainId, sUserNick);
972     }
973     return sRetVal;
974   } // getIdFromNick
975

976   // ----------------------------------------------------------
977

978   /**
979    * <p>Delete User</p>
980    * <p>Categories owned by this user are also deleted, but other data and references for user are not checked.</p>
981    * @param oConn Database Connection
982    * @param sUserGUID User Unique Identifier
983    * @throws SQLException
984    * @throws IOException
985    */

986
987   public static boolean delete(JDCConnection oConn, String JavaDoc sUserGUID) throws SQLException JavaDoc,IOException JavaDoc {
988     boolean bRetVal;
989     int iCats, iConts, iMsgs;
990     DBSubset oCats, oProds, oConts, oMsgs;
991     Statement JavaDoc oStmt;
992     PreparedStatement JavaDoc oPtmt;
993     ResultSet JavaDoc oRSet;
994     CallableStatement JavaDoc oCall;
995
996     if (DebugFile.trace) {
997       DebugFile.writeln("Begin ACLUser.delete([Connection], " + sUserGUID + ")");
998       DebugFile.incIdent();
999     }
1000
1001    // -----------------------------------------------------------------------------------
1002
// Verify that user exists before proceeding and, also, avoid deleting more registers
1003
// than should by a malicious inyection of SQL code at sUserGUID
1004

1005    oPtmt = oConn.prepareStatement("SELECT "+DB.gu_user+" FROM "+DB.k_users+" WHERE "+DB.gu_user+"=?",
1006                                   ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
1007    oPtmt.setString(1, sUserGUID);
1008    oRSet = oPtmt.executeQuery();
1009    boolean bExists = oRSet.next();
1010    oRSet.close();
1011    oPtmt.close();
1012
1013    if (!bExists) {
1014      if (DebugFile.trace) {
1015        DebugFile.writeln("user " + sUserGUID + " not found");
1016        DebugFile.decIdent();
1017        DebugFile.writeln("End ACLUser.delete() : false");
1018      }
1019      return false;
1020    }
1021
1022    // ************
1023
// New for v3.0
1024

1025    if (DBBind.exists(oConn, DB.k_user_pwd, "U")) {
1026      oStmt = oConn.createStatement();
1027      if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_user_pwd + " WHERE " + DB.gu_user + "='" + sUserGUID + "')");
1028      oStmt.executeUpdate("DELETE FROM " + DB.k_user_pwd + " WHERE " + DB.gu_user + "='" + sUserGUID + "'");
1029      oStmt.close();
1030    }
1031
1032    if (DBBind.exists(oConn, DB.k_user_mail, "U")) {
1033      oStmt = oConn.createStatement();
1034      if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_user_mail + " WHERE " + DB.gu_user + "='" + sUserGUID + "')");
1035      oStmt.executeUpdate("DELETE FROM " + DB.k_user_mail + " WHERE " + DB.gu_user + "='" + sUserGUID + "'");
1036      oStmt.close();
1037    }
1038
1039    /* Actualizar los estados de negocio creados por el usuario */
1040    if (DBBind.exists(oConn, DB.k_business_states, "U")) {
1041      oStmt = oConn.createStatement();
1042      if (DebugFile.trace)
1043        DebugFile.writeln("UPDATE " + DB.k_business_states + " SET " + DB.gu_writer + "=NULL WHERE " + DB.gu_writer + "='" + sUserGUID + "'");
1044      oStmt.executeUpdate("UPDATE " + DB.k_business_states + " SET " + DB.gu_writer + "=NULL WHERE " + DB.gu_writer + "='" + sUserGUID + "'");
1045      oStmt.close();
1046    }
1047
1048    /* Borrar la referencia este usuario desde los cuestionarios que haya rellenado */
1049    if (DBBind.exists(oConn, DB.k_pageset_answers, "U")) {
1050      oStmt = oConn.createStatement();
1051      if (DebugFile.trace)
1052        DebugFile.writeln("UPDATE " + DB.k_pageset_answers + " SET " + DB.gu_writer + "=NULL WHERE " + DB.gu_writer + "='" + sUserGUID + "'");
1053      oStmt.executeUpdate("UPDATE " + DB.k_pageset_answers + " SET " + DB.gu_writer + "=NULL WHERE " + DB.gu_writer + "='" + sUserGUID + "'");
1054      oStmt.close();
1055    }
1056
1057    /* Desasociar las evaluaciones */
1058    if (DBBind.exists(oConn, DB.k_evaluations, "U")) {
1059      oStmt = oConn.createStatement();
1060      if (DebugFile.trace)
1061        DebugFile.writeln("UPDATE " + DB.k_evaluations + " SET " + DB.gu_writer + "=NULL WHERE " + DB.gu_writer + "='" + sUserGUID + "'");
1062      oStmt.executeUpdate("UPDATE " + DB.k_evaluations + " SET " + DB.gu_writer + "=NULL WHERE " + DB.gu_writer + "='" + sUserGUID + "'");
1063      oStmt.close();
1064    }
1065
1066    /* Desasociar los partes de absentismo */
1067    if (DBBind.exists(oConn, DB.k_absentisms, "U")) {
1068      oStmt = oConn.createStatement();
1069      if (DebugFile.trace)
1070        DebugFile.writeln("UPDATE " + DB.k_absentisms + " SET " + DB.gu_writer + "=NULL WHERE " + DB.gu_writer + "='" + sUserGUID + "'");
1071      oStmt.executeUpdate("UPDATE " + DB.k_absentisms + " SET " + DB.gu_writer + "=NULL WHERE " + DB.gu_writer + "='" + sUserGUID + "'");
1072      oStmt.close();
1073    }
1074
1075    // End new for v2.2
1076
// ****************
1077

1078    // ************
1079
// New for v2.1
1080

1081    /* Desasociar los e-mails */
1082    if (DBBind.exists(oConn, DB.k_inet_addrs, "U")) {
1083      oStmt = oConn.createStatement();
1084
1085      if (DebugFile.trace)
1086        DebugFile.writeln("UPDATE " + DB.k_inet_addrs + " SET " + DB.gu_user + "=NULL WHERE " + DB.gu_user + "='" + sUserGUID + "'");
1087
1088      oStmt.executeUpdate("UPDATE " + DB.k_inet_addrs + " SET " + DB.gu_user + "=NULL WHERE " + DB.gu_user + "='" + sUserGUID + "'");
1089
1090      oStmt.close();
1091    }
1092
1093    if (DBBind.exists(oConn, DB.k_x_portlet_user, "U")) {
1094      oStmt = oConn.createStatement();
1095
1096      if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_x_portlet_user + " WHERE " + DB.gu_user + "='" + sUserGUID + "')");
1097
1098      oStmt.executeUpdate("DELETE FROM " + DB.k_x_portlet_user + " WHERE " + DB.gu_user + "='" + sUserGUID + "'");
1099
1100      oStmt.close();
1101    }
1102
1103    if (DBBind.exists(oConn, DB.k_images, "U")) {
1104
1105      oStmt = oConn.createStatement();
1106
1107      if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(UPDATE " + DB.k_images + " SET " + DB.gu_writer + "=NULL WHERE " + DB.gu_writer + "='" + sUserGUID + "')");
1108
1109      oStmt.executeUpdate("UPDATE " + DB.k_images + " SET " + DB.gu_writer + "=NULL WHERE " + DB.gu_writer + "='" + sUserGUID + "'");
1110
1111      oStmt.close();
1112    }
1113
1114    if (DBBind.exists(oConn, DB.k_bugs_lookup, "U")) {
1115
1116      oStmt = oConn.createStatement();
1117
1118      if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(UPDATE " + DB.k_bugs + " SET " + DB.nm_assigned + "=NULL WHERE " + DB.nm_assigned + "='" + sUserGUID + "')");
1119
1120      oStmt.executeUpdate("UPDATE " + DB.k_bugs + " SET " + DB.nm_assigned + "=NULL WHERE " + DB.nm_assigned + "='" + sUserGUID + "'");
1121
1122      oStmt.close();
1123
1124      oStmt = oConn.createStatement();
1125
1126      if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_bugs_lookup + " WHERE " + DB.vl_lookup + "='" + sUserGUID + "')");
1127
1128      oStmt.executeUpdate("DELETE FROM " + DB.k_bugs_lookup + " WHERE " + DB.vl_lookup + "='" + sUserGUID + "'");
1129
1130      oStmt.close();
1131    }
1132
1133    if (DBBind.exists(oConn, DB.k_x_duty_resource, "U")) {
1134      oStmt = oConn.createStatement();
1135
1136      if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_x_duty_resource + " WHERE " + DB.nm_resource + "='" + sUserGUID + "')");
1137
1138      oStmt.executeUpdate("DELETE FROM " + DB.k_x_duty_resource + " WHERE " + DB.nm_resource + "='" + sUserGUID + "'");
1139
1140      oStmt.close();
1141
1142      oStmt = oConn.createStatement();
1143
1144      if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_duties_lookup + " WHERE " + DB.vl_lookup + "='" + sUserGUID + "')");
1145
1146      oStmt.executeUpdate("DELETE FROM " + DB.k_duties_lookup + " WHERE " + DB.vl_lookup + "='" + sUserGUID + "'");
1147
1148      oStmt.close();
1149    }
1150
1151    // End new for v2.1
1152
// ****************
1153

1154    // ************
1155
// New for v2.0
1156

1157    if (DBBind.exists(oConn, DB.k_newsgroup_subscriptions, "U")) {
1158      oStmt = oConn.createStatement();
1159
1160      if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_newsgroup_subscriptions + " WHERE " + DB.gu_user + "='" + sUserGUID + "')");
1161
1162      oStmt.executeUpdate("DELETE FROM " + DB.k_newsgroup_subscriptions + " WHERE " + DB.gu_user + "='" + sUserGUID + "'");
1163
1164      oStmt.close();
1165    }
1166
1167    // End new for v2.0
1168
// ****************
1169

1170    if (DBBind.exists(oConn, DB.k_newsmsgs, "U")) {
1171      oStmt = oConn.createStatement();
1172
1173      // Remove user GUID from messages he validated
1174
if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(UPDATE " + DB.k_newsmsgs + " SET " + DB.gu_validator + "=NULL WHERE " + DB.gu_validator + "='" + sUserGUID + "'");
1175
1176      oStmt.executeUpdate("UPDATE " + DB.k_newsmsgs + " SET " + DB.gu_validator + "=NULL WHERE " + DB.gu_validator + "='" + sUserGUID + "'");
1177
1178      // Delete forum messages written by this user without file attachments
1179
if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_newsmsgs + " WHERE " + DB.gu_writer + "='" + sUserGUID + "' AND " + DB.gu_product + " IS NULL");
1180
1181      oStmt.executeUpdate("DELETE FROM " + DB.k_newsmsgs + " WHERE " + DB.gu_writer + "='" + sUserGUID + "' AND " + DB.gu_product + " IS NULL");
1182
1183      oStmt.close();
1184
1185      // Delete forum messages written by this user with file attachments
1186
oMsgs = new DBSubset(DB.k_newsmsgs, DB.gu_msg, DB.gu_writer + "='" + sUserGUID + "'", 100);
1187
1188      iMsgs = oMsgs.load(oConn);
1189
1190      for (int m=0; m<iMsgs; m++)
1191        com.knowgate.forums.NewsMessage.delete(oConn, oMsgs.getString(0,m));
1192
1193      oMsgs = null;
1194    } // fi (exists(k_newsmsgs,))
1195

1196    if (DBBind.exists(oConn, DB.k_member_address, "U")) {
1197      oStmt = oConn.createStatement();
1198
1199      if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(UPDATE " + DB.k_member_address + " SET " + DB.gu_writer + "=NULL WHERE " + DB.gu_writer + "='" + sUserGUID + "')");
1200
1201      oStmt.executeUpdate("UPDATE " + DB.k_member_address + " SET " + DB.gu_writer + "=NULL WHERE " + DB.gu_writer + "='" + sUserGUID + "'");
1202
1203      if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(UPDATE " + DB.k_member_address + " SET " + DB.gu_sales_man + "=NULL WHERE " + DB.gu_sales_man + "='" + sUserGUID + "')");
1204
1205      oStmt.executeUpdate("UPDATE " + DB.k_member_address + " SET " + DB.gu_sales_man + "=NULL WHERE " + DB.gu_sales_man + "='" + sUserGUID + "'");
1206
1207      oStmt.close();
1208    }
1209
1210    if (DBBind.exists(oConn, DB.k_companies_recent, "U")) {
1211      oStmt = oConn.createStatement();
1212
1213      if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_companies_recent + " WHERE " + DB.gu_user + "='" + sUserGUID + "')");
1214
1215      oStmt.executeUpdate("DELETE FROM " + DB.k_companies_recent + " WHERE " + DB.gu_user + "='" + sUserGUID + "'");
1216
1217      oStmt.close();
1218    }
1219
1220    if (DBBind.exists(oConn, DB.k_companies, "U")) {
1221      oStmt = oConn.createStatement();
1222
1223      if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(UPDATE " + DB.k_companies + " SET " + DB.gu_sales_man + "=NULL WHERE " + DB.gu_sales_man + "='" + sUserGUID + "')");
1224
1225      oStmt.executeUpdate("UPDATE " + DB.k_companies + " SET " + DB.gu_sales_man + "=NULL WHERE " + DB.gu_sales_man + "='" + sUserGUID + "'");
1226
1227      oStmt.close();
1228    }
1229
1230    if (DBBind.exists(oConn, DB.k_contacts_recent, "U")) {
1231      oStmt = oConn.createStatement();
1232
1233      if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_contacts_recent + " WHERE " + DB.gu_user + "='" + sUserGUID + "')");
1234
1235      oStmt.executeUpdate("DELETE FROM " + DB.k_contacts_recent + " WHERE " + DB.gu_user + "='" + sUserGUID + "'");
1236
1237      oStmt.close();
1238    }
1239
1240    if (DBBind.exists(oConn, DB.k_contacts, "U")) {
1241      oConts = new DBSubset(DB.k_contacts, DB.gu_contact, DB.gu_writer + "='" + sUserGUID + "' AND " + DB.bo_private + "<>0", 100);
1242      iConts = oConts.load(oConn);
1243
1244      for (int t=0; t<iConts; t++)
1245         com.knowgate.crm.Contact.delete(oConn, oConts.getString(0,t));
1246
1247      oConts = null;
1248
1249      oStmt = oConn.createStatement();
1250
1251      if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(UPDATE " + DB.k_contacts + " SET " + DB.gu_writer + "=NULL WHERE " + DB.gu_writer + "='" + sUserGUID + "' AND " + DB.bo_private + "=0)");
1252
1253      oStmt.executeUpdate("UPDATE " + DB.k_contacts + " SET " + DB.gu_writer + "=NULL WHERE " + DB.gu_writer + "='" + sUserGUID + "' AND " + DB.bo_private + "=0");
1254
1255      if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(UPDATE " + DB.k_contact_notes + " SET " + DB.gu_writer + "=NULL WHERE " + DB.gu_writer + "='" + sUserGUID + "')");
1256
1257      oStmt.executeUpdate("UPDATE " + DB.k_contact_notes + " SET " + DB.gu_writer + "=NULL WHERE " + DB.gu_writer + "='" + sUserGUID + "'");
1258
1259      if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(UPDATE " + DB.k_contact_attachs + " SET " + DB.gu_writer + "=NULL WHERE " + DB.gu_writer + "='" + sUserGUID + "')");
1260
1261      oStmt.executeUpdate("UPDATE " + DB.k_contact_attachs + " SET " + DB.gu_writer + "=NULL WHERE " + DB.gu_writer + "='" + sUserGUID + "'");
1262
1263      oStmt.close();
1264    }
1265
1266    if (DBBind.exists(oConn, DB.k_oportunities, "U")) {
1267      oStmt = oConn.createStatement();
1268
1269      if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_oportunities + " WHERE " + DB.gu_writer + "='" + sUserGUID + "' AND " + DB.bo_private + "<>0)");
1270
1271      oStmt.executeUpdate("DELETE FROM " + DB.k_oportunities + " WHERE " + DB.gu_writer + "='" + sUserGUID + "' AND " + DB.bo_private + "<>0");
1272
1273      if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(UPDATE " + DB.k_oportunities + " SET " + DB.gu_writer + "=NULL WHERE " + DB.gu_writer + "='" + sUserGUID + "' AND " + DB.bo_private + "=0)");
1274
1275      oStmt.executeUpdate("UPDATE " + DB.k_oportunities + " SET " + DB.gu_writer + "=NULL WHERE " + DB.gu_writer + "='" + sUserGUID + "' AND " + DB.bo_private + "=0");
1276
1277      oStmt.close();
1278    }
1279
1280    if (DBBind.exists(oConn, DB.k_sales_men, "U")) {
1281       if (DebugFile.trace) DebugFile.writeln("Connection.prepareCall({ call k_sp_del_sales_man ('" + sUserGUID + "') })");
1282
1283       oCall = oConn.prepareCall("{call k_sp_del_sales_man ('" + sUserGUID + "')}");
1284       bRetVal = oCall.execute();
1285       oCall.close();
1286     }
1287
1288    if (DBBind.exists(oConn, DB.k_products, "U")) {
1289      oProds = new DBSubset(DB.k_products, DB.gu_product, DB.gu_owner + "='" + sUserGUID + "'", 100);
1290      int iProds = oProds.load(oConn);
1291
1292      for (int p=0; p<iProds; p++)
1293        new com.knowgate.hipergate.Product(oConn, oProds.getString(0, p)).delete(oConn);
1294      oProds = null;
1295    } // fi (exists(DB.k_products))
1296

1297    // Delete categories associated with user
1298
if (DBBind.exists(oConn, DB.k_categories, "U")) {
1299      String JavaDoc sGuRootCat = null;
1300      oStmt = oConn.createStatement();
1301      ResultSet JavaDoc oRCat = oStmt.executeQuery("SELECT " + DB.gu_category + " FROM " + DB.k_users + " WHERE " + DB.gu_user + "='" + sUserGUID + "'");
1302      if (oRCat.next())
1303        sGuRootCat = oRCat.getString(1);
1304      oRCat.close();
1305      oStmt.close();
1306      if (sGuRootCat!=null) {
1307        oStmt = oConn.createStatement();
1308        oStmt.executeUpdate("UPDATE " + DB.k_users + " SET " + DB.gu_category + "=NULL WHERE " + DB.gu_user + "='" + sUserGUID + "'");
1309        oStmt.close();
1310        Category.delete(oConn, sGuRootCat);
1311      }
1312
1313      oCats = new DBSubset(DB.k_categories, DB.gu_category, DB.gu_owner + "=?", 10);
1314      iCats = oCats.load(oConn, new Object JavaDoc[]{sUserGUID});
1315
1316      for (int r=0; r<iCats; r++)
1317         Category.delete(oConn, oCats.getString(0,r));
1318
1319      oCats = null;
1320    } // fi (exists(oConn, DB.k_categories))
1321

1322    if (DBBind.exists(oConn, DB.k_phone_calls, "U")) {
1323      oStmt = oConn.createStatement();
1324      try { oStmt.setQueryTimeout(30); } catch (SQLException JavaDoc sqle) {}
1325      if (DebugFile.trace) DebugFile.writeln("Statement.execute(DELETE FROM " + DB.k_phone_calls + " WHERE " + DB.gu_user + "='" + sUserGUID + "' OR " + DB.gu_writer + "='" + sUserGUID + "')");
1326      oStmt.executeUpdate("DELETE FROM " + DB.k_phone_calls + " WHERE " + DB.gu_user + "='" + sUserGUID + "' OR " + DB.gu_writer + "='" + sUserGUID + "'");
1327      oStmt.close();
1328    } // fi (exists(oConn, DB.k_phone_calls))
1329

1330    if (DBBind.exists(oConn, DB.k_to_do, "U")) {
1331      oStmt = oConn.createStatement();
1332      try { oStmt.setQueryTimeout(30); } catch (SQLException JavaDoc sqle) {}
1333      if (DebugFile.trace) DebugFile.writeln("Statement.execute(DELETE FROM " + DB.k_to_do + " WHERE " + DB.gu_user + "='" + sUserGUID + "')");
1334      oStmt.executeUpdate("DELETE FROM " + DB.k_to_do + " WHERE " + DB.gu_user + "='" + sUserGUID + "'");
1335      oStmt.close();
1336    } // fi (exists(oConn, DB.k_to_do))
1337

1338    if (DebugFile.trace) DebugFile.writeln("Connection.prepareCall({ call k_sp_del_user ('" + sUserGUID + "') })");
1339
1340    oCall = oConn.prepareCall("{call k_sp_del_user ('" + sUserGUID + "')}");
1341    bRetVal = oCall.execute();
1342    oCall.close();
1343
1344    if (DebugFile.trace) {
1345      DebugFile.decIdent();
1346      DebugFile.writeln("End ACLUser.delete() : " + String.valueOf(bRetVal));
1347    }
1348
1349    return bRetVal;
1350  } // delete
1351

1352  // ----------------------------------------------------------
1353

1354  /**
1355   * <p>Shortcut for creating a new user</p>
1356   * @param oConn Database Connection
1357   * @param Values User fields, all required, must be in this order { (Integer)id_domain, (String)tx_nickname, (String)tx_pwd, (Short)bo_active, (Short)bo_searchable, (Short)bo_change_pwd, (String)tx_main_email, (String)tx_alt_email, (String)nm_user, (String)tx_surname1, (String)tx_surname2, (String)tx_challenge, (String)tx_reply, (String)nm_company, (String)de_title, (String)gu_workarea }<br>
1358   * Values up to and including tx_surname1 must be NOT NULL, values from tx_surname2 are required but may be null.
1359   * @return New User Unique Identifier
1360   * @throws SQLException
1361   * @throws ClassCastException
1362   * @throws NullPointerException
1363   * @deprecated Use standard put/store methods instead.
1364   */

1365  public static String JavaDoc create(JDCConnection oConn, Object JavaDoc[] Values)
1366    throws SQLException JavaDoc,ClassCastException JavaDoc,NullPointerException JavaDoc {
1367
1368    if (DebugFile.trace) {
1369       DebugFile.writeln("Begin ACLUser.create([Connection])");
1370       DebugFile.incIdent();
1371    }
1372
1373      ACLUser oUsr = new ACLUser();
1374
1375      if (null==Values[0]) {
1376        if (DebugFile.trace) DebugFile.decIdent();
1377        throw new NullPointerException JavaDoc("ACLUSer.create() a domain identifier is required");
1378      }
1379      if (Values[0].getClass().equals(Integer.TYPE)) {
1380        if (DebugFile.trace) DebugFile.decIdent();
1381        throw new ClassCastException JavaDoc("ACLUSer.create() the domain identifier must be an object of type Integer");
1382      }
1383
1384      oUsr.put(DB.id_domain, Values[0]);
1385      oUsr.put(DB.tx_nickname, Values[1]);
1386      oUsr.put(DB.tx_pwd, Values[2]);
1387      oUsr.put(DB.bo_active, Values[3]);
1388      oUsr.put(DB.bo_searchable, Values[4]);
1389      oUsr.put(DB.bo_change_pwd, Values[5]);
1390      oUsr.put(DB.tx_main_email, Values[6]);
1391      oUsr.put(DB.tx_alt_email, Values[7]);
1392      oUsr.put(DB.nm_user, Values[8]);
1393      oUsr.put(DB.tx_surname1, Values[9]);
1394      if (Values.length>10)
1395        if (null!=Values[10]) oUsr.put(DB.tx_surname2, Values[10]);
1396      if (Values.length>11)
1397        if (null!=Values[11]) oUsr.put(DB.tx_challenge, Values[11]);
1398      if (Values.length>12)
1399        if (null!=Values[12]) oUsr.put(DB.tx_reply, Values[12]);
1400      if (Values.length>13)
1401        if (null!=Values[13]) oUsr.put(DB.nm_company, Values[13]);
1402      if (Values.length>14)
1403        if (null!=Values[14]) oUsr.put(DB.de_title, Values[14]);
1404      if (Values.length>15)
1405        if (null!=Values[15]) oUsr.put(DB.gu_workarea, Values[15]);
1406      if (Values.length>16)
1407        if (null!=Values[16]) oUsr.put(DB.tx_comments, Values[16]);
1408
1409      oUsr.store(oConn);
1410
1411      String JavaDoc sRetVal = oUsr.getString(DB.gu_user);
1412
1413      if (oConn.exists(DB.k_mime_msgs, "U")) {
1414        oUsr.getMailFolder(oConn, "inbox");
1415        oUsr.getMailFolder(oConn, "outbox");
1416        oUsr.getMailFolder(oConn, "drafts");
1417        oUsr.getMailFolder(oConn, "deleted");
1418        oUsr.getMailFolder(oConn, "sent");
1419        oUsr.getMailFolder(oConn, "spam");
1420      }
1421
1422      if (DebugFile.trace) {
1423        DebugFile.decIdent();
1424         DebugFile.writeln("End ACLUser.create() : " + sRetVal);
1425      }
1426      return sRetVal;
1427  } // create()
1428

1429  // **********************************************************
1430
// Public Constants
1431

1432  public static final short ClassId = 2;
1433
1434  // **********************************************************
1435
// Private Variables
1436

1437  private DBSubset oAddresses;
1438  private DBSubset oGroups;
1439}
1440
Popular Tags