View Javadoc

1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   
6   package ar.com.fdvs.dj.test;
7   
8   import java.awt.Color;
9   import java.io.File;
10  import java.io.FileNotFoundException;
11  import java.io.FileOutputStream;
12  import java.util.ArrayList;
13  import java.util.HashMap;
14  import java.util.List;
15  import java.util.Map;
16  
17  import junit.framework.TestCase;
18  import net.sf.jasperreports.engine.JRDataSource;
19  import net.sf.jasperreports.engine.JRException;
20  import net.sf.jasperreports.engine.JRExporterParameter;
21  import net.sf.jasperreports.engine.JasperPrint;
22  import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
23  import net.sf.jasperreports.engine.export.JRXlsExporter;
24  import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
25  import ar.com.fdvs.dj.core.DynamicJasperHelper;
26  import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
27  import ar.com.fdvs.dj.core.layout.LayoutManager;
28  import ar.com.fdvs.dj.domain.CustomExpression;
29  import ar.com.fdvs.dj.domain.DynamicReport;
30  import ar.com.fdvs.dj.domain.Style;
31  import ar.com.fdvs.dj.domain.builders.ColumnBuilder;
32  import ar.com.fdvs.dj.domain.builders.ColumnBuilderException;
33  import ar.com.fdvs.dj.domain.builders.DynamicReportBuilder;
34  import ar.com.fdvs.dj.domain.constants.Font;
35  import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
36  import ar.com.fdvs.dj.domain.constants.Page;
37  import ar.com.fdvs.dj.domain.constants.Transparency;
38  import ar.com.fdvs.dj.domain.constants.VerticalAlign;
39  import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
40  import ar.com.fdvs.dj.domain.entities.conditionalStyle.ConditionStyleExpression;
41  import ar.com.fdvs.dj.domain.entities.conditionalStyle.ConditionalStyle;
42  
43  /**
44   *
45   * @author rve
46   */
47  public class ConditionalStyleReportTest2 extends TestCase {
48      private DynamicReportBuilder drb;
49  
50      public DynamicReport buildDynamicReport() {
51          return drb.build();
52      }
53  
54      public ConditionalStyleReportTest2() {
55          try {
56              drb = new DynamicReportBuilder();
57              drb.setGrandTotalLegend("Total");
58              drb.setPageSizeAndOrientation(new Page(585, 842));
59              drb.setUseFullPageWidth(true);
60              drb.setAllowDetailSplit(false);
61              drb.setWhenNoData("No data", null, true, true);
62              //drb.setReportName("Test inner crosstab");
63              ArrayList listCondStyle = getConditonalStyles();
64              AbstractColumn columnState1 = ColumnBuilder.getNew()
65                                             .setColumnProperty("1", Integer.class.getName()).setTitle("Sales")
66                                             .setHeaderStyle(getHeaderStyle()).setStyle(getDataStyle())
67                                             .addConditionalStyles(listCondStyle)
68                                             .build();
69              drb.addColumn(columnState1);
70  
71              AbstractColumn columnState2 = ColumnBuilder.getNew()
72                                             .setColumnProperty("2", String.class.getName()).setTitle("Year")
73                                             .setHeaderStyle(getHeaderStyle()).setStyle(getDataStyle())
74                                             .addConditionalStyles(listCondStyle)
75                                             .build();
76              drb.addColumn(columnState2);
77  
78              drb.addField("3", Boolean.class.getName());
79  
80          } catch (ColumnBuilderException ex) {
81              ex.printStackTrace();
82              drb = null;
83          }
84      }
85  
86      private static Style getRedStyle(){
87          Style alertStyle = new Style();
88          alertStyle.setTransparency(Transparency.OPAQUE);
89          alertStyle.setBackgroundColor(Color.RED);
90          alertStyle.setTextColor(Color.BLACK);
91          alertStyle.setVerticalAlign(VerticalAlign.TOP);
92          return alertStyle;
93      }
94  
95      private static Style getBlueStyle(){
96          Style alertStyle = new Style();
97          alertStyle.setTransparency(Transparency.OPAQUE);
98          alertStyle.setBackgroundColor(Color.BLUE);
99          alertStyle.setTextColor(Color.BLACK);
100         alertStyle.setVerticalAlign(VerticalAlign.TOP);
101         return alertStyle;
102     }
103 
104     private static Style getGrayStyle(){
105         Style alertStyle = new Style();
106         alertStyle.setTransparency(Transparency.OPAQUE);
107         alertStyle.setBackgroundColor(Color.LIGHT_GRAY);
108         alertStyle.setTextColor(Color.BLACK);
109         alertStyle.setVerticalAlign(VerticalAlign.TOP);
110         return alertStyle;
111     }
112 
113     private static Style getGreenStyle(){
114         Style alertStyle = new Style();
115         alertStyle.setTransparency(Transparency.OPAQUE);
116         alertStyle.setBackgroundColor(Color.GREEN);
117         alertStyle.setTextColor(Color.BLACK);
118         alertStyle.setVerticalAlign(VerticalAlign.TOP);
119         return alertStyle;
120     }
121 
122     private static Style getYellowStyle(){
123         Style alertStyle = new Style();
124         alertStyle.setTransparency(Transparency.OPAQUE);
125         alertStyle.setBackgroundColor(Color.YELLOW);
126         alertStyle.setTextColor(Color.BLACK);
127         alertStyle.setVerticalAlign(VerticalAlign.TOP);
128         return alertStyle;
129     }
130 
131     private static Style getOrangeStyle(){
132         Style alertStyle = new Style();
133         alertStyle.setTransparency(Transparency.OPAQUE);
134         alertStyle.setBackgroundColor(Color.ORANGE);
135         alertStyle.setTextColor(Color.BLACK);
136         alertStyle.setVerticalAlign(VerticalAlign.TOP);
137         return alertStyle;
138     }
139 
140     private static Style getHeaderStyle(){
141         Style headerStyle = new Style();
142         headerStyle.setFont(Font.ARIAL_MEDIUM_BOLD);
143         headerStyle.setTransparency(Transparency.OPAQUE);
144         headerStyle.setBackgroundColor(Color.BLUE);
145         headerStyle.setTextColor(Color.WHITE);
146         headerStyle.setVerticalAlign(VerticalAlign.TOP);
147 
148         headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
149         return headerStyle;
150     }
151 
152     private static Style getDataStyle(){
153         Style dataStyle = new Style();
154         dataStyle.setTransparency(Transparency.TRANSPARENT);
155         dataStyle.setTextColor(Color.BLACK);
156         dataStyle.setVerticalAlign(VerticalAlign.TOP);
157         return dataStyle;
158     }
159 
160     public static List getList() {
161         List list = new ArrayList();
162 
163         // Add the first line
164         Map result1 = new HashMap();
165         result1.put("1", new Integer(150));
166         result1.put("2", "2007");
167         result1.put("3", "Rouge");
168 
169         list.add(result1);
170 
171         // Add the second line
172         Map result2 = new HashMap();
173         result2.put("1", new Integer(250));
174         result2.put("2", "2008");
175         result2.put("3", "Vert");
176 
177         list.add(result2);
178 
179         // Add the third line
180         Map result3 = new HashMap();
181         result3.put("1", new Integer(203));
182         result3.put("2", String.valueOf("2009"));
183         result3.put("3", "Gris");
184 
185         list.add(result3);
186 
187         return list;
188     }
189 
190     private ArrayList getConditonalStyles() {
191         ArrayList conditionalStyles = new ArrayList();
192         FicheCondition fc = new FicheCondition("3", "Rouge");
193 
194         ConditionalStyle cs = new ConditionalStyle(fc, getRedStyle());
195         conditionalStyles.add(cs);
196 
197         fc = new FicheCondition("3", "Vert");
198 
199         cs = new ConditionalStyle(fc, getGreenStyle());
200         conditionalStyles.add(cs);
201 
202         fc = new FicheCondition("3", "Gris");
203 
204         cs = new ConditionalStyle(fc, getGrayStyle());
205         conditionalStyles.add(cs);
206 
207         return conditionalStyles;
208     }
209 
210     private class FicheCondition extends ConditionStyleExpression implements CustomExpression {
211         private String fieldName;
212         private String colorValue;
213 
214         public FicheCondition(String fieldName, String colorValue) {
215             this.fieldName = fieldName;
216             this.colorValue = colorValue;
217         }
218 
219         public Object evaluate(Map fields, Map variables, Map parameters) {
220             boolean condition = false;
221             Object currentValue = fields.get(fieldName);
222             if (currentValue instanceof String) {
223                 String s = (String)currentValue;
224                 if (s != null) {
225                     condition = colorValue.equals(currentValue);
226                 }
227             }
228             return Boolean.valueOf(condition);
229         }
230 
231         public String getClassName() {
232             return Boolean.class.getName();
233         }
234     }
235 
236     public static LayoutManager getLayoutManager() {
237         return new ClassicLayoutManager();
238     }
239     
240     public void test() throws JRException, FileNotFoundException{
241         ConditionalStyleReportTest2 db = new ConditionalStyleReportTest2();
242         List list = getList();
243         DynamicReport dynamicReport = db.buildDynamicReport();
244                 JRDataSource ds = new JRBeanCollectionDataSource(list);
245                 JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dynamicReport, getLayoutManager(), ds);
246                 JRXlsExporter exporter = new JRXlsExporter();
247 
248                 File outputFile = new File(System.getProperty("user.dir")+ "/target/" + this.getClass().getName() + ".xls");
249                 FileOutputStream fos = new FileOutputStream(outputFile);
250 
251                 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
252                 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fos); //and output stream
253 
254                 //Excel specific parameter
255                 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
256                 exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
257                 exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
258 
259                 exporter.exportReport();
260 
261     }
262 
263     public static void main(String[] args) throws Exception {
264     	ConditionalStyleReportTest2 test = new ConditionalStyleReportTest2();
265     	test.test();
266     }
267 }