Search in sources :

Example 1 with FieldType

use of edu.harvard.iq.dataverse.DatasetFieldType.FieldType in project dataverse by IQSS.

the class DatasetFieldValueValidator method isValid.

public boolean isValid(DatasetFieldValue value, ConstraintValidatorContext context) {
    // we do this so we can have different messages depending on the different issue
    context.disableDefaultConstraintViolation();
    boolean lengthOnly = false;
    DatasetFieldType dsfType = value.getDatasetField().getDatasetFieldType();
    FieldType fieldType = dsfType.getFieldType();
    if (value.getDatasetField().getTemplate() != null) {
        lengthOnly = true;
    }
    if (value.getDatasetField().getParentDatasetFieldCompoundValue() != null && value.getDatasetField().getParentDatasetFieldCompoundValue().getParentDatasetField().getTemplate() != null) {
        lengthOnly = true;
    }
    if (StringUtils.isBlank(value.getValue()) || StringUtils.equals(value.getValue(), DatasetField.NA_VALUE)) {
        return true;
    }
    if (fieldType.equals(FieldType.TEXT) && !lengthOnly && value.getDatasetField().getDatasetFieldType().getValidationFormat() != null) {
        boolean valid = value.getValue().matches(value.getDatasetField().getDatasetFieldType().getValidationFormat());
        if (!valid) {
            try {
                context.buildConstraintViolationWithTemplate(dsfType.getDisplayName() + " is not a valid entry.").addConstraintViolation();
            } catch (NullPointerException e) {
                return false;
            }
            return false;
        }
    }
    if (fieldType.equals(FieldType.DATE) && !lengthOnly) {
        boolean valid = false;
        String testString = value.getValue();
        if (!valid) {
            valid = isValidDate(testString, "yyyy-MM-dd");
        }
        if (!valid) {
            valid = isValidDate(testString, "yyyy-MM");
        }
        // If AD must be a 4 digit year
        if (value.getValue().contains("AD")) {
            testString = (testString.substring(0, testString.indexOf("AD"))).trim();
        }
        String YYYYformat = "yyyy";
        if (!valid) {
            valid = isValidDate(testString, YYYYformat);
            if (!StringUtils.isNumeric(testString)) {
                valid = false;
            }
        }
        // If BC must be numeric
        if (!valid && value.getValue().contains("BC")) {
            testString = (testString.substring(0, testString.indexOf("BC"))).trim();
            if (StringUtils.isNumeric(testString)) {
                valid = true;
            }
        }
        // Must start with "[", end with "?]" and not start with "[-"
        if (!valid && value.getValue().startsWith("[") && value.getValue().endsWith("?]") && !value.getValue().startsWith("[-")) {
            testString = value.getValue().replace("[", " ").replace("?]", " ").replace("-", " ").replace("BC", " ").replace("AD", " ").trim();
            if (value.getValue().contains("BC") && StringUtils.isNumeric(testString)) {
                valid = true;
            } else {
                valid = isValidDate(testString, YYYYformat);
                if (!StringUtils.isNumeric(testString)) {
                    valid = false;
                }
            }
        }
        if (!valid) {
            // TODO:
            // This is a temporary fix for the early beta!
            // (to accommodate dates with time stamps from Astronomy files)
            // As a real fix, we need to introduce a different type -
            // "datetime" for ex. and use it for timestamps;
            // We do NOT want users to be able to enter a full time stamp
            // as the release date...
            // -- L.A. 4.0 beta
            valid = (isValidDate(value.getValue(), "yyyy-MM-dd'T'HH:mm:ss") || isValidDate(value.getValue(), "yyyy-MM-dd'T'HH:mm:ss.SSS") || isValidDate(value.getValue(), "yyyy-MM-dd HH:mm:ss"));
        }
        if (!valid) {
            try {
                context.buildConstraintViolationWithTemplate(dsfType.getDisplayName() + " is not a valid date. \"" + YYYYformat + "\" is a supported format.").addConstraintViolation();
            } catch (NullPointerException npe) {
            }
            return false;
        }
    }
    if (fieldType.equals(FieldType.FLOAT) && !lengthOnly) {
        try {
            Double.parseDouble(value.getValue());
        } catch (Exception e) {
            logger.fine("Float value failed validation: " + value.getValue() + " (" + dsfType.getDisplayName() + ")");
            try {
                context.buildConstraintViolationWithTemplate(dsfType.getDisplayName() + " is not a valid number.").addConstraintViolation();
            } catch (NullPointerException npe) {
            }
            return false;
        }
    }
    if (fieldType.equals(FieldType.INT) && !lengthOnly) {
        try {
            Integer.parseInt(value.getValue());
        } catch (Exception e) {
            try {
                context.buildConstraintViolationWithTemplate(dsfType.getDisplayName() + " is not a valid integer.").addConstraintViolation();
            } catch (NullPointerException npe) {
            }
            return false;
        }
    }
    if (fieldType.equals(FieldType.URL) && !lengthOnly) {
        try {
            URL url = new URL(value.getValue());
        } catch (MalformedURLException e) {
            try {
                context.buildConstraintViolationWithTemplate(dsfType.getDisplayName() + " " + value.getValue() + "  is not a valid URL.").addConstraintViolation();
            } catch (NullPointerException npe) {
            }
            return false;
        }
    }
    if (fieldType.equals(FieldType.EMAIL) && !lengthOnly) {
        if (value.getDatasetField().isRequired() && value.getValue() == null) {
            return false;
        }
        return EMailValidator.isEmailValid(value.getValue(), context);
    }
    return true;
}
Also used : MalformedURLException(java.net.MalformedURLException) MalformedURLException(java.net.MalformedURLException) ParseException(java.text.ParseException) URL(java.net.URL) FieldType(edu.harvard.iq.dataverse.DatasetFieldType.FieldType)

