/*
 * Copyright 2018 Scytl Secure Electronic Voting SA
 *
 * All rights reserved
 *
 * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
 */
package com.scytl.products.ov.commons.beans.exceptions;

import javax.validation.ConstraintViolation;
import java.util.HashSet;
import java.util.Set;

/**
 * Exception class for handling a resource which is not found.
 */
public class ValidationException extends Exception {

	private static final long serialVersionUID = 1938526782459549399L;

	// violations
	private Set<ConstraintViolation<?>> constraintViolations = null;

	// resource which provokes the exception
	private String resource;

	// error code of the exception
	private String errorCode;

	/**
	 * Constructs a new exception with the specified detail message.
	 *
	 * @param constraintViolations the constraint violations.
	 */
	public ValidationException(Set<? extends ConstraintViolation<?>> constraintViolations) {
		if (constraintViolations != null) {
			this.constraintViolations = new HashSet<>(constraintViolations);
		}
	}

	/**
	 * Constructs a new exception with the specified detail message.
	 *
	 * @param constraintViolations the constraint violations
	 * @param message the detail message.
	 */
	public ValidationException(Set<ConstraintViolation<?>> constraintViolations, String message) {
		super(message);
		if (constraintViolations != null) {
			this.constraintViolations = new HashSet<>(constraintViolations);
		}		
	}

	/**
	 * Constructs a new exception with the specified detail message, resource and errorCode.
	 *
	 * @param constraintViolations the constraint violations
	 * @param message The detail message.
	 * @param resource The resource which has provoked the exception.
	 * @param errorCode The error code of the exception.
	 */
	public ValidationException(Set<ConstraintViolation<?>> constraintViolations, String message, String resource,
			String errorCode) {
		super(message);
		this.resource = resource;
		this.errorCode = errorCode;
		if (constraintViolations != null) {
			this.constraintViolations = new HashSet<>(constraintViolations);
		}
	}

	/**
	 * Gets the value of field resource.
	 *
	 * @return the resource.
	 */
	public String getResource() {
		return resource;
	}

	/**
	 * gets the value of field errorCode.
	 *
	 * @return the field error Code
	 */
	public String getErrorCode() {
		return errorCode;
	}

	/**
	 * Set of constraint violations reported during a validation.
	 *
	 * @return {@code Set} of {@link ConstraintViolation}
	 */
	public Set<ConstraintViolation<?>> getConstraintViolations() {
		return constraintViolations;
	}

    /**
     * This is mainly used in logging, so we can add some extra info to help us see what the problem was
     * @return
     */
    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("ValidationException{");
        sb.append("message='").append(errorCode).append('\'');
        sb.append(", errorCode='").append(errorCode).append('\'');
        sb.append(", resource='").append(resource).append('\'');
        sb.append(", constraintViolations=").append(constraintViolations);
        sb.append('}');
        return sb.toString();
    }
}
