📄 totalhandler.java
字号:
/* * Copyright 2004 original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.extremecomponents.table.handler;import java.math.BigDecimal;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import org.apache.commons.beanutils.PropertyUtils;import org.apache.commons.lang.StringUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.extremecomponents.table.bean.Column;import org.extremecomponents.table.core.BaseModel;import org.extremecomponents.util.ExtremeUtils;/** * @author phorn */public class TotalHandler { private static Log logger = LogFactory.getLog(TotalHandler.class); private static final BigDecimal ZERO = new BigDecimal("0"); private BaseModel model; private Map totals; public TotalHandler(BaseModel model) { this.model = model; } public Map getTotals() { return this.totals; } public void setTotals(Map totals) { this.totals = totals; } public void buildTotals(Collection rows) throws Exception { Map result = initializeTotalsMap(model.getColumnHandler()); if (result == null) { return; } if (logger.isDebugEnabled()) { logger.debug("TotalHandler.buildTotals() - iterating keys for " + result); } iterateColumns(result, rows); if (logger.isDebugEnabled()) { logger.debug("TotalHandler.buildTotals() - totals = " + result); } this.totals = result; } private Map initializeTotalsMap(ColumnHandler columnHandler) throws Exception { Map result = null; if (columnHandler == null) { return result; } for (Iterator iter = columnHandler.getColumns().iterator(); iter.hasNext();) { Column column = (Column) iter.next(); if (column.showTotal()) { if (result == null) { result = new HashMap(); } result.put(column.getProperty(), ZERO); } } return result; } private void iterateColumns(Map totals, Collection rows) throws Exception { for (Iterator iter = totals.keySet().iterator(); iter.hasNext();) { String property = (String) iter.next(); iterateRows(totals, rows, property); } } private void iterateRows(Map totals, Collection rows, String property) throws Exception { for (Iterator listIter = rows.iterator(); listIter.hasNext();) { Object row = listIter.next(); Object colVal = null; if (ExtremeUtils.isBeanPropertyReadable(row, property)) { colVal = PropertyUtils.getProperty(row, property); } if (colVal == null) { continue; } double totalVal = ((BigDecimal) totals.get(property)).doubleValue(); if (colVal instanceof Number) { double sum = ((Number) colVal).doubleValue() + totalVal; totals.put(property, new BigDecimal(sum)); } else { String colValStr = "" + colVal; if (StringUtils.isNotBlank(colValStr)) { double colNumVal = 0.00; try { colNumVal = new BigDecimal(colValStr).doubleValue(); } catch (NumberFormatException e) { String errorMessage = "Problem parsing numeric value for property [" + property + "] with value [" + colValStr + "] to create a total!"; logger.error("TotalHandler.iterateRows() - " + errorMessage); } double sum = colNumVal + totalVal; totals.put(property, new BigDecimal(sum)); } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -