Skip to content

Commit

Permalink
Add support to display value of agregate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
yamelsenih committed Jul 18, 2024
1 parent e95e5dd commit bb7093c
Show file tree
Hide file tree
Showing 11 changed files with 400 additions and 73 deletions.
Binary file modified docker-compose/postgresql/seed.backup
Binary file not shown.
103 changes: 103 additions & 0 deletions docs/adempiere_report_engine.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion resources/env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ server:
log_level: WARNING
database:
host: localhost
port: 5432
port: 5959
name: "adempiere"
user: adempiere
password: adempiere
Expand Down
105 changes: 105 additions & 0 deletions src/main/java/org/spin/report_engine/data/Cell.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,111 @@ public Cell withFunction(SummaryFunction function) {
return this;
}

public Cell withSumDisplayValue(String displayValue) {
if(function == null) {
return this;
}
function.setDisplayValue(SummaryFunction.F_SUM, displayValue);
return this;
}

public Cell withMeanDisplayValue(String displayValue) {
if(function == null) {
return this;
}
function.setDisplayValue(SummaryFunction.F_MEAN, displayValue);
return this;
}

public Cell withCountDisplayValue(String displayValue) {
if(function == null) {
return this;
}
function.setDisplayValue(SummaryFunction.F_COUNT, displayValue);
return this;
}

public Cell withMinimumDisplayValue(String displayValue) {
if(function == null) {
return this;
}
function.setDisplayValue(SummaryFunction.F_MIN, displayValue);
return this;
}

public Cell withMaximumDisplayValue(String displayValue) {
if(function == null) {
return this;
}
function.setDisplayValue(SummaryFunction.F_MAX, displayValue);
return this;
}

public Cell withVarianceDisplayValue(String displayValue) {
if(function == null) {
return this;
}
function.setDisplayValue(SummaryFunction.F_VARIANCE, displayValue);
return this;
}

public Cell withDeviationDisplayValue(String displayValue) {
if(function == null) {
return this;
}
function.setDisplayValue(SummaryFunction.F_DEVIATION, displayValue);
return this;
}

public String getSumDisplayValue() {
if(function == null) {
return null;
}
return function.getDisplayValue(SummaryFunction.F_SUM);
}

public String getMeanDisplayValue() {
if(function == null) {
return null;
}
return function.getDisplayValue(SummaryFunction.F_MEAN);
}

public String getCountDisplayValue() {
if(function == null) {
return null;
}
return function.getDisplayValue(SummaryFunction.F_COUNT);
}

public String getMinimumDisplayValue() {
if(function == null) {
return null;
}
return function.getDisplayValue(SummaryFunction.F_MIN);
}

public String getMaximumDisplayValue() {
if(function == null) {
return null;
}
return function.getDisplayValue(SummaryFunction.F_MAX);
}

public String getVarianceDisplayValue() {
if(function == null) {
return null;
}
return function.getDisplayValue(SummaryFunction.F_VARIANCE);
}

public String getDeviationDisplayValue() {
if(function == null) {
return null;
}
return function.getDisplayValue(SummaryFunction.F_DEVIATION);
}

