Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OP-1130 JPA admtype changes (take 2) #1111

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 60 additions & 40 deletions src/main/java/org/isf/admtype/manager/AdmissionTypeBrowserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,24 @@
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;

/**
* Returns all the available {@link AdmissionType}s.
*
* @return a list of admission types or <code>null</code> if the operation fails.
* @return a list of admission types or {@code null} if the operation fails.
* @throws OHServiceException
*/
public List<AdmissionType> getAdmissionType() throws OHServiceException {
Expand All @@ -52,78 +54,96 @@ public List<AdmissionType> getAdmissionType() throws OHServiceException {
/**
* Stores a new {@link AdmissionType}.
*
* @param admissionType the admission type to store.
* @return <code>true</code> if the admission type has been stored, <code>false</code> 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<String> 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 <code>true</code> if the admission type has been updated, <code>false</code> 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<String> 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 <code>true</code> if the code is already used, <code>false</code> 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 <code>true</code> if the admission type has been deleted, <code>false</code> 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 <code>true</code> or updated <code>false</code>
* @throws OHDataValidationException
* @param insert {@code true} or updated {@code false}
*/
protected void validateAdmissionType(AdmissionType admissionType, boolean insert) throws OHServiceException {
List<OHExceptionMessage> errors = new ArrayList<>();
protected List<String> validateAdmissionType(AdmissionType admissionType, boolean insert) {
List<String> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,38 +54,36 @@ public List<AdmissionType> getAdmissionType() throws OHServiceException {
/**
* Updates the specified {@link AdmissionType}.
* @param admissionType the admission type to update.
* @return <code>true</code> if the admission type has been updated, <code>false</code> 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 <code>true</code> if the admission type has been stored, <code>false</code> 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 <code>true</code> if the admission type has been deleted, <code>false</code> 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 <code>true</code> if the code is already used, <code>false</code> otherwise.
* @return {@code true} if the code is already used, <code>false</code> otherwise.
* @throws OHServiceException if an error occurs during the check.
*/
public boolean isCodePresent(String code) throws OHServiceException {
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/org/isf/utils/tuple/JPAImmutableTriple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Open Hospital (www.open-hospital.org)
* Copyright © 2006-2023 Informatici Senza Frontiere ([email protected])
*
* 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 <https://www.gnu.org/licenses/>.
*/
package org.isf.utils.tuple;

import java.util.List;

public final class JPAImmutableTriple {

private boolean result;
private Object object;
private List<String> 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<String>} of formatted error messages.
*/
public JPAImmutableTriple(boolean result, Object object, List<String> 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<String>} of formatted message text
*/
public List<String> getErrors() {
return errors;
}

}
Loading