001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/CategoryXML.java,v 1.13 2007/10/09 11:09:12 lexuanttkhtn Exp $
003: * $Author: lexuanttkhtn $
004: * $Revision: 1.13 $
005: * $Date: 2007/10/09 11:09:12 $
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.admin.importexport.XMLUtil;
046: import com.mvnforum.admin.importexport.XMLWriter;
047: import com.mvnforum.db.*;
048: import net.myvietnam.mvncore.exception.*;
049: import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;
050: import net.myvietnam.mvncore.filter.EnableHtmlTagFilter;
051:
052: /**
053: * @author Igor Manic
054: * @version $Revision: 1.13 $, $Date: 2007/10/09 11:09:12 $
055: * <br/>
056: * <code>CategoryXML</code> todo Igor: enter description
057: *
058: */
059: public class CategoryXML {
060:
061: private int categoryID;
062:
063: /** Returns <code>CategoryID</code> of this category or
064: * <code>-1</code> if category is not created yet. */
065: public int getCategoryID() {
066: return categoryID;
067: }
068:
069: private int parentCategoryID;
070:
071: /** Returns <code>ThreadID</code> of this category's parent category or
072: * <code>0</code> if this category is not created yet or has no parent category. */
073: public int getParentCategoryID() {
074: return parentCategoryID;
075: }
076:
077: public CategoryXML() {
078: super ();
079: categoryID = -1;
080: parentCategoryID = 0;
081: }
082:
083: public void setCategoryID(String id) {
084: categoryID = XMLUtil.stringToIntDef(id, -1);
085: }
086:
087: public void setParentCategory(CategoryXML parentCategory) {
088: parentCategoryID = parentCategory.getCategoryID();
089: }
090:
091: public void setParentCategoryID(int value) {
092: if (value < 0)
093: parentCategoryID = -1;
094: else
095: parentCategoryID = value;
096: }
097:
098: /**
099: * Creates a category. All argument values (<code>int</code>s, <code>Timestamp</code>s, ...)
100: * are represented as <code>String</code>s, because of more convenient using
101: * of this method for XML parsing.
102: *
103: * @param categoryName Name of a category to be created.
104: * @param categoryDesc Can be null.
105: * @param categoryCreationDate Can be null.
106: * @param categoryModifiedDate Can be null.
107: * @param categoryOrder Can be null.
108: * @param categoryOption Can be null.
109: * @param categoryStatus Can be null.
110: *
111: * @throws CreateException
112: * @throws DuplicateKeyException
113: * @throws ObjectNotFoundException
114: * @throws DatabaseException
115: * @throws ForeignKeyNotFoundException
116: *
117: */
118: public void addCategory(String categoryName, String categoryDesc,
119: String categoryCreationDate, String categoryModifiedDate,
120: String categoryOrder, String categoryOption,
121: String categoryStatus) throws CreateException,
122: DuplicateKeyException, ObjectNotFoundException,
123: DatabaseException, ForeignKeyNotFoundException {
124:
125: //parentCategoryID can be 0, and don't need to check if it's set or not
126: if ((categoryName == null) || (categoryName.equals(""))) {
127: throw new CreateException(
128: "Can't create a category with empty CategoryName.");
129: } else {
130: java.sql.Timestamp categoryCreationDate1;
131: java.sql.Timestamp categoryModifiedDate1;
132: int categoryOrder1;
133: int categoryOption1;
134: int categoryStatus1;
135:
136: try {
137: if (categoryDesc == null)
138: categoryDesc = "";
139: categoryCreationDate1 = XMLUtil
140: .stringToSqlTimestampDefNow(categoryCreationDate);
141: categoryModifiedDate1 = XMLUtil
142: .stringToSqlTimestampDefNow(categoryModifiedDate);
143: categoryOrder1 = XMLUtil.stringToIntDef(categoryOrder,
144: 0);
145: categoryOption1 = XMLUtil.stringToIntDef(
146: categoryOption, 0);
147: categoryStatus1 = XMLUtil.stringToIntDef(
148: categoryStatus, 0);
149: } catch (NumberFormatException e) {
150: throw new CreateException(
151: "Invalid data for a category. Expected a number.");
152: }
153:
154: categoryName = EnableHtmlTagFilter.filter(categoryName);
155: categoryDesc = EnableHtmlTagFilter.filter(categoryDesc);
156:
157: DAOFactory.getCategoryDAO().create(parentCategoryID,
158: categoryName, categoryDesc, categoryCreationDate1,
159: categoryModifiedDate1, categoryOrder1,
160: categoryOption1, categoryStatus1);
161:
162: //todo Igor: Minh, you could move next piece of code into CategoryWebHelper.getCategoryIDFromPrimaryKey method
163: Collection categories = DAOFactory.getCategoryDAO()
164: .getCategories();
165: Iterator iter = categories.iterator();
166: try {
167: CategoryBean cat = null;
168: categoryID = -1;
169: while ((cat = (CategoryBean) iter.next()) != null) {
170: if ((cat.getCategoryName().equals(categoryName))
171: && (cat.getParentCategoryID() == parentCategoryID)) {
172: categoryID = cat.getCategoryID();
173: break;
174: }
175: }
176: if (categoryID < 0) {
177: throw new ObjectNotFoundException(
178: "Can't find category I've just added.");
179: }
180: } catch (NoSuchElementException e) {
181: throw new ObjectNotFoundException(
182: "Can't find category I've just added.");
183: }
184:
185: }
186: }
187:
188: /**
189: * Creates a category watch for this category. In order to know which category we are
190: * reffering to, this method is supposed to be called after {@link #setCategoryID(String)}
191: * or {@link #addCategory(String, String, String, String, String, String, String)}
192: * have been called. Otherwise, this watch will be simply ignored.
193: *
194: * @param memberName
195: * @param watchType Can be null.
196: * @param watchOption Can be null.
197: * @param watchStatus Can be null.
198: * @param watchCreationDate Can be null.
199: * @param watchLastSentDate Can be null.
200: * @param watchEndDate Can be null.
201: *
202: * @throws BadInputException
203: * @throws CreateException
204: * @throws DatabaseException
205: * @throws ObjectNotFoundException
206: * @throws DuplicateKeyException
207: * @throws ForeignKeyNotFoundException
208: *
209: */
210: public void addCategoryWatch(String memberName, String watchType,
211: String watchOption, String watchStatus,
212: String watchCreationDate, String watchLastSentDate,
213: String watchEndDate) throws CreateException,
214: DatabaseException, ObjectNotFoundException,
215: DuplicateKeyException, ForeignKeyNotFoundException {
216:
217: if (categoryID < 0) {
218: throw new CreateException(
219: "Found category watch that is not assigned to any known category.");
220: }
221:
222: int watchType1;
223: int watchOption1;
224: int watchStatus1;
225: java.sql.Timestamp watchCreationDate1;
226: java.sql.Timestamp watchLastSentDate1;
227: java.sql.Timestamp watchEndDate1;
228:
229: try {
230: if (memberName == null)
231: memberName = "";
232: watchType1 = XMLUtil.stringToIntDef(watchType, 0);
233: watchOption1 = XMLUtil.stringToIntDef(watchOption, 0);
234: watchStatus1 = XMLUtil.stringToIntDef(watchStatus, 0);
235: watchCreationDate1 = XMLUtil
236: .stringToSqlTimestampDefNow(watchCreationDate);
237: watchLastSentDate1 = XMLUtil
238: .stringToSqlTimestampDefNull(watchLastSentDate);
239: watchEndDate1 = XMLUtil
240: .stringToSqlTimestampDefNull(watchEndDate);
241: } catch (NumberFormatException e) {
242: throw new CreateException(
243: "Invalid data for a category. Expected a number.");
244: }
245:
246: //todo Igor: Shoud I allow memberID==0 here?
247: int memberID = 0;
248: if (!memberName.equals("")) {
249: memberID = DAOFactory.getMemberDAO()
250: .getMemberIDFromMemberName(memberName);
251: }
252: DAOFactory.getWatchDAO().create(memberID, categoryID,
253: 0/*forumId*/, 0/*threadId*/, watchType1,
254: watchOption1, watchStatus1, watchCreationDate1,
255: watchLastSentDate1, watchEndDate1);
256: }
257:
258: // ===============================================================
259: // ==================== STATIC EXPORT METHODS ====================
260: // ===============================================================
261:
262: public static void exportCategoryWatchesForCategory(
263: XMLWriter xmlWriter, int categoryID) throws IOException,
264: ExportException, NumberFormatException,
265: ObjectNotFoundException, DatabaseException {
266:
267: Collection categoryWatches = ExportWebHelper
268: .execSqlQuery("SELECT MemberID, WatchType, WatchOption, WatchStatus, WatchCreationDate, WatchLastSentDate, WatchEndDate"
269: + " FROM "
270: + WatchDAO.TABLE_NAME
271: + " WHERE ForumID=0 AND ThreadID=0"
272: + " AND CategoryID="
273: + Integer.toString(categoryID));
274: Iterator iter = categoryWatches.iterator();
275: String[] categoryWatch = null;
276: //try {
277: xmlWriter.startElement("CategoryWatchList");
278: try {
279: while ((categoryWatch = (String[]) iter.next()) != null) {
280: if (categoryWatch.length != 7) {
281: throw new ExportException(
282: "Error while retrieving data about category watch for categoryID=="
283: + categoryID);
284: }
285: String memberName = DAOFactory.getMemberDAO()
286: .getMember(Integer.parseInt(categoryWatch[0]))
287: .getMemberName();
288: xmlWriter.startElement("CategoryWatch");
289: xmlWriter.startElement("MemberName");
290: xmlWriter.writeData(memberName);
291: xmlWriter.endElement("MemberName");
292: xmlWriter.startElement("WatchType");
293: xmlWriter.writeData(categoryWatch[1]);
294: xmlWriter.endElement("WatchType");
295: xmlWriter.startElement("WatchOption");
296: xmlWriter.writeData(categoryWatch[2]);
297: xmlWriter.endElement("WatchOption");
298: xmlWriter.startElement("WatchStatus");
299: xmlWriter.writeData(categoryWatch[3]);
300: xmlWriter.endElement("WatchStatus");
301: xmlWriter.startElement("WatchCreationDate");
302: xmlWriter.writeData(categoryWatch[4]);
303: xmlWriter.endElement("WatchCreationDate");
304: xmlWriter.startElement("WatchLastSentDate");
305: xmlWriter.writeData(categoryWatch[5]);
306: xmlWriter.endElement("WatchLastSentDate");
307: xmlWriter.startElement("WatchEndDate");
308: xmlWriter.writeData(categoryWatch[6]);
309: xmlWriter.endElement("WatchEndDate");
310: xmlWriter.endElement("CategoryWatch");
311: }
312: } catch (NoSuchElementException e) {
313: //no more database records
314: }
315: xmlWriter.endElement("CategoryWatchList");
316: //} catch throw exportexception
317: }
318:
319: public static void exportCategory(XMLWriter xmlWriter,
320: int categoryID) throws NumberFormatException, IOException,
321: ExportException, ObjectNotFoundException, DatabaseException {
322:
323: Collection category1 = ExportWebHelper
324: .execSqlQuery("SELECT CategoryName, CategoryDesc,"
325: + " CategoryCreationDate, CategoryModifiedDate, CategoryOrder,"
326: + " CategoryOption, CategoryStatus FROM "
327: + CategoryDAO.TABLE_NAME + " WHERE CategoryID="
328: + Integer.toString(categoryID));
329: Iterator iter = category1.iterator();
330: String[] category = null;
331: //try {
332: try {
333: if ((category = (String[]) iter.next()) == null) {
334: throw new ExportException(
335: "Can't find data for categoryID==" + categoryID);
336: }
337: if (category.length != 7) {
338: throw new ExportException(
339: "Error while retrieving data about category with categoryID=="
340: + categoryID);
341: }
342: } catch (NoSuchElementException e) {
343: throw new ExportException(
344: "Can't find data for categoryID==" + categoryID);
345: }
346:
347: //if I am here, that means I now have correct object category
348: xmlWriter.startElement("Category");
349: xmlWriter.startElement("CategoryName");
350: xmlWriter.writeData(DisableHtmlTagFilter.filter(category[0]));
351: xmlWriter.endElement("CategoryName");
352: xmlWriter.startElement("CategoryDesc");
353: xmlWriter.writeData(DisableHtmlTagFilter.filter(category[1]));
354: xmlWriter.endElement("CategoryDesc");
355: xmlWriter.startElement("CategoryCreationDate");
356: xmlWriter.writeData(category[2]);
357: xmlWriter.endElement("CategoryCreationDate");
358: xmlWriter.startElement("CategoryModifiedDate");
359: xmlWriter.writeData(category[3]);
360: xmlWriter.endElement("CategoryModifiedDate");
361: xmlWriter.startElement("CategoryOrder");
362: xmlWriter.writeData(category[4]);
363: xmlWriter.endElement("CategoryOrder");
364: xmlWriter.startElement("CategoryOption");
365: xmlWriter.writeData(category[5]);
366: xmlWriter.endElement("CategoryOption");
367: xmlWriter.startElement("CategoryStatus");
368: xmlWriter.writeData(category[6]);
369: xmlWriter.endElement("CategoryStatus");
370: exportCategoryWatchesForCategory(xmlWriter, categoryID);
371: ForumXML.exportForumList(xmlWriter, categoryID);
372: exportSubCategoryList(xmlWriter, categoryID);
373: xmlWriter.endElement("Category");
374: //} catch throw exportexception
375: }
376:
377: public static void exportSubCategoryList(XMLWriter xmlWriter,
378: int parentCategoryID) throws IOException, ExportException,
379: ObjectNotFoundException, DatabaseException {
380:
381: Collection categoryIDs = ExportWebHelper
382: .execSqlQuery("SELECT CategoryID" + " FROM "
383: + CategoryDAO.TABLE_NAME
384: + " WHERE ParentCategoryID="
385: + Integer.toString(parentCategoryID));
386: Iterator iter = categoryIDs.iterator();
387: String[] categoryID = null;
388: //try {
389: xmlWriter.startElement("CategoryList");
390: try {
391: while ((categoryID = (String[]) iter.next()) != null) {
392: if (categoryID.length != 1) {
393: throw new ExportException(
394: "Error while retrieving list of categories.");
395: }
396: try {
397: int i = Integer.parseInt(categoryID[0]);
398: exportCategory(xmlWriter, i);
399: } catch (NumberFormatException e) {
400: throw new ExportException(
401: "Error while retrieving list of categories.");
402: }
403: }
404: } catch (NoSuchElementException e) {
405: //no more database records
406: }
407: xmlWriter.endElement("CategoryList");
408: //} catch throw exportexception
409: }
410:
411: public static void exportCategoryList(XMLWriter xmlWriter)
412: throws IOException, ExportException,
413: ObjectNotFoundException, DatabaseException {
414:
415: exportSubCategoryList(xmlWriter, 0/*parentCategoryID*/);
416: }
417:
418: }
|