net.sf.dynamicreports.report.builder.column.PercentageColumnBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dynamicreports-core Show documentation
Show all versions of dynamicreports-core Show documentation
DynamicReports is an open source Java reporting library based on JasperReports. It allows to create dynamic report designs and it doesn't need a visual report designer.
You can very quickly create reports and produce documents that can be displayed, printed or exported into many popular formats such as PDF, Excel, Word and others.
/**
* DynamicReports - Free Java reporting library for creating reports dynamically
*
* Copyright (C) 2010 - 2016 Ricardo Mariaca
* http://www.dynamicreports.org
*
* This file is part of DynamicReports.
*
* DynamicReports is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DynamicReports is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with DynamicReports. If not, see .
*/
package net.sf.dynamicreports.report.builder.column;
import net.sf.dynamicreports.report.base.DRGroup;
import net.sf.dynamicreports.report.base.DRVariable;
import net.sf.dynamicreports.report.builder.FieldBuilder;
import net.sf.dynamicreports.report.builder.datatype.DataTypes;
import net.sf.dynamicreports.report.builder.expression.PercentageExpression;
import net.sf.dynamicreports.report.builder.group.GroupBuilder;
import net.sf.dynamicreports.report.constant.Calculation;
import net.sf.dynamicreports.report.constant.Constants;
import net.sf.dynamicreports.report.constant.Evaluation;
import net.sf.dynamicreports.report.constant.PercentageTotalType;
import net.sf.dynamicreports.report.definition.expression.DRIExpression;
import net.sf.dynamicreports.report.exception.DRReportException;
import org.apache.commons.lang3.Validate;
/**
* It calculates percentage values from the field or column values.
*
* @author Ricardo Mariaca ([email protected])
*/
public class PercentageColumnBuilder extends ValueColumnBuilder {
private static final long serialVersionUID = Constants.SERIAL_VERSION_UID;
private DRIExpression actualExpression;
private PercentageTotalType totalType;
private DRGroup totalGroup;
protected PercentageColumnBuilder(ValueColumnBuilder column) {
Validate.notNull(column, "column must not be null");
this.actualExpression = column.build();
}
protected PercentageColumnBuilder(FieldBuilder field) {
Validate.notNull(field, "field must not be null");
this.actualExpression = field.getField();
}
/*protected PercentageColumnBuilder(DRISimpleExpression valueExpression) {
Validate.notNull(valueExpression, "valueExpression must not be null");
this.actualExpression = valueExpression;
}*/
/**
* Sets the total type. Has effect only when the report contains at least one group.
*
* @param totalType the total type
* @return a column builder
*/
public PercentageColumnBuilder setTotalType(PercentageTotalType totalType) {
this.totalType = totalType;
return this;
}
/**
* Sets the total group. Has effect only when the report contains at least one group.
*
* @param totalGroup the total group
* @return a column builder
*/
public PercentageColumnBuilder setTotalGroup(GroupBuilder totalGroup) {
if (totalGroup != null) {
this.totalGroup = totalGroup.getGroup();
setTotalType(PercentageTotalType.GROUP);
}
else {
this.totalGroup = null;
}
return this;
}
@Override
protected void configure() {
if (getComponent().getDataType() == null) {
getComponent().setDataType(DataTypes.percentageType());
}
DRVariable totalExpression = new DRVariable(actualExpression, Calculation.SUM);
if (totalType != null) {
switch (totalType) {
case REPORT:
totalExpression.setResetType(Evaluation.REPORT);
break;
case GROUP:
totalExpression.setResetType(Evaluation.GROUP);
break;
case FIRST_GROUP:
totalExpression.setResetType(Evaluation.FIRST_GROUP);
break;
case LAST_GROUP:
totalExpression.setResetType(Evaluation.LAST_GROUP);
break;
default:
throw new DRReportException("Percentage total type " + totalType.name() + " not supported.");
}
}
else {
totalExpression.setResetType(Evaluation.LAST_GROUP);
}
totalExpression.setResetGroup(totalGroup);
setValueExpression(new PercentageExpression(actualExpression, totalExpression));
super.configure();
}
}