KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > GenericOutput


1 package dinamica;
2
3 /**
4  * Base class to create Output classes. This kind of classes
5  * represent the "view" part of this framework MVC mechanism.
6  * This class consumes recordsets published by transactions, which
7  * have been previously executed by the controller.<br>
8  * <br>
9  * Creation date: 4/10/2003<br>
10  * Last Update: 4/10/2003<br>
11  * (c) 2003 Martin Cordova<br>
12  * This code is released under the LGPL license<br>
13  * @author Martin Cordova
14  */

15 public class GenericOutput
16     extends AbstractModule
17     implements IRowEvent
18 {
19
20     /* flag used to alternate colors (method onNewRow) */
21     private int rowColor = 0;
22
23     /**
24      * Generate text-based output using automatic data binding.<br>
25      * This class will consume Recordsets from the "data" object
26      * passed to this method, which is a Transaction that has been
27      * executed before calling the method.<br>
28      * This methods performs the data binding between the templates
29      * and the recordsets, according to the parameters defined in the
30      * config.xml file - it is a kind of VB DataControl, if you will!
31      * All its functionality relies on the TemplateEngine class.
32      * Since this method performs a lot of work, all descendants of this class that
33      * want to reimplement this method, should call super.print(TemplateEngine, Transaction) in the first line
34      * of the reimplemented method in order to reuse all this functionality.
35      * @param te TemplateEngine containing the text based template ready for data binding
36      * @param data Transaction object that publishes one or more recordsets
37      * @throws Throwable
38      */

39     public void print(TemplateEngine te, GenericTransaction data) throws Throwable JavaDoc
40     {
41         
42         /* for each print command */
43         if (data!=null)
44         {
45             Recordset rs = _config.getPrintCommands();
46             
47             while (rs.next())
48             {
49                 
50                 String JavaDoc nullExpr = null;
51                 if (_config.contentType.equals("text/html"))
52                     nullExpr = "&nbsp;";
53                 else
54                     nullExpr = "";
55                     
56                 // patch 2004-08-31 - set pagesize via request parameter (optional)
57
String JavaDoc pagesize = getRequest().getParameter("pagesize");
58                 if (pagesize!=null && pagesize.trim().equals(""))
59                     pagesize=null;
60                 
61                 if (pagesize==null)
62                     pagesize = (String JavaDoc)rs.getValue("pagesize");
63                 // end patch
64

65                 String JavaDoc mode = (String JavaDoc)rs.getValue("mode");
66                 String JavaDoc tag = (String JavaDoc)rs.getValue("tag");
67                 String JavaDoc control = (String JavaDoc)rs.getValue("control");
68                 String JavaDoc rsname = (String JavaDoc)rs.getValue("recordset");
69                 String JavaDoc altColors = (String JavaDoc)rs.getValue("alternate-colors");
70                 String JavaDoc nullValue = (String JavaDoc)rs.getValue("null-value");
71                 
72                 if (nullValue!=null)
73                     nullExpr = nullValue;
74                     
75                 if (mode.equals("table"))
76                 {
77                     Recordset x = data.getRecordset(rsname);
78                     if (pagesize!=null && !rs.isNull("pagesize"))
79                     {
80                         int page = 0;
81                         
82                         // patch 2004-08-31 - mantain original pagesize if available
83
if (x.getPageCount()==0)
84                             x.setPageSize(Integer.parseInt(pagesize));
85                         // end patch
86

87                         String JavaDoc pageNumber = getRequest().getParameter("pagenumber");
88                         if (pageNumber==null || pageNumber.equals(""))
89                         {
90                             page = x.getPageNumber();
91                             if (page==0)
92                                 page = 1;
93                         }
94                         else
95                         {
96                             page = Integer.parseInt(pageNumber);
97                         }
98                         x = x.getPage(page);
99                     }
100
101                     if (altColors!=null && altColors.equals("true"))
102                         te.setRowEventObject(this);
103                         
104                     te.replace(x,nullExpr,tag);
105                 }
106                 else if (mode.equals("form"))
107                 {
108                     nullExpr = "";
109                     Recordset x = data.getRecordset(rsname);
110                     if (x.getRecordCount()>0 && x.getRecordNumber()<0)
111                         x.first();
112                         
113                     //PATCH 2005-03-01 - enhance error message if recordset is empty
114
if (x.getRecordCount()==0)
115                         throw new Throwable JavaDoc("Recordset [" + rsname + "] has no records; can't print (mode=form) using an empty Recordset.");
116                         
117                     te.replace(x, nullExpr);
118                 }
119                 else if (mode.equals("combo"))
120                 {
121                     if (control==null)
122                         throw new Throwable JavaDoc("'control' attribute cannot be null when print-mode='combo'");
123                     Recordset x = data.getRecordset(rsname);
124                     if (x.getRecordCount()>1)
125                         te.setComboValue(control,x);
126                     else
127                         if (x.getRecordCount()>0 && x.getRecordNumber()<0)
128                             x.first();
129                         te.setComboValue(control,String.valueOf(x.getValue(control)));
130                 }
131                 else if (mode.equals("checkbox"))
132                 {
133                     if (control==null)
134                         throw new Throwable JavaDoc("'control' attribute cannot be null when print-mode='checkbox'");
135                     Recordset x = data.getRecordset(rsname);
136                     te.setCheckbox(control,x);
137                 }
138                 else if (mode.equals("radio"))
139                 {
140                     if (control==null)
141                         throw new Throwable JavaDoc("'control' attribute cannot be null when print-mode='radio'");
142                     Recordset x = data.getRecordset(rsname);
143                     if (x.getRecordCount()>0 && x.getRecordNumber()<0)
144                         x.first();
145                     te.setRadioButton(control, String.valueOf(x.getValue(control)));
146                 }
147                 else if (mode.equals("clear"))
148                 {
149                     te.clearFieldMarkers();
150                 }
151                 else
152                 {
153                     throw new Throwable JavaDoc("Unkown print mode attribute in config.xml: " + _config.path);
154                 }
155                 
156             }
157             
158         }
159         
160     }
161     
162     /**
163      * This method is called for non text based output (images, binaries, etc.).
164      * Reimplementations of this method MUST write the output
165      * thru the Servlet OutputStream.
166      * @param data Transaction object
167      * @throws Throwable
168      */

169     public void print(GenericTransaction data) throws Throwable JavaDoc
170     {
171     }
172
173     /**
174      * Implementation of the interface dinamica.IRowEvent.<br>
175      * This code is used to alternate row colors,
176      * the row template must include the special
177      * field marker ${fld:_rowStyle} which will be replaced
178      * by the style parameters set in web.xml.
179      * @see dinamica.IRowEvent#onNewRow(dinamica.Recordset, int, java.lang.String)
180      */

181     public String JavaDoc onNewRow(Recordset rs, String JavaDoc rowTemplate) throws Throwable JavaDoc
182     {
183
184         /*
185          * This code is used to alternate row colors,
186          * the row template must include the special
187          * field marker ${fld:_rowStyle} which will be replaced
188          * by the style parameters set in web.xml.
189          */

190         
191         String JavaDoc style1 = getContext().getInitParameter("def-color1");
192         String JavaDoc style2 = getContext().getInitParameter("def-color2");
193         String JavaDoc currentStyle="";
194         
195         if (rowColor==0)
196         {
197             rowColor=1;
198             currentStyle = style1;
199         }
200         else
201         {
202             rowColor=0;
203             currentStyle = style2;
204         }
205         
206         rowTemplate = StringUtil.replace(rowTemplate, "${fld:_rowStyle}", currentStyle);
207         
208         return rowTemplate;
209
210     }
211
212     /**
213      * resets value of private variable rowColor. This
214      * variable control alternate printing of colors
215      * for tables (print mode="table")
216      *
217      */

218     protected void resetRowColor()
219     {
220         this.rowColor = 0;
221     }
222
223 }
224
Popular Tags