intive-FDV / DynamicJasper

Dynamic Reports using Jasper Reports
http://intive-fdv.github.io/DynamicJasper/
GNU Lesser General Public License v3.0
244 stars 128 forks source link

DynamicJasperHelper.generateJasperReport vs. DynamicJasperHelper.generateJasperPrint - ConditionalStyles problem #42

Open jhudziak opened 7 years ago

jhudziak commented 7 years ago

Can someone explain me why ConditionStyles not working if we first generate JasperReport from DynamicReport and later fill report and create JasperPrint object?

In ConditionalStyleReportTest2 report is generated by:

JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dynamicReport, getLayoutManager(), ds);

After generation, we can display this report using:

JasperViewer.viewReport(jp);

But If I modify this test and add this code to the end of test() method I get two reports one with style another without.

//Paste this code after exporter.exportReport(); in test() method
JasperViewer.viewReport(jp);
try {
    JasperReport jr = DynamicJasperHelper.generateJasperReport(drb.build(), getLayoutManager(), new HashMap());
    JasperPrint jasperPrint = JasperFillManager.fillReport(jr, new HashMap<String, Object>(), new    JRBeanCollectionDataSource(list));
    JasperViewer.viewReport(jasperPrint);
} catch (JRException e) {
    e.printStackTrace();
}

Is there some reason why styles are not supported when JasperReport is created?

juanalvarezg commented 7 years ago

Are you building twice from the same "drb" instance? that should rise an excepcion since the builder cannot be re-used,

Which version of DJ are you using?

--

Juan Manuel Alvarez Gimenez CTO

Office: +54 11 5239 9899 Celular: +54 9 11 4400 0852 Skype: juan.manuel.alvarez.gimenez Connect me on: LINKEDIN http://ar.linkedin.com/in/jmalvarez

FDVSOLUTIONS | Collective Innovation Avenida Belgrano 1580 2º piso C1093AAA , Buenos Aires, Argentina. www.fdvsolutions.com

This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.

2016-12-13 13:24 GMT-03:00 jhudziak notifications@github.com:

Can someone explain me why ConditionStyles not working if we first generate JasperReport from DynamicReport and later fill report and create JasperPrint object?

In ConditionalStyleReportTest2 report is generated by:

JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dynamicReport, getLayoutManager(), ds);

After generation, we can display this report using:

JasperViewer.viewReport(jp);

But If I modify this test and add this code to the end of test() method I get two reports one with style another without.

//Paste this code after exporter.exportReport(); in test() methodJasperViewer.viewReport(jp);try { JasperReport jr = DynamicJasperHelper.generateJasperReport(drb.build(), getLayoutManager(), new HashMap()); JasperPrint jasperPrint = JasperFillManager.fillReport(jr, new HashMap<String, Object>(), new JRBeanCollectionDataSource(list)); JasperViewer.viewReport(jasperPrint); } catch (JRException e) { e.printStackTrace(); }

Is there some reason why styles are not supported when JasperReport is created?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/FDVSolutions/DynamicJasper/issues/42, or mute the thread https://github.com/notifications/unsubscribe-auth/AA23FzxuEENVP5LEH9aXeZ-233CNnJ22ks5rHsbNgaJpZM4LL7mF .

jhudziak commented 7 years ago

It's not build twice. First report is build from local variable instanitiated from ConditionalStyleReportTest2 db = new ConditionalStyleReportTest2(); and build from db object. But lower i use drb.build() form ConditionalStyleReportTest2 instantieted from main method. So it's correct. Version of DJ is 5.0.11

