View Javadoc

1   /***
2    * Copyright (c) 2003 held jointly by the individual authors.            
3    *                                                                          
4    * This library is free software; you can redistribute it and/or modify it    
5    * under the terms of the GNU Lesser General Public License as published      
6    * by the Free Software Foundation; either version 2.1 of the License, or 
7    * (at your option) any later version.                                            
8    *                                                                            
9    * This library is distributed in the hope that it will be useful, but 
10   * WITHOUT ANY WARRANTY; with out even the implied warranty of 
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
12   * GNU Lesser General Public License for more details.                                                  
13   *                                                                           
14   * You should have received a copy of the GNU Lesser General Public License   
15   * along with this library;  if not, write to the Free Software Foundation,   
16   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.              
17   *                                                                            
18   * > http://www.gnu.org/copyleft/lesser.html                                  
19   * > http://www.opensource.org/licenses/lgpl-license.php
20   */
21  package net.mlw.vlh.web.tag;
22  
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.LinkedHashMap;
27  import java.util.Map;
28  
29  import javax.servlet.jsp.JspException;
30  
31  import net.mlw.vlh.DefaultListBackedValueList;
32  import net.mlw.vlh.ValueList;
33  import net.mlw.vlh.web.tag.support.ColumnInfo;
34  import net.mlw.vlh.web.tag.support.DisplayProvider;
35  import net.mlw.vlh.web.util.JspUtils;
36  
37  import org.apache.commons.beanutils.PropertyUtils;
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  
41  /***
42   * AAA add locale for format
43   * @author Matthew L. Wilson, Andrej Zachar
44   * @version $Revision: 1.12 $ $Date: 2005/11/23 14:55:49 $
45   */
46  public class InvertedRowTag extends DefaultRowTag
47  {
48  
49     private static final long serialVersionUID = 8608872950341954907L;
50  
51     private static final Log LOGGER = LogFactory.getLog(InvertedRowTag.class);
52  
53     private static final String DEFAULT_FORMAT = "0.00";
54  
55     private Map yAxisMap = new LinkedHashMap();
56  
57     private Map xAxisMap = new LinkedHashMap();
58  
59     private String title;
60  
61     private String format = DEFAULT_FORMAT;
62  
63     public void convertValueList() throws JspException
64     {
65        ValueList vl = getRootTag().getValueList();
66  
67        try
68        {
69  
70           while (vl.hasNext())
71           {
72              Object bean = vl.next();
73              Object xAxis = PropertyUtils.getProperty(bean, "ixaxis");
74              Object yAxis = PropertyUtils.getProperty(bean, "iyaxis");
75              Object value = PropertyUtils.getProperty(bean, "ivalue");
76  
77              xAxisMap.put(xAxis, null);
78  
79              Map map = (Map) yAxisMap.get(yAxis);
80              if (map == null)
81              {
82                 yAxisMap.put(yAxis, map = new HashMap());
83                 map.put("yaxis", yAxis);
84              }
85              map.put(JspUtils.format(xAxis, null, null).toLowerCase().replace(' ', '_'), value);
86           }
87        }
88        catch (Exception e)
89        {
90           LOGGER.error("InvertedRowTag.convertValueList() exception...", e);
91        }
92  
93        //Add all the columns to the tag context.
94        for (Iterator iter = xAxisMap.keySet().iterator(); iter.hasNext();)
95        {
96           String label = JspUtils.format(iter.next(), null, null);
97           addColumnInfo(new ColumnInfo(label, label.toLowerCase().replace(' ', '_'), null, null));
98        }
99  
100       getRootTag().setValueList(new DefaultListBackedValueList(new ArrayList(yAxisMap.values()), vl.getValueListInfo()));
101    }
102 
103    /***
104     * @see javax.servlet.jsp.tagext.Tag#doStartTag()
105     */
106    public int doStartTag() throws JspException
107    {
108       init();
109       convertValueList();
110       return super.doStartTag();
111    }
112 
113    /***
114     * @see javax.servlet.jsp.tagext.IterationTag#doAfterBody()
115     *
116     * @todo figure out why release is not working.
117     */
118    public int doAfterBody() throws JspException
119    {
120 
121       DisplayProvider displayProvider = getDisplayProvider();
122 
123       //If this is the first row, then print the column headers!
124       if (currentRowNumber == 0)
125       {
126          JspUtils.writePrevious(pageContext, displayProvider.getHeaderRowPreProcess());
127          JspUtils.writePrevious(pageContext, displayProvider.getHeaderCellPreProcess(null, null) + title
128                + displayProvider.getHeaderCellPostProcess());
129          for (Iterator iter = getColumns().iterator(); iter.hasNext();)
130          {
131             ColumnInfo info = (ColumnInfo) iter.next();
132             JspUtils.writePrevious(pageContext, displayProvider.getHeaderCellPreProcess(null, null));
133             JspUtils.writePrevious(pageContext, info.getTitle());
134             JspUtils.writePrevious(pageContext, displayProvider.getHeaderCellPostProcess());
135          }
136          JspUtils.writePrevious(pageContext, displayProvider.getHeaderRowPostProcess());
137 
138          getColumns().clear();
139       }
140 
141       if (beanInPageScope != null)
142       {
143          Map bean = (Map) beanInPageScope;
144          String style = getRowStyleClass();
145          pageContext.setAttribute(bean + "Style", style);
146          appendClassCellAttribute(style);
147 
148          JspUtils.writePrevious(pageContext, displayProvider.getRowPreProcess(getCellAttributes()));
149 
150          //Add all the columns to the tag context.
151 
152          JspUtils.writePrevious(pageContext, displayProvider.getCellPreProcess(null) + bean.get("yaxis")
153                + displayProvider.getCellPostProcess());
154          for (Iterator iter = xAxisMap.keySet().iterator(); iter.hasNext();)
155          {
156             String label = JspUtils.format(iter.next(), null, null).toLowerCase().replace(' ', '_');
157 
158             JspUtils.writePrevious(pageContext, displayProvider.getCellPreProcess(null));
159             if (bean.get(label) != null)
160             {
161                JspUtils.writePrevious(pageContext, JspUtils.format(bean.get(label), format, null));
162             }
163             else
164             {
165                JspUtils.writePrevious(pageContext, getRootTag().getConfig().getNullToken());
166             }
167             JspUtils.writePrevious(pageContext, displayProvider.getCellPostProcess());
168          }
169 
170          JspUtils.writePrevious(pageContext, displayProvider.getRowPostProcess());
171          bodyContent.clearBody();
172       }
173 
174       currentRowNumber++;
175       return processIteration();
176    }
177 
178    /***
179     * @see javax.servlet.jsp.tagext.Tag#doEndTag()
180     */
181    public int doEndTag() throws JspException
182    {
183       int result = super.doEndTag();
184       reset();
185       return result;
186    }
187 
188    private void reset()
189    {
190       this.format = DEFAULT_FORMAT;
191       this.title = null;
192       this.xAxisMap.clear();
193       this.yAxisMap.clear();
194    }
195 
196    /***
197     * @see javax.servlet.jsp.tagext.Tag#release()
198     * @todo Create clean up method, call it at doEndTag, check whether is needed
199     *       to be called in processIteration insteed of release method.
200     */
201    public void release()
202    {
203       super.release();
204       reset();
205    }
206 
207    /***
208     * @param title
209     *           The title to set.
210     */
211    public void setTitle(String title)
212    {
213       this.title = title;
214    }
215 
216    /***
217     * @param format
218     *           The format to set.
219     */
220    public void setFormat(String format)
221    {
222       this.format = format;
223    }
224 }