Validator.java 2.72 KB
Newer Older
1 2 3 4 5
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 org.springframework.stereotype.Component;
6
import org.springframework.web.multipart.MultipartFile;
7 8 9 10 11 12 13 14 15 16 17 18 19

@Component
public class Validator {



    public void validateNewOwner(Owner owner) throws ValidationException {
    }

    public void validateUpdateOwner(Owner owner) throws ValidationException {
    }

    public void validateNewHorse(Horse horse) throws ValidationException {
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
        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");
37
        }
38 39 40
        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");
        }
41 42 43 44 45 46 47 48 49 50 51 52
        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());
        }
53
    }
54

55
}