Search in sources :

Example 1 with VALIDATE_REFERENCE_CONSTRAINT

use of org.molgenis.data.RepositoryCapability.VALIDATE_REFERENCE_CONSTRAINT in project molgenis by molgenis.

the class RepositoryValidationDecorator method initReferenceValidation.

private void initReferenceValidation(ValidationResource validationResource) {
    // get reference attrs
    List<Attribute> refAttrs;
    if (!getCapabilities().contains(VALIDATE_REFERENCE_CONSTRAINT)) {
        // get reference attrs
        refAttrs = stream(getEntityType().getAtomicAttributes()).filter(attr -> isReferenceType(attr) && attr.getExpression() == null).collect(toList());
    } else {
        // validate cross-repository collection reference constraints. the decorated repository takes
        // care of
        // validating other reference constraints
        String backend = dataService.getMeta().getBackend(getEntityType()).getName();
        refAttrs = stream(getEntityType().getAtomicAttributes()).filter(attr -> isReferenceType(attr) && attr.getExpression() == null && isDifferentBackend(backend, attr)).collect(toList());
    }
    // get referenced entity ids
    if (!refAttrs.isEmpty()) {
        Map<String, HugeSet<Object>> refEntitiesIds = new HashMap<>();
        refAttrs.forEach(refAttr -> {
            EntityType refEntityType = refAttr.getRefEntity();
            String refEntityName = refEntityType.getId();
            HugeSet<Object> refEntityIds = refEntitiesIds.get(refEntityName);
            if (refEntityIds == null) {
                refEntityIds = new HugeSet<>();
                refEntitiesIds.put(refEntityName, refEntityIds);
                Query<Entity> q = new QueryImpl<>().fetch(new Fetch().field(refEntityType.getIdAttribute().getName()));
                for (Iterator<Entity> it = dataService.findAll(refEntityName, q).iterator(); it.hasNext(); ) {
                    refEntityIds.add(it.next().getIdValue());
                }
            }
        });
        validationResource.setRefEntitiesIds(refEntitiesIds);
    }
    validationResource.setSelfReferencing(refAttrs.stream().anyMatch(refAttr -> refAttr.getRefEntity().getId().equals(getEntityType().getId())));
    validationResource.setRefAttrs(refAttrs);
}
Also used : Collections.unmodifiableList(java.util.Collections.unmodifiableList) HashMap(java.util.HashMap) VALIDATE_REFERENCE_CONSTRAINT(org.molgenis.data.RepositoryCapability.VALIDATE_REFERENCE_CONSTRAINT) Fetch(org.molgenis.data.Fetch) QueryImpl(org.molgenis.data.support.QueryImpl) VALIDATE_NOTNULL_CONSTRAINT(org.molgenis.data.RepositoryCapability.VALIDATE_NOTNULL_CONSTRAINT) Attribute(org.molgenis.data.meta.model.Attribute) VALIDATE_UNIQUE_CONSTRAINT(org.molgenis.data.RepositoryCapability.VALIDATE_UNIQUE_CONSTRAINT) EntityTypeUtils.isSingleReferenceType(org.molgenis.data.util.EntityTypeUtils.isSingleReferenceType) Collections.singleton(java.util.Collections.singleton) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntityTypeUtils.isMultipleReferenceType(org.molgenis.data.util.EntityTypeUtils.isMultipleReferenceType) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) AbstractRepositoryDecorator(org.molgenis.data.AbstractRepositoryDecorator) LinkedHashSet(java.util.LinkedHashSet) DataConverter(org.molgenis.data.DataConverter) Collections.emptyMap(java.util.Collections.emptyMap) Collections.emptySet(java.util.Collections.emptySet) EntityTypeUtils.isReferenceType(org.molgenis.data.util.EntityTypeUtils.isReferenceType) Iterator(java.util.Iterator) Collections.emptyList(java.util.Collections.emptyList) Set(java.util.Set) VALIDATE_READONLY_CONSTRAINT(org.molgenis.data.RepositoryCapability.VALIDATE_READONLY_CONSTRAINT) EntityType(org.molgenis.data.meta.model.EntityType) String.format(java.lang.String.format) Streams.stream(com.google.common.collect.Streams.stream) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) ConstraintViolation(org.molgenis.validation.ConstraintViolation) Collections.unmodifiableSet(java.util.Collections.unmodifiableSet) Stream(java.util.stream.Stream) Repository(org.molgenis.data.Repository) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) DataService(org.molgenis.data.DataService) Query(org.molgenis.data.Query) HugeSet(org.molgenis.util.HugeSet) HugeMap(org.molgenis.util.HugeMap) Entity(org.molgenis.data.Entity) Entity(org.molgenis.data.Entity) Attribute(org.molgenis.data.meta.model.Attribute) HashMap(java.util.HashMap) EntityType(org.molgenis.data.meta.model.EntityType) Fetch(org.molgenis.data.Fetch) HugeSet(org.molgenis.util.HugeSet)

Aggregations

Streams.stream (com.google.common.collect.Streams.stream)1 String.format (java.lang.String.format)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Collections.emptySet (java.util.Collections.emptySet)1 Collections.singleton (java.util.Collections.singleton)1 Collections.unmodifiableList (java.util.Collections.unmodifiableList)1 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)1 Collections.unmodifiableSet (java.util.Collections.unmodifiableSet)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Set (java.util.Set)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors.toList (java.util.stream.Collectors.toList)1 Stream (java.util.stream.Stream)1 AbstractRepositoryDecorator (org.molgenis.data.AbstractRepositoryDecorator)1