null
+ * @param accountGroupName the name of the account group to use
*
* @return {@link ResponseReason#INVALID_ACCOUNT_TYPE} if the given type is not a valid {@link AccountType}, {@link
* ResponseReason#UNKNOWN_ERROR} if an unexpected error occurs, {@link ResponseReason#OK} if the operation completed
* successfully, {@link ResponseReason#DUPLICATE_ACCOUNT_KEY} if an account with the given key already exists and
- * {@link ResponseReason#ACCOUNT_GROUP_NOT_FOUND} if the optional parameter
+ * {@link ResponseReason#ACCOUNT_GROUP_NOT_FOUND} if the parameter
* accountGroupName does not identify a valid account group. Never returns null.
*/
@Transactional(propagation = Propagation.SUPPORTS)
@@ -87,17 +87,14 @@ public class AccountService {
}
final Account account = new Account();
+ final AccountGroup accountGroup = this.accountGroupService.getAccountGroupByName(accountGroupName);
- if (StringUtils.isNotEmpty(accountGroupName)) {
- final AccountGroup accountGroup = this.accountGroupService.getAccountGroupByName(accountGroupName);
-
- if (accountGroup == null) {
- return ResponseReason.ACCOUNT_GROUP_NOT_FOUND; // early return
- }
-
- account.setAccountGroup(accountGroup);
+ if (accountGroup == null) {
+ return ResponseReason.ACCOUNT_GROUP_NOT_FOUND; // early return
}
+ account.setAccountGroup(accountGroup);
+
account.setKey(key);
account.setType(AccountType.valueOf(type));
// If we create an account it's implicitly open
@@ -120,6 +117,51 @@ public class AccountService {
return ResponseReason.OK;
}
+ /**
+ * This method edits the account with the given id.
+ *
+ * @param id the id of the account to edit
+ * @param key the new key of the account
+ * @param accountGroupName the new name of the account group to use
+ *
+ * @return {@link ResponseReason#OK} if the operation completed successfully, {@link ResponseReason#UNKNOWN_ERROR}
+ * if an unexpected error occurs, {@link ResponseReason#DUPLICATE_ACCOUNT_KEY} if an account with the given key
+ * already exists and {@link ResponseReason#ACCOUNT_GROUP_NOT_FOUND} if the parameter
+ * accountGroupName does not identify a valid account group or {@link ResponseReason#ACCOUNT_NOT_FOUND}
+ * if the given id does not identify a valid account. Never returns null.
+ */
+ @Transactional(propagation = Propagation.REQUIRED)
+ public ResponseReason editAccount(Long id, String key, String accountGroupName) {
+ final Account account = this.accountRepository.findById(id).orElse(null);
+
+ if(account == null) {
+ return ResponseReason.ACCOUNT_NOT_FOUND;
+ }
+
+ final AccountGroup accountGroup = this.accountGroupService.getAccountGroupByName(accountGroupName);
+
+ if (accountGroup == null) {
+ return ResponseReason.ACCOUNT_GROUP_NOT_FOUND;
+ }
+
+ account.setKey(key);
+ account.setAccountGroup(accountGroup);
+
+ try {
+ this.accountRepository.save(account);
+ } catch (DataIntegrityViolationException dive) {
+ LOGGER.error(String.format("Duplicate key! %s|%s", key, accountGroupName), dive);
+
+ return ResponseReason.DUPLICATE_ACCOUNT_KEY;
+ } catch (Exception e) {
+ LOGGER.error(String.format("Could not save account %s|%s", key, accountGroupName), e);
+
+ return ResponseReason.UNKNOWN_ERROR;
+ }
+
+ return ResponseReason.OK;
+ }
+
@Transactional(propagation = Propagation.REQUIRED)
public ResponseReason closeAccount(String key) {
return setAccountStatus(key, AccountStatus.CLOSED);
diff --git a/financer-server/src/test/java/de/financer/service/AccountService_createAccountTest.java b/financer-server/src/test/java/de/financer/service/AccountService_createAccountTest.java
index 5d5d965..a485a65 100644
--- a/financer-server/src/test/java/de/financer/service/AccountService_createAccountTest.java
+++ b/financer-server/src/test/java/de/financer/service/AccountService_createAccountTest.java
@@ -3,6 +3,7 @@ package de.financer.service;
import de.financer.ResponseReason;
import de.financer.dba.AccountRepository;
import de.financer.model.Account;
+import de.financer.model.AccountGroup;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -40,9 +41,11 @@ public class AccountService_createAccountTest {
public void test_createAccount_UNKNOWN_ERROR() {
// Arrange
Mockito.doThrow(new NullPointerException()).when(this.accountRepository).save(Mockito.any(Account.class));
+ Mockito.when(this.accountGroupService.getAccountGroupByName(Mockito.anyString()))
+ .thenReturn(Mockito.mock(AccountGroup.class));
// Act
- ResponseReason response = this.classUnderTest.createAccount("Test", "BANK", null);
+ ResponseReason response = this.classUnderTest.createAccount("Test", "BANK", "Group1");
// Assert
Assert.assertEquals(ResponseReason.UNKNOWN_ERROR, response);
@@ -51,10 +54,11 @@ public class AccountService_createAccountTest {
@Test
public void test_createAccount_OK() {
// Arrange
- // Nothing to do
+ Mockito.when(this.accountGroupService.getAccountGroupByName(Mockito.anyString()))
+ .thenReturn(Mockito.mock(AccountGroup.class));
// Act
- ResponseReason response = this.classUnderTest.createAccount("Test", "BANK", null);
+ ResponseReason response = this.classUnderTest.createAccount("Test", "BANK", "Group1");
// Assert
Assert.assertEquals(ResponseReason.OK, response);
@@ -79,9 +83,11 @@ public class AccountService_createAccountTest {
public void test_createAccount_DUPLICATE_ACCOUNT_KEY() {
// Arrange
Mockito.doThrow(new DataIntegrityViolationException("DIVE")).when(this.accountRepository).save(Mockito.any(Account.class));
+ Mockito.when(this.accountGroupService.getAccountGroupByName(Mockito.anyString()))
+ .thenReturn(Mockito.mock(AccountGroup.class));
// Act
- ResponseReason response = this.classUnderTest.createAccount("Test", "BANK", null);
+ ResponseReason response = this.classUnderTest.createAccount("Test", "BANK", "Group1");
// Assert
Assert.assertEquals(ResponseReason.DUPLICATE_ACCOUNT_KEY, response);
diff --git a/financer-web-client/src/main/java/de/financer/controller/AccountController.java b/financer-web-client/src/main/java/de/financer/controller/AccountController.java
index 97bb17c..b9ae470 100644
--- a/financer-web-client/src/main/java/de/financer/controller/AccountController.java
+++ b/financer-web-client/src/main/java/de/financer/controller/AccountController.java
@@ -5,6 +5,7 @@ import de.financer.config.FinancerConfig;
import de.financer.decorator.AccountDecorator;
import de.financer.dto.Order;
import de.financer.dto.SearchTransactionsResponseDto;
+import de.financer.form.EditAccountForm;
import de.financer.form.NewAccountForm;
import de.financer.model.*;
import de.financer.notification.Notification;
@@ -17,6 +18,7 @@ import de.financer.util.TransactionUtils;
import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
+import org.jfree.data.resources.DataPackageResources_es;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
@@ -30,6 +32,7 @@ import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
+import java.util.Optional;
import java.util.stream.Collectors;
@Controller
@@ -87,7 +90,7 @@ public class AccountController {
}
@PostMapping("/saveAccount")
- public String saveAccont(NewAccountForm form, Model model) {
+ public String saveAccount(NewAccountForm form, Model model) {
final UriComponentsBuilder builder = UriComponentsBuilder
.fromHttpUrl(ControllerUtils.buildUrl(this.financerConfig, Function.ACC_CREATE_ACCOUNT))
.queryParam("key", form.getKey())
@@ -225,6 +228,56 @@ public class AccountController {
return "redirect:" + navigateTo;
}
+ @GetMapping("/editAccount")
+ public String editAccount(Model model, String key) {
+ _editAccount(model, key, Optional.empty(), Optional.empty());
+
+ return "account/editAccount";
+ }
+
+ private void _editAccount(Model model, String originalKey, Optional