Validator.java 4.48 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 16 17
        if(owner.getName() == null || owner.getName().isEmpty()) {
            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
    }

    public void validateNewHorse(Horse horse) throws ValidationException {
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
        if(horse.getName() == null || 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.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");
47
        }
48 49 50
        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");
        }
51 52 53 54 55 56 57 58 59 60 61 62
        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());
        }
63
    }
64

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    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");
            }
        }
    }

87
}