diff --git a/src/main/java/org/isf/admtype/manager/AdmissionTypeBrowserManager.java b/src/main/java/org/isf/admtype/manager/AdmissionTypeBrowserManager.java index 16a53c3df..391e5858d 100644 --- a/src/main/java/org/isf/admtype/manager/AdmissionTypeBrowserManager.java +++ b/src/main/java/org/isf/admtype/manager/AdmissionTypeBrowserManager.java @@ -27,14 +27,16 @@ import org.isf.admtype.model.AdmissionType; import org.isf.admtype.service.AdmissionTypeIoOperation; import org.isf.generaldata.MessageBundle; -import org.isf.utils.exception.OHDataValidationException; import org.isf.utils.exception.OHServiceException; -import org.isf.utils.exception.model.OHExceptionMessage; +import org.isf.utils.tuple.JPAImmutableTriple; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class AdmissionTypeBrowserManager { + private static final Logger LOGGER = LoggerFactory.getLogger(AdmissionTypeBrowserManager.class); @Autowired private AdmissionTypeIoOperation ioOperations; @@ -42,7 +44,7 @@ public class AdmissionTypeBrowserManager { /** * Returns all the available {@link AdmissionType}s. * - * @return a list of admission types or null if the operation fails. + * @return a list of admission types or {@code null} if the operation fails. * @throws OHServiceException */ public List getAdmissionType() throws OHServiceException { @@ -52,78 +54,96 @@ public List getAdmissionType() throws OHServiceException { /** * Stores a new {@link AdmissionType}. * - * @param admissionType the admission type to store. - * @return true if the admission type has been stored, false otherwise. - * @throws OHServiceException + * @param admissionType the admission type object to store. + * @return {@code JPAImmutableTriple} */ - public boolean newAdmissionType(AdmissionType admissionType) throws OHServiceException { - validateAdmissionType(admissionType, true); - return ioOperations.newAdmissionType(admissionType); + public JPAImmutableTriple newAdmissionType(AdmissionType admissionType) { + List errors = validateAdmissionType(admissionType, true); + if (!errors.isEmpty()) { + return new JPAImmutableTriple(false, null, errors); + } + try { + AdmissionType newAdmissionType = ioOperations.newAdmissionType(admissionType); + return new JPAImmutableTriple(true, newAdmissionType, null); + } catch (OHServiceException ohServiceException) { + errors.add(ohServiceException.getMessage()); + return new JPAImmutableTriple(false, null, errors); + } } /** * Updates the specified {@link AdmissionType}. * * @param admissionType the admission type to update. - * @return true if the admission type has been updated, false otherwise. - * @throws OHServiceException + * @return {@code JPAImmutableTriple} */ - public boolean updateAdmissionType(AdmissionType admissionType) throws OHServiceException { - validateAdmissionType(admissionType, false); - return ioOperations.updateAdmissionType(admissionType); + public JPAImmutableTriple updateAdmissionType(AdmissionType admissionType) { + List errors = validateAdmissionType(admissionType, false); + if (!errors.isEmpty()) { + return new JPAImmutableTriple(false, null, errors); + } + try { + AdmissionType updatedAdmissionType = ioOperations.updateAdmissionType(admissionType); + return new JPAImmutableTriple(true, updatedAdmissionType, null); + } catch (OHServiceException ohServiceException) { + errors.add(ohServiceException.getMessage()); + return new JPAImmutableTriple(false, null, errors); + } } /** * Checks if the specified Code is already used by others {@link AdmissionType}s. * * @param code the admission type code to check. - * @return true if the code is already used, false otherwise. - * @throws OHServiceException + * @return {@code true} if the code is already used, {@code false} otherwise. */ - public boolean isCodePresent(String code) throws OHServiceException { - return ioOperations.isCodePresent(code); + public boolean isCodePresent(String code) { + try { + return ioOperations.isCodePresent(code); + } catch (OHServiceException ohServiceException) { + LOGGER.error(ohServiceException.getMessage(), ohServiceException); + return false; + } } /** * Deletes the specified {@link AdmissionType}. * * @param admissionType the admission type to delete. - * @return true if the admission type has been deleted, false otherwise. - * @throws OHServiceException + * @return {@code true} if the admission type has been deleted, {@code false} otherwise. */ - public boolean deleteAdmissionType(AdmissionType admissionType) throws OHServiceException { - return ioOperations.deleteAdmissionType(admissionType); + public boolean deleteAdmissionType(AdmissionType admissionType) { + try { + ioOperations.deleteAdmissionType(admissionType); + return true; + } catch (OHServiceException ohServiceException) { + LOGGER.error(ohServiceException.getMessage(), ohServiceException); + return false; + } } /** * Verify if the object is valid for CRUD and return a list of errors, if any * * @param admissionType - * @param insert true or updated false - * @throws OHDataValidationException + * @param insert {@code true} or updated {@code false} */ - protected void validateAdmissionType(AdmissionType admissionType, boolean insert) throws OHServiceException { - List errors = new ArrayList<>(); + protected List validateAdmissionType(AdmissionType admissionType, boolean insert) { + List errors = new ArrayList<>(); String key = admissionType.getCode(); - if (key.equals("")) { - errors.add(new OHExceptionMessage(MessageBundle.getMessage("angal.common.pleaseinsertacode.msg"))); + if (key.isEmpty()) { + errors.add(MessageBundle.getMessage("angal.common.pleaseinsertacode.msg")); } if (key.length() > 10) { - errors.add(new OHExceptionMessage(MessageBundle.formatMessage("angal.common.thecodeistoolongmaxchars.fmt.msg", 10))); + errors.add(MessageBundle.formatMessage("angal.common.thecodeistoolongmaxchars.fmt.msg", 10)); } - - if (insert) { - if (isCodePresent(key)) { - errors.add(new OHExceptionMessage(MessageBundle.getMessage("angal.common.thecodeisalreadyinuse.msg"))); - } - } - if (admissionType.getDescription().equals("")) { - errors.add( - new OHExceptionMessage(MessageBundle.getMessage("angal.common.pleaseinsertavaliddescription.msg"))); + if (insert && isCodePresent(key)) { + errors.add(MessageBundle.getMessage("angal.common.thecodeisalreadyinuse.msg")); } - if (!errors.isEmpty()) { - throw new OHDataValidationException(errors); + if (admissionType.getDescription().isEmpty()) { + errors.add(MessageBundle.getMessage("angal.common.pleaseinsertavaliddescription.msg")); } + return errors; } } diff --git a/src/main/java/org/isf/admtype/service/AdmissionTypeIoOperation.java b/src/main/java/org/isf/admtype/service/AdmissionTypeIoOperation.java index b89b42307..d5d236c7b 100644 --- a/src/main/java/org/isf/admtype/service/AdmissionTypeIoOperation.java +++ b/src/main/java/org/isf/admtype/service/AdmissionTypeIoOperation.java @@ -54,38 +54,36 @@ public List getAdmissionType() throws OHServiceException { /** * Updates the specified {@link AdmissionType}. * @param admissionType the admission type to update. - * @return true if the admission type has been updated, false otherwise. + * @return the updated {@link AdmissionType} object. * @throws OHServiceException if an error occurs during the update. */ - public boolean updateAdmissionType(AdmissionType admissionType) throws OHServiceException { - return repository.save(admissionType) != null; + public AdmissionType updateAdmissionType(AdmissionType admissionType) throws OHServiceException { + return repository.save(admissionType); } /** * Stores a new {@link AdmissionType}. * @param admissionType the admission type to store. - * @return true if the admission type has been stored, false otherwise. + * @return the new {@link AdmissionType} object. * @throws OHServiceException if an error occurs during the storing operation. */ - public boolean newAdmissionType(AdmissionType admissionType) throws OHServiceException { - return repository.save(admissionType) != null; + public AdmissionType newAdmissionType(AdmissionType admissionType) throws OHServiceException { + return repository.save(admissionType); } /** * Deletes the specified {@link AdmissionType}. * @param admissionType the admission type to delete. - * @return true if the admission type has been deleted, false otherwise. * @throws OHServiceException if an error occurs during the delete operation. */ - public boolean deleteAdmissionType(AdmissionType admissionType) throws OHServiceException { + public void deleteAdmissionType(AdmissionType admissionType) throws OHServiceException { repository.delete(admissionType); - return true; } /** * Checks if the specified Code is already used by others {@link AdmissionType}s. * @param code the admission type code to check. - * @return true if the code is already used, false otherwise. + * @return {@code true} if the code is already used, false otherwise. * @throws OHServiceException if an error occurs during the check. */ public boolean isCodePresent(String code) throws OHServiceException { diff --git a/src/main/java/org/isf/utils/tuple/JPAImmutableTriple.java b/src/main/java/org/isf/utils/tuple/JPAImmutableTriple.java new file mode 100644 index 000000000..4d326dfc3 --- /dev/null +++ b/src/main/java/org/isf/utils/tuple/JPAImmutableTriple.java @@ -0,0 +1,66 @@ +/* + * Open Hospital (www.open-hospital.org) + * Copyright © 2006-2023 Informatici Senza Frontiere (info@informaticisenzafrontiere.org) + * + * Open Hospital is a free and open source software for healthcare data management. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * https://www.gnu.org/licenses/gpl-3.0-standalone.html + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.isf.utils.tuple; + +import java.util.List; + +public final class JPAImmutableTriple { + + private boolean result; + private Object object; + private List errors; + + /** + * Create a new JPA immutable triple instance. + * + * @param result the Boolean result of the action. + * @param object the object of the action. + * @param errors the {@code List} of formatted error messages. + */ + public JPAImmutableTriple(boolean result, Object object, List errors) { + this.result = result; + this.object = object; + this.errors = errors; + } + + /** + * @return {@code true} if successful, otherwise {@code false}. + */ + public boolean getResult() { + return result; + } + + /** + * @return the object associated with the operation. + */ + public Object getObject() { + return object; + } + + /** + * @return {@code List} of formatted message text + */ + public List getErrors() { + return errors; + } + +} diff --git a/src/test/java/org/isf/admtype/test/Tests.java b/src/test/java/org/isf/admtype/test/Tests.java index deaf08234..8442698e7 100644 --- a/src/test/java/org/isf/admtype/test/Tests.java +++ b/src/test/java/org/isf/admtype/test/Tests.java @@ -22,18 +22,16 @@ package org.isf.admtype.test; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.util.List; -import org.assertj.core.api.Condition; import org.isf.OHCoreTestCase; import org.isf.admtype.manager.AdmissionTypeBrowserManager; import org.isf.admtype.model.AdmissionType; import org.isf.admtype.service.AdmissionTypeIoOperation; import org.isf.admtype.service.AdmissionTypeIoOperationRepository; import org.isf.utils.exception.OHException; -import org.isf.utils.exception.OHServiceException; +import org.isf.utils.tuple.JPAImmutableTriple; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; @@ -74,7 +72,7 @@ public void testAdmissionTypeSets() throws Exception { @Test public void testIoGetAdmissionType() throws Exception { - String code = setupTestAdmissionType(false); + setupTestAdmissionType(false); List admissionTypes = admissionTypeIoOperation.getAdmissionType(); assertThat(admissionTypes).hasSize(1); assertThat(admissionTypes.get(0).getDescription()).isEqualTo("TestDescription"); @@ -83,20 +81,18 @@ public void testIoGetAdmissionType() throws Exception { @Test public void testIoUpdateAdmissionType() throws Exception { String code = setupTestAdmissionType(false); - AdmissionType foundAdmissionType = admissionTypeIoOperationRepository.findById(code).get(); - foundAdmissionType.setDescription("Update"); - boolean result = admissionTypeIoOperation.updateAdmissionType(foundAdmissionType); - assertThat(result).isTrue(); - AdmissionType updateAdmissionType = admissionTypeIoOperationRepository.findById(code).get(); - assertThat(updateAdmissionType.getDescription()).isEqualTo("Update"); + AdmissionType admissionType = admissionTypeIoOperationRepository.findById(code).orElse(null); + assertThat(admissionType).isNotNull(); + admissionType.setDescription("Update"); + AdmissionType updatedAdmissionType = admissionTypeIoOperation.updateAdmissionType(admissionType); + assertThat(updatedAdmissionType.getDescription()).isEqualTo("Update"); } @Test public void testIoNewAdmissionType() throws Exception { AdmissionType admissionType = testAdmissionType.setup(true); - boolean result = admissionTypeIoOperation.newAdmissionType(admissionType); - assertThat(result).isTrue(); - checkAdmissionTypeIntoDb(admissionType.getCode()); + AdmissionType newAdmissionType = admissionTypeIoOperation.newAdmissionType(admissionType); + checkAdmissionTypeIntoDb(newAdmissionType.getCode()); } @Test @@ -109,16 +105,15 @@ public void testIoIsCodePresent() throws Exception { @Test public void testIoDeleteAdmissionType() throws Exception { String code = setupTestAdmissionType(false); - AdmissionType foundAdmissionType = admissionTypeIoOperationRepository.findById(code).get(); - boolean result = admissionTypeIoOperation.deleteAdmissionType(foundAdmissionType); - assertThat(result).isTrue(); - result = admissionTypeIoOperation.isCodePresent(code); - assertThat(result).isFalse(); + AdmissionType foundAdmissionType = admissionTypeIoOperationRepository.findById(code).orElse(null); + assertThat(foundAdmissionType).isNotNull(); + admissionTypeIoOperation.deleteAdmissionType(foundAdmissionType); + assertThat(admissionTypeIoOperation.isCodePresent(code)).isFalse(); } @Test public void testMgrGetAdmissionType() throws Exception { - String code = setupTestAdmissionType(false); + setupTestAdmissionType(false); List admissionTypes = admissionTypeBrowserManager.getAdmissionType(); assertThat(admissionTypes).hasSize(1); assertThat(admissionTypes.get(0).getDescription()).isEqualTo("TestDescription"); @@ -127,21 +122,22 @@ public void testMgrGetAdmissionType() throws Exception { @Test public void testMgrUpdateAdmissionType() throws Exception { String code = setupTestAdmissionType(false); - AdmissionType foundAdmissionType = admissionTypeIoOperationRepository.findById(code).get(); + AdmissionType foundAdmissionType = admissionTypeIoOperationRepository.findById(code).orElse(null); + assertThat(foundAdmissionType).isNotNull(); foundAdmissionType.setDescription("Update"); - boolean result = admissionTypeBrowserManager.updateAdmissionType(foundAdmissionType); - assertThat(result).isTrue(); - AdmissionType updateAdmissionType = admissionTypeIoOperationRepository.findById(code).get(); - assertThat(updateAdmissionType.getDescription()).isEqualTo("Update"); + JPAImmutableTriple results = admissionTypeBrowserManager.updateAdmissionType(foundAdmissionType); + assertThat(results.getResult()).isTrue(); + assertThat(((AdmissionType)results.getObject()).getDescription()).isEqualTo("Update"); } @Test public void testAdmissionTypeEqualHashToString() throws Exception { String code = setupTestAdmissionType(false); - AdmissionType admissionType = admissionTypeIoOperationRepository.findById(code).get(); + AdmissionType admissionType = admissionTypeIoOperationRepository.findById(code).orElse(null); + assertThat(admissionType).isNotNull(); AdmissionType admissionType2 = new AdmissionType("someCode", "someDescription"); - assertThat(admissionType).isEqualTo(admissionType); assertThat(admissionType) + .isEqualTo(admissionType) .isNotEqualTo(admissionType2) .isNotEqualTo("xyzzy"); admissionType2.setCode(code); @@ -155,50 +151,37 @@ public void testAdmissionTypeEqualHashToString() throws Exception { @Test public void testMgrAdmissionValidation() throws Exception { String code = setupTestAdmissionType(false); - AdmissionType admissionType = admissionTypeIoOperationRepository.findById(code).get(); + AdmissionType admissionType = admissionTypeIoOperationRepository.findById(code).orElse(null); + assertThat(admissionType).isNotNull(); // Empty string admissionType.setCode(""); - assertThatThrownBy(() -> admissionTypeBrowserManager.updateAdmissionType(admissionType)) - .isInstanceOf(OHServiceException.class) - .has( - new Condition( - (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error") - ); + JPAImmutableTriple results = admissionTypeBrowserManager.updateAdmissionType(admissionType); + assertThat(results.getErrors()).hasSize(1); + // Code is too long admissionType.setCode("123456789ABCDEF"); - assertThatThrownBy(() -> admissionTypeBrowserManager.updateAdmissionType(admissionType)) - .isInstanceOf(OHServiceException.class) - .has( - new Condition( - (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error") - ); + results = admissionTypeBrowserManager.updateAdmissionType(admissionType); + assertThat(results.getErrors()).hasSize(1); + // Description is empty admissionType.setCode(code); String description = admissionType.getDescription(); admissionType.setDescription(""); - assertThatThrownBy(() -> admissionTypeBrowserManager.updateAdmissionType(admissionType)) - .isInstanceOf(OHServiceException.class) - .has( - new Condition( - (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error") - ); + results = admissionTypeBrowserManager.updateAdmissionType(admissionType); + assertThat(results.getErrors()).hasSize(1); + // Code already exists admissionType.setDescription(description); - assertThatThrownBy(() -> admissionTypeBrowserManager.newAdmissionType(admissionType)) - .isInstanceOf(OHServiceException.class) - .has( - new Condition( - (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error") - ); - + results = admissionTypeBrowserManager.newAdmissionType(admissionType); + assertThat(results.getErrors()).hasSize(1); } @Test public void testMgrNewAdmissionType() throws Exception { AdmissionType admissionType = testAdmissionType.setup(true); - boolean result = admissionTypeBrowserManager.newAdmissionType(admissionType); - assertThat(result).isTrue(); + JPAImmutableTriple results = admissionTypeBrowserManager.newAdmissionType(admissionType); + assertThat(results.getResult()).isTrue(); checkAdmissionTypeIntoDb(admissionType.getCode()); } @@ -212,7 +195,8 @@ public void testMgrIsCodePresent() throws Exception { @Test public void testMgrDeleteAdmissionType() throws Exception { String code = setupTestAdmissionType(false); - AdmissionType foundAdmissionType = admissionTypeIoOperationRepository.findById(code).get(); + AdmissionType foundAdmissionType = admissionTypeIoOperationRepository.findById(code).orElse(null); + assertThat(foundAdmissionType).isNotNull(); boolean result = admissionTypeBrowserManager.deleteAdmissionType(foundAdmissionType); assertThat(result).isTrue(); result = admissionTypeBrowserManager.isCodePresent(code); @@ -226,7 +210,8 @@ private String setupTestAdmissionType(boolean usingSet) throws OHException { } private void checkAdmissionTypeIntoDb(String code) throws OHException { - AdmissionType foundAdmissionType = admissionTypeIoOperationRepository.findById(code).get(); + AdmissionType foundAdmissionType = admissionTypeIoOperationRepository.findById(code).orElse(null); + assertThat(foundAdmissionType).isNotNull(); testAdmissionType.check(foundAdmissionType); } } \ No newline at end of file