Whole test method.


 public void test() throws JRException, FileNotFoundException {
        ConditionalStyleReportTest2 db = new ConditionalStyleReportTest2();
        List list = getList();
        DynamicReport dynamicReport = db.buildDynamicReport();
        JRDataSource ds = new JRBeanCollectionDataSource(list);
        JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dynamicReport, getLayoutManager(), ds);
        JRXlsExporter exporter = new JRXlsExporter();

        File outputFile = new File(System.getProperty("user.dir") + "/target/" + this.getClass().getName() + ".xls");
        FileOutputStream fos = new FileOutputStream(outputFile);

        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fos); //and output stream

        //Excel specific parameter
        exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
        exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
        exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);

        exporter.exportReport();
        JasperViewer.viewReport(jp);

        try {
            JasperReport jr = DynamicJasperHelper.generateJasperReport(drb.build(), getLayoutManager(), new HashMap());
            JasperPrint jasperPrint = JasperFillManager.fillReport(jr, new HashMap<String, Object>(), new JRBeanCollectionDataSource(list));
            JasperViewer.viewReport(jasperPrint, false);
        } catch (JRException e) {
            e.printStackTrace();
        }
    }

If you run this code you get something like this: screen shot 2016-12-15 at 09 29 51

juanalvarezg commented 7 years ago

Send me the full file

--

Juan Manuel Alvarez Gimenez CTO

Office: +54 11 5239 9899 Celular: +54 9 11 4400 0852 Skype: juan.manuel.alvarez.gimenez Connect me on: LINKEDIN http://ar.linkedin.com/in/jmalvarez

FDVSOLUTIONS | Collective Innovation Avenida Belgrano 1580 2º piso C1093AAA , Buenos Aires, Argentina. www.fdvsolutions.com

This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.

2016-12-15 5:32 GMT-03:00 jhudziak notifications@github.com:

It's not build twice. First report is build from local variable instanitiated from ConditionalStyleReportTest2 db = new ConditionalStyleReportTest2(); and build from db object. But lower i use drb.build() form ConditionalStyleReportTest2 instantieted from main method. So it's correct. Version of DJ is 5.0.11

Whole test method.

public void test() throws JRException, FileNotFoundException { ConditionalStyleReportTest2 db = new ConditionalStyleReportTest2(); List list = getList(); DynamicReport dynamicReport = db.buildDynamicReport(); JRDataSource ds = new JRBeanCollectionDataSource(list); JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dynamicReport, getLayoutManager(), ds); JRXlsExporter exporter = new JRXlsExporter();

    File outputFile = new File(System.getProperty("user.dir") + "/target/" + this.getClass().getName() + ".xls");
    FileOutputStream fos = new FileOutputStream(outputFile);

    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fos); //and output stream

    //Excel specific parameter
    exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
    exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
    exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);

    exporter.exportReport();
    JasperViewer.viewReport(jp);

    try {
        JasperReport jr = DynamicJasperHelper.generateJasperReport(drb.build(), getLayoutManager(), new HashMap());
        JasperPrint jasperPrint = JasperFillManager.fillReport(jr, new HashMap<String, Object>(), new JRBeanCollectionDataSource(list));
        JasperViewer.viewReport(jasperPrint, false);
    } catch (JRException e) {
        e.printStackTrace();
    }
}

If you run this code you get something like this: [image: screen shot 2016-12-15 at 09 29 51] https://cloud.githubusercontent.com/assets/1451540/21216803/1ae342e8-c2a9-11e6-9634-37221108292b.png

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/FDVSolutions/DynamicJasper/issues/42#issuecomment-267268343, or mute the thread https://github.com/notifications/unsubscribe-auth/AA23F77asNQEL0wp10aFbv8lK4dnmLAmks5rIPsvgaJpZM4LL7mF .

jhudziak commented 7 years ago
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.fdvs.dj.test;

