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;
}
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());
}
}
Aggregations