103 lines
5.3 KiB
Java
103 lines
5.3 KiB
Java
package at.ac.tuwien.sepm.assignment.individual.util;
|
|
|
|
import at.ac.tuwien.sepm.assignment.individual.entity.Horse;
|
|
import at.ac.tuwien.sepm.assignment.individual.entity.Owner;
|
|
import at.ac.tuwien.sepm.assignment.individual.enums.ERace;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import java.sql.Date;
|
|
import java.util.Map;
|
|
|
|
@Component
|
|
public class Validator {
|
|
public void validateNewOwner(Owner owner) throws ValidationException {
|
|
if(owner.getName() == null || owner.getName().isEmpty() || owner.getName().isBlank()) {
|
|
throw new ValidationException("Required value name missing");
|
|
}
|
|
}
|
|
|
|
public void validateUpdateOwner(Owner owner) throws ValidationException {
|
|
if(owner.getId() == null || owner.getId() == 0) {
|
|
throw new ValidationException("Owner Id cannot be null or 0");
|
|
}
|
|
if(owner.getName() == null || owner.getName().isEmpty() || owner.getName().isBlank()) {
|
|
throw new ValidationException("Required value for owner missing: name");
|
|
}
|
|
}
|
|
|
|
public void validateOwnerFilter(Map<String, String> filters) {
|
|
if(filters.get("name") == null || filters.get("name").isEmpty()) {
|
|
throw new ValidationException("Owner name cannot be empty");
|
|
}
|
|
}
|
|
|
|
public void validateNewHorse(Horse horse) throws ValidationException {
|
|
if(horse.getName() == null || horse.getName().isEmpty() || horse.getName().isBlank() || horse.getScore() == 0 || horse.getBirthday() == null || horse.getRace() == null || horse.getImagePath() == null){
|
|
throw new ValidationException("All or some required values missing: name, score, birthday, race, imagePath");
|
|
}
|
|
if(horse.getScore() > 5 || horse.getScore() < 1) {
|
|
throw new ValidationException("Score value " + horse.getScore() + " not allowed. The score must be an integer between 1 and 5");
|
|
}
|
|
if(!horse.getImagePath().endsWith(".png") && !horse.getImagePath().endsWith(".jpg") && !horse.getImagePath().endsWith(".jpeg")) {
|
|
throw new ValidationException("Unsupported file type supplied. Supported file types are jpg and png");
|
|
}
|
|
}
|
|
|
|
public void validateUpdateHorse(Horse horse) throws ValidationException {
|
|
if(horse.getId() == null || horse.getId() == 0) {
|
|
throw new ValidationException("Horse Id cannot be null or 0");
|
|
}
|
|
if(horse.getName() == null || horse.getName().isEmpty() || horse.getName().isBlank() || horse.getScore() == 0 || horse.getBirthday() == null || horse.getRace() == null || horse.getImagePath() == null){
|
|
throw new ValidationException("All or some required values missing: name, score, birthday, race. imagePath");
|
|
}
|
|
if(horse.getScore() > 5 || horse.getScore() < 1) {
|
|
throw new ValidationException("Score value " + horse.getScore() + " not allowed. The score must be an integer between 1 and 5");
|
|
}
|
|
if(!horse.getImagePath().endsWith(".png") && !horse.getImagePath().endsWith(".jpg") && !horse.getImagePath().endsWith(".jpeg")) {
|
|
throw new ValidationException("Unsupported file type supplied. Supported file types are jpg and png");
|
|
}
|
|
}
|
|
|
|
public void validateHorseImage(MultipartFile image) throws ValidationException {
|
|
if(image == null || image.getContentType() == null || image.isEmpty()) {
|
|
throw new ValidationException("No image supplied");
|
|
}
|
|
if(!image.getContentType().equals("image/png") && !image.getContentType().equals("image/jpeg")) {
|
|
throw new ValidationException("Unsupported file type supplied: " + image.getContentType());
|
|
}
|
|
}
|
|
|
|
public void validateImageRequest(String path) throws ValidationException {
|
|
if(path == null || path.isEmpty()) {
|
|
throw new ValidationException("No image path supplied");
|
|
}
|
|
if(!path.endsWith(".png") && !path.endsWith(".jpg") && !path.endsWith(".jpeg")) {
|
|
throw new ValidationException("Unsupported file extension supplied.");
|
|
}
|
|
}
|
|
|
|
public void validateHorseFilter(Map<String, String> filters) throws ValidationException {
|
|
if(filters.get("score") != null) {
|
|
try {
|
|
if (Integer.parseInt(filters.get("score")) < 1 || Integer.parseInt(filters.get("score")) > 5)
|
|
throw new ValidationException("Score value " + filters.get("score") + " not allowed. The score must be an integer between 1 and 5");
|
|
} catch(NumberFormatException e) {
|
|
throw new ValidationException("Score value " + filters.get("score") + " not allowed. The score must be an integer between 1 and 5");
|
|
}
|
|
}
|
|
if(filters.get("race") != null) {
|
|
if(!ERace.contains(filters.get("race").toUpperCase()))
|
|
throw new ValidationException("Race value " + filters.get("race") + " not allowed. Races allowed are " + ERace.valuesToString());
|
|
}
|
|
if(filters.get("birthday") != null) {
|
|
try {
|
|
Date.valueOf(filters.get("birthday"));
|
|
} catch(IllegalArgumentException e) {
|
|
throw new ValidationException("Date value " + filters.get("birthday") + " not allowed. The birthday must be a valid date");
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|