📄 attachmentxml.java
字号:
DatabaseException, ForeignKeyNotFoundException {
if (attachmentID>0) {
addAttachment(Integer.toString(attachmentID),
memberName, attachFilename, attachFileSize, attachMimeType,
attachDesc, attachCreationIP, attachCreationDate, attachModifiedDate,
attachDownloadCount, attachOption, attachStatus);
} else {
throw new CreateException("Can't create an attachment, because it has no ID assigned yet.");
}
}
/**
* Creates an attachment. All argument values (<code>int</code>s, <code>Timestamp</code>s, ...)
* are represented as <code>String</code>s, because of more convenient using
* of this method for XML parsing.
*
* @param strAttachmentID Must be non-null valid integer number, because it
* is the only way to know which file on server corresponds
* to this attachment.
* @param memberName Can be null.
* @param attachFilename Name of attachment file to be displayed on forum pages.
* @param attachFileSize Size of attachment file.
* @param attachMimeType MIME type of attachment file.
* @param attachDesc Can be null.
* @param attachCreationIP Can be null.
* @param attachCreationDate Can be null.
* @param attachModifiedDate Can be null.
* @param attachDownloadCount Can be null.
* @param attachOption Can be null.
* @param attachStatus Can be null.
*
* @throws CreateException
* @throws DuplicateKeyException
* @throws ObjectNotFoundException
* @throws DatabaseException
* @throws ForeignKeyNotFoundException
*/
public void addAttachment(String strAttachmentID,
String memberName, String attachFilename,
String attachFileSize, String attachMimeType,
String attachDesc, String attachCreationIP,
String attachCreationDate, String attachModifiedDate,
String attachDownloadCount, String attachOption,
String attachStatus)
throws CreateException, DuplicateKeyException, ObjectNotFoundException,
DatabaseException, ForeignKeyNotFoundException {
if (parentPostID<0) {
throw new CreateException("Can't create an attachment, because no parent post assigned yet.");
}
boolean idOk = (attachFilename!=null) &&
(attachFileSize!=null) &&
(attachMimeType!=null) &&
(strAttachmentID!=null) &&
(!strAttachmentID.equals(""));
//now ensure that strAttachmentID is valid number, and >=0
if (idOk) try {
attachmentID = Integer.parseInt(strAttachmentID);
idOk = (attachmentID>=0);
} catch (NumberFormatException e) {
idOk=false;
}
if (!idOk) {
attachmentID=-1;
throw new CreateException("Not enough data to create an attachment, or the ID is invalid.");
} else {
//these values are not neccessary here, but I wanted to use them to validate
//the strings - e.g, does attachOption string really contain a number ?
int attachFileSize1;
java.sql.Timestamp attachCreationDate1;
java.sql.Timestamp attachModifiedDate1;
int attachDownloadCount1;
int attachOption1;
int attachStatus1;
try {
if (memberName==null) memberName="";
attachFileSize1= XMLUtil.stringToIntDef(attachFileSize, 0);
if (attachDesc==null) attachDesc="";
if (attachCreationIP==null) attachCreationIP="0.0.0.0";
attachCreationDate1= XMLUtil.stringToSqlTimestampDefNow(attachCreationDate);
attachModifiedDate1= XMLUtil.stringToSqlTimestampDefNull(attachModifiedDate);
attachDownloadCount1= XMLUtil.stringToIntDef(attachDownloadCount, 0);
attachOption1= XMLUtil.stringToIntDef(attachOption, 0);
attachStatus1= XMLUtil.stringToIntDef(attachStatus, 0);
} catch (NumberFormatException e) {
throw new CreateException("Invalid data for an attachment. Expected a number.");
}
//allow memberName to be empty, meaning unknown user (don't use MEMBER_ID_OF_GUEST)
int memberID=0;
if (!memberName.equals("")) {
memberID=DAOFactory.getMemberDAO().getMemberIDFromMemberName(memberName);
}
//I must change all possible nulls into "", so I don't get "'null'" in sql query
String attachModifiedDate2= XMLUtil.sqlTimestampToStringDefEmpty(attachModifiedDate1);
attachFilename=EnableHtmlTagFilter.filter(attachFilename);
attachMimeType=EnableHtmlTagFilter.filter(attachMimeType);
attachDesc=EnableHtmlTagFilter.filter(attachDesc);
if (ImportWebHelper.execUpdateQuery(
"INSERT INTO "+ AttachmentDAO.TABLE_NAME +
" (AttachID, PostID, MemberID," +
" AttachFilename, AttachFileSize, AttachMimeType," +
" AttachDesc, AttachCreationIP, AttachCreationDate, AttachModifiedDate," +
" AttachDownloadCount, AttachOption, AttachStatus)" +
" VALUES (" +strAttachmentID+ ", "+parentPostID+", " +memberID+
", '" +attachFilename+ "', " +attachFileSize1+
", '" +attachMimeType+ "', '" +attachDesc+
"', '" +attachCreationIP+ "', '" +attachCreationDate1+
"', '" +attachModifiedDate2+ "', " +attachDownloadCount1+
", " +attachOption1+ ", " +attachStatus1+ ")"
) != 1) {
throw new CreateException("Error adding attachment \""+attachFilename+"\" into table '"+
AttachmentDAO.TABLE_NAME +"'.");
}
//attachmentID was already set up
}
}
// ===============================================================
// ==================== STATIC EXPORT METHODS ====================
// ===============================================================
public static void exportAttachmentList(XMLWriter xmlWriter, int parentPostID)
throws IOException, ExportException, ObjectNotFoundException, DatabaseException {
Collection attachments=ExportWebHelper.execSqlQuery(
"SELECT AttachID, MemberID,"+
" AttachFilename, AttachFileSize, AttachMimeType, AttachDesc,"+
" AttachCreationIP, AttachCreationDate, AttachModifiedDate,"+
" AttachDownloadCount, AttachOption, AttachStatus"+
" FROM "+AttachmentDAO.TABLE_NAME+
" WHERE PostID="+Integer.toString(parentPostID));
Iterator iter=attachments.iterator();
String[] attachment=null;
//try {
xmlWriter.startElement("AttachmentList");
try {
while ( (attachment=(String[])iter.next()) !=null) {
if (attachment.length!=12) {
throw new ExportException("Error while retrieving list of attachments for postID="+parentPostID+".");
}
xmlWriter.startElement("Attachment", new String[]{"id", attachment[0]});
String memberName=DAOFactory.getMemberDAO().getMember_forPublic(Integer.parseInt(attachment[1])).getMemberName();
xmlWriter.startElement("MemberName");
xmlWriter.writeData(memberName);
xmlWriter.endElement("MemberName");
xmlWriter.startElement("AttachFilename");
xmlWriter.writeData(DisableHtmlTagFilter.filter(attachment[2]));
xmlWriter.endElement("AttachFilename");
xmlWriter.startElement("AttachFileSize");
xmlWriter.writeData(attachment[3]);
xmlWriter.endElement("AttachFileSize");
xmlWriter.startElement("AttachMimeType");
xmlWriter.writeData(DisableHtmlTagFilter.filter(attachment[4]));
xmlWriter.endElement("AttachMimeType");
xmlWriter.startElement("AttachDesc");
xmlWriter.writeData(DisableHtmlTagFilter.filter(attachment[5]));
xmlWriter.endElement("AttachDesc");
xmlWriter.startElement("AttachCreationIP");
xmlWriter.writeData(attachment[6]);
xmlWriter.endElement("AttachCreationIP");
xmlWriter.startElement("AttachCreationDate");
xmlWriter.writeData(attachment[7]);
xmlWriter.endElement("AttachCreationDate");
xmlWriter.startElement("AttachModifiedDate");
xmlWriter.writeData(attachment[8]);
xmlWriter.endElement("AttachModifiedDate");
xmlWriter.startElement("AttachDownloadCount");
xmlWriter.writeData(attachment[9]);
xmlWriter.endElement("AttachDownloadCount");
xmlWriter.startElement("AttachOption");
xmlWriter.writeData(attachment[10]);
xmlWriter.endElement("AttachOption");
xmlWriter.startElement("AttachStatus");
xmlWriter.writeData(attachment[11]);
xmlWriter.endElement("AttachStatus");
xmlWriter.endElement("Attachment");
}
} catch (NoSuchElementException e) {
//no more database records
}
xmlWriter.endElement("AttachmentList");
//} catch throw exportexception
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -