Validator.java 5.23 KB
Newer Older
1 2 3 4
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;
5
import at.ac.tuwien.sepm.assignment.individual.enums.ERace;
6
import org.springframework.stereotype.Component;
7
import org.springframework.web.multipart.MultipartFile;
8

9 10 11
import java.sql.Date;
import java.util.Map;

12 13 14
@Component
public class Validator {
    public void validateNewOwner(Owner owner) throws ValidationException {
15
        if(owner.getName() == null || owner.getName().isEmpty() || owner.getName().isBlank()) {
16 17
            throw new ValidationException("Required value name missing");
        }
18 19 20
    }

    public void validateUpdateOwner(Owner owner) throws ValidationException {
21 22 23 24 25 26
        if(owner.getId() == null || owner.getId() == 0) {
            throw new ValidationException("Owner Id cannot be null or 0");
        }
        if(owner.getName() == null || owner.getName().isEmpty()) {
            throw new ValidationException("Required value for owner missing: name");
        }
27 28
    }

29 30 31 32 33 34
    public void validateOwnerFilter(Map<String, String> filters) {
        if(filters.get("name") == null || filters.get("name").isEmpty()) {
            throw new ValidationException("Owner name cannot be empty");
        }
    }

35
    public void validateNewHorse(Horse horse) throws ValidationException {
36
        if(horse.getName() == null || horse.getName().isEmpty() || horse.getName().isBlank() || horse.getScore() == 0 || horse.getBirthday() == null || horse.getRace() == null || horse.getImagePath() == null){
37 38 39 40 41 42 43 44 45 46 47 48 49 50
            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");
        }
51
        if(horse.getName() == null || horse.getName().isEmpty() || horse.getName().isBlank() || horse.getScore() == 0 || horse.getBirthday() == null || horse.getRace() == null || horse.getImagePath() == null){
52
            throw new ValidationException("All or some required values missing: name, score, birthday, race. imagePath");
53
        }
54 55 56
        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");
        }
57 58 59 60 61 62 63 64 65 66 67 68
        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());
        }
69
    }
70

71 72 73 74 75 76 77 78 79
    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.");
        }
    }

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    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");
            }
        }
    }

102
}