KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/PostXML.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.admin.importexport.XMLUtil;
46 import com.mvnforum.admin.importexport.XMLWriter;
47 import com.mvnforum.db.DAOFactory;
48 import com.mvnforum.db.PostDAO;
49 import net.myvietnam.mvncore.exception.*;
50 import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;
51 import net.myvietnam.mvncore.filter.EnableHtmlTagFilter;
52
53 /**
54  * @author Igor Manic
55  * @version $Revision: 1.9 $, $Date: 2006/04/14 17:36:29 $
56  * <br/>
57  * <code>PostXML</code> todo Igor: enter description
58  *
59  */

60 public class PostXML {
61
62     private int postID;
63     /** Returns <code>PostID</code> of this post or
64       * <code>-1</code> if post is not created yet. */

65     public int getPostID() { return postID; }
66
67     private int parentPostID;
68     /** Returns <code>PostID</code> of this post's parent post (which means this post is
69       * reply to that parent post), or <code>0</code> if this post is not created yet or
70       * has no parent post (first post in a thread). */

71     public int getParentPostID() { return parentPostID; }
72
73     private int parentThreadID;
74     /** Returns <code>ThreadID</code> of this post's parent thread or
75       * <code>-1</code> if this post is not created yet. */

76     public int getParentThreadID() { return parentThreadID; }
77
78     private int parentForumID;
79     /** Returns <code>ForumID</code> of this post's parent forum or
80       * <code>-1</code> if this post is not created yet. */

81     public int getParentForumID() { return parentForumID; }
82
83     private int parentCategoryID;
84     /** Returns <code>CategoryID</code> of this post's parent category or
85       * <code>-1</code> if this post is not created yet. */

86     public int getParentCategoryID() { return parentCategoryID; }
87
88     public PostXML() {
89         super();
90         postID=-1;
91         parentPostID=0;
92         parentThreadID=-1;
93         parentForumID=-1;
94         parentCategoryID=-1;
95     }
96
97     public void setPostID(String JavaDoc id) {
98         postID=XMLUtil.stringToIntDef(id, -1);
99     }
100
101     public void setParentPost(Object JavaDoc o)
102     throws ForeignKeyNotFoundException {
103         if (o instanceof PostXML) {
104             parentPostID=((PostXML)o).getPostID();
105         } else {
106             throw new ForeignKeyNotFoundException("Can't find parent post's ID");
107         }
108     }
109
110     public void setParentPostID(int value) {
111         if (value<0) parentPostID=-1;
112         else parentPostID=value;
113     }
114
115
116     /* WARNING: this post can be reply (in which case next 3 setParent***() methods
117      * will get PostXML from the Digester stack), or it can be the first post in
118      * (a thread (in which case they will get ThreadXML from the stack)
119      */

120     public void setParentThread(Object JavaDoc o)
121     throws ForeignKeyNotFoundException {
122         if (o instanceof ThreadXML) {
123             parentThreadID=((ThreadXML)o).getThreadID();
124         } else if (o instanceof PostXML) {
125             parentThreadID=((PostXML)o).getParentThreadID();
126         } else {
127             throw new ForeignKeyNotFoundException("Can't find parent thread's ID");
128         }
129     }
130
131     public void setParentThreadID(int value) {
132         if (value<0) parentThreadID=-1;
133         else parentThreadID=value;
134     }
135
136     public void setParentForum(Object JavaDoc o)
137     throws ForeignKeyNotFoundException {
138         if (o instanceof ThreadXML) {
139             parentForumID=((ThreadXML)o).getParentForumID();
140         } else if (o instanceof PostXML) {
141             parentForumID=((PostXML)o).getParentForumID();
142         } else {
143             throw new ForeignKeyNotFoundException("Can't find parent forum's ID");
144         }
145     }
146
147     public void setParentForumID(int value) {
148         if (value<0) parentForumID=-1;
149         else parentForumID=value;
150     }
151
152     public void setParentCategory(Object JavaDoc o)
153     throws ForeignKeyNotFoundException {
154         if (o instanceof ThreadXML) {
155             parentCategoryID=((ThreadXML)o).getParentCategoryID();
156         } else if (o instanceof PostXML) {
157             parentCategoryID=((PostXML)o).getParentCategoryID();
158         } else {
159             throw new ForeignKeyNotFoundException("Can't find parent category's ID");
160         }
161     }
162
163     public void setParentCategoryID(int value) {
164         if (value<0) parentCategoryID=-1;
165         else parentCategoryID=value;
166     }
167
168     /**
169      * Creates a post. All argument values (<code>int</code>s, <code>Timestamp</code>s, ...)
170      * are represented as <code>String</code>s, because of more convenient using
171      * of this method for XML parsing.
172      *
173      * @param memberName Member that created the post. Can be null.
174      * @param lastEditMemberName Can be null.
175      * @param postTopic Subject of post.
176      * @param postBody Message body.
177      * @param postCreationDate Can be null.
178      * @param postLastEditDate Can be null.
179      * @param postCreationIP Can be null.
180      * @param postLastEditIP Can be null.
181      * @param postEditCount Can be null.
182      * @param postFormatOption Can be null.
183      * @param postOption Can be null.
184      * @param postStatus Can be null.
185      * @param postIcon Can be null.
186      * @param postAttachCount Can be null.
187      *
188      * @throws CreateException
189      * @throws DuplicateKeyException
190      * @throws ObjectNotFoundException
191      * @throws DatabaseException
192      * @throws ForeignKeyNotFoundException
193      *
194      */

195     public void addPost(String JavaDoc memberName, String JavaDoc lastEditMemberName,
196                         String JavaDoc postTopic, String JavaDoc postBody,
197                         String JavaDoc postCreationDate, String JavaDoc postLastEditDate,
198                         String JavaDoc postCreationIP, String JavaDoc postLastEditIP,
199                         String JavaDoc postEditCount, String JavaDoc postFormatOption,
200                         String JavaDoc postOption, String JavaDoc postStatus,
201                         String JavaDoc postIcon, String JavaDoc postAttachCount)
202         throws CreateException, ObjectNotFoundException,DatabaseException, ForeignKeyNotFoundException {
203
204         if (parentPostID<0) {
205             throw new CreateException("Can't create a post, because no parent post assigned yet.");
206         } else if (parentThreadID<0) {
207             throw new CreateException("Can't create a post, because no parent thread assigned yet.");
208         } else if (parentForumID<0) {
209             throw new CreateException("Can't create a post, because no parent forum assigned yet.");
210         }
211
212         if ((postTopic==null) || (postBody==null)) {
213             throw new CreateException("Can't create a post with empty PostBody.");
214         } else {
215             java.sql.Timestamp JavaDoc postCreationDate1;
216             java.sql.Timestamp JavaDoc postLastEditDate1;
217             int postEditCount1;
218             int postFormatOption1;
219             int postOption1;
220             int postStatus1;
221             int postAttachCount1;
222
223             try {
224                 if (memberName==null) memberName="";
225                 if (lastEditMemberName==null) lastEditMemberName="";
226                 postCreationDate1= XMLUtil.stringToSqlTimestampDefNow(postCreationDate);
227                 postLastEditDate1= XMLUtil.stringToSqlTimestampDefNull(postLastEditDate);
228                 if (postCreationIP==null) postCreationIP="0.0.0.0";
229                 if (postLastEditIP==null) postLastEditIP="0.0.0.0";
230                 postEditCount1= XMLUtil.stringToIntDef(postEditCount, 0);
231                 postFormatOption1= XMLUtil.stringToIntDef(postFormatOption, 0);
232                 postOption1= XMLUtil.stringToIntDef(postOption, 0);
233                 postStatus1= XMLUtil.stringToIntDef(postStatus, 0);
234                 if (postIcon==null) postIcon="";
235                 postAttachCount1= XMLUtil.stringToIntDef(postAttachCount, 0);
236             } catch (NumberFormatException JavaDoc e) {
237                 throw new CreateException("Invalid data for a post. Expected a number.");
238             }
239
240             int memberID=0;
241             if (!memberName.equals("")) {
242                 memberID=DAOFactory.getMemberDAO().getMemberIDFromMemberName(memberName);
243             }
244
245             postTopic=EnableHtmlTagFilter.filter(postTopic);
246             postBody=EnableHtmlTagFilter.filter(postBody);
247             postIcon=EnableHtmlTagFilter.filter(postIcon);
248             this.postID = DAOFactory.getPostDAO().createPost(parentPostID,
249                            parentForumID, parentThreadID,
250                            memberID, memberName, lastEditMemberName,
251                            postTopic, postBody, postCreationDate1, postLastEditDate1,
252                            postCreationIP, postLastEditIP,
253                            postEditCount1, postFormatOption1, postOption1,
254                            postStatus1, postIcon, postAttachCount1);
255         }
256     }
257
258
259     public void increasePostAttachCount()
260     throws ObjectNotFoundException, AssertionException, DatabaseException {
261         if (postID<0) {
262             throw new ObjectNotFoundException("Can't update PostAttachCount on post that is not created yet.");
263         }
264         // we dont want the exception to throw below this
265
int attachCount = DAOFactory.getAttachmentDAO().getNumberOfAttachments_inPost(postID);
266         DAOFactory.getPostDAO().updateAttachCount(postID, attachCount);
267     }
268
269
270 // ===============================================================
271
// ==================== STATIC EXPORT METHODS ====================
272
// ===============================================================
273
public static void exportPost(XMLWriter xmlWriter, int postID)
274     throws NumberFormatException JavaDoc, IOException JavaDoc, ExportException, ObjectNotFoundException,
275     DatabaseException {
276         Collection post1=ExportWebHelper.execSqlQuery(
277                    "SELECT MemberName, LastEditMemberName,"+
278                    " PostTopic, PostBody, PostCreationDate, PostLastEditDate,"+
279                    " PostCreationIP, PostLastEditIP, PostEditCount, PostFormatOption,"+
280                    " PostOption, PostStatus, PostIcon, PostAttachCount"+
281                    " FROM "+PostDAO.TABLE_NAME+
282                    " WHERE PostID="+Integer.toString(postID));
283         Iterator iter=post1.iterator();
284         String JavaDoc[] post=null;
285         //try {
286
try {
287                 if ( (post=(String JavaDoc[])iter.next()) ==null) {
288                     throw new ExportException("Can't find data for postID="+postID);
289                 }
290                 if (post.length!=14) {
291                     throw new ExportException("Error while retrieving data about post with postID="+postID);
292                 }
293             } catch (NoSuchElementException e) {
294                 throw new ExportException("Can't find data for postID=="+postID);
295             }
296
297             //if I am here, that means I now have correct object post
298
xmlWriter.startElement("Post");
299
300             xmlWriter.startElement("MemberName");
301             xmlWriter.writeData(post[0]);
302             xmlWriter.endElement("MemberName");
303             xmlWriter.startElement("LastEditMemberName");
304             xmlWriter.writeData(post[1]);
305             xmlWriter.endElement("LastEditMemberName");
306             xmlWriter.startElement("PostTopic");
307             xmlWriter.writeData(DisableHtmlTagFilter.filter(post[2]));
308             xmlWriter.endElement("PostTopic");
309             xmlWriter.startElement("PostBody");
310             xmlWriter.writeData(DisableHtmlTagFilter.filter(post[3]));
311             xmlWriter.endElement("PostBody");
312             xmlWriter.startElement("PostCreationDate");
313             xmlWriter.writeData(post[4]);
314             xmlWriter.endElement("PostCreationDate");
315
316             xmlWriter.startElement("PostLastEditDate");
317             xmlWriter.writeData(post[5]);
318             xmlWriter.endElement("PostLastEditDate");
319             xmlWriter.startElement("PostCreationIP");
320             xmlWriter.writeData(post[6]);
321             xmlWriter.endElement("PostCreationIP");
322             xmlWriter.startElement("PostLastEditIP");
323             xmlWriter.writeData(post[7]);
324             xmlWriter.endElement("PostLastEditIP");
325             xmlWriter.startElement("PostEditCount");
326             xmlWriter.writeData(post[8]);
327             xmlWriter.endElement("PostEditCount");
328             xmlWriter.startElement("PostFormatOption");
329             xmlWriter.writeData(post[9]);
330             xmlWriter.endElement("PostFormatOption");
331
332             xmlWriter.startElement("PostOption");
333             xmlWriter.writeData(post[10]);
334             xmlWriter.endElement("PostOption");
335             xmlWriter.startElement("PostStatus");
336             xmlWriter.writeData(post[11]);
337             xmlWriter.endElement("PostStatus");
338             xmlWriter.startElement("PostIcon");
339             xmlWriter.writeData(DisableHtmlTagFilter.filter(post[12]));
340             xmlWriter.endElement("PostIcon");
341             xmlWriter.startElement("PostAttachCount");
342             xmlWriter.writeData(post[13]);
343             xmlWriter.endElement("PostAttachCount");
344
345             AttachmentXML.exportAttachmentList(xmlWriter, postID);
346             exportPostList_Replies(xmlWriter, postID/*parentPostID*/);
347             xmlWriter.endElement("Post");
348          //} catch throw exportexception
349
}
350
351     public static void exportPostList_FirstPosts(XMLWriter xmlWriter, int parentThreadID)
352     throws IOException JavaDoc, ExportException, ObjectNotFoundException, DatabaseException {
353         Collection postIDs=ExportWebHelper.execSqlQuery(
354                    "SELECT PostID"+
355                    " FROM "+PostDAO.TABLE_NAME+
356                    " WHERE ThreadID="+Integer.toString(parentThreadID)+
357                    " AND ParentPostID=0");
358         Iterator iter=postIDs.iterator();
359         String JavaDoc[] postID=null;
360         //try {
361
xmlWriter.startElement("PostList");
362             try {
363                 while ( (postID=(String JavaDoc[])iter.next()) !=null) {
364                     if (postID.length!=1) {
365                         throw new ExportException("Error while retrieving list of posts in threadID="+parentThreadID+".");
366                     }
367                     try {
368                         int i=Integer.parseInt(postID[0]);
369                         exportPost(xmlWriter, i);
370                     } catch (NumberFormatException JavaDoc e) {
371                         throw new ExportException("Error while retrieving list of posts in threadID="+parentThreadID+".");
372                     }
373                 }
374             } catch (NoSuchElementException e) {
375                 //no more database records
376
}
377             xmlWriter.endElement("PostList");
378         //} catch throw exportexception
379
}
380
381     public static void exportPostList_Replies(XMLWriter xmlWriter, int parentPostID)
382     throws IOException JavaDoc, ExportException, ObjectNotFoundException, DatabaseException {
383         Collection postIDs=ExportWebHelper.execSqlQuery(
384                    "SELECT PostID"+
385                    " FROM "+PostDAO.TABLE_NAME+
386                    " WHERE ParentPostID="+Integer.toString(parentPostID));
387         Iterator iter=postIDs.iterator();
388         String JavaDoc[] postID=null;
389         //try {
390
xmlWriter.startElement("PostList");
391             try {
392                 while ( (postID=(String JavaDoc[])iter.next()) !=null) {
393                     if (postID.length!=1) {
394                         throw new ExportException("Error while retrieving list of replies to postID="+parentPostID+".");
395                     }
396                     try {
397                         int i=Integer.parseInt(postID[0]);
398                         exportPost(xmlWriter, i);
399                     } catch (NumberFormatException JavaDoc e) {
400                         throw new ExportException("Error while retrieving list of replies to postID="+parentPostID+".");
401                     }
402                 }
403             } catch (NoSuchElementException e) {
404                 //no more database records
405
}
406             xmlWriter.endElement("PostList");
407         //} catch throw exportexception
408
}
409
410     //todo Igor important: merge exportPostList and exportPost so I use only one SQL query
411
//same for category(list), ...
412
public static void exportPostList(XMLWriter xmlWriter, int parentThreadID)
413     throws IOException JavaDoc, ExportException, ObjectNotFoundException, DatabaseException {
414         /* Export only root posts (of thread parentThreadID) here.
415          * Replies will be exported under <PostList> of each root <Post>.
416          */

417         exportPostList_FirstPosts(xmlWriter, parentThreadID);
418     }
419
420 }
421
Popular Tags