001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/GroupXML.java,v 1.11 2007/10/09 11:09:13 lexuanttkhtn Exp $
003: * $Author: lexuanttkhtn $
004: * $Revision: 1.11 $
005: * $Date: 2007/10/09 11:09:13 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author: Igor Manic
039: */
040: package com.mvnforum.admin;
041:
042: import java.io.IOException;
043: import java.util.*;
044:
045: import com.mvnforum.MVNForumConstant;
046: import com.mvnforum.admin.importexport.XMLUtil;
047: import com.mvnforum.admin.importexport.XMLWriter;
048: import com.mvnforum.auth.MVNForumPermission;
049: import com.mvnforum.db.*;
050: import net.myvietnam.mvncore.exception.*;
051: import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;
052: import net.myvietnam.mvncore.filter.EnableHtmlTagFilter;
053:
054: /**
055: * @author Igor Manic
056: * @version $Revision: 1.11 $, $Date: 2007/10/09 11:09:13 $
057: * <br/>
058: * <code>GroupXML</code> todo Igor: enter description
059: *
060: */
061: public class GroupXML {
062:
063: private int groupID;
064:
065: /** Returns <code>GroupID</code> of this group or
066: * <code>-1</code> if group is not created yet. */
067: public int getGroupID() {
068: return groupID;
069: }
070:
071: public GroupXML() {
072: super ();
073: groupID = -1;
074: }
075:
076: public void setGroupID(String id) {
077: groupID = XMLUtil.stringToIntDef(id, -1);
078: }
079:
080: /**
081: * Creates a group. All argument values (<code>int</code>s, <code>Timestamp</code>s, ...)
082: * are represented as <code>String</code>s, because of more convenient using
083: * of this method for XML parsing.
084: *
085: * @param groupOwnerName Can be null.
086: * @param groupName Name of a group to be created.
087: * @param groupDesc Can be null.
088: * @param groupOption Can be null.
089: * @param groupCreationDate Can be null.
090: * @param groupModifiedDate Can be null.
091: *
092: * @throws CreateException
093: * @throws DuplicateKeyException
094: * @throws ObjectNotFoundException
095: * @throws DatabaseException
096: * @throws ForeignKeyNotFoundException
097: *
098: */
099: public void addGroup(String groupOwnerName, String groupName,
100: String groupDesc, String groupOption,
101: String groupCreationDate, String groupModifiedDate)
102: throws CreateException, DuplicateKeyException,
103: ObjectNotFoundException, DatabaseException,
104: ForeignKeyNotFoundException {
105: String strGroupID = null;
106: if (groupID >= 0)
107: strGroupID = Integer.toString(groupID);
108: addGroup(strGroupID, groupOwnerName, groupName, groupDesc,
109: groupOption, groupCreationDate, groupModifiedDate);
110: }
111:
112: /**
113: * Creates a group. All argument values (<code>int</code>s, <code>Timestamp</code>s, ...)
114: * are represented as <code>String</code>s, because of more convenient using
115: * of this method for XML parsing.
116: *
117: * @param strGroupID Can be null, and it probably will be in most occasions,
118: * except when you want to setup an explicit value, like
119: * for virtual "Registered Members" groups.
120: * @param groupOwnerName Can be null.
121: * @param groupName Name of a group to be created.
122: * @param groupDesc Can be null.
123: * @param groupOption Can be null.
124: * @param groupCreationDate Can be null.
125: * @param groupModifiedDate Can be null.
126: *
127: * @throws CreateException
128: * @throws DuplicateKeyException
129: * @throws ObjectNotFoundException
130: * @throws DatabaseException
131: * @throws ForeignKeyNotFoundException
132: *
133: */
134: public void addGroup(String strGroupID, String groupOwnerName,
135: String groupName, String groupDesc, String groupOption,
136: String groupCreationDate, String groupModifiedDate)
137: throws CreateException, DuplicateKeyException,
138: ObjectNotFoundException, DatabaseException,
139: ForeignKeyNotFoundException {
140: if ((groupName == null) || (groupName.equals(""))) {
141: throw new CreateException(
142: "Can't create a group with empty GroupName.");
143: } else {
144: int groupOption1;
145: java.sql.Timestamp groupCreationDate1;
146: java.sql.Timestamp groupModifiedDate1;
147: try {
148: if (groupOwnerName == null)
149: groupOwnerName = "";
150: if (groupDesc == null)
151: groupDesc = "";
152: groupOption1 = XMLUtil.stringToIntDef(groupOption, 0);
153: groupCreationDate1 = XMLUtil
154: .stringToSqlTimestampDefNow(groupCreationDate);
155: groupModifiedDate1 = XMLUtil
156: .stringToSqlTimestampDefNow(groupModifiedDate);
157: } catch (NumberFormatException e) {
158: throw new CreateException(
159: "Invalid data for a group. Expected a number.");
160: }
161:
162: //now ensure that strGroupID is valid number, or null
163: if ((strGroupID != null) && (!strGroupID.equals(""))) {
164: try {
165: if (Integer.parseInt(strGroupID) < 0)
166: strGroupID = null;
167: } catch (NumberFormatException e) {
168: strGroupID = null;
169: }
170: } else
171: strGroupID = null;
172:
173: groupName = EnableHtmlTagFilter.filter(groupName);
174: groupDesc = EnableHtmlTagFilter.filter(groupDesc);
175: if (strGroupID == null) {
176: //GroupsWebHelper correctly replaces empty groupOwnerName with GroupOwnerID=0
177: DAOFactory.getGroupsDAO().create(groupOwnerName,
178: groupName, groupDesc, groupOption1,
179: groupCreationDate1, groupModifiedDate1);
180: } else {
181: int groupOwnerID = 0;
182: try {
183: if (!groupOwnerName.equals("")) {
184: groupOwnerID = DAOFactory.getMemberDAO()
185: .getMemberIDFromMemberName(
186: groupOwnerName);
187: }
188: } catch (ObjectNotFoundException e) {
189: groupOwnerID = 0;
190: }
191: if (ImportWebHelper
192: .execUpdateQuery("INSERT INTO "
193: + GroupsDAO.TABLE_NAME
194: + " (GroupID, GroupOwnerID, GroupOwnerName, GroupName, GroupDesc,"
195: + " GroupOption, GroupCreationDate, GroupModifiedDate)"
196: + " VALUES (" + strGroupID + ", "
197: + groupOwnerID + ", '" + groupOwnerName
198: + "', '" + groupName + "', '"
199: + groupDesc + "', " + groupOption1
200: + ", '" + groupCreationDate1 + "', '"
201: + groupModifiedDate1 + "')") != 1) {
202: throw new CreateException("Error adding group \""
203: + groupName + "\" into table '"
204: + GroupsDAO.TABLE_NAME + "'.");
205: }
206: }
207:
208: this .groupID = DAOFactory.getGroupsDAO()
209: .getGroupIDFromGroupName(groupName);
210: }
211: }
212:
213: /**
214: * Adds a permission to this group. In order to know which group we are
215: * reffering to, this method is supposed to be called after {@link #setGroupID(String)},
216: * {@link #addGroup(String, String, String, String, String, String, String)}
217: * or {@link #addGroup(String, String, String, String, String, String)}
218: * have been called. Otherwise, this permission will be simply ignored.
219: *
220: * @param permission Permission to be added to this group.
221: *
222: * @throws CreateException
223: * @throws DatabaseException
224: * @throws ForeignKeyNotFoundException
225: *
226: */
227: public void addGroupPermission(String permission)
228: throws CreateException, DatabaseException,
229: ForeignKeyNotFoundException {
230: if (groupID < 0) {
231: throw new CreateException(
232: "Found group permission that is not assigned to any known group.");
233: }
234: int permission1;
235: try {
236: permission1 = XMLUtil.stringToIntDef(permission,
237: MVNForumPermission.PERMISSION_NO_PERMISSIONS);
238: } catch (NumberFormatException e) {
239: throw new CreateException(
240: "Invalid data for a group permission. Expected a number.");
241: }
242: try {
243: DAOFactory.getGroupPermissionDAO().create(groupID,
244: permission1);
245: } catch (DuplicateKeyException e) {
246: //ignore if already had that permission
247: }
248: }
249:
250: public static void addRegisteredMembersGroupPermission(
251: String permission) throws CreateException,
252: DatabaseException, DuplicateKeyException,
253: ForeignKeyNotFoundException {
254: int permission1;
255: try {
256: permission1 = XMLUtil.stringToIntDef(permission,
257: MVNForumPermission.PERMISSION_NO_PERMISSIONS);
258: } catch (NumberFormatException e) {
259: throw new CreateException(
260: "Invalid data for a group permission. Expected a number.");
261: }
262: DAOFactory.getGroupPermissionDAO().create(
263: MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS,
264: permission1);
265: }
266:
267: public static void addGroupPermission(String groupname,
268: String permission) throws CreateException,
269: DatabaseException, DuplicateKeyException,
270: ForeignKeyNotFoundException, ObjectNotFoundException {
271: int permission1;
272: try {
273: permission1 = XMLUtil.stringToIntDef(permission,
274: MVNForumPermission.PERMISSION_NO_PERMISSIONS);
275: } catch (NumberFormatException e) {
276: throw new CreateException(
277: "Invalid data for a group permission. Expected a number.");
278: }
279: DAOFactory.getGroupPermissionDAO().create(
280: DAOFactory.getGroupsDAO().getGroupIDFromGroupName(
281: groupname), permission1);
282: }
283:
284: /**
285: * Adds a member to this group. In order to know which group we are
286: * reffering to, this method is supposed to be called after {@link #setGroupID(String)},
287: * {@link #addGroup(String, String, String, String, String, String, String)}
288: * or {@link #addGroup(String, String, String, String, String, String)}
289: * have been called. Otherwise, this member assignment will be simply ignored.
290: *
291: * @param memberName MemberName of a meber to be added to this group.
292: * @param privilege Can be null.
293: * @param creationDate Can be null.
294: * @param modifiedDate Can be null.
295: *
296: * @throws CreateException
297: * @throws DatabaseException
298: * @throws DuplicateKeyException
299: * @throws ForeignKeyNotFoundException
300: *
301: */
302: public void addMemberGroup(String memberName, String privilege,
303: String creationDate, String modifiedDate)
304: throws CreateException, DatabaseException,
305: DuplicateKeyException, ForeignKeyNotFoundException {
306: if (groupID < 0) {
307: throw new CreateException(
308: "Found group member that is not assigned to any known group.");
309: }
310: if ((memberName == null) || (memberName.equals(""))) {
311: throw new CreateException(
312: "Can't create a group member with empty MemberName.");
313: }
314:
315: int privilege1;
316: java.sql.Timestamp creationDate1;
317: java.sql.Timestamp modifiedDate1;
318: try {
319: privilege1 = XMLUtil.stringToIntDef(privilege, 0);
320: creationDate1 = XMLUtil
321: .stringToSqlTimestampDefNow(creationDate);
322: modifiedDate1 = XMLUtil
323: .stringToSqlTimestampDefNow(modifiedDate);
324: } catch (NumberFormatException e) {
325: throw new CreateException(
326: "Invalid data for a group member. Expected a number.");
327: }
328: DAOFactory.getMemberGroupDAO().create(this .groupID, memberName,
329: privilege1, creationDate1, modifiedDate1);
330: }
331:
332: // ===============================================================
333: // ==================== STATIC EXPORT METHODS ====================
334: // ===============================================================
335:
336: public static void exportGlobalPermissionsForGroup(
337: XMLWriter xmlWriter, int groupID) throws IOException,
338: DatabaseException, ExportException {
339: Collection globalPermissions = ExportWebHelper
340: .execSqlQuery("SELECT Permission" + " FROM "
341: + GroupPermissionDAO.TABLE_NAME
342: + " WHERE GroupID=" + Integer.toString(groupID));
343: Iterator iter = globalPermissions.iterator();
344: String[] globalPermission = null;
345: //try {
346: xmlWriter.startElement("GlobalPermissionList");
347: try {
348: while ((globalPermission = (String[]) iter.next()) != null) {
349: if (globalPermission.length != 1) {
350: throw new ExportException(
351: "Error while retrieving data about global permissions for groupID=="
352: + groupID);
353: }
354: xmlWriter.startElement("GlobalPermission");
355: xmlWriter.writeData(globalPermission[0]);
356: xmlWriter.endElement("GlobalPermission");
357: }
358: } catch (NoSuchElementException e) {
359: //no more database records
360: }
361: xmlWriter.endElement("GlobalPermissionList");
362: //} catch throw exportexception
363: }
364:
365: public static void exportGroupMembersForGroup(XMLWriter xmlWriter,
366: int groupID) throws IOException, DatabaseException,
367: ExportException {
368: Collection groupMembers = ExportWebHelper
369: .execSqlQuery("SELECT MemberName, Privilege, CreationDate, ModifiedDate"
370: + " FROM "
371: + MemberGroupDAO.TABLE_NAME
372: + " WHERE GroupID=" + Integer.toString(groupID));
373: //todo Igor: I am using MemberName, but nobody can guarantee it will be consistent with MemberID
374: Iterator iter = groupMembers.iterator();
375: String[] groupMember = null;
376: //try {
377: xmlWriter.startElement("GroupMemberList");
378: try {
379: while ((groupMember = (String[]) iter.next()) != null) {
380: if (groupMember.length != 4) {
381: throw new ExportException(
382: "Error while retrieving data about group member for groupID=="
383: + groupID);
384: }
385: xmlWriter.startElement("GroupMember");
386: xmlWriter.startElement("MemberName");
387: xmlWriter.writeData(groupMember[0]);
388: xmlWriter.endElement("MemberName");
389: xmlWriter.startElement("Privilege");
390: xmlWriter.writeData(groupMember[1]);
391: xmlWriter.endElement("Privilege");
392: xmlWriter.startElement("CreationDate");
393: xmlWriter.writeData(groupMember[2]);
394: xmlWriter.endElement("CreationDate");
395: xmlWriter.startElement("ModifiedDate");
396: xmlWriter.writeData(groupMember[3]);
397: xmlWriter.endElement("ModifiedDate");
398: xmlWriter.endElement("GroupMember");
399: }
400: } catch (NoSuchElementException e) {
401: //no more database records
402: }
403: xmlWriter.endElement("GroupMemberList");
404: //} catch throw exportexception
405: }
406:
407: public static void exportGroup(XMLWriter xmlWriter, int groupID)
408: throws IOException, DatabaseException, ExportException {
409: Collection group1 = ExportWebHelper
410: .execSqlQuery("SELECT GroupOwnerName, GroupName,"
411: + " GroupDesc, GroupOption, GroupCreationDate, GroupModifiedDate"
412: + " FROM " + GroupsDAO.TABLE_NAME
413: + " WHERE GroupID=" + Integer.toString(groupID));
414: Iterator iter = group1.iterator();
415: String[] group = null;
416: //try {
417: try {
418: if ((group = (String[]) iter.next()) == null) {
419: throw new ExportException(
420: "Can't find data for groupID==" + groupID);
421: }
422: if (group.length != 6) {
423: throw new ExportException(
424: "Error while retrieving data about group with groupID=="
425: + groupID);
426: }
427: } catch (NoSuchElementException e) {
428: throw new ExportException("Can't find data for groupID=="
429: + groupID);
430: }
431:
432: //if I am here, that means I now have correct object group
433: if (groupID == MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS) {
434: xmlWriter.startElement("Group", new String[] { "class",
435: "RegisteredMembers" });
436: } else {
437: xmlWriter.startElement("Group");
438: }
439: xmlWriter.startElement("GroupOwnerName");
440: xmlWriter.writeData(group[0]);
441: xmlWriter.endElement("GroupOwnerName");
442: xmlWriter.startElement("GroupName");
443: xmlWriter.writeData(DisableHtmlTagFilter.filter(group[1]));
444: xmlWriter.endElement("GroupName");
445: xmlWriter.startElement("GroupDesc");
446: xmlWriter.writeData(DisableHtmlTagFilter.filter(group[2]));
447: xmlWriter.endElement("GroupDesc");
448: xmlWriter.startElement("GroupOption");
449: xmlWriter.writeData(group[3]);
450: xmlWriter.endElement("GroupOption");
451: xmlWriter.startElement("GroupCreationDate");
452: xmlWriter.writeData(group[4]);
453: xmlWriter.endElement("GroupCreationDate");
454: xmlWriter.startElement("GroupModifiedDate");
455: xmlWriter.writeData(group[5]);
456: xmlWriter.endElement("GroupModifiedDate");
457: exportGlobalPermissionsForGroup(xmlWriter, groupID);
458: exportGroupMembersForGroup(xmlWriter, groupID);
459: xmlWriter.endElement("Group");
460: //} catch throw exportexception
461: }
462:
463: public static void exportGroupList(XMLWriter xmlWriter)
464: throws IOException, DatabaseException, ExportException {
465: Collection groupIDs = ExportWebHelper
466: .execSqlQuery("SELECT GroupID" + " FROM "
467: + GroupsDAO.TABLE_NAME);
468: Iterator iter = groupIDs.iterator();
469: String[] groupID = null;
470: //try {
471: xmlWriter.startElement("GroupList");
472: /* First, I'll export Registered Members group. If it doesn't exist, just continue. */
473: try {
474: exportGroup(xmlWriter,
475: MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS);
476: } catch (Exception e) {
477: //doesn't exist => ignore
478: }
479: try {
480: while ((groupID = (String[]) iter.next()) != null) {
481: if (groupID.length != 1) {
482: throw new ExportException(
483: "Error while retrieving list of groups.");
484: }
485: try {
486: int i = Integer.parseInt(groupID[0]);
487: if (i != MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS) {
488: exportGroup(xmlWriter, i);
489: }
490: } catch (NumberFormatException e) {
491: throw new ExportException(
492: "Error while retrieving list of groups.");
493: }
494: }
495: } catch (NoSuchElementException e) {
496: //no more database records
497: }
498: xmlWriter.endElement("GroupList");
499: //} catch throw exportexception
500: }
501:
502: }
|