public BigDecimal getSum() {
if(function == null) {
return null;
Expand Down
28 changes: 27 additions & 1 deletion src/main/java/org/spin/report_engine/data/ReportInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@
import java.util.stream.IntStream;
import java.util.stream.Stream;

import org.compiere.util.Language;
import org.compiere.util.Util;
import org.spin.report_engine.format.PrintFormat;
import org.spin.report_engine.format.PrintFormatItem;
import org.spin.report_engine.format.QueryDefinition;
import org.spin.report_engine.mapper.DefaultMapping;
import org.spin.report_engine.mapper.IColumnMapping;
import org.spin.report_engine.util.ClassLoaderMapping;


/**
Expand All @@ -53,8 +58,10 @@ public class ReportInfo {
private QueryDefinition queryDefinition;
private long recordCount;
private int instanceId;
private PrintFormat printFormat;

private ReportInfo(PrintFormat printFormat, QueryDefinition queryDefinition) {
this.printFormat = printFormat;
name = printFormat.getName();
description = printFormat.getDescription();
columns = printFormat.getPrintedItems().stream().map(item -> ColumnInfo.newInstance(item)).collect(Collectors.toList());
Expand Down Expand Up @@ -207,7 +214,26 @@ public ReportInfo completeInfo() {
List<Row> completeRows = Stream.concat(getRows().stream(), groupedRows.stream())
.sorted(getSortingValue(false))
.collect(Collectors.toList());
rows = completeRows;
Language language = Language.getLoginLanguage();
rows = new ArrayList<Row>();
completeRows.forEach(row -> {
Row newRow = Row.newInstance();
// Items
printFormat.getItems().forEach(printFormatItem -> {
Cell cell = row.getCell(printFormatItem.getPrintFormatItemId());
// Apply Default Mask
if(!Util.isEmpty(printFormatItem.getMappingClassName())) {
IColumnMapping customMapping = ClassLoaderMapping.loadClass(printFormatItem.getMappingClassName());
if(customMapping != null) {
customMapping.processValue(printFormatItem, language, cell);
}
} else {
DefaultMapping.newInstance().processValue(printFormatItem, language, cell);
}
newRow.withCell(printFormatItem.getPrintFormatItemId(), cell);
});
rows.add(newRow);
});
return this;
}

Expand Down
23 changes: 23 additions & 0 deletions src/main/java/org/spin/report_engine/data/SummaryFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

import org.compiere.util.DisplayType;
import org.compiere.util.Env;
Expand All @@ -35,6 +37,8 @@ public class SummaryFunction {
private BigDecimal maximum;
/** Sum of Squares */
private BigDecimal sumSquare;
/** Display values */
private Map<String, String> displayValues;

/** Sum */
static public final char F_SUM = 'S';
Expand Down Expand Up @@ -65,6 +69,7 @@ private SummaryFunction() {
sum = Env.ZERO;
sumSquare = Env.ZERO;
count = 0;
displayValues = new HashMap<String, String>();
}

public static SummaryFunction newInstance() {
Expand Down Expand Up @@ -152,6 +157,24 @@ public BigDecimal getValue(char function) {
return deviation;
} // getValue

/**
* Get display value of function
* @param function
* @return
*/
public String getDisplayValue(char function) {
return displayValues.get(String.valueOf(function));
}

/**
* Set display value for function
* @param function
* @param displayValue
*/
public void setDisplayValue(char function, String displayValue) {
displayValues.put(String.valueOf(function), displayValue);
}

/*************************************************************************/

/**
Expand Down
67 changes: 52 additions & 15 deletions src/main/java/org/spin/report_engine/mapper/DefaultMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,38 @@ public static DefaultMapping newInstance() {
return new DefaultMapping();
}

public void processValue(PrintFormatItem printFormatLine, Language language, Cell cell) {
processValue(printFormatLine, null, language, null, cell);
}

public void processValue(PrintFormatItem printFormatLine, PrintFormatColumn column, Language language, ResultSet resultSet, Cell cell) {
if(cell == null) {
return;
}
try {
if(DisplayType.isLookup(column.getReferenceId()) && column.getReferenceId() != DisplayType.List || column.getColumnName().equals("Record_ID")) {
int valueId = resultSet.getInt(column.getColumnName());
cell.withValue(valueId);
if(column.getColumnName().equals("Record_ID")) {
try {
String tableName = resultSet.getString("TableName");
if(!Util.isEmpty(tableName)) {
cell.withTableName(tableName);
if(valueId > 0) {
MTable table = MTable.get(Env.getCtx(), tableName);
PO entity = table.getPO(valueId, null);
cell.withDisplayValue(entity.getDisplayValue());
if(resultSet!= null && column != null) {
if(DisplayType.isLookup(column.getReferenceId()) && column.getReferenceId() != DisplayType.List || column.getColumnName().equals("Record_ID")) {
int valueId = resultSet.getInt(column.getColumnName());
cell.withValue(valueId);
if(column.getColumnName().equals("Record_ID")) {
try {
String tableName = resultSet.getString("TableName");
if(!Util.isEmpty(tableName)) {
cell.withTableName(tableName);
if(valueId > 0) {
MTable table = MTable.get(Env.getCtx(), tableName);
PO entity = table.getPO(valueId, null);
cell.withDisplayValue(entity.getDisplayValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return;
}
} else if(DisplayType.isDate(printFormatLine.getReferenceId())) {
}
if(DisplayType.isDate(printFormatLine.getReferenceId())) {
if(cell.getValue() != null) {
Timestamp date = (Timestamp) cell.getValue();
cell.withDisplayValue(DisplayType.getDateFormat(printFormatLine.getReferenceId(), language, printFormatLine.getFormatPattern()).format(date));
Expand All @@ -86,6 +94,35 @@ public void processValue(PrintFormatItem printFormatLine, PrintFormatColumn colu
}
}
}
// Set display value to functions
BigDecimal value = cell.getSum();
if(value != null) {
cell.withSumDisplayValue(DisplayType.getNumberFormat(printFormatLine.getReferenceId(), language, printFormatLine.getFormatPattern()).format(value));
}
value = cell.getMean();
if(value != null) {
cell.withMeanDisplayValue(DisplayType.getNumberFormat(printFormatLine.getReferenceId(), language, printFormatLine.getFormatPattern()).format(value));
}
value = cell.getCount();
if(value != null) {
cell.withCountDisplayValue(DisplayType.getNumberFormat(printFormatLine.getReferenceId(), language, printFormatLine.getFormatPattern()).format(value));
}
value = cell.getMinimum();
if(value != null) {
cell.withMinimumDisplayValue(DisplayType.getNumberFormat(printFormatLine.getReferenceId(), language, printFormatLine.getFormatPattern()).format(value));
}
value = cell.getMaximum();
if(value != null) {
cell.withMaximumDisplayValue(DisplayType.getNumberFormat(printFormatLine.getReferenceId(), language, printFormatLine.getFormatPattern()).format(value));
}
value = cell.getVariance();
if(value != null) {
cell.withVarianceDisplayValue(DisplayType.getNumberFormat(printFormatLine.getReferenceId(), language, printFormatLine.getFormatPattern()).format(value));
}
value = cell.getDeviation();
if(value != null) {
cell.withDeviationDisplayValue(DisplayType.getNumberFormat(printFormatLine.getReferenceId(), language, printFormatLine.getFormatPattern()).format(value));
}
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ public interface IColumnMapping {
* @param cell
*/
public void processValue(PrintFormatItem printFormatLine, PrintFormatColumn column, Language language, ResultSet resultSet, Cell cell);

/**
* Process value from complete list
* @param printFormatLine
* @param language
* @param cell
*/
public void processValue(PrintFormatItem printFormatLine, Language language, Cell cell);
}
Loading

0 comments on commit bb7093c

Please sign in to comment.