1 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 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 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 106 public int doStartTag() throws JspException 107 { 108 init(); 109 convertValueList(); 110 return super.doStartTag(); 111 } 112 113 118 public int doAfterBody() throws JspException 119 { 120 121 DisplayProvider displayProvider = getDisplayProvider(); 122 123 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 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 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 201 public void release() 202 { 203 super.release(); 204 reset(); 205 } 206 207 211 public void setTitle(String title) 212 { 213 this.title = title; 214 } 215 216 220 public void setFormat(String format) 221 { 222 this.format = format; 223 } 224 } | Popular Tags |