use of org.compiere.model.MAttachment in project adempiere by adempiere.
the class ImageElement method loadAttachment.
// loadFromDB
/**
* Load Attachment
* @param AD_PrintFormatItem_ID record id
*/
private void loadAttachment(int AD_PrintFormatItem_ID) {
MAttachment attachment = MAttachment.get(Env.getCtx(), MPrintFormatItem.Table_ID, AD_PrintFormatItem_ID);
if (attachment == null) {
log.log(Level.WARNING, "No Attachment - AD_PrintFormatItem_ID=" + AD_PrintFormatItem_ID);
return;
}
if (attachment.getEntryCount() != 1) {
log.log(Level.WARNING, "Need just 1 Attachment Entry = " + attachment.getEntryCount());
return;
}
byte[] imageData = attachment.getEntryData(0);
if (imageData != null)
//m_image = Toolkit.getDefaultToolkit().createImage(imageData);
try {
BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(imageData));
m_image = bufferedImage;
} catch (Exception e) {
log.log(Level.WARNING, "(byteArray)", e);
}
if (m_image != null)
log.fine(attachment.getEntryName(0) + " - Size=" + imageData.length);
else
log.log(Level.WARNING, attachment.getEntryName(0) + " - not loaded (must be gif or jpg) - AD_PrintFormatItem_ID=" + AD_PrintFormatItem_ID);
}
use of org.compiere.model.MAttachment in project adempiere by adempiere.
the class MPrintTableFormat method getImage.
// get
/**
* Get the Image
* @return image
*/
public Image getImage() {
if (m_image != null) {
return m_image;
}
//
if (isImageIsAttached()) {
MAttachment attachment = MAttachment.get(getCtx(), Table_ID, get_ID());
if (attachment == null) {
log.log(Level.WARNING, "No Attachment - ID=" + get_ID());
return null;
}
if (attachment.getEntryCount() != 1) {
log.log(Level.WARNING, "Need just 1 Attachment Entry = " + attachment.getEntryCount());
return null;
}
byte[] imageData = attachment.getEntryData(0);
if (imageData != null) {
m_image = Toolkit.getDefaultToolkit().createImage(imageData);
}
if (m_image != null) {
log.fine(attachment.getEntryName(0) + " - Size=" + imageData.length);
} else {
log.log(Level.WARNING, attachment.getEntryName(0) + " - not loaded (must be gif or jpg) - ID=" + get_ID());
}
} else if (getImageURL() != null) {
URL url;
try {
url = new URL(getImageURL());
Toolkit tk = Toolkit.getDefaultToolkit();
m_image = tk.getImage(url);
} catch (MalformedURLException e) {
log.log(Level.WARNING, "Malformed URL - " + getImageURL(), e);
}
}
return m_image;
}
use of org.compiere.model.MAttachment in project adempiere by adempiere.
the class WAttachment method streamAttachment.
// createPage
/**
* Stream Attachment Entry
* @param AD_Attachment_ID attachment
* @param attachmentIndex index
* @param response response
* @param ws status
* @return m_error message
*/
private String streamAttachment(int AD_Attachment_ID, int attachmentIndex, HttpServletResponse response, WWindowStatus ws) {
log.info("AD_Attachment_ID=" + AD_Attachment_ID + ", AttachmentIndex=" + attachmentIndex);
MAttachment attachment = new MAttachment(ws.ctx, AD_Attachment_ID, null);
if (attachment.get_ID() == 0) {
log.fine("No Attachment AD_Attachment_ID=" + AD_Attachment_ID);
return "Attachment not found";
}
// Make sure it's the right attachment
if (ws.curTab.getAD_AttachmentID() != AD_Attachment_ID) {
log.warning("Tab AD_Attachment_ID=" + ws.curTab.getAD_AttachmentID() + " <> " + AD_Attachment_ID);
return "Your Attachment not found";
}
// Stream it
return WebUtil.streamAttachment(response, attachment, attachmentIndex);
}
use of org.compiere.model.MAttachment in project adempiere by adempiere.
the class WAttachment method processPost.
// streamAttachment
/**
* Process Post.
* Update / Create Attachment
* Upload Attachment Entry
* @param request request
* @param response response
* @return m_error message
*/
private MAttachment processPost(HttpServletRequest request, HttpServletResponse response, WWindowStatus ws) {
int AD_Attachment_ID = 0;
int AD_Table_ID = 0;
int Record_ID = 0;
String textMsg = null;
FileUpload upload = null;
// URL Encrypted
if (request.getContentType().equals(form.ENC_DEFAULT)) {
AD_Attachment_ID = WebUtil.getParameterAsInt(request, P_Attachment_ID);
AD_Table_ID = WebUtil.getParameterAsInt(request, "AD_Table_ID");
Record_ID = WebUtil.getParameterAsInt(request, "Record_ID");
textMsg = WebUtil.getParameter(request, P_TEXTMSG);
} else {
upload = new FileUpload(request);
m_error = upload.getError();
if (m_error != null) {
log.warning("pocessPost - " + m_error);
return null;
}
AD_Attachment_ID = upload.getParameterAsInt(P_Attachment_ID);
AD_Table_ID = upload.getParameterAsInt("AD_Table_ID");
Record_ID = upload.getParameterAsInt("Record_ID");
}
// WebEnv.dump(request);
log.info("processPost - AD_Attachment_ID=" + AD_Attachment_ID + ", AD_Table_ID=" + AD_Table_ID + ", Record_ID=" + Record_ID + " - Upload=" + upload);
// Check if you own the attachment
if (ws.curTab.getAD_AttachmentID() != AD_Attachment_ID) {
m_error = "Your Attachment not found";
return null;
}
// Check if we can save
if (AD_Attachment_ID != 0 && Record_ID == 0) {
m_error = "Need to save record first";
return null;
}
MAttachment attachment = null;
if (AD_Attachment_ID == 0)
attachment = new MAttachment(ws.ctx, AD_Table_ID, Record_ID, null);
else
attachment = new MAttachment(ws.ctx, AD_Attachment_ID, null);
// Update Attachment Text
if (textMsg != null)
attachment.setTextMsg(textMsg);
// Create Attachment
if (upload != null)
attachment.addEntry(upload.getFileName(), upload.getData());
// Save and update
if (attachment.save())
// update Tab
ws.curTab.loadAttachments();
else {
m_error = "Attachment not saved";
return null;
}
return attachment;
}
use of org.compiere.model.MAttachment in project adempiere by adempiere.
the class WAttachment method doPost.
// doGet
/**
* Process the HTTP Post request.
* Update Attachment
* @param request request
* @parem response response
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
log.info("From " + request.getRemoteHost() + " - " + request.getRemoteAddr());
HttpSession sess = request.getSession(false);
WWindowStatus ws = WWindowStatus.get(request);
//
WebDoc doc = null;
if (ws == null)
doc = WebDoc.create("Help - No Context");
else {
MAttachment attachment = processPost(request, response, ws);
doc = createPage(ws.ctx, attachment, m_error);
}
//
WebUtil.createResponse(request, response, this, null, doc, false);
}
Aggregations