001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/PostXML.java,v 1.12 2007/10/09 11:09:14 lexuanttkhtn Exp $
003: * $Author: lexuanttkhtn $
004: * $Revision: 1.12 $
005: * $Date: 2007/10/09 11:09:14 $
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.DAOFactory;
048: import com.mvnforum.db.PostDAO;
049: import net.myvietnam.mvncore.exception.*;
050: import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;
051: import net.myvietnam.mvncore.filter.EnableHtmlTagFilter;
052:
053: /**
054: * @author Igor Manic
055: * @version $Revision: 1.12 $, $Date: 2007/10/09 11:09:14 $
056: * <br/>
057: * <code>PostXML</code> todo Igor: enter description
058: *
059: */
060: public class PostXML {
061:
062: private int postID;
063:
064: /** Returns <code>PostID</code> of this post or
065: * <code>-1</code> if post is not created yet. */
066: public int getPostID() {
067: return postID;
068: }
069:
070: private int parentPostID;
071:
072: /** Returns <code>PostID</code> of this post's parent post (which means this post is
073: * reply to that parent post), or <code>0</code> if this post is not created yet or
074: * has no parent post (first post in a thread). */
075: public int getParentPostID() {
076: return parentPostID;
077: }
078:
079: private int parentThreadID;
080:
081: /** Returns <code>ThreadID</code> of this post's parent thread or
082: * <code>-1</code> if this post is not created yet. */
083: public int getParentThreadID() {
084: return parentThreadID;
085: }
086:
087: private int parentForumID;
088:
089: /** Returns <code>ForumID</code> of this post's parent forum or
090: * <code>-1</code> if this post is not created yet. */
091: public int getParentForumID() {
092: return parentForumID;
093: }
094:
095: private int parentCategoryID;
096:
097: /** Returns <code>CategoryID</code> of this post's parent category or
098: * <code>-1</code> if this post is not created yet. */
099: public int getParentCategoryID() {
100: return parentCategoryID;
101: }
102:
103: public PostXML() {
104: super ();
105: postID = -1;
106: parentPostID = 0;
107: parentThreadID = -1;
108: parentForumID = -1;
109: parentCategoryID = -1;
110: }
111:
112: public void setPostID(String id) {
113: postID = XMLUtil.stringToIntDef(id, -1);
114: }
115:
116: public void setParentPost(Object o)
117: throws ForeignKeyNotFoundException {
118: if (o instanceof PostXML) {
119: parentPostID = ((PostXML) o).getPostID();
120: } else {
121: throw new ForeignKeyNotFoundException(
122: "Can't find parent post's ID");
123: }
124: }
125:
126: public void setParentPostID(int value) {
127: if (value < 0)
128: parentPostID = -1;
129: else
130: parentPostID = value;
131: }
132:
133: /* WARNING: this post can be reply (in which case next 3 setParent***() methods
134: * will get PostXML from the Digester stack), or it can be the first post in
135: * (a thread (in which case they will get ThreadXML from the stack)
136: */
137: public void setParentThread(Object o)
138: throws ForeignKeyNotFoundException {
139: if (o instanceof ThreadXML) {
140: parentThreadID = ((ThreadXML) o).getThreadID();
141: } else if (o instanceof PostXML) {
142: parentThreadID = ((PostXML) o).getParentThreadID();
143: } else {
144: throw new ForeignKeyNotFoundException(
145: "Can't find parent thread's ID");
146: }
147: }
148:
149: public void setParentThreadID(int value) {
150: if (value < 0)
151: parentThreadID = -1;
152: else
153: parentThreadID = value;
154: }
155:
156: public void setParentForum(Object o)
157: throws ForeignKeyNotFoundException {
158: if (o instanceof ThreadXML) {
159: parentForumID = ((ThreadXML) o).getParentForumID();
160: } else if (o instanceof PostXML) {
161: parentForumID = ((PostXML) o).getParentForumID();
162: } else {
163: throw new ForeignKeyNotFoundException(
164: "Can't find parent forum's ID");
165: }
166: }
167:
168: public void setParentForumID(int value) {
169: if (value < 0)
170: parentForumID = -1;
171: else
172: parentForumID = value;
173: }
174:
175: public void setParentCategory(Object o)
176: throws ForeignKeyNotFoundException {
177: if (o instanceof ThreadXML) {
178: parentCategoryID = ((ThreadXML) o).getParentCategoryID();
179: } else if (o instanceof PostXML) {
180: parentCategoryID = ((PostXML) o).getParentCategoryID();
181: } else {
182: throw new ForeignKeyNotFoundException(
183: "Can't find parent category's ID");
184: }
185: }
186:
187: public void setParentCategoryID(int value) {
188: if (value < 0)
189: parentCategoryID = -1;
190: else
191: parentCategoryID = value;
192: }
193:
194: /**
195: * Creates a post. All argument values (<code>int</code>s, <code>Timestamp</code>s, ...)
196: * are represented as <code>String</code>s, because of more convenient using
197: * of this method for XML parsing.
198: *
199: * @param memberName Member that created the post. Can be null.
200: * @param lastEditMemberName Can be null.
201: * @param postTopic Subject of post.
202: * @param postBody Message body.
203: * @param postCreationDate Can be null.
204: * @param postLastEditDate Can be null.
205: * @param postCreationIP Can be null.
206: * @param postLastEditIP Can be null.
207: * @param postEditCount Can be null.
208: * @param postFormatOption Can be null.
209: * @param postOption Can be null.
210: * @param postStatus Can be null.
211: * @param postIcon Can be null.
212: * @param postAttachCount Can be null.
213: *
214: * @throws CreateException
215: * @throws DuplicateKeyException
216: * @throws ObjectNotFoundException
217: * @throws DatabaseException
218: * @throws ForeignKeyNotFoundException
219: *
220: */
221: public void addPost(String memberName, String lastEditMemberName,
222: String postTopic, String postBody, String postCreationDate,
223: String postLastEditDate, String postCreationIP,
224: String postLastEditIP, String postEditCount,
225: String postFormatOption, String postOption,
226: String postStatus, String postIcon, String postAttachCount)
227: throws CreateException, ObjectNotFoundException,
228: DatabaseException, ForeignKeyNotFoundException {
229:
230: if (parentPostID < 0) {
231: throw new CreateException(
232: "Can't create a post, because no parent post assigned yet.");
233: } else if (parentThreadID < 0) {
234: throw new CreateException(
235: "Can't create a post, because no parent thread assigned yet.");
236: } else if (parentForumID < 0) {
237: throw new CreateException(
238: "Can't create a post, because no parent forum assigned yet.");
239: }
240:
241: if ((postTopic == null) || (postBody == null)) {
242: throw new CreateException(
243: "Can't create a post with empty PostBody.");
244: } else {
245: java.sql.Timestamp postCreationDate1;
246: java.sql.Timestamp postLastEditDate1;
247: int postEditCount1;
248: int postFormatOption1;
249: int postOption1;
250: int postStatus1;
251: int postAttachCount1;
252:
253: try {
254: if (memberName == null)
255: memberName = "";
256: if (lastEditMemberName == null)
257: lastEditMemberName = "";
258: postCreationDate1 = XMLUtil
259: .stringToSqlTimestampDefNow(postCreationDate);
260: postLastEditDate1 = XMLUtil
261: .stringToSqlTimestampDefNull(postLastEditDate);
262: if (postCreationIP == null)
263: postCreationIP = "0.0.0.0";
264: if (postLastEditIP == null)
265: postLastEditIP = "0.0.0.0";
266: postEditCount1 = XMLUtil.stringToIntDef(postEditCount,
267: 0);
268: postFormatOption1 = XMLUtil.stringToIntDef(
269: postFormatOption, 0);
270: postOption1 = XMLUtil.stringToIntDef(postOption, 0);
271: postStatus1 = XMLUtil.stringToIntDef(postStatus, 0);
272: if (postIcon == null)
273: postIcon = "";
274: postAttachCount1 = XMLUtil.stringToIntDef(
275: postAttachCount, 0);
276: } catch (NumberFormatException e) {
277: throw new CreateException(
278: "Invalid data for a post. Expected a number.");
279: }
280:
281: int memberID = 0;
282: if (!memberName.equals("")) {
283: memberID = DAOFactory.getMemberDAO()
284: .getMemberIDFromMemberName(memberName);
285: }
286:
287: postTopic = EnableHtmlTagFilter.filter(postTopic);
288: postBody = EnableHtmlTagFilter.filter(postBody);
289: postIcon = EnableHtmlTagFilter.filter(postIcon);
290: this .postID = DAOFactory.getPostDAO().createPost(
291: parentPostID, parentForumID, parentThreadID,
292: memberID, memberName, lastEditMemberName,
293: postTopic, postBody, postCreationDate1,
294: postLastEditDate1, postCreationIP, postLastEditIP,
295: postEditCount1, postFormatOption1, postOption1,
296: postStatus1, postIcon, postAttachCount1);
297: }
298: }
299:
300: public void increasePostAttachCount()
301: throws ObjectNotFoundException, DatabaseException {
302: if (postID < 0) {
303: throw new ObjectNotFoundException(
304: "Can't update PostAttachCount on post that is not created yet.");
305: }
306: // we dont want the exception to throw below this
307: int attachCount = DAOFactory.getAttachmentDAO()
308: .getNumberOfAttachments_inPost(postID);
309: DAOFactory.getPostDAO().updateAttachCount(postID, attachCount);
310: }
311:
312: // ===============================================================
313: // ==================== STATIC EXPORT METHODS ====================
314: // ===============================================================
315: public static void exportPost(XMLWriter xmlWriter, int postID)
316: throws NumberFormatException, IOException, ExportException,
317: ObjectNotFoundException, DatabaseException {
318: Collection post1 = ExportWebHelper
319: .execSqlQuery("SELECT MemberName, LastEditMemberName,"
320: + " PostTopic, PostBody, PostCreationDate, PostLastEditDate,"
321: + " PostCreationIP, PostLastEditIP, PostEditCount, PostFormatOption,"
322: + " PostOption, PostStatus, PostIcon, PostAttachCount"
323: + " FROM " + PostDAO.TABLE_NAME
324: + " WHERE PostID=" + Integer.toString(postID));
325: Iterator iter = post1.iterator();
326: String[] post = null;
327: //try {
328: try {
329: if ((post = (String[]) iter.next()) == null) {
330: throw new ExportException("Can't find data for postID="
331: + postID);
332: }
333: if (post.length != 14) {
334: throw new ExportException(
335: "Error while retrieving data about post with postID="
336: + postID);
337: }
338: } catch (NoSuchElementException e) {
339: throw new ExportException("Can't find data for postID=="
340: + postID);
341: }
342:
343: //if I am here, that means I now have correct object post
344: xmlWriter.startElement("Post");
345:
346: xmlWriter.startElement("MemberName");
347: xmlWriter.writeData(post[0]);
348: xmlWriter.endElement("MemberName");
349: xmlWriter.startElement("LastEditMemberName");
350: xmlWriter.writeData(post[1]);
351: xmlWriter.endElement("LastEditMemberName");
352: xmlWriter.startElement("PostTopic");
353: xmlWriter.writeData(DisableHtmlTagFilter.filter(post[2]));
354: xmlWriter.endElement("PostTopic");
355: xmlWriter.startElement("PostBody");
356: xmlWriter.writeData(DisableHtmlTagFilter.filter(post[3]));
357: xmlWriter.endElement("PostBody");
358: xmlWriter.startElement("PostCreationDate");
359: xmlWriter.writeData(post[4]);
360: xmlWriter.endElement("PostCreationDate");
361:
362: xmlWriter.startElement("PostLastEditDate");
363: xmlWriter.writeData(post[5]);
364: xmlWriter.endElement("PostLastEditDate");
365: xmlWriter.startElement("PostCreationIP");
366: xmlWriter.writeData(post[6]);
367: xmlWriter.endElement("PostCreationIP");
368: xmlWriter.startElement("PostLastEditIP");
369: xmlWriter.writeData(post[7]);
370: xmlWriter.endElement("PostLastEditIP");
371: xmlWriter.startElement("PostEditCount");
372: xmlWriter.writeData(post[8]);
373: xmlWriter.endElement("PostEditCount");
374: xmlWriter.startElement("PostFormatOption");
375: xmlWriter.writeData(post[9]);
376: xmlWriter.endElement("PostFormatOption");
377:
378: xmlWriter.startElement("PostOption");
379: xmlWriter.writeData(post[10]);
380: xmlWriter.endElement("PostOption");
381: xmlWriter.startElement("PostStatus");
382: xmlWriter.writeData(post[11]);
383: xmlWriter.endElement("PostStatus");
384: xmlWriter.startElement("PostIcon");
385: xmlWriter.writeData(DisableHtmlTagFilter.filter(post[12]));
386: xmlWriter.endElement("PostIcon");
387: xmlWriter.startElement("PostAttachCount");
388: xmlWriter.writeData(post[13]);
389: xmlWriter.endElement("PostAttachCount");
390:
391: AttachmentXML.exportAttachmentList(xmlWriter, postID);
392: exportPostList_Replies(xmlWriter, postID/*parentPostID*/);
393: xmlWriter.endElement("Post");
394: //} catch throw exportexception
395: }
396:
397: public static void exportPostList_FirstPosts(XMLWriter xmlWriter,
398: int parentThreadID) throws IOException, ExportException,
399: ObjectNotFoundException, DatabaseException {
400: Collection postIDs = ExportWebHelper
401: .execSqlQuery("SELECT PostID" + " FROM "
402: + PostDAO.TABLE_NAME + " WHERE ThreadID="
403: + Integer.toString(parentThreadID)
404: + " AND ParentPostID=0");
405: Iterator iter = postIDs.iterator();
406: String[] postID = null;
407: //try {
408: xmlWriter.startElement("PostList");
409: try {
410: while ((postID = (String[]) iter.next()) != null) {
411: if (postID.length != 1) {
412: throw new ExportException(
413: "Error while retrieving list of posts in threadID="
414: + parentThreadID + ".");
415: }
416: try {
417: int i = Integer.parseInt(postID[0]);
418: exportPost(xmlWriter, i);
419: } catch (NumberFormatException e) {
420: throw new ExportException(
421: "Error while retrieving list of posts in threadID="
422: + parentThreadID + ".");
423: }
424: }
425: } catch (NoSuchElementException e) {
426: //no more database records
427: }
428: xmlWriter.endElement("PostList");
429: //} catch throw exportexception
430: }
431:
432: public static void exportPostList_Replies(XMLWriter xmlWriter,
433: int parentPostID) throws IOException, ExportException,
434: ObjectNotFoundException, DatabaseException {
435: Collection postIDs = ExportWebHelper
436: .execSqlQuery("SELECT PostID" + " FROM "
437: + PostDAO.TABLE_NAME + " WHERE ParentPostID="
438: + Integer.toString(parentPostID));
439: Iterator iter = postIDs.iterator();
440: String[] postID = null;
441: //try {
442: xmlWriter.startElement("PostList");
443: try {
444: while ((postID = (String[]) iter.next()) != null) {
445: if (postID.length != 1) {
446: throw new ExportException(
447: "Error while retrieving list of replies to postID="
448: + parentPostID + ".");
449: }
450: try {
451: int i = Integer.parseInt(postID[0]);
452: exportPost(xmlWriter, i);
453: } catch (NumberFormatException e) {
454: throw new ExportException(
455: "Error while retrieving list of replies to postID="
456: + parentPostID + ".");
457: }
458: }
459: } catch (NoSuchElementException e) {
460: //no more database records
461: }
462: xmlWriter.endElement("PostList");
463: //} catch throw exportexception
464: }
465:
466: //todo Igor important: merge exportPostList and exportPost so I use only one SQL query
467: //same for category(list), ...
468: public static void exportPostList(XMLWriter xmlWriter,
469: int parentThreadID) throws IOException, ExportException,
470: ObjectNotFoundException, DatabaseException {
471: /* Export only root posts (of thread parentThreadID) here.
472: * Replies will be exported under <PostList> of each root <Post>.
473: */
474: exportPostList_FirstPosts(xmlWriter, parentThreadID);
475: }
476:
477: }
|