Search in sources :

Example 1 with MAttachment

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);
}
Also used : MAttachment(org.compiere.model.MAttachment) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedImage(java.awt.image.BufferedImage) MalformedURLException(java.net.MalformedURLException)

Example 2 with MAttachment

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;
}
Also used : MAttachment(org.compiere.model.MAttachment) MalformedURLException(java.net.MalformedURLException) Toolkit(java.awt.Toolkit) URL(java.net.URL)

Example 3 with MAttachment

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);
}
Also used : MAttachment(org.compiere.model.MAttachment)

Example 4 with MAttachment

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;
}
Also used : MAttachment(org.compiere.model.MAttachment) FileUpload(org.compiere.util.FileUpload)

Example 5 with MAttachment

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);
}
Also used : MAttachment(org.compiere.model.MAttachment) HttpSession(javax.servlet.http.HttpSession) WebDoc(org.compiere.util.WebDoc)

Aggregations

MAttachment (org.compiere.model.MAttachment)14 HttpSession (javax.servlet.http.HttpSession)5 Properties (java.util.Properties)4 MNote (org.compiere.model.MNote)4 File (java.io.File)3 MRequest (org.compiere.model.MRequest)3 MUser (org.compiere.model.MUser)3 WebUser (org.compiere.util.WebUser)3 MalformedURLException (java.net.MalformedURLException)2 Timestamp (java.sql.Timestamp)2 MClient (org.compiere.model.MClient)2 MPInstance (org.compiere.model.MPInstance)2 ReportEngine (org.compiere.print.ReportEngine)2 ProcessInfo (org.compiere.process.ProcessInfo)2 FileUpload (org.compiere.util.FileUpload)2 WebDoc (org.compiere.util.WebDoc)2 Toolkit (java.awt.Toolkit)1 BufferedImage (java.awt.image.BufferedImage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1