finance-analyzer/src/main/java/de/arminwolf/financeanalyzer/dao/ReportDAO.java

152 lines
4.5 KiB
Java

package de.arminwolf.financeanalyzer.dao;
import de.arminwolf.financeanalyzer.dao.charts.model.HighChartDAO;
import de.arminwolf.financeanalyzer.dao.charts.model.Series;
import de.arminwolf.financeanalyzer.dao.charts.factories.BubbleChartFactory;
import de.arminwolf.financeanalyzer.dao.charts.factories.ColumnChartFactory;
import de.arminwolf.financeanalyzer.dao.charts.factories.LineChartFactory;
import de.arminwolf.financeanalyzer.dao.model.OutputData;
import de.arminwolf.financeanalyzer.util.NumberUtil;
import de.arminwolf.financeanalyzer.util.OrderDateStringComparator;
import org.apache.commons.math3.util.Pair;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
public class ReportDAO {
private HighChartDAO cashFlowChart;
private HighChartDAO accountBalanceLineChart;
private HighChartDAO transactionCategoriesBubbleChart;
private Set<OutputData> standingOutputData;
private Set<OutputData> notStandingOutputData;
private List<OutputData> incomeList;
public ReportDAO() {
this.standingOutputData = new HashSet<>();
this.notStandingOutputData = new HashSet<>();
this.incomeList = new ArrayList<>();
}
public Set<OutputData> getStandingOrders() {
return this.standingOutputData;
}
public List<OutputData> getSortedStandingOrders() {
return this.standingOutputData.stream().sorted(new OrderDateStringComparator()).collect(Collectors.toList());
}
public Float getStandingOrdersAmount() {
return NumberUtil.round(getStandingOrders().stream().map(OutputData::getAmountAsFloat).reduce(0f, Float::sum));
}
public Float getNotStandingOrdersAmount() {
return NumberUtil.round(getNotStandingOrders().stream().map(OutputData::getAmountAsFloat).reduce(0f, Float::sum));
}
public HighChartDAO getTransactionCategoriesBubbleChart() {
return transactionCategoriesBubbleChart;
}
public void setIncomeList(final List<TransactionDAO> incomes) {
this.incomeList = incomes.stream()
.map(t -> new OutputData(t.getVerwendungszweck(), t))
.sorted(new OrderDateStringComparator())
.collect(Collectors.toList());
}
public List<OutputData> getIncomeList() {
return incomeList;
}
public void setNotStandingOrders(final List<TransactionDAO> notStandingOrders) {
Set<OutputData> list = new HashSet<>();
notStandingOrders.forEach(order -> list.add(new OutputData(order)));
this.notStandingOutputData = list;
}
public final Set<OutputData> getNotStandingOrders() {
return notStandingOutputData;
}
public final List<OutputData> getSortedNotStandingOrders() {
return notStandingOutputData.stream().sorted(new OrderDateStringComparator()).collect(Collectors.toList());
}
public Float getTotalExpense() {
return getStandingOrdersAmount() + getNotStandingOrdersAmount();
}
public void setTransactionCategoriesBubbleChart(final List<TransactionDAO> transactions) {
this.transactionCategoriesBubbleChart = BubbleChartFactory.createBubbleChart("Kateogrien der Transaktionen", transactions);
}
public void setAccountBalanceLineChart(final List<Pair<String, String>> pairList) {
String title = "Verlauf des Kontostandes";
String chartTitle = "Kontostand in EUR";
List<Series> series = new ArrayList<>();
Series data = new Series();
data.setData(pairList.stream().map(e -> Float.parseFloat(e.getValue())).collect(Collectors.toList()));
series.add(data);
this.accountBalanceLineChart = LineChartFactory.createLineChart(title, chartTitle, pairList.stream().map(Pair::getKey).collect(Collectors.toList()), series);
}
public HighChartDAO getAccountBalanceLineChart() {
return accountBalanceLineChart;
}
public void setCashFlowChart(final Map<String, List<Float>> ausgaben, final Map<String, List<Float>> einnahmen) {
this.cashFlowChart =
ColumnChartFactory.createColumnChart("Cashflow",
"Einnahmen und Ausgaben",
ausgaben, "Ausgaben",
einnahmen, "Einnahmen");
}
public HighChartDAO getCashFlowChart() {
return cashFlowChart;
}
public Float getIncome() {
return this.incomeList.stream().map(OutputData::getAmountAsFloat).reduce(0f, Float::sum);
}
public Float getDiff() {
return NumberUtil.round(Math.abs(getIncome()) - Math.abs(getTotalExpense()));
}
public void setStandingOrders(final Collection<TransactionDAO> orders) {
Set<OutputData> list = new HashSet<>();
orders.forEach(order -> list.add(new OutputData(order)));
this.standingOutputData = list;
}
}