net.sf.dynamicreports.report.builder.crosstab.CrosstabMeasureBuilder 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.
The newest version!
/*
* DynamicReports - Free Java reporting library for creating reports dynamically
*
* Copyright (C) 2010 - 2018 Ricardo Mariaca and the Dynamic Reports Contributors
*
* 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.crosstab;
import org.apache.commons.lang3.Validate;
import net.sf.dynamicreports.report.base.crosstab.DRCrosstabCellStyle;
import net.sf.dynamicreports.report.base.crosstab.DRCrosstabMeasure;
import net.sf.dynamicreports.report.base.crosstab.DRCrosstabVariable;
import net.sf.dynamicreports.report.builder.AbstractBuilder;
import net.sf.dynamicreports.report.builder.FieldBuilder;
import net.sf.dynamicreports.report.builder.HyperLinkBuilder;
import net.sf.dynamicreports.report.builder.column.ValueColumnBuilder;
import net.sf.dynamicreports.report.builder.datatype.DataTypes;
import net.sf.dynamicreports.report.builder.expression.Expressions;
import net.sf.dynamicreports.report.builder.style.ReportStyleBuilder;
import net.sf.dynamicreports.report.constant.Calculation;
import net.sf.dynamicreports.report.constant.Constants;
import net.sf.dynamicreports.report.constant.CrosstabPercentageType;
import net.sf.dynamicreports.report.constant.HorizontalAlignment;
import net.sf.dynamicreports.report.constant.HorizontalTextAlignment;
import net.sf.dynamicreports.report.constant.TextAdjust;
import net.sf.dynamicreports.report.definition.DRICrosstabValue;
import net.sf.dynamicreports.report.definition.DRIField;
import net.sf.dynamicreports.report.definition.component.DRITextField;
import net.sf.dynamicreports.report.definition.datatype.DRIDataType;
import net.sf.dynamicreports.report.definition.expression.DRIExpression;
import net.sf.dynamicreports.report.definition.expression.DRIPropertyExpression;
import net.sf.dynamicreports.report.definition.expression.DRIValueFormatter;
import net.sf.dynamicreports.report.exception.DRReportException;
/**
* CrosstabMeasureBuilder class.
*
* @author Ricardo Mariaca, Jan Moxter
*
*/
@SuppressWarnings("deprecation")
public class CrosstabMeasureBuilder extends AbstractBuilder, DRCrosstabMeasure> implements DRICrosstabValue {
private static final long serialVersionUID = Constants.SERIAL_VERSION_UID;
/**
* Constructor for CrosstabMeasureBuilder.
*
* @param column a {@link net.sf.dynamicreports.report.builder.column.ValueColumnBuilder} object.
* @param calculation a {@link net.sf.dynamicreports.report.constant.Calculation} object.
*/
protected CrosstabMeasureBuilder(final ValueColumnBuilder column, final Calculation calculation) {
super(new DRCrosstabMeasure(Crosstabs.variable(column, calculation).build()));
if (calculation.equals(Calculation.COUNT) || calculation.equals(Calculation.DISTINCT_COUNT)) {
setDataType(DataTypes.longType());
} else if (calculation.equals(Calculation.AVERAGE) || calculation.equals(Calculation.STANDARD_DEVIATION) || calculation.equals(Calculation.VARIANCE)) {
setDataType(DataTypes.doubleType());
} else {
final DRITextField columnComponent = column.getColumn().getComponent();
setDataType(columnComponent.getDataType());
setPattern(columnComponent.getPattern());
}
}
/**
* Constructor for CrosstabMeasureBuilder.
*
* @param field a {@link net.sf.dynamicreports.report.builder.FieldBuilder} object.
* @param calculation a {@link net.sf.dynamicreports.report.constant.Calculation} object.
*/
protected CrosstabMeasureBuilder(final FieldBuilder field, final Calculation calculation) {
super(new DRCrosstabMeasure(Crosstabs.variable(field, calculation).build()));
if (calculation.equals(Calculation.COUNT) || calculation.equals(Calculation.DISTINCT_COUNT)) {
setDataType(DataTypes.longType());
} else if (calculation.equals(Calculation.AVERAGE) || calculation.equals(Calculation.STANDARD_DEVIATION) || calculation.equals(Calculation.VARIANCE)) {
setDataType(DataTypes.doubleType());
} else {
setDataType(field.getField().getDataType());
}
}
/**
* Constructor for CrosstabMeasureBuilder.
*
* @param expression a {@link net.sf.dynamicreports.report.definition.expression.DRIExpression} object.
* @param calculation a {@link net.sf.dynamicreports.report.constant.Calculation} object.
*/
protected CrosstabMeasureBuilder(final DRIExpression expression, final Calculation calculation) {
super(new DRCrosstabMeasure(Crosstabs.variable(expression, calculation).build()));
if (calculation.equals(Calculation.COUNT) || calculation.equals(Calculation.DISTINCT_COUNT)) {
setDataType(DataTypes.longType());
} else if (calculation.equals(Calculation.AVERAGE) || calculation.equals(Calculation.STANDARD_DEVIATION) || calculation.equals(Calculation.VARIANCE)) {
setDataType(DataTypes.doubleType());
} else if (expression instanceof DRIField) {
setDataType(((DRIField) expression).getDataType());
}
}
/**
* Constructor for CrosstabMeasureBuilder.
*
* @param expression a {@link net.sf.dynamicreports.report.definition.expression.DRIExpression} object.
*/
protected CrosstabMeasureBuilder(final DRIExpression expression) {
super(new DRCrosstabMeasure(expression));
}
/**
* setDataType.
*
* @param dataType a {@link net.sf.dynamicreports.report.definition.datatype.DRIDataType} object.
* @return a {@link net.sf.dynamicreports.report.builder.crosstab.CrosstabMeasureBuilder} object.
*/
@SuppressWarnings( {"unchecked", "rawtypes"})
public CrosstabMeasureBuilder setDataType(final DRIDataType dataType) {
getObject().setDataType(dataType);
return this;
}
/**
* setPercentageType.
*
* @param percentageType a {@link net.sf.dynamicreports.report.constant.CrosstabPercentageType} object.
* @return a {@link net.sf.dynamicreports.report.builder.crosstab.CrosstabMeasureBuilder} object.
*/
public CrosstabMeasureBuilder setPercentageType(final CrosstabPercentageType percentageType) {
if (getObject().getExpression() instanceof DRCrosstabVariable) {
final DRCrosstabVariable variable = (DRCrosstabVariable) getObject().getExpression();
if (percentageType != null && percentageType.equals(CrosstabPercentageType.GRAND_TOTAL) && !variable.getCalculation().equals(Calculation.COUNT) &&
!variable.getCalculation().equals(Calculation.DISTINCT_COUNT)) {
setDataType(DataTypes.doubleType());
}
variable.setPercentageType(percentageType);
} else {
throw new DRReportException("Percentage is not supported in this type of measure");
}
return this;
}
/**
* setPattern.
*
* @param pattern a {@link java.lang.String} object.
* @return a {@link net.sf.dynamicreports.report.builder.crosstab.CrosstabMeasureBuilder} object.
*/
public CrosstabMeasureBuilder setPattern(final String pattern) {
getObject().setPattern(pattern);
return this;
}
/**
* setHorizontalAlignment.
*
* @param horizontalAlignment a {@link net.sf.dynamicreports.report.constant.HorizontalAlignment} object.
* @return a {@link net.sf.dynamicreports.report.builder.crosstab.CrosstabMeasureBuilder} object.
* @deprecated use setHorizontalTextAlignment instead
*/
@Deprecated
public CrosstabMeasureBuilder setHorizontalAlignment(final HorizontalAlignment horizontalAlignment) {
if (horizontalAlignment != null) {
getObject().setHorizontalTextAlignment(HorizontalTextAlignment.valueOf(horizontalAlignment.name()));
} else {
getObject().setHorizontalTextAlignment(null);
}
return this;
}
/**
* setHorizontalTextAlignment.
*
* @param horizontalTextAlignment a {@link net.sf.dynamicreports.report.constant.HorizontalTextAlignment} object.
* @return a {@link net.sf.dynamicreports.report.builder.crosstab.CrosstabMeasureBuilder} object.
*/
public CrosstabMeasureBuilder setHorizontalTextAlignment(final HorizontalTextAlignment horizontalTextAlignment) {
getObject().setHorizontalTextAlignment(horizontalTextAlignment);
return this;
}
/**
* setValueFormatter.
*
* @param valueFormatter a {@link net.sf.dynamicreports.report.definition.expression.DRIValueFormatter} object.
* @return a {@link net.sf.dynamicreports.report.builder.crosstab.CrosstabMeasureBuilder} object.
*/
public CrosstabMeasureBuilder setValueFormatter(final DRIValueFormatter valueFormatter) {
getObject().setValueFormatter(valueFormatter);
return this;
}
/**
* setStretchWithOverflow.
*
* @param stretchWithOverflow a {@link java.lang.Boolean} object.
* @return a {@link net.sf.dynamicreports.report.builder.crosstab.CrosstabMeasureBuilder} object.
* @deprecated replaced by {@link #setTextAdjust(TextAdjust)}
*/
@Deprecated
public CrosstabMeasureBuilder setStretchWithOverflow(final Boolean stretchWithOverflow) {
getObject().setStretchWithOverflow(stretchWithOverflow);
return this;
}
/**
* setTotalHeaderTextAdjust.
*
* @param textAdjust a {@link net.sf.dynamicreports.report.constant.TextAdjust} object.
* @return a T object.
*/
public CrosstabMeasureBuilder setTextAdjust(final TextAdjust textAdjust) {
getObject().setTextAdjust(textAdjust);
return this;
}
/**
* Sets the crosstab measure hyperlink.
*
* @param hyperLink the measure hyperlink
* @return a crosstab measure builder
*/
public CrosstabMeasureBuilder setHyperLink(final HyperLinkBuilder hyperLink) {
if (hyperLink != null) {
getObject().setHyperLink(hyperLink.getHyperLink());
} else {
getObject().setHyperLink(null);
}
return this;
}
/**
* Adds a jasper property to the measure.
*
* @param propertyExpression the property expression
* @return a crosstab measure builder
*/
public CrosstabMeasureBuilder addProperty(final DRIPropertyExpression propertyExpression) {
getObject().addPropertyExpression(propertyExpression);
return this;
}
/**
* Adds a jasper property to the measure.
*
* @param name the property name
* @param valueExpression the property value expression
* @return a crosstab measure builder
*/
public CrosstabMeasureBuilder addProperty(final String name, final DRIExpression valueExpression) {
getObject().addPropertyExpression(Expressions.property(name, valueExpression));
return this;
}
/**
* Adds a jasper property to the measure.
*
* @param name the property name
* @param value the property value
* @return a crosstab measure builder
*/
public CrosstabMeasureBuilder addProperty(final String name, final String value) {
getObject().addPropertyExpression(Expressions.property(name, value));
return this;
}
/**
* setStyle.
*
* @param style a {@link net.sf.dynamicreports.report.builder.style.ReportStyleBuilder} object.
* @return a {@link net.sf.dynamicreports.report.builder.crosstab.CrosstabMeasureBuilder} object.
*/
public CrosstabMeasureBuilder setStyle(final ReportStyleBuilder style) {
Validate.notNull(style, "style must not be null");
getObject().getStyles().add(new DRCrosstabCellStyle(style.getStyle()));
return this;
}
/**
* setStyle.
*
* @param style a {@link net.sf.dynamicreports.report.builder.style.ReportStyleBuilder} object.
* @param rowGroup a {@link net.sf.dynamicreports.report.builder.crosstab.CrosstabRowGroupBuilder} object.
* @return a {@link net.sf.dynamicreports.report.builder.crosstab.CrosstabMeasureBuilder} object.
*/
public CrosstabMeasureBuilder setStyle(final ReportStyleBuilder style, final CrosstabRowGroupBuilder rowGroup) {
Validate.notNull(style, "style must not be null");
Validate.notNull(rowGroup, "rowGroup must not be null");
getObject().getStyles().add(new DRCrosstabCellStyle(style.getStyle(), rowGroup.build(), null));
return this;
}
/**
* setStyle.
*
* @param style a {@link net.sf.dynamicreports.report.builder.style.ReportStyleBuilder} object.
* @param columnGroup a {@link net.sf.dynamicreports.report.builder.crosstab.CrosstabColumnGroupBuilder} object.
* @return a {@link net.sf.dynamicreports.report.builder.crosstab.CrosstabMeasureBuilder} object.
*/
public CrosstabMeasureBuilder setStyle(final ReportStyleBuilder style, final CrosstabColumnGroupBuilder columnGroup) {
Validate.notNull(style, "style must not be null");
Validate.notNull(columnGroup, "columnGroup must not be null");
getObject().getStyles().add(new DRCrosstabCellStyle(style.getStyle(), null, columnGroup.build()));
return this;
}
/**
* setStyle.
*
* @param style a {@link net.sf.dynamicreports.report.builder.style.ReportStyleBuilder} object.
* @param rowGroup a {@link net.sf.dynamicreports.report.builder.crosstab.CrosstabRowGroupBuilder} object.
* @param columnGroup a {@link net.sf.dynamicreports.report.builder.crosstab.CrosstabColumnGroupBuilder} object.
* @return a {@link net.sf.dynamicreports.report.builder.crosstab.CrosstabMeasureBuilder} object.
*/
public CrosstabMeasureBuilder setStyle(final ReportStyleBuilder style, final CrosstabRowGroupBuilder rowGroup, final CrosstabColumnGroupBuilder columnGroup) {
Validate.notNull(style, "style must not be null");
Validate.notNull(rowGroup, "rowGroup must not be null");
Validate.notNull(columnGroup, "columnGroup must not be null");
getObject().getStyles().add(new DRCrosstabCellStyle(style.getStyle(), rowGroup.build(), columnGroup.build()));
return this;
}
/**
* setTitle.
*
* @param titleExpression a {@link net.sf.dynamicreports.report.definition.expression.DRIExpression} object.
* @return a {@link net.sf.dynamicreports.report.builder.crosstab.CrosstabMeasureBuilder} object.
*/
public CrosstabMeasureBuilder setTitle(final DRIExpression titleExpression) {
getObject().setTitleExpression(titleExpression);
return this;
}
/**
* setTitle.
*
* @param title a {@link java.lang.String} object.
* @return a {@link net.sf.dynamicreports.report.builder.crosstab.CrosstabMeasureBuilder} object.
*/
public CrosstabMeasureBuilder setTitle(final String title) {
getObject().setTitleExpression(Expressions.text(title));
return this;
}
/**
* setTitleStyle.
*
* @param titleStyle a {@link net.sf.dynamicreports.report.builder.style.ReportStyleBuilder} object.
* @return a {@link net.sf.dynamicreports.report.builder.crosstab.CrosstabMeasureBuilder} object.
*/
public CrosstabMeasureBuilder setTitleStyle(final ReportStyleBuilder titleStyle) {
if (titleStyle != null) {
getObject().setTitleStyle(titleStyle.getStyle());
} else {
getObject().setTitleStyle(null);
}
return this;
}
/** {@inheritDoc} */
@Override
public String getName() {
return build().getExpression().getName();
}
}