import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import ar.com.fdvs.dj.core.DynamicJasperHelper;
import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
import ar.com.fdvs.dj.core.layout.LayoutManager;
import ar.com.fdvs.dj.domain.CustomExpression;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.Style;
import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
import ar.com.fdvs.dj.domain.builders.ColumnBuilderException;
import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
import ar.com.fdvs.dj.domain.constants.Font;
import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
import ar.com.fdvs.dj.domain.constants.Page;
import ar.com.fdvs.dj.domain.constants.Transparency;
import ar.com.fdvs.dj.domain.constants.VerticalAlign;
import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
import ar.com.fdvs.dj.domain.entities.conditionalStyle.ConditionStyleExpression;
import ar.com.fdvs.dj.domain.entities.conditionalStyle.ConditionalStyle;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author rve
 */
public class ConditionalStyleReportTest2 extends TestCase {

    private DynamicReportBuilder drb;

    public DynamicReport buildDynamicReport() {
        return drb.build();
    }

    public ConditionalStyleReportTest2() {
        try {
            drb = new DynamicReportBuilder();
            drb.setGrandTotalLegend("Total");
            drb.setPageSizeAndOrientation(new Page(585, 842));
            drb.setUseFullPageWidth(true);
            drb.setAllowDetailSplit(false);
            drb.setWhenNoData("No data", null, true, true);
            //drb.setReportName("Test inner crosstab");
            ArrayList listCondStyle = getConditonalStyles();
            AbstractColumn columnState1 = ColumnBuilder.getNew()
                    .setColumnProperty("1", Integer.class.getName()).setTitle("Sales")
                    .setHeaderStyle(getHeaderStyle()).setStyle(getDataStyle())
                    .addConditionalStyles(listCondStyle)
                    .build();
            drb.addColumn(columnState1);

            AbstractColumn columnState2 = ColumnBuilder.getNew()
                    .setColumnProperty("2", String.class.getName()).setTitle("Year")
                    .setHeaderStyle(getHeaderStyle()).setStyle(getDataStyle())
                    .addConditionalStyles(listCondStyle)
                    .build();
            drb.addColumn(columnState2);

            drb.addField("3", Boolean.class.getName());

        } catch (ColumnBuilderException ex) {
            ex.printStackTrace();
            drb = null;
        }
    }

    private static Style getRedStyle() {
        Style alertStyle = new Style();
        alertStyle.setTransparency(Transparency.OPAQUE);
        alertStyle.setBackgroundColor(Color.RED);
        alertStyle.setTextColor(Color.BLACK);
        alertStyle.setVerticalAlign(VerticalAlign.TOP);
        return alertStyle;
    }

    private static Style getBlueStyle() {
        Style alertStyle = new Style();
        alertStyle.setTransparency(Transparency.OPAQUE);
        alertStyle.setBackgroundColor(Color.BLUE);
        alertStyle.setTextColor(Color.BLACK);
        alertStyle.setVerticalAlign(VerticalAlign.TOP);
        return alertStyle;
    }

    private static Style getGrayStyle() {
        Style alertStyle = new Style();
        alertStyle.setTransparency(Transparency.OPAQUE);
        alertStyle.setBackgroundColor(Color.LIGHT_GRAY);
        alertStyle.setTextColor(Color.BLACK);
        alertStyle.setVerticalAlign(VerticalAlign.TOP);
        return alertStyle;
    }

    private static Style getGreenStyle() {
        Style alertStyle = new Style();
        alertStyle.setTransparency(Transparency.OPAQUE);
        alertStyle.setBackgroundColor(Color.GREEN);
        alertStyle.setTextColor(Color.BLACK);
        alertStyle.setVerticalAlign(VerticalAlign.TOP);
        return alertStyle;
    }

    private static Style getYellowStyle() {
        Style alertStyle = new Style();
        alertStyle.setTransparency(Transparency.OPAQUE);
        alertStyle.setBackgroundColor(Color.YELLOW);
        alertStyle.setTextColor(Color.BLACK);
        alertStyle.setVerticalAlign(VerticalAlign.TOP);
        return alertStyle;
    }

