📄 attachment.java
字号:
+ "AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,"
+ "Title, AD_Table_ID, Record_ID, Text, BinaryData) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,Empty_BLOB())";
try
{
PreparedStatement pstmt = DB.prepareStatement(SQL);
pstmt.setInt(1, m_AD_Attachment_ID);
pstmt.setInt(2, Env.getContextAsInt(Env.getCtx(), m_WindowNo, "AD_Client_ID"));
pstmt.setInt(3, Env.getContextAsInt(Env.getCtx(), m_WindowNo, "AD_Org_ID"));
pstmt.setString(4, "Y");
pstmt.setTimestamp(5, today);
pstmt.setInt(6, userID);
pstmt.setTimestamp(7, today);
pstmt.setInt(8, userID);
pstmt.setString(9, sName);
pstmt.setInt(10, m_AD_Table_ID);
pstmt.setInt(11, m_Record_ID);
pstmt.setString(12, text.getText());
int i = pstmt.executeUpdate();
Log.trace(Log.l4_Data, "Attachment.save (insert) - Records=" + i);
}
catch (SQLException ei)
{
Log.error("Attachment.save (insert)", ei);
}
}
// Update Attachment (text info)
else
{
String SQL = "UPDATE AD_Attachment "
+ "SET Title=?, Text=?, Updated=?, UpdatedBy=? "
+ "WHERE AD_Attachment_ID=" + m_AD_Attachment_ID;
try
{
PreparedStatement pstmt = DB.prepareStatement(SQL);
pstmt.setString(1, sName);
pstmt.setString(2, text.getText());
pstmt.setTimestamp(3, today);
pstmt.setInt(4, userID);
//
int i = pstmt.executeUpdate();
Log.trace(Log.l4_Data, "Attachment.save (update) - Records=" + i);
}
catch (SQLException eu)
{
Log.error("Attachment.save (update)", eu);
}
}
// Upload BLOB
if (m_file != null)
{
boolean success = true;
String sql = "SELECT BinaryData FROM AD_Attachment WHERE AD_Attachment_ID=? FOR UPDATE";
try
{
Connection con = DB.createConnection(false, Connection.TRANSACTION_SERIALIZABLE);
PreparedStatement pstmt = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
pstmt.setInt(1, m_AD_Attachment_ID);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
BLOB blob = ((OracleResultSet)rs).getBLOB(1);
long blobLength = blob.length();
int size = blob.getBufferSize();
Log.trace(Log.l3_Util, "Attachment.save blob - "
+ m_file.getAbsolutePath() + ", Size=" + m_file.length()
+ ", Buffer=" + size + ", BLOB length=" + blobLength);
try
{
FileInputStream fis = new FileInputStream (m_file);
OutputStream os = blob.getBinaryOutputStream();
byte[] buffer = new byte[size];
int length = -1;
while ((length = fis.read(buffer)) != -1)
os.write(buffer, 0, length);
fis.close();
os.close();
Log.trace(Log.l4_Data, "Attachment.save - New length=" + blob.length());
}
catch (IOException ioe)
{
Log.error("Attachment.save (blob-io)", ioe);
success = false;
}
}
else
{
Log.error("Attachment.save - No Record for BLOB");
success = false;
}
rs.close();
pstmt.close();
con.close();
}
catch (SQLException e)
{
Log.error ("Attachment.save (blob)", e);
success = false;
}
if (!success)
{
title.setText(NONE);
m_file = null;
}
} // load blob
} // saveAttachment
/**
* Get File Name
*/
private void getFileName()
{
Log.trace(Log.l1_User, "Attachment.getFileName");
JFileChooser chooser = new JFileChooser();
chooser.setDialogType(JFileChooser.OPEN_DIALOG);
chooser.setDialogTitle(Msg.getMsg(Env.getCtx(), "AttachmentNew"));
if (!title.getText().equals(NONE))
{
File f = new File(title.getText());
chooser.setSelectedFile(f);
}
int returnVal = chooser.showOpenDialog(this);
String fileName = "";
if (returnVal == JFileChooser.APPROVE_OPTION)
{
fileName = chooser.getSelectedFile().getName();
Log.trace(Log.l4_Data, "Get: " + fileName);
m_file = chooser.getSelectedFile();
// Show
title.setText(fileName);
bOpen.setEnabled(true);
}
else
{
title.setText(NONE);
m_file = null;
}
this.setTitle(Msg.getMsg(Env.getCtx(), "Attachment") + " " + title.getText());
// load and show image
String t = fileName.toLowerCase();
if (t.endsWith(".gif") || t.endsWith(".jpg"))
{
try
{
FileInputStream fis = new FileInputStream (m_file);
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[1024*8]; // 8kB
int length = -1;
while ((length = fis.read(buffer)) != -1)
os.write(buffer, 0, length);
fis.close();
displayData(os.toByteArray());
os.close();
}
catch (IOException ioe)
{
Log.error("Attachment.getFileName", ioe);
}
}
} // getFileName
/**
* Delete Attachment
*/
private void deleteAttachment()
{
Log.trace(Log.l1_User, "Attachment.delete");
if (m_AD_Attachment_ID != 0 && ADialog.ask(m_WindowNo, this, "AttachmentDelete?"))
{
String SQL = "DELETE AD_Attachment WHERE AD_Attachment_ID=" + m_AD_Attachment_ID;
try
{
int i = DB.createStatement().executeUpdate(SQL);
Log.trace(Log.l4_Data, "Attachment.delete - Records=" + i);
}
catch (SQLException e)
{
Log.error("Attachment.delete", e);
}
}
} // deleteAttachment
/**
* Open Attachment
*/
private void openAttachment()
{
Log.trace(Log.l1_User, "Attachment.open");
if (m_data == null || m_data.length == 0)
return;
String fileName = title.getText();
String ext = fileName.substring (fileName.lastIndexOf("."));
Log.trace(Log.l4_Data, "Ext=" + ext);
JFileChooser chooser = new JFileChooser();
chooser.setDialogType(JFileChooser.SAVE_DIALOG);
chooser.setDialogTitle(Msg.getMsg(Env.getCtx(), "AttachmentSave"));
if (!fileName.equals(NONE))
{
File f = new File(fileName);
chooser.setSelectedFile(f);
}
int returnVal = chooser.showSaveDialog(this);
File saveFile = null;
if (returnVal == JFileChooser.APPROVE_OPTION)
saveFile = chooser.getSelectedFile();
// Save to file
if (saveFile != null)
{
Log.trace(Log.l3_Util, "Attachment.open", "Save to " + saveFile.getAbsolutePath());
try
{
FileOutputStream fos = new FileOutputStream(saveFile);
fos.write(m_data);
fos.close();
}
catch (IOException ioe)
{
Log.error("Attachment.open", ioe);
}
}
} // openAttachment
/*************************************************************************/
/**
* Graphic Image Panel
*/
class GImage extends JPanel
{
/**
* Graphic Image
*/
public GImage()
{
super();
} // GImage
/** The Image */
private Image m_image = null;
/**
* Set Image
* @param image image
*/
public void setImage (Image image)
{
m_image = image;
if (m_image == null)
return;
MediaTracker mt = new MediaTracker(this);
mt.addImage(m_image, 0);
try {
mt.waitForID(0);
} catch (Exception e) {}
Dimension dim = new Dimension(m_image.getWidth(this), m_image.getHeight(this));
this.setPreferredSize(dim);
} // setImage
/**
* Paint
* @param g graphics
*/
public void paint (Graphics g)
{
Insets in = getInsets();
if (m_image != null)
g.drawImage(m_image, in.left, in.top, this);
} // paint
/**
* Update
* @param g graphics
*/
public void update (Graphics g)
{
paint(g);
} // update
} // GImage
} // Attachment
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -