/*********************************************************************** * This file is part of iDempiere ERP Open Source * * http://www.idempiere.org * * * * Copyright (C) Contributors * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License * * as published by the Free Software Foundation; either version 2 * * of the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * * MA 02110-1301, USA. * **********************************************************************/ package org.idempiere.zk.pivot; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; import java.net.URISyntaxException; import java.net.URL; import java.util.Iterator; import java.util.logging.Level; import org.adempiere.exceptions.AdempiereException; import org.compiere.print.MPrintFormat; import org.compiere.print.MPrintFormatItem; import org.compiere.print.PrintData; import org.compiere.print.PrintDataElement; import org.compiere.print.ReportEngine; import org.compiere.util.CLogger; import org.compiere.util.DisplayType; import org.compiere.util.Ini; import org.compiere.util.Language; import org.compiere.util.Msg; import org.compiere.util.Util; import org.idempiere.print.renderer.IReportRenderer; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.osgi.service.component.annotations.Component; import org.zkoss.zk.ui.Executions; import com.google.common.net.MediaType; @Component(service = IReportRenderer.class, immediate = true) public class PivotReportRenderer implements IReportRenderer { private static final CLogger log = CLogger.getCLogger(PivotReportRenderer.class); public PivotReportRenderer() { } @Override public String getId() { return PivotReportRendererConfiguration.ID; } @Override public String getName() { return getId(); } @Override public String getContentType() { return MediaType.HTML_UTF_8.toString(); } @Override public String getFileExtension() { return PivotReportRendererConfiguration.FILE_EXTENSION; } @Override public void renderReport(ReportEngine reportEngine, PivotReportRendererConfiguration configuration) { try { Language lang = configuration.getLanguage(); if (lang == null) lang = Language.getLoginLanguage(); Writer fw = null; if (configuration.getOutputFile() != null) fw = new OutputStreamWriter(new FileOutputStream(configuration.getOutputFile(), false), Ini.getCharset()); else fw = configuration.getOutputWriter(); createHTML (reportEngine, new BufferedWriter(fw), lang, configuration.isExport()); } catch (FileNotFoundException fnfe) { log.log(Level.SEVERE, "(f) - " + fnfe.toString()); } catch (Exception e) { log.log(Level.SEVERE, "(f)", e); throw new AdempiereException(e); } } /** * Write html content to writer * @param reportEngine * @param bw * @param lang * @param export */ private void createHTML(ReportEngine reportEngine, BufferedWriter bw, Language lang, boolean export) { PrintData printData = reportEngine.getPrintData(); MPrintFormat printFormat = reportEngine.getPrintFormat(); PrintWriter w = new PrintWriter(bw); try { if (lang == null) lang = Language.getLoginLanguage(); w.println( """ \s\ """); appendStyle(w, export, "~./js/pivot/pivot.css"); appendScript(w, export, "~./js/pivot/pivot.js"); appendScript(w, export, "~./js/pivot/c3_renderers.js"); w.println( """ """); // Prepare Derived Attributes JSONObject derivedAttributes = new JSONObject(); JSONArray ja = new JSONArray(); // for all rows (-1 = header row) for (int row = 0; row < printData.getRowCount(); row++) { JSONObject jo = new JSONObject(); printData.setRowIndex(row); if (printData.isFunctionRow()) continue; // for all columns for (int col = 0; col < printFormat.getItemCount(); col++) { MPrintFormatItem item = printFormat.getItem(col); if (item.isPrinted()) { var printName = item.getPrintName(lang); if (Util.isEmpty(printName)) continue; Object obj = printData.getNodeByPrintFormatItem(item); if (obj instanceof PrintDataElement) { PrintDataElement pde = (PrintDataElement) obj; String value = pde.getValueDisplay(lang); // formatted // Check if Item is Date if(pde.isDate()) { putPivotAttributes(derivedAttributes, item.getPrintName(lang), lang); value = pde.getValueAsString(); // unformatted Date for Parsing Fix } if (DisplayType.isNumeric(pde.getDisplayType())) value = pde.getValueAsString(); jo.append(printName, value); } } // for all columns } ja.put(jo); } // for all rows w.println(); w.println("
"""); w.flush(); w.close(); } catch (Exception e) { log.log(Level.SEVERE, "(w)", e); } } /** * Add Prepared Date Derivates * @param derivedAttributes * @param dateName * @param language */ private void putPivotAttributes(JSONObject derivedAttributes, String dateName, Language language) { // Prepare Derived Attributes try { derivedAttributes.put( dateName+"("+Msg.translate(language, "CalendarYear")+")", "dateFormat(\""+dateName+"\", \"%y\", true)"); derivedAttributes.put( dateName+"("+Msg.translate(language, "Month") +")", "dateFormat(\""+dateName+"\", \"%m-%n\", true)"); } catch (JSONException e) { log.log(Level.SEVERE, "(w)", e); } } @Override public boolean isBinary() { return false; } @Override public Class getConfigurationType() { return PivotReportRendererConfiguration.class; } /** * If isExport embed css content, otherwise embed css link * @param writer * @param isExport * @param csslink * @throws IOException * @throws URISyntaxException */ private void appendStyle(PrintWriter writer, boolean isExport, String csslink) throws IOException, URISyntaxException{ if (isExport){ // embed extend css by content URL url = getClass().getResource(csslink.replace("~./", "/web/")); writer.println(""); } else { writer.print(""); } } /** * If isExport embed script content, otherwise embed script link * @param writer * @param isExport * @param scriptlink * @throws IOException * @throws URISyntaxException */ private void appendScript(PrintWriter writer, boolean isExport, String scriptlink) throws IOException, URISyntaxException{ if (isExport){ // embed extend css by content URL url = getClass().getResource(scriptlink.replace("~./", "/web/")); writer.println(""); } else { writer.print("