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

Fixed #704 - unset accounts in templates on account delete

parent 3ec26d79
No related branches found
No related tags found
No related merge requests found
......@@ -7,6 +7,7 @@ import de.deadlocker8.budgetmaster.icon.IconService;
import de.deadlocker8.budgetmaster.services.AccessAllEntities;
import de.deadlocker8.budgetmaster.services.AccessEntityByID;
import de.deadlocker8.budgetmaster.services.Resettable;
import de.deadlocker8.budgetmaster.templates.TemplateService;
import de.deadlocker8.budgetmaster.transactions.TransactionService;
import de.deadlocker8.budgetmaster.utils.Strings;
import de.thecodelabs.utils.util.Localization;
......@@ -31,14 +32,16 @@ public class AccountService implements Resettable, AccessAllEntities<Account>, A
private final AccountRepository accountRepository;
private final TransactionService transactionService;
private final TemplateService templateService;
private final UserRepository userRepository;
private final IconService iconService;
@Autowired
public AccountService(AccountRepository accountRepository, TransactionService transactionService, UserRepository userRepository, IconService iconService)
public AccountService(AccountRepository accountRepository, TransactionService transactionService, TemplateService templateService, UserRepository userRepository, IconService iconService)
{
this.accountRepository = accountRepository;
this.transactionService = transactionService;
this.templateService = templateService;
this.userRepository = userRepository;
this.iconService = iconService;
......@@ -95,6 +98,8 @@ public class AccountService implements Resettable, AccessAllEntities<Account>, A
transactionService.deleteTransactionsWithAccount(accountToDelete);
accountToDelete.setReferringTransactions(new ArrayList<>());
templateService.unsetTemplatesWithAccount(accountToDelete);
// select "all accounts" as selected account
selectAccount(accountRepository.findAllByType(AccountType.ALL).get(0).getID());
......
......@@ -155,7 +155,7 @@ public class TemplateController extends BaseController
newTransaction.setTags(template.getTags());
newTransaction.setIsExpenditure(template.isExpenditure());
templateService.prepareTemplateForNewTransaction(newTransaction, true);
templateService.prepareTemplateForNewTransaction(newTransaction, true, accountService.getSelectedAccountOrDefaultAsFallback());
if(newTransaction.getAmount() == null && newTransaction.isExpenditure() == null)
{
......@@ -176,7 +176,7 @@ public class TemplateController extends BaseController
public String newTemplate(Model model)
{
final Template emptyTemplate = new Template();
templateService.prepareTemplateForNewTransaction(emptyTemplate, false);
templateService.prepareTemplateForNewTransaction(emptyTemplate, false, accountService.getSelectedAccountOrDefaultAsFallback());
templateService.prepareModelNewOrEdit(model, false, emptyTemplate, accountService.getAllActivatedAccountsAsc());
return ReturnValues.NEW_ENTITY;
}
......@@ -260,7 +260,7 @@ public class TemplateController extends BaseController
}
Template template = templateOptional.get();
templateService.prepareTemplateForNewTransaction(template, false);
templateService.prepareTemplateForNewTransaction(template, false, accountService.getSelectedAccountOrDefaultAsFallback());
templateService.prepareModelNewOrEdit(model, true, template, accountService.getAllActivatedAccountsAsc());
return ReturnValues.NEW_ENTITY;
......
package de.deadlocker8.budgetmaster.templates;
import de.deadlocker8.budgetmaster.accounts.Account;
import de.deadlocker8.budgetmaster.tags.Tag;
import de.deadlocker8.budgetmaster.transactions.Transaction;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
......@@ -12,4 +14,8 @@ public interface TemplateRepository extends JpaRepository<Template, Integer>, Jp
List<Template> findAllByOrderByTemplateNameAsc();
List<Template> findAllByTagsContaining(Tag tag);
List<Template> findAllByAccount(Account account);
List<Template> findAllByTransferAccount(Account account);
}
\ No newline at end of file
......@@ -3,7 +3,6 @@ package de.deadlocker8.budgetmaster.templates;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import de.deadlocker8.budgetmaster.accounts.Account;
import de.deadlocker8.budgetmaster.accounts.AccountService;
import de.deadlocker8.budgetmaster.accounts.AccountState;
import de.deadlocker8.budgetmaster.categories.CategoryService;
import de.deadlocker8.budgetmaster.categories.CategoryType;
......@@ -37,15 +36,13 @@ public class TemplateService implements Resettable, AccessAllEntities<Template>,
.create();
private final TemplateRepository templateRepository;
private final AccountService accountService;
private final CategoryService categoryService;
private final TemplateGroupService templateGroupService;
@Autowired
public TemplateService(TemplateRepository templateRepository, AccountService accountService, CategoryService categoryService, TemplateGroupService templateGroupService)
public TemplateService(TemplateRepository templateRepository, CategoryService categoryService, TemplateGroupService templateGroupService)
{
this.templateRepository = templateRepository;
this.accountService = accountService;
this.categoryService = categoryService;
this.templateGroupService = templateGroupService;
......@@ -107,7 +104,7 @@ public class TemplateService implements Resettable, AccessAllEntities<Template>,
return getRepository().save(template);
}
public void prepareTemplateForNewTransaction(TransactionBase template, boolean prepareAccount)
public void prepareTemplateForNewTransaction(TransactionBase template, boolean prepareAccount, Account selectedOrFallbackAccount)
{
if(template.getCategory() == null)
{
......@@ -116,19 +113,19 @@ public class TemplateService implements Resettable, AccessAllEntities<Template>,
if(prepareAccount && template.getAccount() == null)
{
template.setAccount(accountService.getSelectedAccountOrDefaultAsFallback());
template.setAccount(selectedOrFallbackAccount);
}
final Account account = template.getAccount();
if(account != null && account.getAccountState() != AccountState.FULL_ACCESS)
{
template.setAccount(accountService.getSelectedAccountOrDefaultAsFallback());
template.setAccount(selectedOrFallbackAccount);
}
final Account transferAccount = template.getTransferAccount();
if(transferAccount != null && transferAccount.getAccountState() != AccountState.FULL_ACCESS)
{
template.setTransferAccount(accountService.getSelectedAccountOrDefaultAsFallback());
template.setTransferAccount(selectedOrFallbackAccount);
}
}
......@@ -162,4 +159,19 @@ public class TemplateService implements Resettable, AccessAllEntities<Template>,
{
return templateRepository.findById(ID);
}
public void unsetTemplatesWithAccount(Account account)
{
for(Template referringTemplate : templateRepository.findAllByAccount(account))
{
referringTemplate.setAccount(null);
templateRepository.save(referringTemplate);
}
for(Template referringTemplate : templateRepository.findAllByTransferAccount(account))
{
referringTemplate.setTransferAccount(null);
templateRepository.save(referringTemplate);
}
}
}
......@@ -5,6 +5,7 @@ import de.deadlocker8.budgetmaster.authentication.UserRepository;
import de.deadlocker8.budgetmaster.icon.Icon;
import de.deadlocker8.budgetmaster.icon.IconRepository;
import de.deadlocker8.budgetmaster.icon.IconService;
import de.deadlocker8.budgetmaster.templates.TemplateService;
import de.deadlocker8.budgetmaster.transactions.TransactionService;
import de.deadlocker8.budgetmaster.unit.helpers.LocalizedTest;
import de.deadlocker8.budgetmaster.utils.Strings;
......@@ -32,6 +33,9 @@ class AccountServiceTest
@Mock
private TransactionService transactionService;
@Mock
private TemplateService templateService;
@Mock
private UserRepository userRepository;
......@@ -82,7 +86,7 @@ class AccountServiceTest
Mockito.when(iconService.getRepository()).thenReturn(iconRepository);
Mockito.when(iconRepository.save(new Icon("fas fa-landmark", null))).thenReturn(ICON_PLACEHOLDER);
accountService = new AccountService(accountRepository, transactionService, userRepository, iconService);
accountService = new AccountService(accountRepository, transactionService, templateService, userRepository, iconService);
}
@Test
......@@ -152,6 +156,7 @@ class AccountServiceTest
accountService.deleteAccount(15);
Mockito.verify(transactionService, Mockito.atLeast(1)).deleteTransactionsWithAccount(Mockito.any());
Mockito.verify(templateService, Mockito.atLeast(1)).unsetTemplatesWithAccount(Mockito.any());
// placeholder account is set as selected account
final Account accountSelected = new Account("Placeholder", AccountType.ALL);
......
......@@ -58,7 +58,7 @@ class TemplateServiceTest
final Template expectedTemplate = new Template();
expectedTemplate.setCategory(CATEGORY_NONE);
templateService.prepareTemplateForNewTransaction(template, false);
templateService.prepareTemplateForNewTransaction(template, false, new Account("my account", AccountType.CUSTOM));
assertThat(template).isEqualTo(expectedTemplate);
}
......@@ -76,7 +76,7 @@ class TemplateServiceTest
expectedTemplate.setCategory(CATEGORY_NONE);
expectedTemplate.setAccount(account);
templateService.prepareTemplateForNewTransaction(template, false);
templateService.prepareTemplateForNewTransaction(template, false, account);
assertThat(template).isEqualTo(expectedTemplate);
}
......@@ -97,7 +97,7 @@ class TemplateServiceTest
expectedTemplate.setCategory(CATEGORY_NONE);
expectedTemplate.setAccount(ACCOUNT_SELECTED);
templateService.prepareTemplateForNewTransaction(template, false);
templateService.prepareTemplateForNewTransaction(template, false, ACCOUNT_SELECTED);
assertThat(template).isEqualTo(expectedTemplate);
}
......@@ -120,7 +120,7 @@ class TemplateServiceTest
expectedTemplate.setAccount(account);
expectedTemplate.setTransferAccount(transferAccount);
templateService.prepareTemplateForNewTransaction(template, false);
templateService.prepareTemplateForNewTransaction(template, false, account);
assertThat(template).isEqualTo(expectedTemplate);
}
......@@ -146,7 +146,7 @@ class TemplateServiceTest
expectedTemplate.setAccount(account);
expectedTemplate.setTransferAccount(ACCOUNT_SELECTED);
templateService.prepareTemplateForNewTransaction(template, false);
templateService.prepareTemplateForNewTransaction(template, false, ACCOUNT_SELECTED);
assertThat(template).isEqualTo(expectedTemplate);
}
......@@ -163,7 +163,7 @@ class TemplateServiceTest
expectedTemplate.setCategory(CATEGORY_NONE);
expectedTemplate.setAccount(ACCOUNT_SELECTED);
templateService.prepareTemplateForNewTransaction(template, true);
templateService.prepareTemplateForNewTransaction(template, true, ACCOUNT_SELECTED);
assertThat(template).isEqualTo(expectedTemplate);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment