KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > MasterDetailOutput


1 package dinamica;
2
3 import electric.xml.*;
4
5 /**
6  * Generic output module to print master/detail html reports.<br>
7  * It will require a published recordset called "group-master.sql"
8  * and will consume a Transaction that is a subclass of MasterDetailReader.
9  * It can be an instance of this class or an instance of a subclass. If your
10  * master-detail scenario is rather simple, then this generic Transaction class
11  * will be enough to create your report.
12  * <br><br>
13  * (c) 2004 Martin Cordova<br>
14  * This code is released under the LGPL license<br>
15  * Dinamica Framework - http://www.martincordova.com
16  * @author Martin Cordova (dinamica@martincordova.com)
17  * */

18 public class MasterDetailOutput extends GenericOutput
19 {
20
21     /** reference to the master recordset */
22     private Recordset _master = null;
23     
24     /**
25      * Makes available the master recordset to subclasses
26      * of this class, because it may be useful to have it when
27      * overriding the onNewRow(...) method, which does not
28      * receive a reference to the master recordset.
29      * @return
30      */

31     protected Recordset getMaster()
32     {
33         return _master;
34     }
35     
36     /**
37      * Subclass GenericOutput to provide the same features
38      * (all the PRINT commands, etc) and generates the
39      * master/detail section of the template with one level
40      * of grouping.
41      */

42     public void print(TemplateEngine te, GenericTransaction data)
43         throws Throwable JavaDoc
44     {
45         
46         //reuse superclass code
47
super.print(te, data);
48         
49         //retrieve master recordset
50
Recordset master = data.getRecordset("master");
51         _master = master;
52         
53         //load repeatable subtemplate
54
Element root = getConfig().getDocument().getRoot();
55         Element gt = root.getElement("group-template");
56         if (gt==null)
57             throw new Throwable JavaDoc("Element <group-template> not found in config.xml. This element is required by the class dinamica.MasterDetailOutput!");
58         
59         //alternate colors?
60
IRowEvent event = null;
61         String JavaDoc altColors = gt.getAttribute("alternate-colors");
62         if (altColors!=null && altColors.equalsIgnoreCase("true"))
63             event = this;
64         
65         //set group template
66
String JavaDoc section = getResource(gt.getString());
67             
68         //buffer
69
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
70
71         /* use custom locale if available */
72         java.util.Locale JavaDoc l = (java.util.Locale JavaDoc)getSession().getAttribute("dinamica.user.locale");
73         
74         //for every master record build a master/detail section
75
master.top();
76         while (master.next())
77         {
78
79             //get detail rows
80
MasterDetailReader m = (MasterDetailReader)data;
81             Recordset items = m.getDetail(master);
82             
83             //build subpage
84
this.resetRowColor();
85             TemplateEngine t = new TemplateEngine(getContext(),getRequest(), section);
86             t.setRowEventObject(event);
87             t.setLocale(l);
88             
89             //print items count if required
90
t.replace("${fld:detail.recordcount}", String.valueOf(items.getRecordCount()));
91             
92             //replace detail rows
93
t.replace(items,"&nbsp;","rows");
94
95             //replace master record columns
96
t.replace(master,"&nbsp;");
97             
98             //append subpage section
99
buf.append(t.toString());
100             
101         }
102         
103         //replace into main template
104
te.replace("${group}", buf.toString());
105         
106     }
107
108 }
109
Popular Tags