Example 2 with FieldType

use of edu.harvard.iq.dataverse.DatasetFieldType.FieldType in project dataverse by IQSS.

the class DatasetFieldServiceApi method getByName.

@GET
@Path("{name}")
public Response getByName(@PathParam("name") String name) {
    try {
        DatasetFieldType dsf = datasetFieldService.findByName(name);
        Long id = dsf.getId();
        String title = dsf.getTitle();
        FieldType fieldType = dsf.getFieldType();
        String solrFieldSearchable = dsf.getSolrField().getNameSearchable();
        String solrFieldFacetable = dsf.getSolrField().getNameFacetable();
        String metadataBlock = dsf.getMetadataBlock().getName();
        boolean hasParent = dsf.isHasParent();
        boolean allowsMultiples = dsf.isAllowMultiples();
        boolean isRequired = dsf.isRequired();
        String parentAllowsMultiplesDisplay = "N/A (no parent)";
        boolean parentAllowsMultiplesBoolean;
        if (hasParent) {
            DatasetFieldType parent = dsf.getParentDatasetFieldType();
            parentAllowsMultiplesBoolean = parent.isAllowMultiples();
            parentAllowsMultiplesDisplay = Boolean.toString(parentAllowsMultiplesBoolean);
        }
        return ok(NullSafeJsonBuilder.jsonObjectBuilder().add("name", dsf.getName()).add("id", id).add("title", title).add("metadataBlock", metadataBlock).add("fieldType", fieldType.name()).add("allowsMultiples", allowsMultiples).add("hasParent", hasParent).add("parentAllowsMultiples", parentAllowsMultiplesDisplay).add("solrFieldSearchable", solrFieldSearchable).add("solrFieldFacetable", solrFieldFacetable).add("isRequired", isRequired));
    } catch (NoResultException nre) {
        return notFound(name);
    } catch (EJBException | NullPointerException ex) {
        Throwable cause = ex;
        StringBuilder sb = new StringBuilder();
        sb.append(ex).append(" ");
        while (cause.getCause() != null) {
            cause = cause.getCause();
            sb.append(cause.getClass().getCanonicalName()).append(" ");
            sb.append(cause.getMessage()).append(" ");
            if (cause instanceof ConstraintViolationException) {
                ConstraintViolationException constraintViolationException = (ConstraintViolationException) cause;
                for (ConstraintViolation<?> violation : constraintViolationException.getConstraintViolations()) {
                    sb.append("(invalid value: <<<").append(violation.getInvalidValue()).append(">>> for ").append(violation.getPropertyPath()).append(" at ").append(violation.getLeafBean()).append(" - ").append(violation.getMessage()).append(")");
                }
            }
        }
        return error(Status.INTERNAL_SERVER_ERROR, sb.toString());
    }
}
Also used : NoResultException(javax.persistence.NoResultException) DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType) DatasetFieldType(edu.harvard.iq.dataverse.DatasetFieldType) FieldType(edu.harvard.iq.dataverse.DatasetFieldType.FieldType) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) EJBException(javax.ejb.EJBException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

FieldType (edu.harvard.iq.dataverse.DatasetFieldType.FieldType)2 DatasetFieldType (edu.harvard.iq.dataverse.DatasetFieldType)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ParseException (java.text.ParseException)1 EJBException (javax.ejb.EJBException)1 NoResultException (javax.persistence.NoResultException)1 ConstraintViolation (javax.validation.ConstraintViolation)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1