Search in sources :

Example 1 with HugeSet

use of org.molgenis.util.HugeSet 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().spliterator(), false).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().spliterator(), false).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 : java.util(java.util) RepositoryCapability(org.molgenis.data.RepositoryCapability) org.molgenis.data(org.molgenis.data) IOException(java.io.IOException) QueryImpl(org.molgenis.data.support.QueryImpl) EntityTypeUtils(org.molgenis.data.support.EntityTypeUtils) Attribute(org.molgenis.data.meta.model.Attribute) EntityType(org.molgenis.data.meta.model.EntityType) String.format(java.lang.String.format) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) StreamSupport.stream(java.util.stream.StreamSupport.stream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Objects.requireNonNull(java.util.Objects.requireNonNull) HugeSet(org.molgenis.util.HugeSet) HugeMap(org.molgenis.util.HugeMap) Collections(java.util.Collections) Attribute(org.molgenis.data.meta.model.Attribute) EntityType(org.molgenis.data.meta.model.EntityType) HugeSet(org.molgenis.util.HugeSet)

Aggregations

IOException (java.io.IOException)1 String.format (java.lang.String.format)1 java.util (java.util)1 Collections (java.util.Collections)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors.toList (java.util.stream.Collectors.toList)1 Stream (java.util.stream.Stream)1 StreamSupport.stream (java.util.stream.StreamSupport.stream)1 org.molgenis.data (org.molgenis.data)1 RepositoryCapability (org.molgenis.data.RepositoryCapability)1 Attribute (org.molgenis.data.meta.model.Attribute)1 EntityType (org.molgenis.data.meta.model.EntityType)1 EntityTypeUtils (org.molgenis.data.support.EntityTypeUtils)1 QueryImpl (org.molgenis.data.support.QueryImpl)1 HugeMap (org.molgenis.util.HugeMap)1 HugeSet (org.molgenis.util.HugeSet)1