KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > admin > GroupXML


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/GroupXML.java,v 1.9 2006/04/14 17:36:29 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.9 $
5  * $Date: 2006/04/14 17:36:29 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding mvnForum MUST remain
12  * intact in the scripts and in the outputted HTML.
13  * The "powered by" text/logo with a link back to
14  * http://www.mvnForum.com and http://www.MyVietnam.net in
15  * the footer of the pages MUST remain visible when the pages
16  * are viewed on the internet or intranet.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  *
32  * Support can be obtained from support forums at:
33  * http://www.mvnForum.com/mvnforum/index
34  *
35  * Correspondence and Marketing Questions can be sent to:
36  * info at MyVietnam net
37  *
38  * @author: Igor Manic
39  */

40 package com.mvnforum.admin;
41
42 import java.io.IOException JavaDoc;
43 import java.util.*;
44
45 import com.mvnforum.MVNForumConstant;
46 import com.mvnforum.admin.importexport.XMLUtil;
47 import com.mvnforum.admin.importexport.XMLWriter;
48 import com.mvnforum.auth.MVNForumPermission;
49 import com.mvnforum.db.*;
50 import net.myvietnam.mvncore.exception.*;
51 import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;
52 import net.myvietnam.mvncore.filter.EnableHtmlTagFilter;
53
54 /**
55  * @author Igor Manic
56  * @version $Revision: 1.9 $, $Date: 2006/04/14 17:36:29 $
57  * <br/>
58  * <code>GroupXML</code> todo Igor: enter description
59  *
60  */

