001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/AttachmentXML.java,v 1.12 2007/10/09 11:09:12 lexuanttkhtn Exp $
003: * $Author: lexuanttkhtn $
004: * $Revision: 1.12 $
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.AttachmentDAO;
048: import com.mvnforum.db.DAOFactory;
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:12 $
056: * <br/>
057: * <code>AttachmentXML</code> todo Igor: enter description
058: *
059: */
060: public class AttachmentXML {
061:
062: private int attachmentID;
063:
064: /** Returns <code>AttachmentID</code> of this attachment or
065: * <code>-1</code> if attachment is not created yet. */
066: public int getAttachmentID() {
067: return attachmentID;
068: }
069:
070: private int parentPostID;
071:
072: /** Returns <code>PostID</code> of this attachment's parent post or
073: * <code>-1</code> if this attachment is not created yet. */
074: public int getParentPostID() {
075: return parentPostID;
076: }
077:
078: private int parentThreadID;
079:
080: /** Returns <code>ThreadID</code> of this attachment's parent thread or
081: * <code>-1</code> if this attachment is not created yet. */
082: public int getParentThreadID() {
083: return parentThreadID;
084: }
085:
086: private int parentForumID;
087:
088: /** Returns <code>ForumID</code> of this attachment's parent forum or
089: * <code>-1</code> if this attachment is not created yet. */
090: public int getParentForumID() {
091: return parentForumID;
092: }
093:
094: private int parentCategoryID;
095:
096: /** Returns <code>CategoryID</code> of this attachment's parent category or
097: * <code>-1</code> if this attachment is not created yet. */
098: public int getParentCategoryID() {
099: return parentCategoryID;
100: }
101:
102: public AttachmentXML() {
103: super ();
104: attachmentID = -1;
105: parentPostID = -1;
106: parentThreadID = -1;
107: parentForumID = -1;
108: parentCategoryID = -1;
109: }
110:
111: public void setAttachmentID(String id) {
112: attachmentID = XMLUtil.stringToIntDef(id, -1);
113: }
114:
115: public void setParentPost(Object o)
116: throws ForeignKeyNotFoundException {
117: if (o instanceof PostXML) {
118: parentPostID = ((PostXML) o).getPostID();
119: } else {
120: throw new ForeignKeyNotFoundException(
121: "Can't find parent post.");
122: }
123: }
124:
125: public void setParentPostID(int value) {
126: if (value < 0)
127: parentPostID = -1;
128: else
129: parentPostID = value;
130: }
131:
132: public void setParentThread(Object o)
133: throws ForeignKeyNotFoundException {
134: if (o instanceof PostXML) {
135: parentThreadID = ((PostXML) o).getParentThreadID();
136: } else {
137: throw new ForeignKeyNotFoundException(
138: "Can't find parent thread.");
139: }
140: }
141:
142: public void setParentThreadID(int value) {
143: if (value < 0)
144: parentThreadID = -1;
145: else
146: parentThreadID = value;
147: }
148:
149: public void setParentForum(Object o)
150: throws ForeignKeyNotFoundException {
151: if (o instanceof PostXML) {
152: parentForumID = ((PostXML) o).getParentForumID();
153: } else {
154: throw new ForeignKeyNotFoundException(
155: "Can't find parent forum.");
156: }
157: }
158:
159: public void setParentForumID(int value) {
160: if (value < 0)
161: parentForumID = -1;
162: else
163: parentForumID = value;
164: }
165:
166: public void setParentCategory(Object o)
167: throws ForeignKeyNotFoundException {
168: if (o instanceof PostXML) {
169: parentCategoryID = ((PostXML) o).getParentCategoryID();
170: } else {
171: throw new ForeignKeyNotFoundException(
172: "Can't find parent category.");
173: }
174: }
175:
176: public void setParentCategoryID(int value) {
177: if (value < 0)
178: parentCategoryID = -1;
179: else
180: parentCategoryID = value;
181: }
182:
183: /**
184: * Creates an attachment. All argument values (<code>int</code>s, <code>Timestamp</code>s, ...)
185: * are represented as <code>String</code>s, because of more convenient using
186: * of this method for XML parsing.<br/>
187: * This method must be called after you've assigned the positive ID to this
188: * attachment (using <code>setAttachmentID(java.lang.String)</code> method).
189: *
190: * @param memberName Can be null.
191: * @param attachFilename Name of attachment file to be displayed on forum pages.
192: * @param attachFileSize Size of attachment file.
193: * @param attachMimeType MIME type of attachment file.
194: * @param attachDesc Can be null.
195: * @param attachCreationIP Can be null.
196: * @param attachCreationDate Can be null.
197: * @param attachModifiedDate Can be null.
198: * @param attachDownloadCount Can be null.
199: * @param attachOption Can be null.
200: * @param attachStatus Can be null.
201: *
202: * @throws CreateException
203: * @throws DuplicateKeyException
204: * @throws ObjectNotFoundException
205: * @throws DatabaseException
206: * @throws ForeignKeyNotFoundException
207: */
208: public void addAttachment(String memberName, String attachFilename,
209: String attachFileSize, String attachMimeType,
210: String attachDesc, String attachCreationIP,
211: String attachCreationDate, String attachModifiedDate,
212: String attachDownloadCount, String attachOption,
213: String attachStatus) throws CreateException,
214: ObjectNotFoundException, DatabaseException {
215:
216: if (attachmentID > 0) {
217: addAttachment(Integer.toString(attachmentID), memberName,
218: attachFilename, attachFileSize, attachMimeType,
219: attachDesc, attachCreationIP, attachCreationDate,
220: attachModifiedDate, attachDownloadCount,
221: attachOption, attachStatus);
222: } else {
223: throw new CreateException(
224: "Can't create an attachment, because it has no ID assigned yet.");
225: }
226: }
227:
228: /**
229: * Creates an attachment. All argument values (<code>int</code>s, <code>Timestamp</code>s, ...)
230: * are represented as <code>String</code>s, because of more convenient using
231: * of this method for XML parsing.
232: *
233: * @param strAttachmentID Must be non-null valid integer number, because it
234: * is the only way to know which file on server corresponds
235: * to this attachment.
236: * @param memberName Can be null.
237: * @param attachFilename Name of attachment file to be displayed on forum pages.
238: * @param attachFileSize Size of attachment file.
239: * @param attachMimeType MIME type of attachment file.
240: * @param attachDesc Can be null.
241: * @param attachCreationIP Can be null.
242: * @param attachCreationDate Can be null.
243: * @param attachModifiedDate Can be null.
244: * @param attachDownloadCount Can be null.
245: * @param attachOption Can be null.
246: * @param attachStatus Can be null.
247: *
248: * @throws CreateException
249: * @throws DuplicateKeyException
250: * @throws ObjectNotFoundException
251: * @throws DatabaseException
252: * @throws ForeignKeyNotFoundException
253: */
254: public void addAttachment(String strAttachmentID,
255: String memberName, String attachFilename,
256: String attachFileSize, String attachMimeType,
257: String attachDesc, String attachCreationIP,
258: String attachCreationDate, String attachModifiedDate,
259: String attachDownloadCount, String attachOption,
260: String attachStatus) throws CreateException,
261: ObjectNotFoundException, DatabaseException {
262:
263: if (parentPostID < 0) {
264: throw new CreateException(
265: "Can't create an attachment, because no parent post assigned yet.");
266: }
267: boolean idOk = (attachFilename != null)
268: && (attachFileSize != null) && (attachMimeType != null)
269: && (strAttachmentID != null)
270: && (!strAttachmentID.equals(""));
271: //now ensure that strAttachmentID is valid number, and >=0
272: if (idOk)
273: try {
274: attachmentID = Integer.parseInt(strAttachmentID);
275: idOk = (attachmentID >= 0);
276: } catch (NumberFormatException e) {
277: idOk = false;
278: }
279:
280: if (!idOk) {
281: attachmentID = -1;
282: throw new CreateException(
283: "Not enough data to create an attachment, or the ID is invalid.");
284: } else {
285: //these values are not neccessary here, but I wanted to use them to validate
286: //the strings - e.g, does attachOption string really contain a number ?
287: int attachFileSize1;
288: java.sql.Timestamp attachCreationDate1;
289: java.sql.Timestamp attachModifiedDate1;
290: int attachDownloadCount1;
291: int attachOption1;
292: int attachStatus1;
293:
294: try {
295: if (memberName == null)
296: memberName = "";
297: attachFileSize1 = XMLUtil.stringToIntDef(
298: attachFileSize, 0);
299: if (attachDesc == null)
300: attachDesc = "";
301: if (attachCreationIP == null)
302: attachCreationIP = "0.0.0.0";
303: attachCreationDate1 = XMLUtil
304: .stringToSqlTimestampDefNow(attachCreationDate);
305: attachModifiedDate1 = XMLUtil
306: .stringToSqlTimestampDefNull(attachModifiedDate);
307: attachDownloadCount1 = XMLUtil.stringToIntDef(
308: attachDownloadCount, 0);
309: attachOption1 = XMLUtil.stringToIntDef(attachOption, 0);
310: attachStatus1 = XMLUtil.stringToIntDef(attachStatus, 0);
311: } catch (NumberFormatException e) {
312: throw new CreateException(
313: "Invalid data for an attachment. Expected a number.");
314: }
315:
316: //allow memberName to be empty, meaning unknown user (don't use MEMBER_ID_OF_GUEST)
317: int memberID = 0;
318: if (!memberName.equals("")) {
319: memberID = DAOFactory.getMemberDAO()
320: .getMemberIDFromMemberName(memberName);
321: }
322: //I must change all possible nulls into "", so I don't get "'null'" in sql query
323: String attachModifiedDate2 = XMLUtil
324: .sqlTimestampToStringDefEmpty(attachModifiedDate1);
325:
326: attachFilename = EnableHtmlTagFilter.filter(attachFilename);
327: attachMimeType = EnableHtmlTagFilter.filter(attachMimeType);
328: attachDesc = EnableHtmlTagFilter.filter(attachDesc);
329: if (ImportWebHelper
330: .execUpdateQuery("INSERT INTO "
331: + AttachmentDAO.TABLE_NAME
332: + " (AttachID, PostID, MemberID,"
333: + " AttachFilename, AttachFileSize, AttachMimeType,"
334: + " AttachDesc, AttachCreationIP, AttachCreationDate, AttachModifiedDate,"
335: + " AttachDownloadCount, AttachOption, AttachStatus)"
336: + " VALUES (" + strAttachmentID + ", "
337: + parentPostID + ", " + memberID + ", '"
338: + attachFilename + "', " + attachFileSize1
339: + ", '" + attachMimeType + "', '"
340: + attachDesc + "', '" + attachCreationIP
341: + "', '" + attachCreationDate1 + "', '"
342: + attachModifiedDate2 + "', "
343: + attachDownloadCount1 + ", "
344: + attachOption1 + ", " + attachStatus1
345: + ")") != 1) {
346: throw new CreateException("Error adding attachment \""
347: + attachFilename + "\" into table '"
348: + AttachmentDAO.TABLE_NAME + "'.");
349: }
350:
351: //attachmentID was already set up
352: }
353: }
354:
355: // ===============================================================
356: // ==================== STATIC EXPORT METHODS ====================
357: // ===============================================================
358: public static void exportAttachmentList(XMLWriter xmlWriter,
359: int parentPostID) throws IOException, ExportException,
360: ObjectNotFoundException, DatabaseException {
361: Collection attachments = ExportWebHelper
362: .execSqlQuery("SELECT AttachID, MemberID,"
363: + " AttachFilename, AttachFileSize, AttachMimeType, AttachDesc,"
364: + " AttachCreationIP, AttachCreationDate, AttachModifiedDate,"
365: + " AttachDownloadCount, AttachOption, AttachStatus"
366: + " FROM " + AttachmentDAO.TABLE_NAME
367: + " WHERE PostID="
368: + Integer.toString(parentPostID));
369: Iterator iter = attachments.iterator();
370: String[] attachment = null;
371: //try {
372: xmlWriter.startElement("AttachmentList");
373: try {
374: while ((attachment = (String[]) iter.next()) != null) {
375: if (attachment.length != 12) {
376: throw new ExportException(
377: "Error while retrieving list of attachments for postID="
378: + parentPostID + ".");
379: }
380: xmlWriter.startElement("Attachment", new String[] {
381: "id", attachment[0] });
382: String memberName = DAOFactory.getMemberDAO()
383: .getMember(Integer.parseInt(attachment[1]))
384: .getMemberName();
385: xmlWriter.startElement("MemberName");
386: xmlWriter.writeData(memberName);
387: xmlWriter.endElement("MemberName");
388: xmlWriter.startElement("AttachFilename");
389: xmlWriter.writeData(DisableHtmlTagFilter
390: .filter(attachment[2]));
391: xmlWriter.endElement("AttachFilename");
392: xmlWriter.startElement("AttachFileSize");
393: xmlWriter.writeData(attachment[3]);
394: xmlWriter.endElement("AttachFileSize");
395: xmlWriter.startElement("AttachMimeType");
396: xmlWriter.writeData(DisableHtmlTagFilter
397: .filter(attachment[4]));
398: xmlWriter.endElement("AttachMimeType");
399: xmlWriter.startElement("AttachDesc");
400: xmlWriter.writeData(DisableHtmlTagFilter
401: .filter(attachment[5]));
402: xmlWriter.endElement("AttachDesc");
403: xmlWriter.startElement("AttachCreationIP");
404: xmlWriter.writeData(attachment[6]);
405: xmlWriter.endElement("AttachCreationIP");
406: xmlWriter.startElement("AttachCreationDate");
407: xmlWriter.writeData(attachment[7]);
408: xmlWriter.endElement("AttachCreationDate");
409: xmlWriter.startElement("AttachModifiedDate");
410: xmlWriter.writeData(attachment[8]);
411: xmlWriter.endElement("AttachModifiedDate");
412: xmlWriter.startElement("AttachDownloadCount");
413: xmlWriter.writeData(attachment[9]);
414: xmlWriter.endElement("AttachDownloadCount");
415: xmlWriter.startElement("AttachOption");
416: xmlWriter.writeData(attachment[10]);
417: xmlWriter.endElement("AttachOption");
418: xmlWriter.startElement("AttachStatus");
419: xmlWriter.writeData(attachment[11]);
420: xmlWriter.endElement("AttachStatus");
421: xmlWriter.endElement("Attachment");
422: }
423: } catch (NoSuchElementException e) {
424: //no more database records
425: }
426: xmlWriter.endElement("AttachmentList");
427: //} catch throw exportexception
428: }
429: }
|