1 20 package net.mlw.vlh.web.tag; 21 22 import java.util.List ; 23 import java.util.Locale ; 24 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.jsp.JspException ; 27 28 import net.mlw.vlh.ValueListInfo; 29 import net.mlw.vlh.web.ValueListConfigBean; 30 import net.mlw.vlh.web.tag.support.ColumnInfo; 31 import net.mlw.vlh.web.tag.support.DisplayProvider; 32 import net.mlw.vlh.web.util.JspUtils; 33 34 import org.apache.commons.beanutils.PropertyUtils; 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 38 47 public class DefaultColumnTag extends BaseColumnTag 49 { 50 51 private static final long serialVersionUID = -1160414311192942253L; 52 53 56 private static final Log LOGGER = LogFactory.getLog(DefaultColumnTag.class); 57 58 59 private String groupKey; 60 61 private String sum; 62 63 64 private Integer defaultSort; 65 66 67 private String property; 68 69 70 private String adapterProperty = null; 71 72 private String format; 73 74 private String defaultValue; 75 76 77 private String emphasisPattern = null; 78 79 80 private Locale locale = null; 81 82 private List nestedColumnInfoList = null; 83 84 87 public String getAdapterProperty() 88 { 89 return adapterProperty == null ? property : adapterProperty; 90 } 91 92 95 public void setAdapterProperty(String sqlProperty) 96 { 97 this.adapterProperty = sqlProperty; 98 } 99 100 105 protected void init() throws JspException 106 { 107 if (bodyContent != null) 108 { 109 bodyContent.clearBody(); 110 } 111 } 112 113 116 public int doStartTag() throws JspException 117 { 118 init(); 119 return super.doStartTag(); 120 } 121 122 125 public int doEndTag() throws JspException 126 { 127 128 ValueListSpaceTag rootTag = (ValueListSpaceTag) JspUtils.getParent(this, ValueListSpaceTag.class); 129 130 DefaultRowTag rowTag = (DefaultRowTag) JspUtils.getParent(this, DefaultRowTag.class); 131 132 ValueListConfigBean config = rootTag.getConfig(); 133 134 appendClassCellAttribute(rowTag.getRowStyleClass()); 135 136 if (locale == null) 137 { 138 locale = config.getLocaleResolver().resolveLocale((HttpServletRequest ) pageContext.getRequest()); 139 } 140 141 if (rowTag.getCurrentRowNumber() == 0) { 143 144 String titleKey = getTitleKey(); 145 String label = (titleKey == null) ? getTitle() : config.getMessageSource().getMessage(titleKey, null, titleKey, locale); 146 147 ColumnInfo columnInfo = new ColumnInfo(config.getDisplayHelper().help(pageContext, label), getAdapterProperty(), defaultSort, 148 getAttributes()); 149 150 String toolTipKey = getToolTipKey(); 153 columnInfo.setToolTip((toolTipKey == null) ? getToolTip() : config.getMessageSource().getMessage(toolTipKey, null, toolTipKey, 154 locale)); 155 156 columnInfo.setNestedList(this.nestedColumnInfoList); 157 158 rowTag.addColumnInfo(columnInfo); 159 } 160 161 DisplayProvider displayProvider = rowTag.getDisplayProvider(); 162 163 StringBuffer sb = new StringBuffer (displayProvider.getCellPreProcess(getCellAttributes())); 164 165 boolean hasBodyContent = false; 166 167 if (displayProvider.doesIncludeBodyContent() && bodyContent != null && bodyContent.getString() != null 168 && bodyContent.getString().trim().length() > 0) 169 { 170 sb.append(bodyContent.getString().trim()); 171 bodyContent.clearBody(); 172 hasBodyContent = true; 173 } 174 175 { 176 if (property != null && rowTag.getBeanName() != null) 177 { 178 try 179 { 180 Object bean = pageContext.getAttribute(rowTag.getBeanName()); 181 if (bean != null) 182 { 183 Object value = null; 184 try 185 { 186 value = PropertyUtils.getProperty(bean, property); 187 } 188 catch (Exception e) 189 { 190 LOGGER.error("<vlh:column> Error getting property='" + property + "' from the iterated JavaBean name='" 193 + rowTag.getBeanName() + "'\n The row's JavaBean was >>>" + bean 194 + "<<<\n Check the syntax or the spelling of the column's property!"); 195 } 196 197 if (value != null) 198 { 199 if (sum != null && value instanceof Number ) 200 { 201 double doubleValue = ((Number ) value).doubleValue(); 202 Double sumValue = (Double ) pageContext.getAttribute(sum); 203 if (sumValue == null) 204 { 205 sumValue = new Double (doubleValue); 206 } 207 else 208 { 209 sumValue = new Double (sumValue.doubleValue() + doubleValue); 210 } 211 pageContext.setAttribute(sum, sumValue); 212 } 213 214 if( ! hasBodyContent) 215 { 216 String formattedValue = JspUtils.format(value, format, locale); 217 218 if (groupKey == null 219 || (config.getCellInterceptor() == null || !config.getCellInterceptor().isHidden(pageContext, groupKey, 220 property, formattedValue))) 221 { 222 sb.append(displayProvider.emphase(formattedValue, getEmphasisPattern(), getColumnStyleClass())); 223 } 224 } 225 226 } 227 else if (!hasBodyContent) 228 { 229 if (LOGGER.isDebugEnabled()) 230 { 231 LOGGER.debug("The property '" + property + "' of the iterated JavaBean '" + bean + "' is null!"); 232 } 233 234 Object nullValue = (defaultValue == null) ? config.getNullToken() : defaultValue; 235 236 if (groupKey == null 237 || (config.getCellInterceptor() == null || !config.getCellInterceptor().isHidden(pageContext, groupKey, 238 property, nullValue))) 239 { 240 sb.append(nullValue); 241 } 242 } 243 } 244 } 245 catch (Exception e) 246 { 247 final String message = "DefaultColumnTag.doEndTag() - <vlh:column> error getting property: " + property + " from bean."; 248 LOGGER.error(message, e); 249 throw new JspException (message, e); 250 } 251 } 252 } 253 254 sb.append(displayProvider.getCellPostProcess()); 255 JspUtils.write(pageContext, sb.toString()); 256 257 release(); 258 259 return EVAL_PAGE; 260 } 261 262 public String getColumnStyleClass() throws JspException 263 { 264 265 ValueListSpaceTag rootTag = (ValueListSpaceTag) JspUtils.getParent(this, ValueListSpaceTag.class); 266 267 ValueListConfigBean config = rootTag.getConfig(); 268 269 if (config == null) 270 { 271 throw new JspException ("No config found on root tag"); 272 } 273 274 return config.getStylePrefix(); 275 } 276 277 283 public void setSortable(String value) 284 { 285 if ("asc".equals(value)) 286 { 287 defaultSort = ValueListInfo.ASCENDING; 288 } 289 else if ("desc".equals(value)) 290 { 291 defaultSort = ValueListInfo.DESCENDING; 292 } 293 } 294 295 298 public String getProperty() 299 { 300 return this.property; 301 } 302 303 309 public void setProperty(String property) 310 { 311 this.property = property; 312 } 313 314 317 public String getFormat() 318 { 319 return this.format; 320 } 321 322 326 public void setFormat(String format) 327 { 328 this.format = format; 329 } 330 331 334 public String getSum() 335 { 336 return this.sum; 337 } 338 339 343 public void setSum(String sum) 344 { 345 this.sum = sum; 346 } 347 348 351 public String getDefault() 352 { 353 return this.defaultValue; 354 } 355 356 359 public void setDefault(String defaultValue) 360 { 361 this.defaultValue = defaultValue; 362 } 363 364 367 public String getEmphasisPattern() 368 { 369 return this.emphasisPattern; 370 } 371 372 377 public void setEmphasisPattern(String emphasisPattern) 378 { 379 this.emphasisPattern = (emphasisPattern == null || emphasisPattern.length() == 0) ? null : emphasisPattern; 380 } 381 382 385 public void setLocale(Locale locale) 386 { 387 this.locale = locale; 388 } 389 390 393 public String getGroupKey() 394 { 395 return this.groupKey; 396 } 397 398 401 public void setGroupKey(String groupKey) 402 { 403 this.groupKey = groupKey; 404 } 405 406 409 public List getNestedColumnInfoList() 410 { 411 return nestedColumnInfoList; 412 } 413 414 417 public void setNestedColumnInfoList(List nestedColumnInfoList) 418 { 419 this.nestedColumnInfoList = nestedColumnInfoList; 420 } 421 422 private void reset() 423 { 424 this.adapterProperty = null; 425 this.defaultSort = null; 426 this.defaultValue = null; 427 this.emphasisPattern = null; 428 this.format = null; 429 this.groupKey = null; 430 this.locale = null; 431 this.property = null; 432 this.sum = null; 433 this.nestedColumnInfoList = null; 434 } 435 436 444 public void release() 445 { 446 super.release(); 447 reset(); 448 } 449 } | Popular Tags |