KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/AttachmentXML.java,v 1.9 2006/04/14 17:36:28 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.9 $
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.AttachmentDAO;
48 import com.mvnforum.db.DAOFactory;
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:28 $
56  * <br/>
57  * <code>AttachmentXML</code> todo Igor: enter description
58  *
59  */

60 public class AttachmentXML {
61
62     private int attachmentID;
63     /** Returns <code>AttachmentID</code> of this attachment or
64       * <code>-1</code> if attachment is not created yet. */

65     public int getAttachmentID() { return attachmentID; }
66
67     private int parentPostID;
68     /** Returns <code>PostID</code> of this attachment's parent post or
69       * <code>-1</code> if this attachment is not created yet. */

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

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

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

85     public int getParentCategoryID() { return parentCategoryID; }
86
87     public AttachmentXML() {
88         super();
89         attachmentID=-1;
90         parentPostID=-1;
91         parentThreadID=-1;
92         parentForumID=-1;
93         parentCategoryID=-1;
94     }
95
96     public void setAttachmentID(String JavaDoc id) {
97         attachmentID=XMLUtil.stringToIntDef(id, -1);
98     }
99
100     public void setParentPost(Object JavaDoc o)
101     throws ForeignKeyNotFoundException {
102         if (o instanceof PostXML) {
103             parentPostID=((PostXML)o).getPostID();
104         } else {
105             throw new ForeignKeyNotFoundException("Can't find parent post.");
106         }
107     }
108
109     public void setParentPostID(int value) {
110         if (value<0) parentPostID=-1;
111         else parentPostID=value;
112     }
113
114     public void setParentThread(Object JavaDoc o)
115     throws ForeignKeyNotFoundException {
116         if (o instanceof PostXML) {
117             parentThreadID=((PostXML)o).getParentThreadID();
118         } else {
119             throw new ForeignKeyNotFoundException("Can't find parent thread.");
120         }
121     }
122
123     public void setParentThreadID(int value) {
124         if (value<0) parentThreadID=-1;
125         else parentThreadID=value;
126     }
127
128     public void setParentForum(Object JavaDoc o)
129     throws ForeignKeyNotFoundException {
130         if (o instanceof PostXML) {
131             parentForumID=((PostXML)o).getParentForumID();
132         } else {
133             throw new ForeignKeyNotFoundException("Can't find parent forum.");
134         }
135     }
136
137     public void setParentForumID(int value) {
138         if (value<0) parentForumID=-1;
139         else parentForumID=value;
140     }
141
142     public void setParentCategory(Object JavaDoc o)
143     throws ForeignKeyNotFoundException {
144         if (o instanceof PostXML) {
145             parentCategoryID=((PostXML)o).getParentCategoryID();
146         } else {
147             throw new ForeignKeyNotFoundException("Can't find parent category.");
148         }
149     }
150
151     public void setParentCategoryID(int value) {
152         if (value<0) parentCategoryID=-1;
153         else parentCategoryID=value;
154     }
155
156     /**
157      * Creates an attachment. All argument values (<code>int</code>s, <code>Timestamp</code>s, ...)
158      * are represented as <code>String</code>s, because of more convenient using
159      * of this method for XML parsing.<br/>
160      * This method must be called after you've assigned the positive ID to this
161      * attachment (using <code>setAttachmentID(java.lang.String)</code> method).
162      *
163      * @param memberName Can be null.
164      * @param attachFilename Name of attachment file to be displayed on forum pages.
165      * @param attachFileSize Size of attachment file.
166      * @param attachMimeType MIME type of attachment file.
167      * @param attachDesc Can be null.
168      * @param attachCreationIP Can be null.
169      * @param attachCreationDate Can be null.
170      * @param attachModifiedDate Can be null.
171      * @param attachDownloadCount Can be null.
172      * @param attachOption Can be null.
173      * @param attachStatus Can be null.
174      *
175      * @throws CreateException
176      * @throws DuplicateKeyException
177      * @throws ObjectNotFoundException
178      * @throws DatabaseException
179      * @throws ForeignKeyNotFoundException
180      */

181     public void addAttachment(
182                        String JavaDoc memberName, String JavaDoc attachFilename,
183                        String JavaDoc attachFileSize, String JavaDoc attachMimeType,
184                        String JavaDoc attachDesc, String JavaDoc attachCreationIP,
185                        String JavaDoc attachCreationDate, String JavaDoc attachModifiedDate,
186                        String JavaDoc attachDownloadCount, String JavaDoc attachOption,
187                        String JavaDoc attachStatus)
188         throws CreateException, ObjectNotFoundException, DatabaseException {
189
190         if (attachmentID>0) {
191             addAttachment(Integer.toString(attachmentID),
192                           memberName, attachFilename, attachFileSize, attachMimeType,
193                           attachDesc, attachCreationIP, attachCreationDate, attachModifiedDate,
194                           attachDownloadCount, attachOption, attachStatus);
195         } else {
196             throw new CreateException("Can't create an attachment, because it has no ID assigned yet.");
197         }
198     }
199
200     /**
201      * Creates an attachment. All argument values (<code>int</code>s, <code>Timestamp</code>s, ...)
202      * are represented as <code>String</code>s, because of more convenient using
203      * of this method for XML parsing.
204      *
205      * @param strAttachmentID Must be non-null valid integer number, because it
206      * is the only way to know which file on server corresponds
207      * to this attachment.
208      * @param memberName Can be null.
209      * @param attachFilename Name of attachment file to be displayed on forum pages.
210      * @param attachFileSize Size of attachment file.
211      * @param attachMimeType MIME type of attachment file.
212      * @param attachDesc Can be null.
213      * @param attachCreationIP Can be null.
214      * @param attachCreationDate Can be null.
215      * @param attachModifiedDate Can be null.
216      * @param attachDownloadCount Can be null.
217      * @param attachOption Can be null.
218      * @param attachStatus Can be null.
219      *
220      * @throws CreateException
221      * @throws DuplicateKeyException
222      * @throws ObjectNotFoundException
223      * @throws DatabaseException
224      * @throws ForeignKeyNotFoundException
225      */

226     public void addAttachment(String JavaDoc strAttachmentID,
227                        String JavaDoc memberName, String JavaDoc attachFilename,
228                        String JavaDoc attachFileSize, String JavaDoc attachMimeType,
229                        String JavaDoc attachDesc, String JavaDoc attachCreationIP,
230                        String JavaDoc attachCreationDate, String JavaDoc attachModifiedDate,
231                        String JavaDoc attachDownloadCount, String JavaDoc attachOption,
232                        String JavaDoc attachStatus)
233         throws CreateException, ObjectNotFoundException, DatabaseException {
234
235         if (parentPostID<0) {
236             throw new CreateException("Can't create an attachment, because no parent post assigned yet.");
237         }
238         boolean idOk = (attachFilename!=null) &&
239                        (attachFileSize!=null) &&
240                        (attachMimeType!=null) &&
241                        (strAttachmentID!=null) &&
242                        (!strAttachmentID.equals(""));
243         //now ensure that strAttachmentID is valid number, and >=0
244
if (idOk) try {
245             attachmentID = Integer.parseInt(strAttachmentID);
246             idOk = (attachmentID>=0);
247         } catch (NumberFormatException JavaDoc e) {
248             idOk=false;
249         }
250
251         if (!idOk) {
252             attachmentID=-1;
253             throw new CreateException("Not enough data to create an attachment, or the ID is invalid.");
254         } else {
255             //these values are not neccessary here, but I wanted to use them to validate
256
//the strings - e.g, does attachOption string really contain a number ?
257
int attachFileSize1;
258             java.sql.Timestamp JavaDoc attachCreationDate1;
259             java.sql.Timestamp JavaDoc attachModifiedDate1;
260             int attachDownloadCount1;
261             int attachOption1;
262             int attachStatus1;
263
264             try {
265                 if (memberName==null) memberName="";
266                 attachFileSize1= XMLUtil.stringToIntDef(attachFileSize, 0);
267                 if (attachDesc==null) attachDesc="";
268                 if (attachCreationIP==null) attachCreationIP="0.0.0.0";
269                 attachCreationDate1= XMLUtil.stringToSqlTimestampDefNow(attachCreationDate);
270                 attachModifiedDate1= XMLUtil.stringToSqlTimestampDefNull(attachModifiedDate);
271                 attachDownloadCount1= XMLUtil.stringToIntDef(attachDownloadCount, 0);
272                 attachOption1= XMLUtil.stringToIntDef(attachOption, 0);
273                 attachStatus1= XMLUtil.stringToIntDef(attachStatus, 0);
274             } catch (NumberFormatException JavaDoc e) {
275                 throw new CreateException("Invalid data for an attachment. Expected a number.");
276             }
277
278             //allow memberName to be empty, meaning unknown user (don't use MEMBER_ID_OF_GUEST)
279
int memberID=0;
280             if (!memberName.equals("")) {
281                 memberID=DAOFactory.getMemberDAO().getMemberIDFromMemberName(memberName);
282             }
283             //I must change all possible nulls into "", so I don't get "'null'" in sql query
284
String JavaDoc attachModifiedDate2= XMLUtil.sqlTimestampToStringDefEmpty(attachModifiedDate1);
285
286             attachFilename=EnableHtmlTagFilter.filter(attachFilename);
287             attachMimeType=EnableHtmlTagFilter.filter(attachMimeType);
288             attachDesc=EnableHtmlTagFilter.filter(attachDesc);
289             if (ImportWebHelper.execUpdateQuery(
290                "INSERT INTO "+ AttachmentDAO.TABLE_NAME +
291                " (AttachID, PostID, MemberID," +
292                " AttachFilename, AttachFileSize, AttachMimeType," +
293                " AttachDesc, AttachCreationIP, AttachCreationDate, AttachModifiedDate," +
294                " AttachDownloadCount, AttachOption, AttachStatus)" +
295                " VALUES (" +strAttachmentID+ ", "+parentPostID+", " +memberID+
296                ", '" +attachFilename+ "', " +attachFileSize1+
297                ", '" +attachMimeType+ "', '" +attachDesc+
298                "', '" +attachCreationIP+ "', '" +attachCreationDate1+
299                "', '" +attachModifiedDate2+ "', " +attachDownloadCount1+
300                ", " +attachOption1+ ", " +attachStatus1+ ")"
301             ) != 1) {
302                 throw new CreateException("Error adding attachment \""+attachFilename+"\" into table '"+
303                     AttachmentDAO.TABLE_NAME +"'.");
304             }
305
306             //attachmentID was already set up
307
}
308     }
309
310
311 // ===============================================================
312
// ==================== STATIC EXPORT METHODS ====================
313
// ===============================================================
314
public static void exportAttachmentList(XMLWriter xmlWriter, int parentPostID)
315     throws IOException JavaDoc, ExportException, ObjectNotFoundException, DatabaseException {
316         Collection attachments=ExportWebHelper.execSqlQuery(
317                    "SELECT AttachID, MemberID,"+
318                    " AttachFilename, AttachFileSize, AttachMimeType, AttachDesc,"+
319                    " AttachCreationIP, AttachCreationDate, AttachModifiedDate,"+
320                    " AttachDownloadCount, AttachOption, AttachStatus"+
321                    " FROM "+AttachmentDAO.TABLE_NAME+
322                    " WHERE PostID="+Integer.toString(parentPostID));
323         Iterator iter=attachments.iterator();
324         String JavaDoc[] attachment=null;
325         //try {
326
xmlWriter.startElement("AttachmentList");
327             try {
328                 while ( (attachment=(String JavaDoc[])iter.next()) !=null) {
329                     if (attachment.length!=12) {
330                         throw new ExportException("Error while retrieving list of attachments for postID="+parentPostID+".");
331                     }
332                     xmlWriter.startElement("Attachment", new String JavaDoc[]{"id", attachment[0]});
333                     String JavaDoc memberName=DAOFactory.getMemberDAO().getMember_forPublic(Integer.parseInt(attachment[1])).getMemberName();
334                     xmlWriter.startElement("MemberName");
335                     xmlWriter.writeData(memberName);
336                     xmlWriter.endElement("MemberName");
337                     xmlWriter.startElement("AttachFilename");
338                     xmlWriter.writeData(DisableHtmlTagFilter.filter(attachment[2]));
339                     xmlWriter.endElement("AttachFilename");
340                     xmlWriter.startElement("AttachFileSize");
341                     xmlWriter.writeData(attachment[3]);
342                     xmlWriter.endElement("AttachFileSize");
343                     xmlWriter.startElement("AttachMimeType");
344                     xmlWriter.writeData(DisableHtmlTagFilter.filter(attachment[4]));
345                     xmlWriter.endElement("AttachMimeType");
346                     xmlWriter.startElement("AttachDesc");
347                     xmlWriter.writeData(DisableHtmlTagFilter.filter(attachment[5]));
348                     xmlWriter.endElement("AttachDesc");
349                     xmlWriter.startElement("AttachCreationIP");
350                     xmlWriter.writeData(attachment[6]);
351                     xmlWriter.endElement("AttachCreationIP");
352                     xmlWriter.startElement("AttachCreationDate");
353                     xmlWriter.writeData(attachment[7]);
354                     xmlWriter.endElement("AttachCreationDate");
355                     xmlWriter.startElement("AttachModifiedDate");
356                     xmlWriter.writeData(attachment[8]);
357                     xmlWriter.endElement("AttachModifiedDate");
358                     xmlWriter.startElement("AttachDownloadCount");
359                     xmlWriter.writeData(attachment[9]);
360                     xmlWriter.endElement("AttachDownloadCount");
361                     xmlWriter.startElement("AttachOption");
362                     xmlWriter.writeData(attachment[10]);
363                     xmlWriter.endElement("AttachOption");
364                     xmlWriter.startElement("AttachStatus");
365                     xmlWriter.writeData(attachment[11]);
366                     xmlWriter.endElement("AttachStatus");
367                     xmlWriter.endElement("Attachment");
368                 }
369             } catch (NoSuchElementException e) {
370                 //no more database records
371
}
372             xmlWriter.endElement("AttachmentList");
373          //} catch throw exportexception
374
}
375 }
376
Free Books   Free Magazines  
Popular Tags