AccountsReportBuilder.java
/*
** Module : AccountsReportBuilder.java
** Abstract : AccountsReportBuilder is an abstract base report builder for accounts reports.
**
** Copyright (c) 2017-2024, Golden Code Development Corporation.
**
** -#- -I- --Date-- ----------------Description----------------------
** 001 SBI 20170601 Created initial version.
** 002 GBB 20241010 Moving getPreviewPageNumber to AbstractReportBuilder to be reused.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program 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 Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
**
** Additional terms under GNU Affero GPL version 3 section 7:
**
** Under Section 7 of the GNU Affero GPL version 3, the following additional
** terms apply to the works covered under the License. These additional terms
** are non-permissive additional terms allowed under Section 7 of the GNU
** Affero GPL version 3 and may not be removed by you.
**
** 0. Attribution Requirement.
**
** You must preserve all legal notices or author attributions in the covered
** work or Appropriate Legal Notices displayed by works containing the covered
** work. You may not remove from the covered work any author or developer
** credit already included within the covered work.
**
** 1. No License To Use Trademarks.
**
** This license does not grant any license or rights to use the trademarks
** Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
** of Golden Code Development Corporation. You are not authorized to use the
** name Golden Code, FWD, or the names of any author or contributor, for
** publicity purposes without written authorization.
**
** 2. No Misrepresentation of Affiliation.
**
** You may not represent yourself as Golden Code Development Corporation or FWD.
**
** You may not represent yourself for publicity purposes as associated with
** Golden Code Development Corporation, FWD, or any author or contributor to
** the covered work, without written authorization.
**
** 3. No Misrepresentation of Source or Origin.
**
** You may not represent the covered work as solely your work. All modified
** versions of the covered work must be marked in a reasonable way to make it
** clear that the modified work is not originating from Golden Code Development
** Corporation or FWD. All modified versions must contain the notices of
** attribution required in this license.
*/
package com.goldencode.p2j.admin.server.reports;
import java.util.*;
import java.util.function.*;
import java.util.stream.Stream;
import com.goldencode.p2j.admin.AdminHelper;
import com.goldencode.p2j.admin.TaggedName;
import com.goldencode.p2j.admin.shared.ReportParameters;
import com.google.gwt.regexp.shared.RegExp;
import static com.goldencode.p2j.admin.shared.ReportParameters.DETAILS;
/**
*
* Abstract base report builder for accounts reports.
*
* @param <TRowObject>
* Type that represents row data for accounts reports.
*/
public abstract class AccountsReportBuilder<TRowObject>
extends AbstractReportBuilder<AbstractReportBuilder.SimpleHeader,
TRowObject,
AbstractReportBuilder.SimpleFooter>
{
/**
* The ordered set of selected accounts to be used as data sources for generated reports
*/
private final Set<String> selectedRows;
/** The report header */
protected final SimpleHeader header;
/** The report footer */
protected final SimpleFooter footer;
/** Defines the flag that controls the visibility of additional extended columns */
protected boolean showExtendedColumns;
/** The number of additional extended columns */
protected int numExtendedRows;
/**
* Creates this builder.
*
* @param reportId
* The report id
* @param filter
* The string that defines the report selection criteria
* @param selected
* The array of the selected accounts
* @param reportParameters
* The report parameters
*/
public AccountsReportBuilder(String reportId,
String filter,
String[] selected,
Map<String, String> reportParameters)
{
super(reportId);
this.selectedRows = selected != null ? new LinkedHashSet<>(
Arrays.asList(selected)) : new LinkedHashSet<String>();
header = new SimpleHeader();
header.setHeader(reportId);
footer = new SimpleFooter();
if (filter != null && !filter.isEmpty())
{
setFilterRecords(createFilterPredicate(RegExp.compile(
AdminHelper.wrapIntoCaseInsensitivePattern(filter))));
}
int pageToPreview = getPreviewPageNumber(reportParameters);
if (pageToPreview > 0)
{
setPreviewPage(pageToPreview);
}
String action = reportParameters.getOrDefault(ReportParameters.ACTION.getParameter(), "");
setSelfPrintedDocument(!action.isEmpty());
showExtendedColumns = "true".equalsIgnoreCase(
reportParameters.get(DETAILS.name().toLowerCase()));
}
/**
* Returns a data stream of selected accounts.
*
* @param accounts
* Set of all target accounts
*
* @return Stream of selected accounts.
*/
public final Stream<TaggedName> getSelectionStream(TaggedName[] accounts)
{
Stream<TaggedName> data = Arrays.asList(accounts).stream().filter(new Predicate<TaggedName>()
{
@Override
public boolean test(TaggedName t)
{
if (AccountsReportBuilder.this.getSelectedRows().contains(t.getName()))
{
return true;
}
return false;
}
});
return data;
}
/**
* Gets ordered set of selected accounts
*
* @return The ordered set of selected accounts.
*/
public final Set<String> getSelectedRows()
{
return selectedRows;
}
/**
* Has selected accounts.
*
* @return True if selected accounts are set up. Thus there exists at least one selected account.
*/
public final boolean hasSelectedRows()
{
return !selectedRows.isEmpty();
}
/**
* {@inheritDoc}
*/
@Override
protected AbstractReportBuilder.SimpleHeader getReportHeader()
{
return header;
}
/**
* {@inheritDoc}
*/
@Override
protected AbstractReportBuilder.SimpleFooter getReportFooter()
{
return footer;
}
/**
* Builds extended column definition.
*
* @param core
* The core columns.
* @param ext
* The extension columns.
* @param numCoreColumns
* The number of core columns to appear on the report.
* @param numTuples
* The number of extended column tuples (Attribute + Value) to appear on the report.
*
* @param <T>
* The row type.
*
* @return Resulting columns configuration.
*/
protected <T extends AccountData> List<ColumnInfo<T>> buildColumns(List<ColumnInfo<T>> core,
List<ColumnInfo<T>> ext,
int numCoreColumns,
int numTuples)
{
List<ColumnInfo<T>> result = new LinkedList<>();
int coreSize = core.size();
int extSize = ext != null ? ext.size() : 0;
List<ColumnInfo<T>> allColumns = new ArrayList<>(coreSize + extSize);
allColumns.addAll(core);
allColumns.addAll(ext);
result.addAll(allColumns.subList(0, numCoreColumns));
int allColumnsCount = coreSize + extSize;
int extColumns = allColumnsCount - numCoreColumns;
if (extColumns < 1)
{
return result;
}
int columnsPerTuple = extColumns / numTuples + extColumns % numTuples;
numExtendedRows = columnsPerTuple;
for (int i = 0; i < numTuples; i++)
{
List<String> labels = new LinkedList<>();
List<ColumnInfo> columns = new LinkedList<>();
int off = numCoreColumns + columnsPerTuple * i;
for (int j = off; j < off + columnsPerTuple; j++)
{
if (j >= allColumnsCount)
{
break;
}
labels.add(allColumns.get(j).getLabels()[0]);
columns.add(allColumns.get(j));
}
StaticColumnInfo scol = new StaticColumnInfo("Attribute",
columnsPerTuple * i,
labels.toArray(new String[0]));
ExtendedColumnInfo ecol = new ExtendedColumnInfo("Value",
columnsPerTuple * i,
columns.toArray(new ColumnInfo[0]));
result.add(scol);
result.add(ecol);
}
return result;
}
/**
* Creates the report selection criteria.
*
* @param filterExp
* The given regular expression
*
* @return The report selection criteria
*/
private Predicate<TRowObject> createFilterPredicate(RegExp filterExp)
{
Predicate<TRowObject> filterRecords = new Predicate<TRowObject>()
{
@Override
public boolean test(TRowObject record)
{
for (ColumnInfo<TRowObject> column : getColumns())
{
String value = column.getValueGetter().apply(record);
if (value != null && filterExp.test(value))
{
return true;
}
}
return false;
}
};
return filterRecords;
}
/**
* A column info that allows to display extended rows.
*
* @param <T>
* The value type the column holds.
*/
public static class AccountColumnInfo<T extends AccountData>
extends ColumnInfo<T>
{
/**
* Ctor.
*
* @param headerLabel
* Column header label.
* @param valueGetter
* Value getter.
*/
public AccountColumnInfo(String headerLabel,
Function<T, String> valueGetter)
{
super(headerLabel, valueGetter);
}
/**
* Ctor.
*
* @param labels
* Column header labels.
* @param valueGetter
* Value getter.
*/
public AccountColumnInfo(String[] labels, Function<T, String> valueGetter)
{
super(labels, valueGetter);
}
/**
* Returns the value getter.
*
* @return see above
*/
@Override
public Function<T, String> getValueGetter()
{
return this::valueGetter;
}
/**
* The value getter implementation capable to handle extended rows.
*
* @param row
* The row object.
*
* @return see above
*/
private String valueGetter(T row)
{
// do not show core column values when on the extended row
if (row.getExtendedRowIndex() != 0)
{
return "";
}
return super.getValueGetter().apply(row);
}
}
}