Search in sources :

Example 1 with ONE_TO_MANY

use of org.molgenis.data.meta.AttributeType.ONE_TO_MANY in project molgenis by molgenis.

the class RestService method updateMappedByEntitiesOneToMany.

/**
 * For entities with the given attribute that is part of a bidirectional one-to-many relationship update the other side of the relationship.
 *
 * @param entity         created or updated entity
 * @param existingEntity existing entity
 * @param attr           bidirectional one-to-many attribute
 */
private void updateMappedByEntitiesOneToMany(@Nonnull Entity entity, @Nullable Entity existingEntity, @Nonnull Attribute attr) {
    if (attr.getDataType() != ONE_TO_MANY || !attr.isMappedBy()) {
        throw new IllegalArgumentException(format("Attribute [%s] is not of type [%s] or not mapped by another attribute", attr.getName(), attr.getDataType().toString()));
    }
    // update ref entities of created/updated entity
    Attribute refAttr = attr.getMappedBy();
    Stream<Entity> stream = stream(entity.getEntities(attr.getName()).spliterator(), false);
    if (existingEntity != null) {
        // filter out unchanged ref entities
        Set<Object> refEntityIds = stream(existingEntity.getEntities(attr.getName()).spliterator(), false).map(Entity::getIdValue).collect(toSet());
        stream = stream.filter(refEntity -> !refEntityIds.contains(refEntity.getIdValue()));
    }
    List<Entity> updatedRefEntities = stream.map(refEntity -> {
        if (refEntity.getEntity(refAttr.getName()) != null) {
            throw new MolgenisDataException(format("Updating [%s] with id [%s] not allowed: [%s] is already referred to by another [%s]", attr.getRefEntity().getId(), refEntity.getIdValue().toString(), refAttr.getName(), entity.getEntityType().getId()));
        }
        refEntity.set(refAttr.getName(), entity);
        return refEntity;
    }).collect(toList());
    // update ref entities of existing entity
    if (existingEntity != null) {
        Set<Object> refEntityIds = stream(entity.getEntities(attr.getName()).spliterator(), false).map(Entity::getIdValue).collect(toSet());
        List<Entity> updatedRefEntitiesExistingEntity = stream(existingEntity.getEntities(attr.getName()).spliterator(), false).filter(refEntity -> !refEntityIds.contains(refEntity.getIdValue())).map(refEntity -> {
            refEntity.set(refAttr.getName(), null);
            return refEntity;
        }).collect(toList());
        updatedRefEntities = Stream.concat(updatedRefEntities.stream(), updatedRefEntitiesExistingEntity.stream()).collect(toList());
    }
    if (!updatedRefEntities.isEmpty()) {
        dataService.update(attr.getRefEntity().getId(), updatedRefEntities.stream());
    }
}
Also used : IdGenerator(org.molgenis.data.populate.IdGenerator) ONE_TO_MANY(org.molgenis.data.meta.AttributeType.ONE_TO_MANY) StringUtils(org.apache.commons.lang3.StringUtils) Attribute(org.molgenis.data.meta.model.Attribute) FileStore(org.molgenis.data.file.FileStore) FileDownloadController(org.molgenis.core.ui.file.FileDownloadController) FileMetaFactory(org.molgenis.data.file.model.FileMetaFactory) Service(org.springframework.stereotype.Service) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Collectors.toSet(java.util.stream.Collectors.toSet) POPULATE(org.molgenis.data.EntityManager.CreationMode.POPULATE) AttributeType(org.molgenis.data.meta.AttributeType) ServletUriComponentsBuilder(org.springframework.web.servlet.support.ServletUriComponentsBuilder) Collections.emptyList(java.util.Collections.emptyList) Set(java.util.Set) IOException(java.io.IOException) Instant(java.time.Instant) MolgenisDateFormat(org.molgenis.data.util.MolgenisDateFormat) EntityType(org.molgenis.data.meta.model.EntityType) FILENAME(org.molgenis.data.file.model.FileMetaMetaData.FILENAME) String.format(java.lang.String.format) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) FileMeta(org.molgenis.data.file.model.FileMeta) FILE_META(org.molgenis.data.file.model.FileMetaMetaData.FILE_META) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) StreamSupport.stream(java.util.stream.StreamSupport.stream) LocalDate(java.time.LocalDate) DataService(org.molgenis.data.DataService) MultipartFile(org.springframework.web.multipart.MultipartFile) MolgenisDataException(org.molgenis.data.MolgenisDataException) EntityManager(org.molgenis.data.EntityManager) Entity(org.molgenis.data.Entity) UriComponents(org.springframework.web.util.UriComponents) Entity(org.molgenis.data.Entity) MolgenisDataException(org.molgenis.data.MolgenisDataException) Attribute(org.molgenis.data.meta.model.Attribute)

Aggregations

IOException (java.io.IOException)1 String.format (java.lang.String.format)1 Instant (java.time.Instant)1 LocalDate (java.time.LocalDate)1 DateTimeParseException (java.time.format.DateTimeParseException)1 Arrays.asList (java.util.Arrays.asList)1 Collections.emptyList (java.util.Collections.emptyList)1 List (java.util.List)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Set (java.util.Set)1 Collectors.toList (java.util.stream.Collectors.toList)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 Stream (java.util.stream.Stream)1 StreamSupport.stream (java.util.stream.StreamSupport.stream)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 StringUtils (org.apache.commons.lang3.StringUtils)1 FileDownloadController (org.molgenis.core.ui.file.FileDownloadController)1 DataService (org.molgenis.data.DataService)1