use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.
the class RepositoryValidationDecorator method validateEntityValueReferences.
private void validateEntityValueReferences(Entity entity, ValidationResource validationResource) {
validationResource.getRefAttrs().forEach(refAttr -> {
HugeSet<Object> refEntityIds = validationResource.getRefEntitiesIds().get(refAttr.getRefEntity().getId());
Iterable<Entity> refEntities;
if (isSingleReferenceType(refAttr)) {
Entity refEntity = entity.getEntity(refAttr.getName());
if (refEntity != null) {
refEntities = singleton(refEntity);
} else {
refEntities = emptyList();
}
} else {
refEntities = entity.getEntities(refAttr.getName());
}
for (Entity refEntity : refEntities) {
if (!refEntityIds.contains(refEntity.getIdValue())) {
boolean selfReference = entity.getEntityType().getId().equals(refAttr.getRefEntity().getId());
if (!(selfReference && entity.getIdValue().equals(refEntity.getIdValue()))) {
String message = String.format("Unknown xref value '%s' for attribute '%s' of entity '%s'.", DataConverter.toString(refEntity.getIdValue()), refAttr.getName(), getName());
ConstraintViolation constraintViolation = new ConstraintViolation(message, (long) validationResource.getRow());
validationResource.addViolation(constraintViolation);
}
}
}
// only do if self reference
if (validationResource.isSelfReferencing()) {
validationResource.addRefEntityId(getName(), entity.getIdValue());
}
});
}
use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.
the class EntityAttributesValidator method createConstraintViolation.
private static ConstraintViolation createConstraintViolation(Entity entity, Attribute attribute, EntityType entityType) {
String message = format("Invalid %s value '%s' for attribute '%s' of entity '%s'.", attribute.getDataType().toString().toLowerCase(), entity.get(attribute.getName()), attribute.getLabel(), entityType.getId());
Range range = attribute.getRange();
if (range != null) {
message += format("Value must be between %d and %d", range.getMin(), range.getMax());
}
Integer maxLength = attribute.getMaxLength();
if (maxLength != null) {
message += format("Value must be less than or equal to %d characters", maxLength);
}
return new ConstraintViolation(message);
}
use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.
the class EntityTypeValidator method validateOwnAttributes.
/**
* Validates that this entity: <br>
* 1) has attributes defined at all <br>
* 2) doesn't have attributes with the same name (also in the parent entities)<br>
* 3) doesn't have attributes with a non-existing parent <br>
*
* @param entityType entity meta data
* @throws MolgenisValidationException if one of the above rules is violated
*/
static void validateOwnAttributes(EntityType entityType) {
// Validate that entity has attributes
if (stream(entityType.getAllAttributes()).collect(toList()).isEmpty()) {
throw new MolgenisValidationException(new ConstraintViolation(format("EntityType [%s] does not contain any attributes. Did you use the correct package+entity name combination in both the entities as well as the attributes sheet?", entityType.getId())));
}
// Validate that entity does not contain multiple attributes with the same name
Multimap<String, Attribute> attrMultiMap = stream(entityType.getAllAttributes()).collect(MultimapCollectors.toArrayListMultimap(Attribute::getName, Function.identity()));
attrMultiMap.keySet().forEach(attrName -> {
if (attrMultiMap.get(attrName).size() > 1) {
throw new MolgenisValidationException(new ConstraintViolation(format("EntityType [%s] contains multiple attributes with name [%s]", entityType.getId(), attrName)));
}
});
// Validate that all parent attributes actually exist
Set<String> attributeIds = stream(entityType.getAllAttributes()).map(Attribute::getIdentifier).collect(toSet());
stream(entityType.getAllAttributes()).filter(attr -> attr.getParent() != null).forEach(attr -> {
if (!attributeIds.contains(attr.getParent().getIdentifier())) {
throw new MolgenisValidationException(new ConstraintViolation(format("Attribute [%s] of EntityType [%s] has a non-existing parent attribute [%s]", attr.getName(), entityType.getId(), attr.getParent().getName())));
}
});
}
use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.
the class TagValidator method validate.
/**
* Validates tag
*
* @param tag tag
* @throws MolgenisValidationException if tag is not valid
*/
@SuppressWarnings("MethodMayBeStatic")
public void validate(Tag tag) {
String relationIri = tag.getRelationIri();
Relation relation = Relation.forIRI(relationIri);
if (relation == null) {
throw new MolgenisValidationException(new ConstraintViolation(format("Unknown relation IRI [%s]", relationIri)));
}
}
use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.
the class JobScheduler method schedule.
/**
* Schedule a {@link ScheduledJob} with a cron expression defined in the entity.
*
* <p>Reschedules job if the job already exists.
*
* <p>If active is false, it unschedules the job
*
* @param scheduledJob the {@link ScheduledJob} to schedule
*/
public synchronized void schedule(ScheduledJob scheduledJob) {
String id = scheduledJob.getId();
String cronExpression = scheduledJob.getCronExpression();
String name = scheduledJob.getName();
// Validate cron expression
if (!CronExpression.isValidExpression(cronExpression)) {
throw new MolgenisValidationException(singleton(new ConstraintViolation("Invalid cronexpression '" + cronExpression + "'", null)));
}
try {
// If already scheduled, remove it from the quartzScheduler
if (quartzScheduler.checkExists(new JobKey(id, SCHEDULED_JOB_GROUP))) {
unschedule(id);
}
// If not active, do not schedule it
if (!scheduledJob.isActive()) {
return;
}
// Schedule with 'cron' trigger
Trigger trigger = newTrigger().withIdentity(id, SCHEDULED_JOB_GROUP).withSchedule(cronSchedule(cronExpression)).build();
schedule(scheduledJob, trigger);
LOG.info("Scheduled Job '{}' with trigger '{}'", name, trigger);
} catch (SchedulerException e) {
LOG.error("Error schedule job", e);
throw new ScheduledJobException("Error schedule job", e);
}
}
Aggregations