KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > reports > PDFCustomerOrdersReport


1 package reports;
2
3 import com.lowagie.text.*;
4 import com.lowagie.text.pdf.*;
5 import java.io.ByteArrayOutputStream JavaDoc;
6 import dinamica.*;
7
8 /**
9  * Customer Profile Report
10  * <br><br>
11  * (c) 2004 Martin Cordova<br>
12  * This code is released under the LGPL license<br>
13  * Dinamica Framework - http://www.martincordova.com
14  * @author Martin Cordova (dinamica@martincordova.com)
15  * */

16 public class PDFCustomerOrdersReport extends AbstractPDFOutput
17 {
18
19     /* (non-Javadoc)
20      * @see dinamica.AbstractPDFOutput#createPDF(dinamica.GenericTransaction, java.io.ByteArrayOutputStream)
21      */

22     protected void createPDF(
23         GenericTransaction data,
24         ByteArrayOutputStream JavaDoc buf)
25         throws Throwable JavaDoc
26     {
27
28         //init pdf - pagesize, margins, etc.
29
Document doc = new Document();
30         PdfWriter docWriter = PdfWriter.getInstance(doc, buf);
31         doc.setPageSize(PageSize.LETTER);
32         
33         //header
34
HeaderFooter header = new HeaderFooter(new Phrase(getHeader()), false);
35         header.setBorder(Rectangle.BOTTOM);
36         header.setAlignment(Rectangle.ALIGN_CENTER);
37         doc.setHeader(header);
38             
39         //footer
40
HeaderFooter footer = new HeaderFooter(new Phrase(getFooter()), true);
41         footer.setBorder(Rectangle.TOP);
42         footer.setAlignment(Rectangle.ALIGN_RIGHT);
43         doc.setFooter(footer);
44
45         doc.open();
46             
47         //title
48
Paragraph t = new Paragraph(getReportTitle(),new Font(Font.HELVETICA, 18f));
49         t.setAlignment(Rectangle.ALIGN_CENTER);
50         doc.add(t);
51
52         //customer basic info
53
PdfPTable custinfo = getCustomerSection(data);
54         doc.add(custinfo);
55
56         //for each master record print a master/detail section
57
MasterDetailReader dataobj = (MasterDetailReader)data;
58         Recordset master = dataobj.getRecordset("master");
59         master.top();
60         while (master.next())
61         {
62             //blank line
63
doc.add(new Paragraph(" "));
64
65             //print master section
66
doc.add( getGroupMaster(master) );
67             
68             //print detail section with subtotal
69
doc.add( getGroupDetail(master, dataobj.getDetail(master)) );
70             
71         }
72         
73         //print grand total
74
doc.add(new Paragraph(" "));
75         doc.add( getTotal(dataobj) );
76         
77         doc.close();
78         docWriter.close();
79         
80     }
81
82     /**
83      * Return a report section formatted as a table
84      * @param data
85      * @return
86      */

87     PdfPTable getCustomerSection(GenericTransaction data) throws Throwable JavaDoc
88     {
89
90         /*
91         * customer basic info
92         */

93         
94         //get recordset
95
Recordset custinfo = data.getRecordset("query.sql");
96         custinfo.first();
97         
98         //cols
99
PdfPTable datatable = new PdfPTable(2);
100             
101         //header
102
datatable.getDefaultCell().setPadding(3);
103         int headerwidths[] = {30, 70}; // percentage
104
datatable.setWidths(headerwidths);
105         datatable.setWidthPercentage(50); // percentage
106
datatable.setHorizontalAlignment(Element.ALIGN_CENTER);
107         datatable.getDefaultCell().setBorderWidth(1);
108         datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
109         
110         PdfPCell c1 = new PdfPCell(new Phrase("Contacto"));
111         c1.setGrayFill(0.9f);
112         datatable.addCell(c1);
113         datatable.addCell(custinfo.getString("contactname"));
114         
115         PdfPCell c2 = new PdfPCell(new Phrase("Empresa"));
116         c2.setGrayFill(0.9f);
117         datatable.addCell(c2);
118         datatable.addCell(custinfo.getString("companyname"));
119         
120         PdfPCell c3 = new PdfPCell(new Phrase("Teléfono"));
121         c3.setGrayFill(0.9f);
122         datatable.addCell(c3);
123         datatable.addCell(custinfo.getString("phone"));
124         
125         PdfPCell c4 = new PdfPCell(new Phrase("País"));
126         c4.setGrayFill(0.9f);
127         datatable.addCell(c4);
128         datatable.addCell(custinfo.getString("country"));
129
130         return datatable;
131         
132     }
133
134     /**
135      * Return group header (master)
136      * @param master Recordset containing master records
137      * @return
138      * @throws Throwable
139      */

140     Paragraph getGroupMaster(Recordset master) throws Throwable JavaDoc
141     {
142
143         String JavaDoc text = "Order #" + master.getString("orderid");
144         text = text + " Date: " + StringUtil.formatDate(master.getDate("orderdate"), "dd-MMM-yyyy");
145         
146         Paragraph t = new Paragraph(text,new Font(Font.HELVETICA, 12f, Font.BOLD));
147         t.setAlignment(Rectangle.ALIGN_LEFT);
148         return t;
149         
150     }
151
152     /**
153      * Return a report section formatted as a table
154      * @param data
155      * @return
156      */

157     PdfPTable getGroupDetail(Recordset master, Recordset detail) throws Throwable JavaDoc
158     {
159
160         //cols
161
PdfPTable datatable = new PdfPTable(6);
162             
163         //header
164
datatable.getDefaultCell().setPadding(1);
165         int headerwidths[] = {35, 20, 10, 10, 10, 15}; // percentage
166
datatable.setWidths(headerwidths);
167         datatable.setWidthPercentage(100); // percentage
168
datatable.setHorizontalAlignment(Element.ALIGN_CENTER);
169         datatable.getDefaultCell().setBorderWidth(1);
170         datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
171         datatable.getDefaultCell().setGrayFill(0.9f);
172         datatable.addCell("Product");
173         datatable.addCell("Category");
174         datatable.addCell("Quantity");
175         datatable.addCell("Unit Price");
176         datatable.addCell("Discount");
177         datatable.addCell("Sub-Total");
178
179         datatable.setHeaderRows(1);
180         datatable.getDefaultCell().setGrayFill(0.0f);
181         datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
182         
183         //orders
184
while (detail.next())
185         {
186             String JavaDoc data = null;
187             
188             datatable.addCell(detail.getString("productname"));
189             datatable.addCell(detail.getString("categoryname"));
190         
191             PdfPCell c1 = new PdfPCell(new Phrase(detail.getString("quantity")));
192             c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
193             datatable.addCell(c1);
194
195             data = StringUtil.formatNumber(detail.getValue("unitprice"),"#,##0.00");
196             PdfPCell c2 = new PdfPCell(new Phrase(data));
197             c2.setHorizontalAlignment(Element.ALIGN_RIGHT);
198             datatable.addCell(c2);
199
200             data = StringUtil.formatNumber(detail.getValue("discount"),"#,##0.00");
201             PdfPCell c3 = new PdfPCell(new Phrase(data));
202             c3.setHorizontalAlignment(Element.ALIGN_RIGHT);
203             datatable.addCell(c3);
204
205             data = StringUtil.formatNumber(detail.getValue("subtotal"),"#,##0.00");
206             PdfPCell c4 = new PdfPCell(new Phrase(data));
207             c4.setHorizontalAlignment(Element.ALIGN_RIGHT);
208             datatable.addCell(c4);
209
210         }
211
212         //total
213
PdfPCell c1 = new PdfPCell(new Phrase("TOTAL", new Font(Font.HELVETICA, 10f, Font.BOLD)));
214         c1.setColspan(5);
215         datatable.addCell(c1);
216
217         PdfPCell c2 = new PdfPCell(new Phrase(StringUtil.formatNumber(master.getValue("total"), "#,##0.00")));
218         c2.setHorizontalAlignment(Element.ALIGN_RIGHT);
219         datatable.addCell(c2);
220
221         return datatable;
222         
223     }
224
225     /**
226      * Return a report section formatted as a table
227      * @param data
228      * @return
229      */

230     PdfPTable getTotal(MasterDetailReader dataobj) throws Throwable JavaDoc
231     {
232
233         //cols
234
PdfPTable datatable = new PdfPTable(2);
235             
236         //header
237
datatable.getDefaultCell().setPadding(1);
238         int headerwidths[] = {85, 15}; // percentage
239
datatable.setWidths(headerwidths);
240         datatable.setWidthPercentage(100); // percentage
241
datatable.setHorizontalAlignment(Element.ALIGN_CENTER);
242         datatable.getDefaultCell().setBorderWidth(1);
243         datatable.getDefaultCell().setGrayFill(0.0f);
244
245         //total
246
Recordset custinfo = dataobj.getRecordset("query.sql");
247         custinfo.first();
248         
249         PdfPCell c1 = new PdfPCell(new Phrase("TOTAL", new Font(Font.HELVETICA, 10f, Font.BOLD)));
250         datatable.addCell(c1);
251
252         PdfPCell c2 = new PdfPCell(new Phrase(StringUtil.formatNumber(custinfo.getValue("total"), "#,##0.00")));
253         c2.setHorizontalAlignment(Element.ALIGN_RIGHT);
254         datatable.addCell(c2);
255
256         return datatable;
257         
258     }
259
260 }
261
Popular Tags