Search in sources :

Example 1 with MAttachmentEntry

use of org.compiere.model.MAttachmentEntry in project adempiere by adempiere.

the class WebUtil method streamAttachment.

//	getClosePopupButton
/**
	 * 	Stream Attachment Entry
	 *	@param response response
	 *	@param attachment attachment
	 *	@param attachmentIndex logical index
	 *	@return error message or null
	 */
public static String streamAttachment(HttpServletResponse response, MAttachment attachment, int attachmentIndex) {
    if (attachment == null)
        return "No Attachment";
    int realIndex = -1;
    MAttachmentEntry[] entries = attachment.getEntries();
    for (int i = 0; i < entries.length; i++) {
        MAttachmentEntry entry = entries[i];
        if (entry.getIndex() == attachmentIndex) {
            realIndex = i;
            break;
        }
    }
    if (realIndex < 0) {
        log.fine("No Attachment Entry for Index=" + attachmentIndex + " - " + attachment);
        return "Attachment Entry not found";
    }
    MAttachmentEntry entry = entries[realIndex];
    if (entry.getData() == null) {
        log.fine("Empty Attachment Entry for Index=" + attachmentIndex + " - " + attachment);
        return "Attachment Entry empty";
    }
    //	Stream Attachment Entry
    try {
        //	2k Buffer
        int bufferSize = 2048;
        int fileLength = entry.getData().length;
        //
        response.setContentType(entry.getContentType());
        response.setBufferSize(bufferSize);
        response.setContentLength(fileLength);
        //
        log.fine(entry.toString());
        //	timer start
        long time = System.currentTimeMillis();
        //
        ServletOutputStream out = response.getOutputStream();
        out.write(entry.getData());
        out.flush();
        out.close();
        //
        time = System.currentTimeMillis() - time;
        double speed = (fileLength / 1024) / ((double) time / 1000);
        log.info("Length=" + fileLength + " - " + time + " ms - " + speed + " kB/sec - " + entry.getContentType());
    } catch (IOException ex) {
        log.log(Level.SEVERE, ex.toString());
        return "Streaming error - " + ex;
    }
    return null;
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) MAttachmentEntry(org.compiere.model.MAttachmentEntry) IOException(java.io.IOException)

Example 2 with MAttachmentEntry

use of org.compiere.model.MAttachmentEntry in project adempiere by adempiere.

the class WAttachment method createPage.

//  doPost
/**
	 * 	Create Attachment Page
	 * 	@param ctx context
	 *	@param AD_Attachment_ID id for existing attachment
	 *	@param AD_Table_ID table for new attachment
	 *	@param Record_ID record for new attachment
	 *	@param m_error optional m_error message
	 *	@return WebDoc
	 */
