Search in sources :

Example 1 with XhtmlDocument

use of org.apache.ecs.XhtmlDocument in project adempiere by adempiere.

the class ReportEngine method createHTML.

/**
	 * 	Write HTML to writer
	 * 	@param writer writer
	 *  @param onlyTable if false create complete HTML document
	 *  @param language optional language - if null numbers/dates are not formatted
	 *  @param extension optional extension for html output
	 * 	@return true if success
	 */
public boolean createHTML(Writer writer, boolean onlyTable, Language language, IHTMLExtension extension) {
    try {
        String cssPrefix = extension != null ? extension.getClassPrefix() : null;
        if (cssPrefix != null && cssPrefix.trim().length() == 0)
            cssPrefix = null;
        table table = new table();
        if (m_printFormat.getAD_PrintFont_ID() != 0) {
            MPrintFont font = (MPrintFont) m_printFormat.getAD_PrintFont();
            Font ff = font.getFont();
            String generic = ff.getName();
            if (generic.equalsIgnoreCase("sansserif"))
                generic = "sans-serif";
            if (generic.equalsIgnoreCase("monospaced"))
                generic = "monospace";
            if (generic.equalsIgnoreCase("dialog"))
                generic = "cursive";
            if (generic.equalsIgnoreCase("dialoginput"))
                generic = "fantasy";
            table.setStyle("font-family:" + generic);
        }
        if (cssPrefix != null)
            table.setClass(cssPrefix + "-table");
        //	for all rows (-1 = header row)
        for (int row = -1; row < m_printData.getRowCount(); row++) {
            tr tr = new tr();
            table.addElement(tr);
            if (cssPrefix != null && row % 2 == 0)
                tr.setClass(cssPrefix + "-odd");
            if (row != -1) {
                m_printData.setRowIndex(row);
                if (extension != null) {
                    extension.extendRowElement(tr, m_printData);
                }
            }
            //	for all columns
            for (int col = 0; col < m_printFormat.getItemCount(); col++) {
                MPrintFormatItem item = m_printFormat.getItem(col);
                if (item.isPrinted()) {
                    //	header row
                    if (row == -1) {
                        th th = new th();
                        tr.addElement(th);
                        th.addElement(Util.maskHTML(item.getPrintName(language)));
                    } else {
                        td td = new td();
                        tr.addElement(td);
                        if (item.isFixedWidth() && item.getMaxWidth() > 0) {
                            // convert to pixels assuming 96dpi
                            int pxs = (item.getMaxWidth() * 96);
                            pxs = pxs / 72;
                            td.setStyle("width:" + pxs + "px");
                        }
                        Object obj = m_printData.getNode(new Integer(item.getAD_Column_ID()));
                        if (obj == null)
                            td.addElement("&nbsp;");
                        else if (obj instanceof PrintDataElement) {
                            PrintDataElement pde = (PrintDataElement) obj;
                            //	formatted
                            String value = pde.getValueDisplay(language);
                            if (pde.getColumnName().endsWith("_ID") && extension != null) {
                                //link for column
                                a href = new a("javascript:void(0)");
                                href.setID(pde.getColumnName() + "_" + row + "_a");
                                td.addElement(href);
                                href.addElement(Util.maskHTML(value));
                                if (cssPrefix != null)
                                    href.setClass(cssPrefix + "-href");
                                extension.extendIDColumn(row, td, href, pde);
                            } else {
                                td.addElement(Util.maskHTML(value));
                            }
                            if (cssPrefix != null) {
                                if (DisplayType.isNumeric(pde.getDisplayType())) {
                                    td.setClass(cssPrefix + "-number");
                                    td.setClass(cssPrefix + "-numberalign");
                                } else if (DisplayType.isDate(pde.getDisplayType()))
                                    td.setClass(cssPrefix + "-date");
                                else
                                    td.setClass(cssPrefix + "-text");
                            }
                        } else if (obj instanceof PrintData) {
                        //	ignore contained Data
                        } else
                            log.log(Level.SEVERE, "Element not PrintData(Element) " + obj.getClass());
                    }
                }
            //	printed
            }
        //	for all columns
        }
        //	for all rows
        //
        PrintWriter w = new PrintWriter(writer);
        if (onlyTable)
            table.output(w);
        else {
            XhtmlDocument doc = new XhtmlDocument();
            doc.appendBody(table);
            if (extension.getStyleURL() != null) {
                link l = new link(extension.getStyleURL(), "stylesheet", "text/css");
                doc.appendHead(l);
            }
            if (extension.getScriptURL() != null) {
                script jslink = new script();
                jslink.setLanguage("javascript");
                jslink.setSrc(extension.getScriptURL());
                doc.appendHead(jslink);
            }
            doc.output(w);
        }
        w.flush();
        w.close();
    } catch (Exception e) {
        log.log(Level.SEVERE, "(w)", e);
    }
    return false;
}
Also used : org.apache.ecs.xhtml.a(org.apache.ecs.xhtml.a) org.apache.ecs.xhtml.link(org.apache.ecs.xhtml.link) Font(java.awt.Font) org.apache.ecs.xhtml.script(org.apache.ecs.xhtml.script) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) IOException(java.io.IOException) XhtmlDocument(org.apache.ecs.XhtmlDocument) org.apache.ecs.xhtml.td(org.apache.ecs.xhtml.td) org.apache.ecs.xhtml.th(org.apache.ecs.xhtml.th) org.apache.ecs.xhtml.table(org.apache.ecs.xhtml.table) org.apache.ecs.xhtml.tr(org.apache.ecs.xhtml.tr) PrintWriter(java.io.PrintWriter)

