KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/CategoryXML.java,v 1.10 2006/04/14 17:36:28 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.10 $
5  * $Date: 2006/04/14 17:36:28 $
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.admin.importexport.XMLUtil;
46 import com.mvnforum.admin.importexport.XMLWriter;
47 import com.mvnforum.db.*;
48 import net.myvietnam.mvncore.exception.*;
49 import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;
50 import net.myvietnam.mvncore.filter.EnableHtmlTagFilter;
51
52 /**
53  * @author Igor Manic
54  * @version $Revision: 1.10 $, $Date: 2006/04/14 17:36:28 $
55  * <br/>
56  * <code>CategoryXML</code> todo Igor: enter description
57  *
58  */

59 public class CategoryXML {
60
61     private int categoryID;
62     /** Returns <code>CategoryID</code> of this category or
63       * <code>-1</code> if category is not created yet. */

64     public int getCategoryID() { return categoryID; }
65
66     private int parentCategoryID;
67
68     /** Returns <code>ThreadID</code> of this category's parent category or
69       * <code>0</code> if this category is not created yet or has no parent category. */

70      public int getParentCategoryID() {
71          return parentCategoryID;
72      }
73
74     public CategoryXML() {
75         super();
76         categoryID = -1;
77         parentCategoryID = 0;
78     }
79
80     public void setCategoryID(String JavaDoc id) {
81         categoryID = XMLUtil.stringToIntDef(id, -1);
82     }
83
84     public void setParentCategory(CategoryXML parentCategory) {
85         parentCategoryID = parentCategory.getCategoryID();
86     }
87
88     public void setParentCategoryID(int value) {
89         if (value < 0) parentCategoryID = -1;
90         else parentCategoryID = value;
91     }
92
93     /**
94      * Creates a category. All argument values (<code>int</code>s, <code>Timestamp</code>s, ...)
95      * are represented as <code>String</code>s, because of more convenient using
96      * of this method for XML parsing.
97      *
98      * @param categoryName Name of a category to be created.
99      * @param categoryDesc Can be null.
100      * @param categoryCreationDate Can be null.
101      * @param categoryModifiedDate Can be null.
102      * @param categoryOrder Can be null.
103      * @param categoryOption Can be null.
104      * @param categoryStatus Can be null.
105      *
106      * @throws CreateException
107      * @throws DuplicateKeyException
108      * @throws ObjectNotFoundException
109      * @throws DatabaseException
110      * @throws ForeignKeyNotFoundException
111      *
112      */

113     public void addCategory(String JavaDoc categoryName,
114                             String JavaDoc categoryDesc, String JavaDoc categoryCreationDate,
115                             String JavaDoc categoryModifiedDate, String JavaDoc categoryOrder,
116                             String JavaDoc categoryOption, String JavaDoc categoryStatus)
117         throws CreateException, DuplicateKeyException, ObjectNotFoundException,
118         DatabaseException, ForeignKeyNotFoundException {
119
120         //parentCategoryID can be 0, and don't need to check if it's set or not
121
if ((categoryName == null) || (categoryName.equals(""))) {
122             throw new CreateException("Can't create a category with empty CategoryName.");
123         } else {
124             java.sql.Timestamp JavaDoc categoryCreationDate1;
125             java.sql.Timestamp JavaDoc categoryModifiedDate1;
126             int categoryOrder1;
127             int categoryOption1;
128             int categoryStatus1;
129
130             try {
131                 if (categoryDesc == null) categoryDesc = "";
132                 categoryCreationDate1 = XMLUtil.stringToSqlTimestampDefNow(categoryCreationDate);
133                 categoryModifiedDate1 = XMLUtil.stringToSqlTimestampDefNow(categoryModifiedDate);
134                 categoryOrder1 = XMLUtil.stringToIntDef(categoryOrder, 0);
135                 categoryOption1 = XMLUtil.stringToIntDef(categoryOption, 0);
136                 categoryStatus1 = XMLUtil.stringToIntDef(categoryStatus, 0);
137             } catch (NumberFormatException JavaDoc e) {
138                 throw new CreateException("Invalid data for a category. Expected a number.");
139             }
140
141             categoryName = EnableHtmlTagFilter.filter(categoryName);
142             categoryDesc = EnableHtmlTagFilter.filter(categoryDesc);
143
144             DAOFactory.getCategoryDAO().create(
145                 parentCategoryID, categoryName, categoryDesc,
146                 categoryCreationDate1, categoryModifiedDate1,
147                 categoryOrder1, categoryOption1, categoryStatus1);
148
149             //todo Igor: Minh, you could move next piece of code into CategoryWebHelper.getCategoryIDFromPrimaryKey method
150
Collection categories = DAOFactory.getCategoryDAO().getCategories();
151             Iterator iter = categories.iterator();
152             try {
153                 CategoryBean cat = null;
154                 categoryID = -1;
155                 while ((cat = (CategoryBean) iter.next()) != null) {
156                     if ((cat.getCategoryName().equals(categoryName)) &&
157                         (cat.getParentCategoryID() == parentCategoryID)) {
158                         categoryID = cat.getCategoryID();
159                         break;
160                     }
161                 }
162                 if (categoryID < 0) {
163                     throw new ObjectNotFoundException("Can't find category I've just added.");
164                 }
165             } catch (NoSuchElementException e) {
166                 throw new ObjectNotFoundException("Can't find category I've just added.");
167             }
168
169         }
170     }
171
172     /**
173      * Creates a category watch for this category. In order to know which category we are
174      * reffering to, this method is supposed to be called after {@link #setCategoryID(String)}
175      * or {@link #addCategory(String, String, String, String, String, String, String)}
176      * have been called. Otherwise, this watch will be simply ignored.
177      *
178      * @param memberName
179      * @param watchType Can be null.
180      * @param watchOption Can be null.
181      * @param watchStatus Can be null.
182      * @param watchCreationDate Can be null.
183      * @param watchLastSentDate Can be null.
184      * @param watchEndDate Can be null.
185      *
186      * @throws BadInputException
187      * @throws CreateException
188      * @throws DatabaseException
189      * @throws ObjectNotFoundException
190      * @throws DuplicateKeyException
191      * @throws ForeignKeyNotFoundException
192      *
193      */

194     public void addCategoryWatch(String JavaDoc memberName,
195                 String JavaDoc watchType, String JavaDoc watchOption,
196                 String JavaDoc watchStatus, String JavaDoc watchCreationDate,
197                 String JavaDoc watchLastSentDate, String JavaDoc watchEndDate)
198         throws CreateException, DatabaseException, ObjectNotFoundException,
199         DuplicateKeyException, ForeignKeyNotFoundException {
200
201         if (categoryID < 0) {
202             throw new CreateException("Found category watch that is not assigned to any known category.");
203         }
204
205         int watchType1;
206         int watchOption1;
207         int watchStatus1;
208         java.sql.Timestamp JavaDoc watchCreationDate1;
209         java.sql.Timestamp JavaDoc watchLastSentDate1;
210         java.sql.Timestamp JavaDoc watchEndDate1;
211
212         try {
213             if (memberName == null) memberName = "";
214             watchType1 = XMLUtil.stringToIntDef(watchType, 0);
215             watchOption1 = XMLUtil.stringToIntDef(watchOption, 0);
216             watchStatus1 = XMLUtil.stringToIntDef(watchStatus, 0);
217             watchCreationDate1 = XMLUtil.stringToSqlTimestampDefNow(watchCreationDate);
218             watchLastSentDate1 = XMLUtil.stringToSqlTimestampDefNull(watchLastSentDate);
219             watchEndDate1 = XMLUtil.stringToSqlTimestampDefNull(watchEndDate);
220         } catch (NumberFormatException JavaDoc e) {
221             throw new CreateException("Invalid data for a category. Expected a number.");
222         }
223
224         //todo Igor: Shoud I allow memberID==0 here?
225
int memberID = 0;
226         if (!memberName.equals("")) {
227             memberID = DAOFactory.getMemberDAO().getMemberIDFromMemberName(memberName);
228         }
229         DAOFactory.getWatchDAO().create(
230              memberID, categoryID, 0/*forumId*/, 0/*threadId*/,
231              watchType1, watchOption1, watchStatus1,
232              watchCreationDate1, watchLastSentDate1, watchEndDate1);
233     }
234
235 // ===============================================================
236
// ==================== STATIC EXPORT METHODS ====================
237
// ===============================================================
238

239     public static void exportCategoryWatchesForCategory(XMLWriter xmlWriter, int categoryID)
240         throws IOException JavaDoc, ExportException, NumberFormatException JavaDoc,
241         ObjectNotFoundException, DatabaseException {
242
243         Collection categoryWatches=ExportWebHelper.execSqlQuery(
244                    "SELECT MemberID, WatchType, WatchOption, WatchStatus, WatchCreationDate, WatchLastSentDate, WatchEndDate"+
245                    " FROM "+WatchDAO.TABLE_NAME+
246                    " WHERE ForumID=0 AND ThreadID=0"+
247                    " AND CategoryID="+Integer.toString(categoryID));
248         Iterator iter=categoryWatches.iterator();
249         String JavaDoc[] categoryWatch=null;
250         //try {
251
xmlWriter.startElement("CategoryWatchList");
252             try {
253                 while ( (categoryWatch=(String JavaDoc[])iter.next()) !=null) {
254                     if (categoryWatch.length!=7) {
255                         throw new ExportException("Error while retrieving data about category watch for categoryID=="+categoryID);
256                     }
257                     String JavaDoc memberName=DAOFactory.getMemberDAO().getMember_forPublic(Integer.parseInt(categoryWatch[0])).getMemberName();
258                     xmlWriter.startElement("CategoryWatch");
259                     xmlWriter.startElement("MemberName");
260                     xmlWriter.writeData(memberName);
261                     xmlWriter.endElement("MemberName");
262                     xmlWriter.startElement("WatchType");
263                     xmlWriter.writeData(categoryWatch[1]);
264                     xmlWriter.endElement("WatchType");
265                     xmlWriter.startElement("WatchOption");
266                     xmlWriter.writeData(categoryWatch[2]);
267                     xmlWriter.endElement("WatchOption");
268                     xmlWriter.startElement("WatchStatus");
269                     xmlWriter.writeData(categoryWatch[3]);
270                     xmlWriter.endElement("WatchStatus");
271                     xmlWriter.startElement("WatchCreationDate");
272                     xmlWriter.writeData(categoryWatch[4]);
273                     xmlWriter.endElement("WatchCreationDate");
274                     xmlWriter.startElement("WatchLastSentDate");
275                     xmlWriter.writeData(categoryWatch[5]);
276                     xmlWriter.endElement("WatchLastSentDate");
277                     xmlWriter.startElement("WatchEndDate");
278                     xmlWriter.writeData(categoryWatch[6]);
279                     xmlWriter.endElement("WatchEndDate");
280                     xmlWriter.endElement("CategoryWatch");
281                 }
282             } catch (NoSuchElementException e) {
283                 //no more database records
284
}
285             xmlWriter.endElement("CategoryWatchList");
286          //} catch throw exportexception
287
}
288
289     public static void exportCategory(XMLWriter xmlWriter, int categoryID)
290         throws NumberFormatException JavaDoc, IOException JavaDoc, ExportException, ObjectNotFoundException, DatabaseException {
291
292         Collection category1=ExportWebHelper.execSqlQuery(
293                    "SELECT CategoryName, CategoryDesc,"+
294                    " CategoryCreationDate, CategoryModifiedDate, CategoryOrder,"+
295                    " CategoryOption, CategoryStatus FROM "+CategoryDAO.TABLE_NAME+
296                    " WHERE CategoryID="+Integer.toString(categoryID));
297         Iterator iter = category1.iterator();
298         String JavaDoc[] category = null;
299         //try {
300
try {
301                 if ((category = (String JavaDoc[]) iter.next()) == null) {
302                     throw new ExportException("Can't find data for categoryID==" + categoryID);
303                 }
304                 if (category.length != 7) {
305                     throw new ExportException("Error while retrieving data about category with categoryID==" + categoryID);
306                 }
307             } catch (NoSuchElementException e) {
308                 throw new ExportException("Can't find data for categoryID=="+categoryID);
309             }
310
311             //if I am here, that means I now have correct object category
312
xmlWriter.startElement("Category");
313             xmlWriter.startElement("CategoryName");
314             xmlWriter.writeData(DisableHtmlTagFilter.filter(category[0]));
315             xmlWriter.endElement("CategoryName");
316             xmlWriter.startElement("CategoryDesc");
317             xmlWriter.writeData(DisableHtmlTagFilter.filter(category[1]));
318             xmlWriter.endElement("CategoryDesc");
319             xmlWriter.startElement("CategoryCreationDate");
320             xmlWriter.writeData(category[2]);
321             xmlWriter.endElement("CategoryCreationDate");
322             xmlWriter.startElement("CategoryModifiedDate");
323             xmlWriter.writeData(category[3]);
324             xmlWriter.endElement("CategoryModifiedDate");
325             xmlWriter.startElement("CategoryOrder");
326             xmlWriter.writeData(category[4]);
327             xmlWriter.endElement("CategoryOrder");
328             xmlWriter.startElement("CategoryOption");
329             xmlWriter.writeData(category[5]);
330             xmlWriter.endElement("CategoryOption");
331             xmlWriter.startElement("CategoryStatus");
332             xmlWriter.writeData(category[6]);
333             xmlWriter.endElement("CategoryStatus");
334             exportCategoryWatchesForCategory(xmlWriter, categoryID);
335             ForumXML.exportForumList(xmlWriter, categoryID);
336             exportSubCategoryList(xmlWriter, categoryID);
337             xmlWriter.endElement("Category");
338          //} catch throw exportexception
339
}
340
341     public static void exportSubCategoryList(XMLWriter xmlWriter, int parentCategoryID)
342         throws IOException JavaDoc, ExportException, ObjectNotFoundException, DatabaseException {
343
344         Collection categoryIDs=ExportWebHelper.execSqlQuery(
345                    "SELECT CategoryID"+
346                    " FROM "+CategoryDAO.TABLE_NAME+
347                    " WHERE ParentCategoryID="+Integer.toString(parentCategoryID));
348         Iterator iter = categoryIDs.iterator();
349         String JavaDoc[] categoryID = null;
350         //try {
351
xmlWriter.startElement("CategoryList");
352             try {
353                 while ((categoryID = (String JavaDoc[]) iter.next()) != null) {
354                     if (categoryID.length != 1) {
355                         throw new ExportException("Error while retrieving list of categories.");
356                     }
357                     try {
358                         int i = Integer.parseInt(categoryID[0]);
359                         exportCategory(xmlWriter, i);
360                     } catch (NumberFormatException JavaDoc e) {
361                         throw new ExportException("Error while retrieving list of categories.");
362                     }
363                 }
364             } catch (NoSuchElementException e) {
365                 //no more database records
366
}
367             xmlWriter.endElement("CategoryList");
368          //} catch throw exportexception
369
}
370
371     public static void exportCategoryList(XMLWriter xmlWriter)
372         throws IOException JavaDoc, ExportException, ObjectNotFoundException, DatabaseException {
373
374         exportSubCategoryList(xmlWriter, 0/*parentCategoryID*/);
375     }
376
377 }
378
379
Popular Tags