61 public class GroupXML {
62
63     private int groupID;
64     /** Returns <code>GroupID</code> of this group or
65       * <code>-1</code> if group is not created yet. */

66     public int getGroupID() { return groupID; }
67
68     public GroupXML() {
69         super();
70         groupID=-1;
71     }
72
73     public void setGroupID(String JavaDoc id) {
74         groupID=XMLUtil.stringToIntDef(id, -1);
75     }
76
77     /**
78      * Creates a group. All argument values (<code>int</code>s, <code>Timestamp</code>s, ...)
79      * are represented as <code>String</code>s, because of more convenient using
80      * of this method for XML parsing.
81      *
82      * @param groupOwnerName Can be null.
83      * @param groupName Name of a group to be created.
84      * @param groupDesc Can be null.
85      * @param groupOption Can be null.
86      * @param groupCreationDate Can be null.
87      * @param groupModifiedDate Can be null.
88      *
89      * @throws CreateException
90      * @throws DuplicateKeyException
91      * @throws ObjectNotFoundException
92      * @throws DatabaseException
93      * @throws ForeignKeyNotFoundException
94      *
95      */

96     public void addGroup(String JavaDoc groupOwnerName, String JavaDoc groupName,
97                          String JavaDoc groupDesc, String JavaDoc groupOption,
98                          String JavaDoc groupCreationDate, String JavaDoc groupModifiedDate)
99     throws CreateException, DuplicateKeyException, ObjectNotFoundException,
100     DatabaseException, ForeignKeyNotFoundException {
101         String JavaDoc strGroupID=null;
102         if (groupID>=0) strGroupID=Integer.toString(groupID);
103         addGroup(strGroupID, groupOwnerName, groupName,
104                  groupDesc, groupOption, groupCreationDate, groupModifiedDate);
105     }
106
107     /**
108      * Creates a group. All argument values (<code>int</code>s, <code>Timestamp</code>s, ...)
109      * are represented as <code>String</code>s, because of more convenient using
110      * of this method for XML parsing.
111      *
112      * @param strGroupID Can be null, and it probably will be in most occasions,
113      * except when you want to setup an explicit value, like
114      * for virtual "Registered Members" groups.
115      * @param groupOwnerName Can be null.
116      * @param groupName Name of a group to be created.
117      * @param groupDesc Can be null.
118      * @param groupOption Can be null.
119      * @param groupCreationDate Can be null.
120      * @param groupModifiedDate Can be null.
121      *
122      * @throws CreateException
123      * @throws DuplicateKeyException
124      * @throws ObjectNotFoundException
125      * @throws DatabaseException
126      * @throws ForeignKeyNotFoundException
127      *
128      */

129     public void addGroup(String JavaDoc strGroupID,
130                          String JavaDoc groupOwnerName, String JavaDoc groupName,
131                          String JavaDoc groupDesc, String JavaDoc groupOption,
132                          String JavaDoc groupCreationDate, String JavaDoc groupModifiedDate)
133         throws CreateException, DuplicateKeyException, ObjectNotFoundException,
134         DatabaseException, ForeignKeyNotFoundException {
135         if ( (groupName==null) || (groupName.equals("")) ) {
136             throw new CreateException("Can't create a group with empty GroupName.");
137         } else {
138             int groupOption1;
139             java.sql.Timestamp JavaDoc groupCreationDate1;
140             java.sql.Timestamp JavaDoc groupModifiedDate1;
141             try {
142                 if (groupOwnerName==null) groupOwnerName="";
143                 if (groupDesc==null) groupDesc="";
144                 groupOption1= XMLUtil.stringToIntDef(groupOption, 0);
145                 groupCreationDate1= XMLUtil.stringToSqlTimestampDefNow(groupCreationDate);
146                 groupModifiedDate1=XMLUtil.stringToSqlTimestampDefNow(groupModifiedDate);
147             } catch (NumberFormatException JavaDoc e) {
148                 throw new CreateException("Invalid data for a group. Expected a number.");
149             }
150
151             //now ensure that strGroupID is valid number, or null
152
if ( (strGroupID!=null) && (!strGroupID.equals("")) ) {
153                 try {
154                     if (Integer.parseInt(strGroupID)<0) strGroupID=null;
155                 } catch (NumberFormatException JavaDoc e) {
156                     strGroupID=null;
157                 }
158             } else strGroupID=null;
159
160             groupName=EnableHtmlTagFilter.filter(groupName);
161             groupDesc=EnableHtmlTagFilter.filter(groupDesc);
162             if (strGroupID==null) {
163                 //GroupsWebHelper correctly replaces empty groupOwnerName with GroupOwnerID=0
164
DAOFactory.getGroupsDAO().create(
165                       groupOwnerName, groupName, groupDesc,
166                       groupOption1, groupCreationDate1, groupModifiedDate1);
167             } else {
168                 int groupOwnerID=0;
169                 try {
170                     if (!groupOwnerName.equals("")) {
171                         groupOwnerID=DAOFactory.getMemberDAO().getMemberIDFromMemberName(groupOwnerName);
172                     }
173                 } catch (ObjectNotFoundException e) {
174                     groupOwnerID=0;
175                 }
176                 if (ImportWebHelper.execUpdateQuery(
177                     "INSERT INTO "+ GroupsDAO.TABLE_NAME +
178                     " (GroupID, GroupOwnerID, GroupOwnerName, GroupName, GroupDesc," +
179                     " GroupOption, GroupCreationDate, GroupModifiedDate)" +
180                     " VALUES (" +strGroupID+ ", " +groupOwnerID+ ", '"+groupOwnerName +
181                     "', '" +groupName+ "', '" +groupDesc+ "', " +groupOption1+
182                     ", '" +groupCreationDate1+ "', '" +groupModifiedDate1+ "')"
183                    ) != 1) {
184                     throw new CreateException("Error adding group \""+groupName+"\" into table '"+
185                               GroupsDAO.TABLE_NAME +"'.");
186                 }
187             }
188
189             this.groupID=DAOFactory.getGroupsDAO().getGroupIDFromGroupName(groupName);
190         }
191     }
192
193     /**
194      * Adds a permission to this group. In order to know which group we are
195      * reffering to, this method is supposed to be called after {@link #setGroupID(String)},
196      * {@link #addGroup(String, String, String, String, String, String, String)}
197      * or {@link #addGroup(String, String, String, String, String, String)}
198      * have been called. Otherwise, this permission will be simply ignored.
199      *
200      * @param permission Permission to be added to this group.
201      *
202      * @throws CreateException
203      * @throws DatabaseException
204      * @throws ForeignKeyNotFoundException
205      *
206      */

207     public void addGroupPermission(String JavaDoc permission)
208     throws CreateException, DatabaseException, ForeignKeyNotFoundException {
209         if (groupID<0) {
210             throw new CreateException("Found group permission that is not assigned to any known group.");
211         }
212         int permission1;
213         try {
214             permission1=XMLUtil.stringToIntDef(permission, MVNForumPermission.PERMISSION_NO_PERMISSIONS);
215         } catch (NumberFormatException JavaDoc e) {
216             throw new CreateException("Invalid data for a group permission. Expected a number.");
217         }
218         try {
219             DAOFactory.getGroupPermissionDAO().create(groupID, permission1);
220         } catch (DuplicateKeyException e) {
221             //ignore if already had that permission
222
}
223     }
224
225     public static void addRegisteredMembersGroupPermission(String JavaDoc permission)
226     throws CreateException, DatabaseException, DuplicateKeyException,
227     ForeignKeyNotFoundException {
228         int permission1;
229         try {
230             permission1=XMLUtil.stringToIntDef(permission, MVNForumPermission.PERMISSION_NO_PERMISSIONS);
231         } catch (NumberFormatException JavaDoc e) {
232             throw new CreateException("Invalid data for a group permission. Expected a number.");
233         }
234         DAOFactory.getGroupPermissionDAO().create(MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS, permission1);
235     }
236
237     public static void addGroupPermission(String JavaDoc groupname, String JavaDoc permission)
238     throws CreateException, DatabaseException, DuplicateKeyException,
239     ForeignKeyNotFoundException, ObjectNotFoundException {
240         int permission1;
241         try {
242             permission1=XMLUtil.stringToIntDef(permission, MVNForumPermission.PERMISSION_NO_PERMISSIONS);
243         } catch (NumberFormatException JavaDoc e) {
244             throw new CreateException("Invalid data for a group permission. Expected a number.");
245         }
246         DAOFactory.getGroupPermissionDAO().create(DAOFactory.getGroupsDAO().getGroupIDFromGroupName(groupname), permission1);
247     }
248
249     /**
250      * Adds a member to this group. In order to know which group we are
251      * reffering to, this method is supposed to be called after {@link #setGroupID(String)},
252      * {@link #addGroup(String, String, String, String, String, String, String)}
253      * or {@link #addGroup(String, String, String, String, String, String)}
254      * have been called. Otherwise, this member assignment will be simply ignored.
255      *
256      * @param memberName MemberName of a meber to be added to this group.
257      * @param privilege Can be null.
258      * @param creationDate Can be null.
259      * @param modifiedDate Can be null.
260      *
261      * @throws CreateException
262      * @throws DatabaseException
263      * @throws DuplicateKeyException
264      * @throws ForeignKeyNotFoundException
265      *
266      */

267     public void addMemberGroup(String JavaDoc memberName, String JavaDoc privilege,
268                 String JavaDoc creationDate, String JavaDoc modifiedDate)
269     throws CreateException, DatabaseException, DuplicateKeyException, ForeignKeyNotFoundException {
270         if (groupID<0) {
271             throw new CreateException("Found group member that is not assigned to any known group.");
272         }
273         if ( (memberName==null) || (memberName.equals("")) ) {
274             throw new CreateException("Can't create a group member with empty MemberName.");
275         }
276
277         int privilege1;
278         java.sql.Timestamp JavaDoc creationDate1;
279         java.sql.Timestamp JavaDoc modifiedDate1;
280         try {
281             privilege1= XMLUtil.stringToIntDef(privilege, 0);
282             creationDate1= XMLUtil.stringToSqlTimestampDefNow(creationDate);
283             modifiedDate1= XMLUtil.stringToSqlTimestampDefNow(modifiedDate);
284         } catch (NumberFormatException JavaDoc e) {
285             throw new CreateException("Invalid data for a group member. Expected a number.");
286         }
287         DAOFactory.getMemberGroupDAO().create(this.groupID, memberName,
288                              privilege1, creationDate1, modifiedDate1);
289     }
290
291 // ===============================================================
292
// ==================== STATIC EXPORT METHODS ====================
293
// ===============================================================
294

295     public static void exportGlobalPermissionsForGroup(XMLWriter xmlWriter, int groupID)
296     throws IOException JavaDoc, DatabaseException, ExportException {
297         Collection globalPermissions=ExportWebHelper.execSqlQuery(
298                    "SELECT Permission"+
299                    " FROM "+GroupPermissionDAO.TABLE_NAME+
300                    " WHERE GroupID="+Integer.toString(groupID));
301         Iterator iter=globalPermissions.iterator();
302         String JavaDoc[] globalPermission=null;
303         //try {
304
xmlWriter.startElement("GlobalPermissionList");
305             try {
306                 while ( (globalPermission=(String JavaDoc[])iter.next()) !=null) {
307                     if (globalPermission.length!=1) {
308                         throw new ExportException("Error while retrieving data about global permissions for groupID=="+groupID);
309                     }
310                     xmlWriter.startElement("GlobalPermission");
311                     xmlWriter.writeData(globalPermission[0]);
312                     xmlWriter.endElement("GlobalPermission");
313                 }
314             } catch (NoSuchElementException e) {
315                 //no more database records
316
}
317             xmlWriter.endElement("GlobalPermissionList");
318          //} catch throw exportexception
319
}
320
321     public static void exportGroupMembersForGroup(XMLWriter xmlWriter, int groupID)
322     throws IOException JavaDoc, DatabaseException, ExportException {
323         Collection groupMembers=ExportWebHelper.execSqlQuery(
324                    "SELECT MemberName, Privilege, CreationDate, ModifiedDate"+
325                    " FROM "+MemberGroupDAO.TABLE_NAME+
326                    " WHERE GroupID="+Integer.toString(groupID));
327         //todo Igor: I am using MemberName, but nobody can guarantee it will be consistent with MemberID
328
Iterator iter=groupMembers.iterator();
329         String JavaDoc[] groupMember=null;
330         //try {
331
xmlWriter.startElement("GroupMemberList");
332             try {
333                 while ( (groupMember=(String JavaDoc[])iter.next()) !=null) {
334                     if (groupMember.length!=4) {
335                         throw new ExportException("Error while retrieving data about group member for groupID=="+groupID);
336                     }
337                     xmlWriter.startElement("GroupMember");
338                     xmlWriter.startElement("MemberName");
339                     xmlWriter.writeData(groupMember[0]);
340                     xmlWriter.endElement("MemberName");
341                     xmlWriter.startElement("Privilege");
342                     xmlWriter.writeData(groupMember[1]);
343                     xmlWriter.endElement("Privilege");
344                     xmlWriter.startElement("CreationDate");
345                     xmlWriter.writeData(groupMember[2]);
346                     xmlWriter.endElement("CreationDate");
347                     xmlWriter.startElement("ModifiedDate");
348                     xmlWriter.writeData(groupMember[3]);
349                     xmlWriter.endElement("ModifiedDate");
350                     xmlWriter.endElement("GroupMember");
351                 }
352             } catch (NoSuchElementException e) {
353                 //no more database records
354
}
355             xmlWriter.endElement("GroupMemberList");
356          //} catch throw exportexception
357
}
358
359     public static void exportGroup(XMLWriter xmlWriter, int groupID)
360     throws IOException JavaDoc, DatabaseException, ExportException {
361         Collection group1=ExportWebHelper.execSqlQuery(
362                    "SELECT GroupOwnerName, GroupName,"+
363                    " GroupDesc, GroupOption, GroupCreationDate, GroupModifiedDate"+
364                    " FROM "+GroupsDAO.TABLE_NAME+
365                    " WHERE GroupID="+Integer.toString(groupID));
366         Iterator iter=group1.iterator();
367         String JavaDoc[] group=null;
368         //try {
369
try {
370                 if ( (group=(String JavaDoc[])iter.next()) ==null) {
371                     throw new ExportException("Can't find data for groupID=="+groupID);
372                 }
373                 if (group.length!=6) {
374                     throw new ExportException("Error while retrieving data about group with groupID=="+groupID);
375                 }
376             } catch (NoSuchElementException e) {
377                 throw new ExportException("Can't find data for groupID=="+groupID);
378             }
379
380             //if I am here, that means I now have correct object group
381
if (groupID==MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS) {
382                 xmlWriter.startElement("Group", new String JavaDoc[]{"class", "RegisteredMembers"});
383             } else {
384                 xmlWriter.startElement("Group");
385             }
386             xmlWriter.startElement("GroupOwnerName");
387             xmlWriter.writeData(group[0]);
388             xmlWriter.endElement("GroupOwnerName");
389             xmlWriter.startElement("GroupName");
390             xmlWriter.writeData(DisableHtmlTagFilter.filter(group[1]));
391             xmlWriter.endElement("GroupName");
392             xmlWriter.startElement("GroupDesc");
393             xmlWriter.writeData(DisableHtmlTagFilter.filter(group[2]));
394             xmlWriter.endElement("GroupDesc");
395             xmlWriter.startElement("GroupOption");
396             xmlWriter.writeData(group[3]);
397             xmlWriter.endElement("GroupOption");
398             xmlWriter.startElement("GroupCreationDate");
399             xmlWriter.writeData(group[4]);
400             xmlWriter.endElement("GroupCreationDate");
401             xmlWriter.startElement("GroupModifiedDate");
402             xmlWriter.writeData(group[5]);
403             xmlWriter.endElement("GroupModifiedDate");
404             exportGlobalPermissionsForGroup(xmlWriter, groupID);
405             exportGroupMembersForGroup(xmlWriter, groupID);
406             xmlWriter.endElement("Group");
407          //} catch throw exportexception
408
}
409
410     public static void exportGroupList(XMLWriter xmlWriter)
411     throws IOException JavaDoc, DatabaseException, ExportException {
412         Collection groupIDs=ExportWebHelper.execSqlQuery(
413                    "SELECT GroupID"+
414                    " FROM "+GroupsDAO.TABLE_NAME);
415         Iterator iter=groupIDs.iterator();
416         String JavaDoc[] groupID=null;
417         //try {
418
xmlWriter.startElement("GroupList");
419             /* First, I'll export Registered Members group. If it doesn't exist, just continue. */
420             try {
421                 exportGroup(xmlWriter, MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS);
422             } catch (Exception JavaDoc e) {
423                 //doesn't exist => ignore
424
}
425             try {
426                 while ( (groupID=(String JavaDoc[])iter.next()) !=null) {
427                     if (groupID.length!=1) {
428                         throw new ExportException("Error while retrieving list of groups.");
429                     }
430                     try {
431                         int i=Integer.parseInt(groupID[0]);
432                         if (i!=MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS) {
433                             exportGroup(xmlWriter, i);
434                         }
435                     } catch (NumberFormatException JavaDoc e) {
436                         throw new ExportException("Error while retrieving list of groups.");
437                     }
438                 }
439             } catch (NoSuchElementException e) {
440                 //no more database records
441
}
442             xmlWriter.endElement("GroupList");
443          //} catch throw exportexception
444
}
445
446
447 }
448
Popular Tags