private ConstraintViolation checkHyperlink( Entity entity, Attribute attribute, EntityType entityType) { String link = entity.getString(attribute.getName()); if (link == null) { return null; } try { new URI(link); } catch (URISyntaxException e) { return createConstraintViolation(entity, attribute, entityType, "Not a valid hyperlink."); } if (link.length() > HYPERLINK.getMaxLength()) { return createConstraintViolation(entity, attribute, entityType); } return null; }
private static ConstraintViolation checkDecimal( Entity entity, Attribute attribute, EntityType entityType) { try { entity.getDouble(attribute.getName()); return null; } catch (Exception e) { return createConstraintViolation(entity, attribute, entityType); } }
private ConstraintViolation checkEnum(Entity entity, Attribute attribute, EntityType entityType) { String value = entity.getString(attribute.getName()); if (value != null) { List<String> enumOptions = attribute.getEnumOptions(); if (!enumOptions.contains(value)) { return createConstraintViolation( entity, attribute, entityType, "Value must be one of " + enumOptions.toString()); } } return null; }
private static ConstraintViolation checkDate( Entity entity, Attribute attribute, EntityType entityType) { try { entity.getLocalDate(attribute.getName()); return null; } catch (Exception e) { return createConstraintViolation(entity, attribute, entityType); } }
private static ConstraintViolation checkInt( Entity entity, Attribute attribute, EntityType entityType) { try { entity.getInt(attribute.getName()); return null; } catch (Exception e) { return createConstraintViolation(entity, attribute, entityType); } }
private static ConstraintViolation checkLong( Entity entity, Attribute attribute, EntityType entityType) { try { entity.getLong(attribute.getName()); return null; } catch (Exception e) { return createConstraintViolation(entity, attribute, entityType); } }
private static ConstraintViolation checkBoolean( Entity entity, Attribute attribute, EntityType entityType) { try { entity.getBoolean(attribute.getName()); return null; } catch (Exception e) { return createConstraintViolation(entity, attribute, entityType); } }
private static ConstraintViolation checkDateTime( Entity entity, Attribute attribute, EntityType entityType) { try { entity.getInstant(attribute.getName()); return null; } catch (Exception e) { return createConstraintViolation(entity, attribute, entityType); } }
private ConstraintViolation checkMref(Entity entity, Attribute attr, EntityType entityType) { Iterable<Entity> refEntities; try { refEntities = entity.getEntities(attr.getName()); } catch (Exception e) { return createConstraintViolation( entity, attr, entityType, "Not a valid entity, expected an entity list."); } if (refEntities == null) { return createConstraintViolation( entity, attr, entityType, "Not a valid entity, expected an entity list."); } for (Entity refEntity : refEntities) { if (refEntity == null) { return createConstraintViolation( entity, attr, entityType, "Not a valid entity, null is not allowed"); } if (!refEntity.getEntityType().getId().equals(attr.getRefEntity().getId())) { return createConstraintViolation(entity, attr, entityType, "Not a valid entity type."); } } return null; }
private static ConstraintViolation checkText( Entity entity, Attribute attribute, EntityType meta, AttributeType fieldType) { String text = entity.getString(attribute.getName()); if (text == null) { return null; } if (text.length() > fieldType.getMaxLength()) { return createConstraintViolation(entity, attribute, meta); } return null; }
private Set<ConstraintViolation> checkValidationExpressions(Entity entity, EntityType meta) { List<String> validationExpressions = new ArrayList<>(); List<Attribute> expressionAttributes = new ArrayList<>(); for (Attribute attribute : meta.getAtomicAttributes()) { if (StringUtils.isNotBlank(attribute.getValidationExpression())) { expressionAttributes.add(attribute); validationExpressions.add(attribute.getValidationExpression()); } } Set<ConstraintViolation> violations = new LinkedHashSet<>(); if (!validationExpressions.isEmpty()) { List<Boolean> results = expressionValidator.resolveBooleanExpressions(validationExpressions, entity); for (int i = 0; i < results.size(); i++) { if (!results.get(i)) { violations.add( createConstraintViolation( entity, expressionAttributes.get(i), meta, format("Offended validation expression: %s", validationExpressions.get(i)))); } } } return violations; }
private Set<ConstraintViolation> checkNullableExpressions(Entity entity, EntityType entityType) { List<String> nullableExpressions = new ArrayList<>(); List<Attribute> expressionAttributes = new ArrayList<>(); for (Attribute attribute : entityType.getAtomicAttributes()) { String nullableExpression = attribute.getNullableExpression(); if (nullableExpression != null) { expressionAttributes.add(attribute); nullableExpressions.add(nullableExpression); } } Set<ConstraintViolation> violations = new LinkedHashSet<>(); if (!nullableExpressions.isEmpty()) { List<Boolean> results = expressionValidator.resolveBooleanExpressions(nullableExpressions, entity); for (int i = 0; i < results.size(); i++) { if (!results.get(i) && EntityUtils.isNullValue(entity, expressionAttributes.get(i))) { violations.add( createConstraintViolation( entity, expressionAttributes.get(i), entityType, format("Offended nullable expression: %s", nullableExpressions.get(i)))); } } } return violations; }
private ConstraintViolation checkEmail( Entity entity, Attribute attribute, EntityType entityType) { String email = entity.getString(attribute.getName()); if (email == null) { return null; } if (emailValidator == null) { emailValidator = new EmailValidator(); } if (!emailValidator.isValid(email, null)) { return createConstraintViolation( entity, attribute, entityType, "Not a valid e-mail address."); } if (email.length() > EMAIL.getMaxLength()) { return createConstraintViolation(entity, attribute, entityType); } return null; }
private ConstraintViolation checkXref(Entity entity, Attribute attr, EntityType entityType) { Entity refEntity; try { refEntity = entity.getEntity(attr.getName()); } catch (Exception e) { return createConstraintViolation(entity, attr, entityType, "Not a valid entity."); } if (refEntity == null) { return null; } if (!refEntity.getEntityType().getId().equals(attr.getRefEntity().getId())) { return createConstraintViolation(entity, attr, entityType, "Not a valid entity type."); } return null; }
private static ConstraintViolation checkRange( Entity entity, Attribute attr, EntityType entityType) { Range range = attr.getRange(); Long value; switch (attr.getDataType()) { case INT: Integer intValue = entity.getInt(attr.getName()); value = intValue != null ? intValue.longValue() : null; break; case LONG: value = entity.getLong(attr.getName()); break; default: throw new RuntimeException( format("Range not allowed for data type [%s]", attr.getDataType().toString())); } if ((value != null) && ((range.getMin() != null && value < range.getMin()) || (range.getMax() != null && value > range.getMax()))) { return createConstraintViolation(entity, attr, entityType); } return null; }