-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#724 - introduced TransactionImportService
- Loading branch information
1 parent
fafc9cd
commit bb568bc
Showing
2 changed files
with
85 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...rver/src/main/java/de/deadlocker8/budgetmaster/transactions/TransactionImportService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package de.deadlocker8.budgetmaster.transactions; | ||
|
||
import de.deadlocker8.budgetmaster.categories.Category; | ||
import de.deadlocker8.budgetmaster.categories.CategoryService; | ||
import de.deadlocker8.budgetmaster.categories.CategoryType; | ||
import de.deadlocker8.budgetmaster.services.HelpersService; | ||
import de.deadlocker8.budgetmaster.settings.SettingsService; | ||
import de.deadlocker8.budgetmaster.transactions.csvimport.*; | ||
import de.thecodelabs.utils.util.Localization; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDate; | ||
import java.util.Optional; | ||
|
||
@Service | ||
public class TransactionImportService | ||
{ | ||
private static final Logger LOGGER = LoggerFactory.getLogger(TransactionImportService.class); | ||
|
||
private final HelpersService helpersService; | ||
private final SettingsService settingsService; | ||
private final CategoryService categoryService; | ||
|
||
@Autowired | ||
public TransactionImportService(HelpersService helpersService, SettingsService settingsService, CategoryService categoryService) | ||
{ | ||
this.helpersService = helpersService; | ||
this.settingsService = settingsService; | ||
this.categoryService = categoryService; | ||
} | ||
|
||
public CsvTransaction createCsvTransactionFromCsvRow(CsvRow csvRow, CsvColumnSettings csvColumnSettings, Integer index) throws CsvTransactionParseException | ||
{ | ||
final String date = csvRow.getColumns().get(csvColumnSettings.columnDate() - 1); | ||
final Optional<LocalDate> parsedDateOptional = DateParser.parse(date, csvColumnSettings.getDatePattern(), settingsService.getSettings().getLanguage().getLocale()); | ||
if(parsedDateOptional.isEmpty()) | ||
{ | ||
throw new CsvTransactionParseException(Localization.getString("transactions.import.error.parse.date", index + 1, date, csvColumnSettings.getDatePattern())); | ||
} | ||
|
||
final String name = csvRow.getColumns().get(csvColumnSettings.columnName() - 1); | ||
final String description = csvRow.getColumns().get(csvColumnSettings.columnDescription() - 1); | ||
|
||
final String amount = csvRow.getColumns().get(csvColumnSettings.columnAmount() - 1); | ||
final Optional<Integer> parsedAmountOptional = AmountParser.parse(amount); | ||
if(parsedAmountOptional.isEmpty()) | ||
{ | ||
throw new CsvTransactionParseException(Localization.getString("transactions.import.error.parse.amount", index + 1)); | ||
} | ||
|
||
final Category categoryNone = categoryService.findByType(CategoryType.NONE); | ||
return new CsvTransaction(parsedDateOptional.get(), name, parsedAmountOptional.get(), description, CsvTransactionStatus.PENDING, categoryNone); | ||
} | ||
|
||
public Transaction createTransactionFromCsvTransaction(CsvTransaction csvTransaction) | ||
{ | ||
final Transaction newTransaction = new Transaction(); | ||
newTransaction.setDate(csvTransaction.getDate()); | ||
newTransaction.setName(csvTransaction.getName()); | ||
newTransaction.setDescription(csvTransaction.getDescription()); | ||
newTransaction.setAmount(csvTransaction.getAmount()); | ||
newTransaction.setIsExpenditure(csvTransaction.getAmount() <= 0); | ||
newTransaction.setAccount(helpersService.getCurrentAccountOrDefault()); | ||
newTransaction.setCategory(csvTransaction.getCategory()); | ||
|
||
return newTransaction; | ||
} | ||
|
||
public void updateCsvTransaction(CsvTransaction existingCsvTransaction, CsvTransaction csvTransactionWithNewAttributes) | ||
{ | ||
existingCsvTransaction.setName(csvTransactionWithNewAttributes.getName()); | ||
existingCsvTransaction.setDescription(csvTransactionWithNewAttributes.getDescription()); | ||
existingCsvTransaction.setCategory(csvTransactionWithNewAttributes.getCategory()); | ||
} | ||
} |