1 40 package com.mvnforum.admin; 41 42 import java.io.IOException ; 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 60 public class AttachmentXML { 61 62 private int attachmentID; 63 65 public int getAttachmentID() { return attachmentID; } 66 67 private int parentPostID; 68 70 public int getParentPostID() { return parentPostID; } 71 72 private int parentThreadID; 73 75 public int getParentThreadID() { return parentThreadID; } 76 77 private int parentForumID; 78 80 public int getParentForumID() { return parentForumID; } 81 82 private int parentCategoryID; 83 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 id) { 97 attachmentID=XMLUtil.stringToIntDef(id, -1); 98 } 99 100 public void setParentPost(Object 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 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 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 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 181 public void addAttachment( 182 String memberName, String attachFilename, 183 String attachFileSize, String attachMimeType, 184 String attachDesc, String attachCreationIP, 185 String attachCreationDate, String attachModifiedDate, 186 String attachDownloadCount, String attachOption, 187 String 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 226 public void addAttachment(String strAttachmentID, 227 String memberName, String attachFilename, 228 String attachFileSize, String attachMimeType, 229 String attachDesc, String attachCreationIP, 230 String attachCreationDate, String attachModifiedDate, 231 String attachDownloadCount, String attachOption, 232 String 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 if (idOk) try { 245 attachmentID = Integer.parseInt(strAttachmentID); 246 idOk = (attachmentID>=0); 247 } catch (NumberFormatException 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 int attachFileSize1; 258 java.sql.Timestamp attachCreationDate1; 259 java.sql.Timestamp 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 e) { 275 throw new CreateException("Invalid data for an attachment. Expected a number."); 276 } 277 278 int memberID=0; 280 if (!memberName.equals("")) { 281 memberID=DAOFactory.getMemberDAO().getMemberIDFromMemberName(memberName); 282 } 283 String 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 } 308 } 309 310 311 public static void exportAttachmentList(XMLWriter xmlWriter, int parentPostID) 315 throws IOException , 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 [] attachment=null; 325 xmlWriter.startElement("AttachmentList"); 327 try { 328 while ( (attachment=(String [])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 []{"id", attachment[0]}); 333 String 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 } 372 xmlWriter.endElement("AttachmentList"); 373 } 375 } 376
| Popular Tags
|