    private static Style getOrangeStyle() {
        Style alertStyle = new Style();
        alertStyle.setTransparency(Transparency.OPAQUE);
        alertStyle.setBackgroundColor(Color.ORANGE);
        alertStyle.setTextColor(Color.BLACK);
        alertStyle.setVerticalAlign(VerticalAlign.TOP);
        return alertStyle;
    }

    private static Style getHeaderStyle() {
        Style headerStyle = new Style();
        headerStyle.setFont(Font.ARIAL_MEDIUM_BOLD);
        headerStyle.setTransparency(Transparency.OPAQUE);
        headerStyle.setBackgroundColor(Color.BLUE);
        headerStyle.setTextColor(Color.WHITE);
        headerStyle.setVerticalAlign(VerticalAlign.TOP);

        headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
        return headerStyle;
    }

    private static Style getDataStyle() {
        Style dataStyle = new Style();
        dataStyle.setTransparency(Transparency.TRANSPARENT);
        dataStyle.setTextColor(Color.BLACK);
        dataStyle.setVerticalAlign(VerticalAlign.TOP);
        return dataStyle;
    }

    public static List getList() {
        List list = new ArrayList();

        // Add the first line
        Map result1 = new HashMap();
        result1.put("1", new Integer(150));
        result1.put("2", "2007");
        result1.put("3", "Rouge");

        list.add(result1);

        // Add the second line
        Map result2 = new HashMap();
        result2.put("1", new Integer(250));
        result2.put("2", "2008");
        result2.put("3", "Vert");

        list.add(result2);

        // Add the third line
        Map result3 = new HashMap();
        result3.put("1", new Integer(203));
        result3.put("2", String.valueOf("2009"));
        result3.put("3", "Gris");

        list.add(result3);

        return list;
    }

    private ArrayList getConditonalStyles() {
        ArrayList conditionalStyles = new ArrayList();
        FicheCondition fc = new FicheCondition("3", "Rouge");

        ConditionalStyle cs = new ConditionalStyle(fc, getRedStyle());
        conditionalStyles.add(cs);

        fc = new FicheCondition("3", "Vert");

        cs = new ConditionalStyle(fc, getGreenStyle());
        conditionalStyles.add(cs);

        fc = new FicheCondition("3", "Gris");

        cs = new ConditionalStyle(fc, getGrayStyle());
        conditionalStyles.add(cs);

        return conditionalStyles;
    }

    private class FicheCondition extends ConditionStyleExpression implements CustomExpression {

        private String fieldName;
        private String colorValue;

        public FicheCondition(String fieldName, String colorValue) {
            this.fieldName = fieldName;
            this.colorValue = colorValue;
        }

        public Object evaluate(Map fields, Map variables, Map parameters) {
            boolean condition = false;
            Object currentValue = fields.get(fieldName);
            if (currentValue instanceof String) {
                String s = (String) currentValue;
                if (s != null) {
                    condition = colorValue.equals(currentValue);
                }
            }
            return Boolean.valueOf(condition);
        }

        public String getClassName() {
            return Boolean.class.getName();
        }
    }

    public static LayoutManager getLayoutManager() {
        return new ClassicLayoutManager();
    }

    public void test() throws JRException, FileNotFoundException {
        ConditionalStyleReportTest2 db = new ConditionalStyleReportTest2();
        List list = getList();
        DynamicReport dynamicReport = db.buildDynamicReport();
        JRDataSource ds = new JRBeanCollectionDataSource(list);
        JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dynamicReport, getLayoutManager(), ds);
        JRXlsExporter exporter = new JRXlsExporter();

        File outputFile = new File(System.getProperty("user.dir") + "/target/" + this.getClass().getName() + ".xls");
        FileOutputStream fos = new FileOutputStream(outputFile);

        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fos); //and output stream

        //Excel specific parameter
        exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
        exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
        exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);

        exporter.exportReport();
        JasperViewer.viewReport(jp);

        try {
            JasperReport jr = DynamicJasperHelper.generateJasperReport(drb.build(), getLayoutManager(), new HashMap());
            JasperPrint jasperPrint = JasperFillManager.fillReport(jr, new HashMap<String, Object>(), new JRBeanCollectionDataSource(list));
            JasperViewer.viewReport(jasperPrint, false);

        } catch (JRException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws Exception {
        ConditionalStyleReportTest2 test = new ConditionalStyleReportTest2();
        test.test();

    }
}
juanalvarezg commented 7 years ago

