Skip to content
Snippets Groups Projects
Commit 05bdd4d5 authored by Robert Goldmann's avatar Robert Goldmann
Browse files

#535 - fix null value for new field "isExpenditure" for existing transactions...

#535 - fix null value for new field "isExpenditure" for existing transactions in database once on start
parent 85e26ea3
No related branches found
No related tags found
No related merge requests found
Pipeline #3675 passed
package de.deadlocker8.budgetmaster.utils.eventlistener;
import de.deadlocker8.budgetmaster.settings.SettingsService;
import de.deadlocker8.budgetmaster.transactions.Transaction;
import de.deadlocker8.budgetmaster.transactions.TransactionRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.text.MessageFormat;
import java.util.List;
@Component
public class IntroduceIsExpenditureMember
{
private static final Logger LOGGER = LoggerFactory.getLogger(IntroduceIsExpenditureMember.class);
private static final int ACTIVATION_VERSION_CODE = 27;
private final TransactionRepository transactionRepository;
private final SettingsService settingsService;
@Autowired
public IntroduceIsExpenditureMember(TransactionRepository transactionRepository, SettingsService settingsService)
{
this.transactionRepository = transactionRepository;
this.settingsService = settingsService;
}
@EventListener
@Transactional
@Order(2)
public void onApplicationEvent(ApplicationStartedEvent event)
{
if(settingsService.getSettings().getInstalledVersionCode() <= ACTIVATION_VERSION_CODE)
{
fixMissingMemberIsExpenditure();
}
}
private void fixMissingMemberIsExpenditure()
{
final List<Transaction> transactions = transactionRepository.findAll();
long fixedTransactionsCount = 0;
for(Transaction transaction : transactions)
{
if(transaction.isExpenditure() != null)
{
continue;
}
transaction.setExpenditure(transaction.getAmount() <= 0);
fixedTransactionsCount++;
}
if(fixedTransactionsCount > 0)
{
LOGGER.debug(MessageFormat.format("Fixed {0} transactions (Introduced new member 'isExpenditure')", fixedTransactionsCount));
}
}
}
......@@ -28,7 +28,7 @@ public class UpdateInstalledVersion
@EventListener
@Transactional
@Order(2)
@Order(3)
public void onApplicationEvent(ApplicationStartedEvent event)
{
final Build build = Build.getInstance();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment