package org.datacite.mds.util;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

import javax.validation.ConstraintValidatorContext;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

/**
 * Class with several static util methods
 */
public class Utils {

    /**
     * returns the prefix of a doi
     * 
     * @param doi
     *            Doi (e.g. "10.5072/foobar")
     * @return doi prefix (e.g. "10.5072")
     */
    public static String getDoiPrefix(String doi) {
        return doi.split("/")[0];
    }

    /**
     * returns the suffix of a doi
     * 
     * @param doi
     *            Doi (e.g. "10.5072/foobar")
     * @return doi prefix (e.g. "foobar")
     */
    public static String getDoiSuffix(String doi) {
        return doi.split("/")[1];
    }

    /**
     * converts a string with comma separated values to a List of Strings
     * 
     * @param csv
     *            comma separated values
     * @return List of Strings
     */
    public static List<String> csvToList(String csv) {
        return Arrays.asList(csv.split(","));
    }

    /**
     * Wrapper to simply validate a property of an object.
     * 
     * @param object
     *            object to validate
     * @param propertyName
     *            property to validate (e.g. field)
     * @return true if property validates
     * @see javax.validation.Validator.validateProperty
     */
    public static boolean isValid(Object object, String propertyName) {
        ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
        Validator validator = validatorFactory.getValidator();
        Set violations = validator.validateProperty(object, propertyName);
        return violations.isEmpty();
    }

    public static void addConstraintViolation(ConstraintValidatorContext context, String message, String node) {
        context.buildConstraintViolationWithTemplate(message).addNode(node).addConstraintViolation();
    }

    public static void addConstraintViolation(ConstraintValidatorContext context, String message) {
        context.buildConstraintViolationWithTemplate(message).addConstraintViolation();
    }

}

@@ -25,16 +25,56 @@
 .util;%0A%0A
+import java.lang.annotation.Annotation;%0A
 import j
@@ -188,16 +188,61 @@
 ontext;%0A
+import javax.validation.ConstraintViolation;%0A
 import j
@@ -1883,32 +1883,894 @@
 Empty();%0A    %7D%0A%0A
+    /**%0A     * Check if a specific constraint annotation is violated%0A     * %0A     * @param object%0A     *            object to validate%0A     * @param constraint%0A     *            constraint annotation to be checked%0A     * @return true if the given constraint is not violated%0A     */%0A    public static %3CT%3E boolean isConstraintValid(T object, Class%3C? extends Annotation%3E constraint) %7B%0A        ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();%0A        Validator validator = validatorFactory.getValidator();%0A        Set%3CConstraintViolation%3CT%3E%3E violations = validator.validate(object);%0A%0A        for (ConstraintViolation%3CT%3E violation : violations) %7B%0A            if (violation.getConstraintDescriptor().getAnnotation().annotationType().equals(constraint)) %7B%0A                return false;%0A            %7D%0A        %7D%0A%0A        return true;%0A    %7D%0A%0A
     public stati

add util to check for specific constraint violation