private WebDoc createPage(Properties ctx, MAttachment attachment, String error) {
    WebDoc doc = WebDoc.createPopup(Msg.translate(ctx, "AD_Attachment_ID"));
    table table = doc.getTable();
    //
    if (error != null) {
        table.addElement(new tr().addElement(new td("popupHeader", AlignType.RIGHT, AlignType.TOP, false, null)).addElement(new td("popupHeader", AlignType.LEFT, AlignType.TOP, false, //	window.css
        new p(error, AlignType.LEFT).setClass("Cerror"))));
    }
    //
    tr tr = new tr();
    td left = new td("popupCenter", AlignType.LEFT, AlignType.TOP, false, new label("TextMsg", "T", Msg.translate(ctx, "TextMsg")));
    //
    td right = new td("popupCenter", AlignType.LEFT, AlignType.TOP, false);
    //	Text Message Update
    form textMsg = new form("WAttachment");
    textMsg.addElement(new input(input.TYPE_HIDDEN, P_Attachment_ID, attachment.getAD_Attachment_ID()));
    textMsg.addElement(new input(input.TYPE_HIDDEN, "AD_Table_ID", attachment.getAD_Table_ID()));
    textMsg.addElement(new input(input.TYPE_HIDDEN, "Record_ID", attachment.getRecord_ID()));
    textarea msg = new textarea(P_TEXTMSG, 5, 40);
    msg.addElement(attachment.getTextMsg());
    textMsg.addElement(msg);
    textMsg.addElement(new br());
    //textMsg.addElement(new input (input.TYPE_SUBMIT, "submit", "Submit"));
    //Submit Button
    //String textbtn = "Submit";
    //String text = null;
    //if (ctx != null)
    //text = Msg.getMsg (ctx, "Submit");		
    //input submitbtn = new input(input.TYPE_SUBMIT, textbtn, "  "+text);		
    //submitbtn.setID(text);
    //submitbtn.setClass("submitbtn");
    //textMsg.addElement(submitbtn);
    right.addElement(textMsg);
    //	Existing Links
    p p = new p();
    MAttachmentEntry[] entries = attachment.getEntries();
    for (int i = 0; i < entries.length; i++) {
        MAttachmentEntry entry = entries[i];
        if (i > 0)
            p.addElement(" - ");
        String url = "WAttachment?" + P_Attachment_ID + "=" + attachment.getAD_Attachment_ID() + "&" + P_ATTACHMENT_INDEX + "=" + entry.getIndex();
        p.addElement(new a(url, null, a.TARGET_BLANK, entry.getName()));
    }
    right.addElement(p);
    //	Upload
    form upload = FileUpload.createForm("WAttachment");
    upload.addElement(new input(input.TYPE_HIDDEN, P_Attachment_ID, attachment.getAD_Attachment_ID()));
    upload.addElement(new input(input.TYPE_HIDDEN, "AD_Table_ID", attachment.getAD_Table_ID()));
    upload.addElement(new input(input.TYPE_HIDDEN, "Record_ID", attachment.getRecord_ID()));
    right.addElement(upload);
    //
    tr.addElement(left);
    tr.addElement(right);
    table.addElement(tr);
    //	Footer
    //Modified by Rob Klein 4/29/07
    doc.addPopupClose(ctx);
    //	System.out.println(doc);
    return doc;
}
Also used : org.apache.ecs.xhtml.textarea(org.apache.ecs.xhtml.textarea) org.apache.ecs.xhtml.a(org.apache.ecs.xhtml.a) MAttachmentEntry(org.compiere.model.MAttachmentEntry) org.apache.ecs.xhtml.textarea(org.apache.ecs.xhtml.textarea) org.apache.ecs.xhtml.label(org.apache.ecs.xhtml.label) org.apache.ecs.xhtml.td(org.apache.ecs.xhtml.td) org.apache.ecs.xhtml.p(org.apache.ecs.xhtml.p) org.apache.ecs.xhtml.br(org.apache.ecs.xhtml.br) org.apache.ecs.xhtml.input(org.apache.ecs.xhtml.input) org.apache.ecs.xhtml.form(org.apache.ecs.xhtml.form) WebDoc(org.compiere.util.WebDoc) org.apache.ecs.xhtml.table(org.apache.ecs.xhtml.table) org.apache.ecs.xhtml.tr(org.apache.ecs.xhtml.tr)

Example 3 with MAttachmentEntry

use of org.compiere.model.MAttachmentEntry in project adempiere by adempiere.

the class ReportStarter method downloadAttachment.

/**
	 * Download db attachment
	 * @param reportPath must of syntax attachment:filename
	 * @return File
	 */
private File downloadAttachment(String reportPath) {
    File reportFile = null;
    String name = reportPath.substring("attachment:".length()).trim();
    MProcess process = new MProcess(Env.getCtx(), processInfo.getAD_Process_ID(), processInfo.getTransactionName());
    attachment = process.getAttachment();
    if (attachment != null) {
        MAttachmentEntry[] entries = attachment.getEntries();
        MAttachmentEntry entry = null;
        for (int i = 0; i < entries.length; i++) {
            if (entries[i].getName().equals(name)) {
                entry = entries[i];
                break;
            }
        }
        if (entry != null) {
            reportFile = getAttachmentEntryFile(entry);
        }
    }
    return reportFile;
}
Also used : MProcess(org.compiere.model.MProcess) MAttachmentEntry(org.compiere.model.MAttachmentEntry) DigestOfFile(org.compiere.util.DigestOfFile) File(java.io.File) JasperPrint(net.sf.jasperreports.engine.JasperPrint)

Example 4 with MAttachmentEntry

use of org.compiere.model.MAttachmentEntry in project adempiere by adempiere.

the class MobileUtil method streamAttachment.

//	getClosePopupButton
/**
	 * 	Stream Attachment Entry
	 *	@param response response
	 *	@param attachment attachment
	 *	@param attachmentIndex logical index
	 *	@return error message or null
	 */
