Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

Commit

Permalink
Closes #35; Implemented "KvAttribute" in "persistence" module.
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelaMariaDespotopoulou committed Nov 9, 2022
1 parent d9fe49b commit 8c16699
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package eu.datacrop.maize.model_repository.commons.dtos.requests.auxiliaries;

import eu.datacrop.maize.model_repository.commons.dtos.requests.templates.RequestDto;
import eu.datacrop.maize.model_repository.commons.enums.ResponseCode;
import eu.datacrop.maize.model_repository.commons.error.messages.KvAttributeErrorMessages;
import eu.datacrop.maize.model_repository.commons.wrappers.ResponseWrapper;
import eu.datacrop.maize.model_repository.commons.wrappers.single.auxiliaries.KvAttributeResponseWrapper;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;

Expand Down Expand Up @@ -249,7 +252,8 @@ public JSONObject toJSON() {
*****************************************************************************************************************/
@Override
public ResponseWrapper performValidation() {
/*KvAttributeResponseWrapper wrapper;

KvAttributeResponseWrapper wrapper;
try {
// Validating attributes.
wrapper = (KvAttributeResponseWrapper) super.getValidator().validateAttributes(this);
Expand All @@ -276,7 +280,6 @@ public ResponseWrapper performValidation() {
String message = "Error occurred during Request DTO validation.";
log.error(message);
return new KvAttributeResponseWrapper(ResponseCode.ERROR, message, null, KvAttributeErrorMessages.INTERNAL_SERVER_ERROR);
}*/
return null; //TODO
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ public enum KvAttributeErrorMessages {
/******************************************************************************************************************
* Indicates that a duplicate parameter value was about to be persisted.
*****************************************************************************************************************/
DUPLICATE_KV_ATTRIBUTE("There is already a KvAttribute with the same group name."),

/******************************************************************************************************************
* Indicates that a duplicate parameter value was about to be persisted.
*****************************************************************************************************************/
DUPLICATE_PARAMETER_VALUE("There is already a Parameter with the same name."),
DUPLICATE_PARAMETER_VALUE("Parameter Values sharing the same name detected:"),

/******************************************************************************************************************
* Indicates that persistence has been aborted due to absence of mandatory fields.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package eu.datacrop.maize.model_repository.persistence.validators;

import eu.datacrop.maize.model_repository.commons.dtos.requests.auxiliaries.KvAttributeRequestDto;
import eu.datacrop.maize.model_repository.commons.dtos.requests.auxiliaries.ParameterValueRequestDto;
import eu.datacrop.maize.model_repository.commons.dtos.requests.templates.RequestDto;
import eu.datacrop.maize.model_repository.commons.enums.ResponseCode;
import eu.datacrop.maize.model_repository.commons.error.messages.KvAttributeErrorMessages;
import eu.datacrop.maize.model_repository.commons.validators.Validator;
import eu.datacrop.maize.model_repository.commons.wrappers.single.auxiliaries.KvAttributeResponseWrapper;
import eu.datacrop.maize.model_repository.commons.wrappers.single.auxiliaries.ParameterValueResponseWrapper;

import java.util.HashSet;
import java.util.Set;
import java.util.Vector;

import static eu.datacrop.maize.model_repository.commons.error.messages.KvAttributeErrorMessages.MANDATORY_FIELDS_MISSING;

/**********************************************************************************************************************
* This class implements methods to validate whether an incoming HTTP Request body contains attributes that are
* valid according to the business rules for a KvAttribute entity.
*
* @author Angela-Maria Despotopoulou [Athens, Greece]
* @since version 0.4.0
*********************************************************************************************************************/
public class KvAttributeValidator implements Validator {

/******************************************************************************************************************
* This method triggers validation of the data transfer object's attributes.
*
* @param requestDto A data transfer object to validate, not null.
* @return A ResponseWrapper (the user will receive a more elaborate one, here it is used only for
* internal intra-module communication).
*
* throws IllegalArgumentException, if requestDto is null.
*****************************************************************************************************************/
@Override
public KvAttributeResponseWrapper validateAttributes(RequestDto requestDto) throws IllegalArgumentException {

// Checking input parameters.
if (requestDto == null) {
throw new IllegalArgumentException("Invalid parameter detected for method KvAttributeValidator.validate().");
}

KvAttributeRequestDto kvAttributeRequestDto = (KvAttributeRequestDto) requestDto;

String message;
KvAttributeResponseWrapper wrapper;
try {
// Checking the request for mandatory fields that were erroneously provided without content.
Vector<String> fields = getNamesOfFieldsThatAreErroneouslyNull(kvAttributeRequestDto);
if (!fields.isEmpty()) {
message = MANDATORY_FIELDS_MISSING.toString().concat(" Field(s): ").concat(fields.toString());
return new KvAttributeResponseWrapper(ResponseCode.BAD_REQUEST, message, null, MANDATORY_FIELDS_MISSING);
}

// Parsing the Parameter Values set for problems.
wrapper = validateParameterValues(kvAttributeRequestDto);
if (wrapper != null && !(wrapper.getCode().equals(ResponseCode.SUCCESS))) {
return wrapper;
}

} catch (IllegalArgumentException e) {
// Reporting internal server error.
throw new IllegalArgumentException(e.getMessage());
}

// Reporting that attribute validation found no issues.
return new KvAttributeResponseWrapper(ResponseCode.SUCCESS, "Validation success.", null, null);
}

/******************************************************************************************************************
* This method triggers validation of the data transfer object's external relationships.
*
* @param requestDto A data transfer object to validate, not null.
* @return A ResponseWrapper (the user will receive a more elaborate one, here it is used only for
* internal intra-module communication).
*
* throws IllegalArgumentException, if requestDto is null.
*****************************************************************************************************************/
@Override
public KvAttributeResponseWrapper validateRelationships(RequestDto requestDto) throws IllegalArgumentException {

if (requestDto == null) {
throw new IllegalArgumentException("Invalid parameter detected for method KvAttributeValidator.validate().");
}

// Always returns SUCCESS according to business logic Parameter Values do not refer to other entities.
return new KvAttributeResponseWrapper(ResponseCode.SUCCESS, "Validation success.", null, null);
}

/******************************************************************************************************************
* This method parses a Parameter Value Request Data Transfer Object for fields that should have not been left
* null according to business logic. Returns empty Vector if no erroneous fields have been located.
*
* @param requestDto The data transfer object to be parsed, not null.
* @return A collection of fields that have been marked as erroneous.
*
* @throws IllegalArgumentException if the method parameter is null.
*****************************************************************************************************************/
private Vector<String> getNamesOfFieldsThatAreErroneouslyNull(KvAttributeRequestDto requestDto) throws IllegalArgumentException {

// Checking input parameters.
if (requestDto == null) {
throw new IllegalArgumentException("Invalid parameter detected for method KvAttributeValidator.getNamesOfFieldsThatAreErroneouslyNull().");
}

Vector<String> fields = new Vector<String>();

// Checking mandatory fields "Name" and "Description".
if (requestDto.getName() == null || requestDto.getName().isBlank()) fields.add("name");
if (requestDto.getParameterValues() == null) fields.add("kvAttributes.parameterValues");

// Logging and returning result.
return fields;
}

/******************************************************************************************************************
* This method parses a Parameter Value Request Data Transfer Object for problematic Parameter Values.
*
* @param requestDto The data transfer object to be parsed, not null.
* @return A ResponseWrapper (the user will receive a more elaborate one, here it is used only for
* internal intra-module communication).
*
* @throws IllegalArgumentException if the method parameter is null.
*****************************************************************************************************************/
private KvAttributeResponseWrapper validateParameterValues(KvAttributeRequestDto requestDto) throws IllegalArgumentException {

// Checking input parameters.
if (requestDto == null) {
throw new IllegalArgumentException("Invalid parameter detected for method KvAttributeValidator.getNamesOfFieldsThatAreErroneouslyNull().");
}

// Validating one by one the Parameter Values...
ParameterValueResponseWrapper wrapper;
final Set<String> set = new HashSet<>();
for (ParameterValueRequestDto parameterValue : requestDto.getParameterValues()) {
// ...for missing field values.
wrapper = (ParameterValueResponseWrapper) parameterValue.performValidation();
if (wrapper != null && !(wrapper.getCode().equals(ResponseCode.SUCCESS))) {
return new KvAttributeResponseWrapper(wrapper.getCode(), wrapper.getMessage(), null, KvAttributeErrorMessages.MANDATORY_FIELDS_MISSING);
// The last parameter is a hack (no other types are currently returned. If they are added a modification is needed.
}

//...for duplicate value names.
if (!set.add(parameterValue.getName())) {
return new KvAttributeResponseWrapper(ResponseCode.BAD_REQUEST, KvAttributeErrorMessages.DUPLICATE_PARAMETER_VALUE.toString(), null, KvAttributeErrorMessages.DUPLICATE_PARAMETER_VALUE);
}
}

// Reporting success of the valudation.
return new KvAttributeResponseWrapper(ResponseCode.SUCCESS, "Validation success.", null, null);
}

}


0 comments on commit 8c16699

Please sign in to comment.