package org.datacite.mds.validation.constraints.impl;

import java.net.URL;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.datacite.mds.domain.Dataset;
import org.datacite.mds.util.Utils;
import org.datacite.mds.validation.constraints.MatchDoiPrefix;
import org.datacite.mds.validation.constraints.MatchDomain;

public class MatchDomainValidator implements ConstraintValidator<MatchDomain, Dataset> {
    String defaultMessage;

    public void initialize(MatchDomain constraintAnnotation) {
        defaultMessage = constraintAnnotation.message();
    }

    public boolean isValid(Dataset dataset, ConstraintValidatorContext context) {
        if (dataset.getUrl() == null || dataset.getDatacentre() == null || !Utils.isValid(dataset, "url")) {
            // don't check until a datacentre is selected and valid url is given
            return true;
        }

        // check each allowed domain of the datacentre against the used one
        String hostname = Utils.getHostname(dataset.getUrl());
        
        for (String domain : Utils.csvToList(dataset.getDatacentre().getDomains())) {
            if (domain.equals(hostname)) {
                return true;
            }
        }

        context.disableDefaultConstraintViolation();
        Utils.addConstraintViolation(context, defaultMessage, "url");
        return false;
    }
}

@@ -733,16 +733,46 @@
  null %7C%7C
+ dataset.getUrl().isEmpty() %7C%7C
  dataset
@@ -791,24 +791,40 @@
 re() == null
+%0A               
  %7C%7C !Utils.i
@@ -1105,24 +1105,16 @@
 Url());%0A
-        
 %0A       

allow empty url