In this code you are mixing 2 objects:

The object that gets properly initialized is "db".

In this line

JasperReport jr = DynamicJasperHelper.generateJasperReport(drb.build(), getLayoutManager(), new HashMap());

You are using the drb from "this" not from "db", in the 2nd case, it will fail because it was used twice

--

Juan Manuel Alvarez Gimenez - CTO Office: +54 11 5239 9899, Cel: +54-9-11-4400-0852, Skype: juan.manuel.alvarez.gimenez, Connect me on: LINKEDIN http://ar.linkedin.com/in/jmalvarez FDVSOLUTIONS | Collective Innovation :: Avenida Belgrano 1580 2º piso C1093AAA , Buenos Aires, Argentina. www.fdvsolutions.com

This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.

2016-12-29 6:18 GMT-03:00 jhudziak notifications@github.com:

/ To change this template, choose Tools | Templates and open the template in the editor. /package ar.com.fdvs.dj.test; import java.awt.Color;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map; import junit.framework.TestCase;import net.sf.jasperreports.engine.JRDataSource;import net.sf.jasperreports.engine.JRException;import net.sf.jasperreports.engine.JRExporterParameter;import net.sf.jasperreports.engine.JasperPrint;import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;import net.sf.jasperreports.engine.export.JRXlsExporter;import net.sf.jasperreports.engine.export.JRXlsExporterParameter;import ar.com.fdvs.dj.core.DynamicJasperHelper;import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;import ar.com.fdvs.dj.core.layout.LayoutManager;import ar.com.fdvs.dj.domain.CustomExpression;import ar.com.fdvs.dj.domain.DynamicReport;import ar.com.fdvs.dj.domain.Style;import ar.com.fdvs.dj.domain.builders.ColumnBuilder;import ar.com.fdvs.dj.domain.builders.ColumnBuilderException;import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;import ar.com.fdvs.dj.domain.constants.Font;import ar.com.fdvs.dj.domain.constants.HorizontalAlign;import ar.com.fdvs.dj.domain.constants.Page;import ar.com.fdvs.dj.domain.constants.Transparency;import ar.com.fdvs.dj.domain.constants.VerticalAlign;import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;import ar.com.fdvs.dj.domain.entities.conditionalStyle.ConditionStyleExpression;import ar.com.fdvs.dj.domain.entities.conditionalStyle.ConditionalStyle;import net.sf.jasperreports.engine.JasperFillManager;import net.sf.jasperreports.engine.JasperReport;import net.sf.jasperreports.view.JasperViewer; /* @author rve /public class ConditionalStyleReportTest2 extends TestCase {

private DynamicReportBuilder drb;

public DynamicReport buildDynamicReport() {
    return drb.build();
}

public ConditionalStyleReportTest2() {
    try {
        drb = new DynamicReportBuilder();
        drb.setGrandTotalLegend("Total");
        drb.setPageSizeAndOrientation(new Page(585, 842));
        drb.setUseFullPageWidth(true);
        drb.setAllowDetailSplit(false);
        drb.setWhenNoData("No data", null, true, true);
        //drb.setReportName("Test inner crosstab");
        ArrayList listCondStyle = getConditonalStyles();
        AbstractColumn columnState1 = ColumnBuilder.getNew()
                .setColumnProperty("1", Integer.class.getName()).setTitle("Sales")
                .setHeaderStyle(getHeaderStyle()).setStyle(getDataStyle())
                .addConditionalStyles(listCondStyle)
                .build();
        drb.addColumn(columnState1);

        AbstractColumn columnState2 = ColumnBuilder.getNew()
                .setColumnProperty("2", String.class.getName()).setTitle("Year")
                .setHeaderStyle(getHeaderStyle()).setStyle(getDataStyle())
                .addConditionalStyles(listCondStyle)
                .build();
        drb.addColumn(columnState2);

        drb.addField("3", Boolean.class.getName());

    } catch (ColumnBuilderException ex) {
        ex.printStackTrace();
        drb = null;
    }
}

private static Style getRedStyle() {
    Style alertStyle = new Style();
    alertStyle.setTransparency(Transparency.OPAQUE);
    alertStyle.setBackgroundColor(Color.RED);
    alertStyle.setTextColor(Color.BLACK);
    alertStyle.setVerticalAlign(VerticalAlign.TOP);
    return alertStyle;
}

private static Style getBlueStyle() {
    Style alertStyle = new Style();
    alertStyle.setTransparency(Transparency.OPAQUE);
    alertStyle.setBackgroundColor(Color.BLUE);
    alertStyle.setTextColor(Color.BLACK);
    alertStyle.setVerticalAlign(VerticalAlign.TOP);
    return alertStyle;
}

private static Style getGrayStyle() {
    Style alertStyle = new Style();
    alertStyle.setTransparency(Transparency.OPAQUE);
    alertStyle.setBackgroundColor(Color.LIGHT_GRAY);
    alertStyle.setTextColor(Color.BLACK);
    alertStyle.setVerticalAlign(VerticalAlign.TOP);
    return alertStyle;
}

private static Style getGreenStyle() {
    Style alertStyle = new Style();
    alertStyle.setTransparency(Transparency.OPAQUE);
    alertStyle.setBackgroundColor(Color.GREEN);
    alertStyle.setTextColor(Color.BLACK);
    alertStyle.setVerticalAlign(VerticalAlign.TOP);
    return alertStyle;
}

private static Style getYellowStyle() {
    Style alertStyle = new Style();
    alertStyle.setTransparency(Transparency.OPAQUE);
    alertStyle.setBackgroundColor(Color.YELLOW);
    alertStyle.setTextColor(Color.BLACK);
    alertStyle.setVerticalAlign(VerticalAlign.TOP);
    return alertStyle;
}

private static Style getOrangeStyle() {
    Style alertStyle = new Style();
    alertStyle.setTransparency(Transparency.OPAQUE);
    alertStyle.setBackgroundColor(Color.ORANGE);
    alertStyle.setTextColor(Color.BLACK);
    alertStyle.setVerticalAlign(VerticalAlign.TOP);
    return alertStyle;
}

private static Style getHeaderStyle() {
    Style headerStyle = new Style();
    headerStyle.setFont(Font.ARIAL_MEDIUM_BOLD);
    headerStyle.setTransparency(Transparency.OPAQUE);
    headerStyle.setBackgroundColor(Color.BLUE);
    headerStyle.setTextColor(Color.WHITE);
    headerStyle.setVerticalAlign(VerticalAlign.TOP);

    headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
    return headerStyle;
}

private static Style getDataStyle() {
    Style dataStyle = new Style();
    dataStyle.setTransparency(Transparency.TRANSPARENT);
    dataStyle.setTextColor(Color.BLACK);
    dataStyle.setVerticalAlign(VerticalAlign.TOP);
    return dataStyle;
}

public static List getList() {
    List list = new ArrayList();

    // Add the first line
    Map result1 = new HashMap();
    result1.put("1", new Integer(150));
    result1.put("2", "2007");
    result1.put("3", "Rouge");

    list.add(result1);

    // Add the second line
    Map result2 = new HashMap();
    result2.put("1", new Integer(250));
    result2.put("2", "2008");
    result2.put("3", "Vert");

    list.add(result2);

    // Add the third line
    Map result3 = new HashMap();
    result3.put("1", new Integer(203));
    result3.put("2", String.valueOf("2009"));
    result3.put("3", "Gris");

    list.add(result3);

    return list;
}

private ArrayList getConditonalStyles() {
    ArrayList conditionalStyles = new ArrayList();
    FicheCondition fc = new FicheCondition("3", "Rouge");

    ConditionalStyle cs = new ConditionalStyle(fc, getRedStyle());
    conditionalStyles.add(cs);

    fc = new FicheCondition("3", "Vert");

    cs = new ConditionalStyle(fc, getGreenStyle());
    conditionalStyles.add(cs);

    fc = new FicheCondition("3", "Gris");

    cs = new ConditionalStyle(fc, getGrayStyle());
    conditionalStyles.add(cs);

    return conditionalStyles;
}

private class FicheCondition extends ConditionStyleExpression implements CustomExpression {

    private String fieldName;
    private String colorValue;

    public FicheCondition(String fieldName, String colorValue) {
        this.fieldName = fieldName;
        this.colorValue = colorValue;
    }

    public Object evaluate(Map fields, Map variables, Map parameters) {
        boolean condition = false;
        Object currentValue = fields.get(fieldName);
        if (currentValue instanceof String) {
            String s = (String) currentValue;
            if (s != null) {
                condition = colorValue.equals(currentValue);
            }
        }
        return Boolean.valueOf(condition);
    }

    public String getClassName() {
        return Boolean.class.getName();
    }
}

public static LayoutManager getLayoutManager() {
    return new ClassicLayoutManager();
}

public void test() throws JRException, FileNotFoundException {
    ConditionalStyleReportTest2 db = new ConditionalStyleReportTest2();
    List list = getList();
    DynamicReport dynamicReport = db.buildDynamicReport();
    JRDataSource ds = new JRBeanCollectionDataSource(list);
    JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dynamicReport, getLayoutManager(), ds);
    JRXlsExporter exporter = new JRXlsExporter();

    File outputFile = new File(System.getProperty("user.dir") + "/target/" + this.getClass().getName() + ".xls");
    FileOutputStream fos = new FileOutputStream(outputFile);

    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fos); //and output stream

    //Excel specific parameter
    exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
    exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
    exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);

    exporter.exportReport();
    JasperViewer.viewReport(jp);

    try {
        JasperReport jr = DynamicJasperHelper.generateJasperReport(drb.build(), getLayoutManager(), new HashMap());
        JasperPrint jasperPrint = JasperFillManager.fillReport(jr, new HashMap<String, Object>(), new JRBeanCollectionDataSource(list));
        JasperViewer.viewReport(jasperPrint, false);

    } catch (JRException e) {
        e.printStackTrace();
    }

}

public static void main(String[] args) throws Exception {
    ConditionalStyleReportTest2 test = new ConditionalStyleReportTest2();
    test.test();

}

}

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/FDVSolutions/DynamicJasper/issues/42#issuecomment-269602270, or mute the thread https://github.com/notifications/unsubscribe-auth/AA23F7Hv86tHxiyna2OBcMafTFv55kyGks5rM3rmgaJpZM4LL7mF .

jhudziak commented 7 years ago

Why do you think

db properly initialized and drb not?

On drb i call build(), the same operation is in buildDynamicReport() method called in line 247.

What do you mean by writing

it will fail because it was used twice

This code run properly, open two views but one without styles. Code above work same as this

try {
            DynamicReport buildDynamicReport = this.buildDynamicReport();
            JasperReport jr = DynamicJasperHelper.generateJasperReport(buildDynamicReport, getLayoutManager(), new HashMap());
            JasperPrint jasperPrint = JasperFillManager.fillReport(jr, new HashMap<String, Object>(), new JRBeanCollectionDataSource(list));
            JasperViewer.viewReport(jasperPrint, false);
        } catch (JRException e) {
            e.printStackTrace();
        }

Did you try to run this?