no message

This commit is contained in:
annabakouli 2017-12-23 17:56:05 +02:00
parent e7ccb8c1d2
commit 4ccafb6b31
9 changed files with 148 additions and 13 deletions

View File

@ -5,6 +5,7 @@ import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.transaction.Transactional; import javax.transaction.Transactional;
import javax.validation.Valid;
import eu.eudat.entities.DMP; import eu.eudat.entities.DMP;
import eu.eudat.entities.Dataset; import eu.eudat.entities.Dataset;
@ -19,17 +20,13 @@ import eu.eudat.models.helpers.DataTableData;
import eu.eudat.models.helpers.responses.*; import eu.eudat.models.helpers.responses.*;
import eu.eudat.models.listingmodels.DataManagementPlanListingModel; import eu.eudat.models.listingmodels.DataManagementPlanListingModel;
import eu.eudat.models.security.Principal; import eu.eudat.models.security.Principal;
import eu.eudat.validators.DataManagementTableRequestValidator;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import eu.eudat.dao.entities.DMPDao; import eu.eudat.dao.entities.DMPDao;
import eu.eudat.dao.entities.DMPProfileDao; import eu.eudat.dao.entities.DMPProfileDao;

View File

@ -0,0 +1,64 @@
package eu.eudat.controllers.controllerhandler;
import eu.eudat.models.errormodels.ValidationErrorContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import java.util.List;
import java.util.Locale;
/**
* Created by ikalyvas on 12/22/2017.
*/
@ControllerAdvice
public class ControllerErrorHandler {
private MessageSource messageSource;
@Autowired
public ControllerErrorHandler(MessageSource messageSource) {
this.messageSource = messageSource;
}
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ValidationErrorContext processValidationError(MethodArgumentNotValidException ex) {
BindingResult result = ex.getBindingResult();
List<FieldError> fieldErrors = result.getFieldErrors();
return processFieldErrors(fieldErrors);
}
private ValidationErrorContext processFieldErrors(List<FieldError> fieldErrors) {
ValidationErrorContext dto = new ValidationErrorContext();
for (FieldError fieldError: fieldErrors) {
String localizedErrorMessage = resolveLocalizedErrorMessage(fieldError);
dto.addFieldError(fieldError.getField(), localizedErrorMessage);
}
return dto;
}
private String resolveLocalizedErrorMessage(FieldError fieldError) {
Locale currentLocale = LocaleContextHolder.getLocale();
String localizedErrorMessage = messageSource.getMessage(fieldError, currentLocale);
if (localizedErrorMessage.equals(fieldError.getDefaultMessage())) {
String[] fieldErrorCodes = fieldError.getCodes();
localizedErrorMessage = fieldErrorCodes[0];
}
return localizedErrorMessage;
}
}

View File

@ -3,6 +3,10 @@ package eu.eudat.models.dmp;
import eu.eudat.models.criteria.DataManagementPlanCriteria; import eu.eudat.models.criteria.DataManagementPlanCriteria;
import eu.eudat.models.helpers.requests.RequestItem; import eu.eudat.models.helpers.requests.RequestItem;
import eu.eudat.models.helpers.requests.TableRequest; import eu.eudat.models.helpers.requests.TableRequest;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
public class DataManagementPlanTableRequest extends TableRequest<DataManagementPlanCriteria> { public class DataManagementPlanTableRequest extends TableRequest<DataManagementPlanCriteria> {
} }

View File

@ -0,0 +1,15 @@
package eu.eudat.models.errormodels;
/**
* Created by ikalyvas on 12/22/2017.
*/
public class FieldError {
private String field;
private String message;
public FieldError(String field, String message) {
this.field = field;
this.message = message;
}
}

View File

@ -0,0 +1,20 @@
package eu.eudat.models.errormodels;
import java.util.ArrayList;
import java.util.List;
/**
* Created by ikalyvas on 12/22/2017.
*/
public class ValidationErrorContext {
private List<FieldError> fieldErrors = new ArrayList<>();
public ValidationErrorContext() {
}
public void addFieldError(String path, String message) {
FieldError error = new FieldError(path, message);
fieldErrors.add(error);
}
}

View File

@ -20,7 +20,8 @@ public interface QueryableList<T> {
QueryableList<T> distinct(); QueryableList<T> distinct();
QueryableList<T> orderBy(OrderByPredicate<T> predicate); QueryableList<T> orderByAsc(OrderByPredicate<T> predicate);
QueryableList<T> orderByDesc(OrderByPredicate<T> predicate);
Long count(); Long count();
} }

View File

@ -78,8 +78,13 @@ public class QueryableHibernateList<T> implements QueryableList<T> {
return this; return this;
} }
public QueryableList<T> orderBy(OrderByPredicate<T> predicate) { public QueryableList<T> orderByAsc(OrderByPredicate<T> predicate) {
this.query.orderBy(predicate.applyPredicate(this.manager.getCriteriaBuilder(), this.root)); this.query.orderBy(this.manager.getCriteriaBuilder().desc(predicate.applyPredicate(this.root)));
return this;
}
public QueryableList<T> orderByDesc(OrderByPredicate<T> predicate) {
this.query.orderBy(this.manager.getCriteriaBuilder().asc(predicate.applyPredicate(this.root)));
return this; return this;
} }

View File

@ -2,9 +2,10 @@ package eu.eudat.queryable.predicates;
import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Order; import javax.persistence.criteria.Order;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root; import javax.persistence.criteria.Root;
public interface OrderByPredicate<T> { public interface OrderByPredicate<T> {
Order applyPredicate(CriteriaBuilder builder, Root<T> root); Path applyPredicate(Root<T> root);
} }

View File

@ -0,0 +1,28 @@
package eu.eudat.validators;
import eu.eudat.models.dmp.DataManagementPlanTableRequest;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
/**
* Created by ikalyvas on 12/22/2017.
*/
@Component("beforeCreateDataManagementTableRequestValidator")
public class DataManagementTableRequestValidator implements Validator {
@Override
public boolean supports(Class<?> aClass) {
return DataManagementPlanTableRequest.class.equals(aClass);
}
@Override
public void validate(Object obj, Errors errors) {
DataManagementPlanTableRequest user = (DataManagementPlanTableRequest) obj;
if(user.getOffset()<0){
errors.rejectValue("offset", "Offset Cannot Be Negative");
}
if(user.getLength()<0){
errors.rejectValue("length", "Length Cannot Be Negative");
}
}
}