Validator.java 1.71 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
        if(horse.getName() == null || horse.getScore() == 0 || horse.getBirthday() == null || horse.getRace() == null){
            throw new ValidationException("All or some required values missing: name, score, birthday, race");
22
        }
23 24 25
        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");
        }
26 27 28 29 30 31 32 33 34 35 36 37
        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());
        }
38
    }
39

40
}