Add charts (based on JFreeChart)
Currently only an expense report is supported, but more will come. Also moved rest templates into own toplevel package.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package de.financer.controller;
|
||||
|
||||
import de.financer.ResponseReason;
|
||||
import de.financer.dto.AccountGroupExpense;
|
||||
import de.financer.model.AccountGroup;
|
||||
import de.financer.service.AccountGroupService;
|
||||
import org.slf4j.Logger;
|
||||
@@ -50,4 +51,13 @@ public class AccountGroupController {
|
||||
|
||||
return responseReason.toResponseEntity();
|
||||
}
|
||||
|
||||
@RequestMapping("getAccountGroupExpenses")
|
||||
public Iterable<AccountGroupExpense> getAccountGroupExpenses(String periodStart, String periodEnd) {
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug(String.format("/accountGroups/getAccountGroupExpenses got parameters: %s|%s", periodStart, periodEnd));
|
||||
}
|
||||
|
||||
return this.accountGroupService.getAccountGroupExpenses(periodStart, periodEnd);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
package de.financer.dba;
|
||||
|
||||
import de.financer.dto.AccountGroupExpense;
|
||||
import de.financer.model.AccountGroup;
|
||||
import de.financer.model.AccountType;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRED)
|
||||
public interface AccountGroupRepository extends CrudRepository<AccountGroup, Long> {
|
||||
AccountGroup findByName(String name);
|
||||
|
||||
@Query("SELECT new de.financer.dto.AccountGroupExpense(ag, SUM(t.amount)) FROM Transaction t JOIN t.toAccount a JOIN a.accountGroup ag WHERE a.type in :expenseTypes AND t.date BETWEEN :startDate AND :startEnd GROUP BY ag")
|
||||
Iterable<AccountGroupExpense> getAccountGroupExpenses(LocalDate startDate, LocalDate startEnd, AccountType... expenseTypes);
|
||||
}
|
||||
|
||||
@@ -16,4 +16,4 @@ public interface TransactionRepository extends CrudRepository<Transaction, Long>
|
||||
|
||||
@Query("SELECT SUM(t.amount) FROM Transaction t JOIN t.toAccount a WHERE t.date BETWEEN :periodStart AND :periodEnd AND a.type IN :expenseTypes")
|
||||
Long getExpensesCurrentPeriod(LocalDate periodStart, LocalDate periodEnd, AccountType... expenseTypes);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
package de.financer.service;
|
||||
|
||||
import de.financer.ResponseReason;
|
||||
import de.financer.config.FinancerConfig;
|
||||
import de.financer.dba.AccountGroupRepository;
|
||||
import de.financer.dto.AccountGroupExpense;
|
||||
import de.financer.model.AccountGroup;
|
||||
import de.financer.model.AccountType;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -11,6 +14,11 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.Collections;
|
||||
|
||||
@Service
|
||||
public class AccountGroupService {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(AccountGroupService.class);
|
||||
@@ -18,6 +26,9 @@ public class AccountGroupService {
|
||||
@Autowired
|
||||
private AccountGroupRepository accountGroupRepository;
|
||||
|
||||
@Autowired
|
||||
private FinancerConfig financerConfig;
|
||||
|
||||
/**
|
||||
* @return all existing account groups
|
||||
*/
|
||||
@@ -29,6 +40,7 @@ public class AccountGroupService {
|
||||
* This method returns the account group with the given name.
|
||||
*
|
||||
* @param name the name to get the account group for
|
||||
*
|
||||
* @return the account group or <code>null</code> if no account group with the given name can be found
|
||||
*/
|
||||
public AccountGroup getAccountGroupByName(String name) {
|
||||
@@ -39,10 +51,10 @@ public class AccountGroupService {
|
||||
* This method creates a new account group with the given name.
|
||||
*
|
||||
* @param name the name of the new account group
|
||||
* @return {@link ResponseReason#DUPLICATE_ACCOUNT_GROUP_NAME} if an account group with the given name already exists,
|
||||
* {@link ResponseReason#UNKNOWN_ERROR} if an unknown error occurs,
|
||||
* {@link ResponseReason#OK} if the operation completed successfully.
|
||||
* Never returns <code>null</code>.
|
||||
*
|
||||
* @return {@link ResponseReason#DUPLICATE_ACCOUNT_GROUP_NAME} if an account group with the given name already
|
||||
* exists, {@link ResponseReason#UNKNOWN_ERROR} if an unknown error occurs, {@link ResponseReason#OK} if the
|
||||
* operation completed successfully. Never returns <code>null</code>.
|
||||
*/
|
||||
@Transactional(propagation = Propagation.SUPPORTS)
|
||||
public ResponseReason createAccountGroup(String name) {
|
||||
@@ -52,13 +64,11 @@ public class AccountGroupService {
|
||||
|
||||
try {
|
||||
this.accountGroupRepository.save(accountGroup);
|
||||
}
|
||||
catch (DataIntegrityViolationException dive) {
|
||||
} catch (DataIntegrityViolationException dive) {
|
||||
LOGGER.error(String.format("Duplicate account group name! %s", name), dive);
|
||||
|
||||
return ResponseReason.DUPLICATE_ACCOUNT_GROUP_NAME;
|
||||
}
|
||||
catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(String.format("Could not save account group %s", name), e);
|
||||
|
||||
return ResponseReason.UNKNOWN_ERROR;
|
||||
@@ -66,4 +76,22 @@ public class AccountGroupService {
|
||||
|
||||
return ResponseReason.OK;
|
||||
}
|
||||
|
||||
public Iterable<AccountGroupExpense> getAccountGroupExpenses(String periodStart, String periodEnd) {
|
||||
LocalDate startDate;
|
||||
LocalDate endDate;
|
||||
|
||||
try {
|
||||
startDate = LocalDate.parse(periodStart, DateTimeFormatter.ofPattern(this.financerConfig.getDateFormat()));
|
||||
endDate = LocalDate.parse(periodEnd, DateTimeFormatter.ofPattern(this.financerConfig.getDateFormat()));
|
||||
} catch (DateTimeParseException dtpe) {
|
||||
LOGGER.error(String
|
||||
.format("Could not parse periodStart (%s) or periodEnd (%s): %s", periodStart, periodEnd, dtpe
|
||||
.getMessage()));
|
||||
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return this.accountGroupRepository.getAccountGroupExpenses(startDate, endDate, AccountType.LIABILITY, AccountType.EXPENSE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
UPDATE account SET account_group_id = (SELECT id FROM account_group WHERE name = 'Car') WHERE "key" IN ('Car', 'Gas')
|
||||
|
||||
UPDATE account SET account_group_id = (SELECT id FROM account_group WHERE name = 'Housing') WHERE "key" IN ('Rent', 'FVS', 'Electricity/Water')
|
||||
Reference in New Issue
Block a user