public static String streamAttachment(HttpServletResponse response, MAttachment attachment, int attachmentIndex) {
    if (attachment == null)
        return "No Attachment";
    int realIndex = -1;
    MAttachmentEntry[] entries = attachment.getEntries();
    for (int i = 0; i < entries.length; i++) {
        MAttachmentEntry entry = entries[i];
        if (entry.getIndex() == attachmentIndex) {
            realIndex = i;
            break;
        }
    }
    if (realIndex < 0) {
        log.fine("No Attachment Entry for Index=" + attachmentIndex + " - " + attachment);
        return "Attachment Entry not found";
    }
    MAttachmentEntry entry = entries[realIndex];
    if (entry.getData() == null) {
        log.fine("Empty Attachment Entry for Index=" + attachmentIndex + " - " + attachment);
        return "Attachment Entry empty";
    }
    //	Stream Attachment Entry
    try {
        //	2k Buffer
        int bufferSize = 2048;
        int fileLength = entry.getData().length;
        //
        response.setContentType(entry.getContentType());
        response.setBufferSize(bufferSize);
        response.setContentLength(fileLength);
        //
        log.fine(entry.toString());
        //	timer start
        long time = System.currentTimeMillis();
        //
        ServletOutputStream out = response.getOutputStream();
        out.write(entry.getData());
        out.flush();
        out.close();
        //
        time = System.currentTimeMillis() - time;
        double speed = (fileLength / 1024) / ((double) time / 1000);
        log.info("Length=" + fileLength + " - " + time + " ms - " + speed + " kB/sec - " + entry.getContentType());
    } catch (IOException ex) {
        log.log(Level.SEVERE, ex.toString());
        return "Streaming error - " + ex;
    }
    return null;
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) MAttachmentEntry(org.compiere.model.MAttachmentEntry) IOException(java.io.IOException)

Example 5 with MAttachmentEntry

use of org.compiere.model.MAttachmentEntry in project adempiere by adempiere.

the class Attachment method displayData.

//	loadAttachment
/**
	 *  Display gif or jpg in gifPanel
	 * 	@param index index
	 */
private void displayData(int index) {
    MAttachmentEntry entry = m_attachment.getEntry(index);
    log.config("Index=" + index + " - " + entry);
    //	Reset UI
    gifPanel.setImage(null);
    graphPanel.removeAll();
    //
    bDelete.setEnabled(false);
    bOpen.setEnabled(false);
    bSave.setEnabled(false);
    Dimension size = null;
    //	no attachment
    if (entry == null || entry.getData() == null) {
        info.setText("-");
    } else {
        bOpen.setEnabled(true);
        bSave.setEnabled(true);
        bDelete.setEnabled(true);
        log.config(entry.toStringX());
        //
        info.setText(entry.toStringX());
        if (entry.isPDF() && pdfViewer != null) {
            try {
                pdfViewer.loadPDF(entry.getInputStream());
                pdfViewer.setScale(50);
                size = pdfViewer.getPreferredSize();
                //	size.width = Math.min(size.width, 400);
                //	size.height = Math.min(size.height, 400);
                //
                graphPanel.add(pdfViewer, BorderLayout.CENTER);
            } catch (Exception e) {
                log.log(Level.SEVERE, "(pdf)", e);
            }
        } else if (entry.isGraphic()) {
            //  Can we display it
            Image image = Toolkit.getDefaultToolkit().createImage(entry.getData());
            if (image != null) {
                gifPanel.setImage(image);
                size = gifPanel.getPreferredSize();
                if (size.width == -1 && size.height == -1) {
                    log.log(Level.SEVERE, "Invalid Image");
                } else {
                    //	size.width += 40;
                    //	size.height += 40;
                    graphPanel.add(gifScroll, BorderLayout.CENTER);
                }
            } else
                log.log(Level.SEVERE, "Could not create image");
        }
    }
    if (graphPanel.getComponentCount() == 0) {
        graphPanel.add(info, BorderLayout.CENTER);
    }
    log.config("Size=" + size);
    //	graphPanel.setPreferredSize(size);
    //	centerPane.setDividerLocation(size.width+30);
    //	size.width += 100;
    //	size.height += 100;
    //	centerPane.setPreferredSize(size);
    pack();
}
Also used : MAttachmentEntry(org.compiere.model.MAttachmentEntry) Dimension(java.awt.Dimension) Image(java.awt.Image) IOException(java.io.IOException)

Aggregations

MAttachmentEntry (org.compiere.model.MAttachmentEntry)13 File (java.io.File)7 JasperPrint (net.sf.jasperreports.engine.JasperPrint)6 IOException (java.io.IOException)5 DigestOfFile (org.compiere.util.DigestOfFile)3 DigestOfFile (org.compiere.utils.DigestOfFile)3 ArrayList (java.util.ArrayList)2 ServletOutputStream (javax.servlet.ServletOutputStream)2 MProcess (org.compiere.model.MProcess)2 AMedia (org.zkoss.util.media.AMedia)2 Dimension (java.awt.Dimension)1 Image (java.awt.Image)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 BigDecimal (java.math.BigDecimal)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Transformer (javax.xml.transform.Transformer)1