Example 2 with XhtmlDocument

use of org.apache.ecs.XhtmlDocument in project adempiere by adempiere.

the class ReportEngineEx method createEXCEL_HTML.

//	createHTML
/**
	 * 	Write EXCEL HTML to writer
	 * 	@param writer writer
	 *  @param onlyTable if false create complete HTML document
	 *  @param language optional language - if null nubers/dates are not formatted
	 * 	@return true if success
	 */
public static boolean createEXCEL_HTML(ReportEngine re, Properties m_ctx, Writer writer, boolean onlyTable, Language language) {
    PrintData m_printData = re.getPrintData();
    MPrintFormat m_printFormat = re.getPrintFormat();
    ///Properties m_ctx = new Properties();
    MQuery m_query = re.getQuery();
    try {
        table table = new table();
        //	for all rows (-1 = header row)
        for (int row = -1; row < m_printData.getRowCount(); row++) {
            tr tr = new tr();
            table.addElement(tr);
            if (row != -1)
                m_printData.setRowIndex(row);
            //	for all columns
            for (int col = 0; col < m_printFormat.getItemCount(); col++) {
                MPrintFormatItem item = m_printFormat.getItem(col);
                if (item.isPrinted()) {
                    //	header row
                    if (row == -1) {
                        th th = new th();
                        tr.addElement(th);
                        th.addElement(Util.maskHTML(item.getPrintName(language)));
                        th.setClass("xl_head");
                    } else {
                        td td = new td();
                        tr.addElement(td);
                        Object obj = m_printData.getNode(new Integer(item.getAD_Column_ID()));
                        if (obj == null)
                            td.addElement("&nbsp;");
                        else if (obj instanceof PrintDataElement) {
                            //	formatted
                            String value = ((PrintDataElement) obj).getValueDisplay(null);
                            int displayType = ((PrintDataElement) obj).getDisplayType();
                            if (((PrintDataElement) obj).isNumeric()) {
                                if (displayType == DisplayType.Integer) {
                                    td.addAttribute("x:num", value);
                                    td.addElement(Util.maskHTML(value));
                                } else if (displayType == DisplayType.Quantity) {
                                    td.addAttribute("x:num", value);
                                    td.addElement(Util.maskHTML(value));
                                } else if (displayType == DisplayType.Amount) {
                                    td.addAttribute("x:num", value);
                                    td.addElement(Util.maskHTML(value));
                                } else if (displayType == DisplayType.CostPrice) {
                                    td.addAttribute("x:num", "");
                                    td.addAttribute("u1:num", value);
                                    td.setClass("xl25");
                                    td.addElement(Util.maskHTML(value));
                                } else //else if (displayType == DisplayType.YesNo)
                                //{
                                //}
                                //	if (displayType == Number)										
                                {
                                    td.addElement(Util.maskHTML(value));
                                }
                            } else if (((PrintDataElement) obj).isDate()) {
                                if (displayType == DisplayType.Date) {
                                    td.setClass("xl24");
                                    td.addElement(Util.maskHTML(value));
                                } else if (displayType == DisplayType.DateTime) {
                                    td.setClass("xl26");
                                    td.addElement(Util.maskHTML(value));
                                } else {
                                    td.addElement(Util.maskHTML(value));
                                }
                            } else
                                td.addElement(Util.maskHTML(value));
                        } else if (obj instanceof PrintData) {
                        //	ignore contained Data
                        } else
                            log.log(Level.SEVERE, "createHTML - Element not PrintData(Element) " + obj.getClass());
                    }
                }
            //	printed
            }
        //	for all columns
        }
        //	for all rows
        table table1 = new table();
        table1.setBorder(0).setCols(10).setCellPadding(0).setCellSpacing(2);
        table1.addElement(new tr().addElement(new td().setColSpan(10).addElement(new h1(re.getName())).setStyle("border:none;")));
        tr tr1;
        String tmp = "Jednostka: ";
        MOrg o = MOrg.get(m_ctx, Env.getAD_Org_ID(m_ctx));
        tmp += o.getName();
        tr1 = new tr();
        tr1.addElement(new td().addElement(tmp).setColSpan(10).setStyle("border:none;"));
        table1.addElement(tr1);
        java.text.DateFormat dateFormat = java.text.DateFormat.getDateInstance(java.text.DateFormat.FULL, new Locale("pl_PL"));
        String dateData = "Czas wygenerowania: " + dateFormat.format(new java.util.Date());
        tr1 = new tr();
        tr1.addElement(new td().addElement(dateData).setColSpan(10).setStyle("border:none;"));
        table1.addElement(tr1);
        for (int r = 0; r < m_query.getRestrictionCount(); r++) {
            tr1 = new tr();
            td td1 = new td().addElement(new i(m_query.getInfoName(r)));
            td1.addElement("   " + m_query.getInfoOperator(r) + "   " + m_query.getInfoDisplayAll(r)).setColSpan(10);
            td1.setStyle("border:none;");
            tr1.addElement(td1);
            table1.addElement(tr1);
        }
        tr1 = new tr();
        tr1.addElement(new td().addElement("&nbsp;").setColSpan(10).setStyle("border:none;height:20px;"));
        table1.addElement(tr1);
        //
        PrintWriter w = new PrintWriter(writer);
        if (onlyTable)
            table.output(w);
        else {
            XhtmlDocument doc = new XhtmlDocument();
            String st = "<!--table " + "	{mso-displayed-decimal-separator:\".\"; " + "	mso-displayed-thousand-separator:\" \";} " + "@page " + "	{margin:.98in .79in .98in .79in; " + "	mso-header-margin:.5in; " + "	mso-footer-margin:.5in;} " + "tr " + "	{mso-height-source:auto;} " + "col " + "	{mso-width-source:auto;} " + "br " + "	{mso-data-placement:same-cell;} " + ".style0 " + "	{mso-number-format:General; " + "	text-align:general; " + "	vertical-align:bottom; " + "	white-space:nowrap; " + "	mso-rotate:0; " + "	mso-background-source:auto; " + "	mso-pattern:auto; " + "	color:windowtext; " + "	font-size:10.0pt; " + "	font-weight:400; " + "	font-style:normal; " + "	text-decoration:none; " + "	font-family:Arial; " + "	mso-generic-font-family:auto; " + "	mso-font-charset:238; " + "	border:none; " + "	mso-protection:locked visible; " + "	mso-style-name:Normalny; " + "	mso-style-id:0;} " + "td " + "	{mso-style-parent:style0; " + "	padding-top:1px; " + "	padding-right:1px; " + "	padding-left:1px; " + "	mso-ignore:padding; " + "	color:windowtext; " + "	font-size:10.0pt; " + "	font-weight:400; " + "	font-style:normal; " + "	text-decoration:none; " + "	font-family:Arial; " + "	mso-generic-font-family:auto; " + "	mso-font-charset:238; " + "	mso-number-format:General; " + "	text-align:general; " + "	vertical-align:bottom; " + //"	border:none; "+
            "border:.5pt solid #a0a0a0; " + //windowtext
            "	mso-background-source:auto; " + "	mso-pattern:auto; " + "	mso-protection:locked visible; " + "	white-space:nowrap; " + "	mso-rotate:0;} " + ".xl24 " + "{mso-style-parent:style0; " + "mso-number-format:\"Short Date\";} " + ".xl25 " + "{mso-style-parent:style0; " + "mso-number-format:Fixed;} " + ".xl26 " + "{mso-style-parent:style0; " + "mso-number-format:\"yy\\/mm\\/dd\\ h\\:mm\\;\\@\";} " + ".xl_head " + "{text-align:center; mso-style-parent:style0; " + "font-weight:700; " + "font-family:Arial, sans-serif; " + "mso-font-charset:238; " + "border:.5pt solid windowtext; " + "background:silver; " + "mso-pattern:auto none;} " + "-->  ";
            doc.appendHead(new style().addElement(st));
            doc.appendBody(table1);
            doc.appendBody(table);
            doc.output(w);
        }
        w.flush();
        w.close();
    } catch (Exception e) {
        log.log(Level.SEVERE, "createHTML(w)", e);
    }
    return false;
}
Also used : Locale(java.util.Locale) org.apache.ecs.xhtml.h1(org.apache.ecs.xhtml.h1) XhtmlDocument(org.apache.ecs.XhtmlDocument) PrintData(org.compiere.print.PrintData) org.apache.ecs.xhtml.table(org.apache.ecs.xhtml.table) PrintWriter(java.io.PrintWriter) MPrintFormatItem(org.compiere.print.MPrintFormatItem) org.apache.ecs.xhtml.i(org.apache.ecs.xhtml.i) MQuery(org.compiere.model.MQuery) FileNotFoundException(java.io.FileNotFoundException) org.apache.ecs.xhtml.td(org.apache.ecs.xhtml.td) MPrintFormat(org.compiere.print.MPrintFormat) MOrg(org.compiere.model.MOrg) org.apache.ecs.xhtml.th(org.apache.ecs.xhtml.th) PrintDataElement(org.compiere.print.PrintDataElement) org.apache.ecs.xhtml.style(org.apache.ecs.xhtml.style) org.apache.ecs.xhtml.tr(org.apache.ecs.xhtml.tr)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)2 PrintWriter (java.io.PrintWriter)2 XhtmlDocument (org.apache.ecs.XhtmlDocument)2 org.apache.ecs.xhtml.table (org.apache.ecs.xhtml.table)2 org.apache.ecs.xhtml.td (org.apache.ecs.xhtml.td)2 org.apache.ecs.xhtml.th (org.apache.ecs.xhtml.th)2 org.apache.ecs.xhtml.tr (org.apache.ecs.xhtml.tr)2 Font (java.awt.Font)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 Locale (java.util.Locale)1 org.apache.ecs.xhtml.a (org.apache.ecs.xhtml.a)1 org.apache.ecs.xhtml.h1 (org.apache.ecs.xhtml.h1)1 org.apache.ecs.xhtml.i (org.apache.ecs.xhtml.i)1 org.apache.ecs.xhtml.link (org.apache.ecs.xhtml.link)1 org.apache.ecs.xhtml.script (org.apache.ecs.xhtml.script)1 org.apache.ecs.xhtml.style (org.apache.ecs.xhtml.style)1 MOrg (org.compiere.model.MOrg)1 MQuery (org.compiere.model.MQuery)1 MPrintFormat (org.compiere.print.MPrintFormat)1