Search in sources :

Example 11 with Selection

use of org.molgenis.api.model.Selection in project molgenis by molgenis.

the class EntityController method getEntity.

@Transactional(readOnly = true)
@GetMapping("/{entityTypeId}/{entityId}")
public EntityResponse getEntity(@Valid ReadEntityRequest entityRequest) {
    Selection filter = entityRequest.getFilter();
    Selection expand = entityRequest.getExpand();
    Entity entity = dataServiceV3.find(entityRequest.getEntityTypeId(), entityRequest.getEntityId(), filter, expand);
    return entityMapper.map(entity, filter, expand);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) Entity(org.molgenis.data.Entity) Selection(org.molgenis.api.model.Selection) GetMapping(org.springframework.web.bind.annotation.GetMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Example 12 with Selection

use of org.molgenis.api.model.Selection in project molgenis by molgenis.

the class EntityController method getEntities.

@Transactional(readOnly = true)
@GetMapping("/{entityTypeId}")
public EntitiesResponse getEntities(@Valid ReadEntitiesRequest entitiesRequest) {
    String entityTypeId = entitiesRequest.getEntityTypeId();
    Selection filter = entitiesRequest.getFilter();
    Selection expand = entitiesRequest.getExpand();
    int size = entitiesRequest.getSize();
    int page = entitiesRequest.getPage();
    Sort sort = entitiesRequest.getSort();
    Entities entities = dataServiceV3.findAll(entityTypeId, entitiesRequest.getQ().orElse(null), filter, expand, sort, size, page);
    EntityCollection entityCollection = EntityCollection.builder().setEntityTypeId(entityTypeId).setEntities(entities.getEntities()).setPage(Page.builder().setOffset(size * page).setPageSize(size).setTotal(entities.getTotal()).build()).build();
    return entityMapper.map(entityCollection, filter, expand, size, page, entities.getTotal());
}
Also used : Selection(org.molgenis.api.model.Selection) Sort(org.molgenis.api.model.Sort) GetMapping(org.springframework.web.bind.annotation.GetMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Example 13 with Selection

use of org.molgenis.api.model.Selection in project molgenis by molgenis.

the class EntityMapperImpl method mapRecursive.

private EntityResponse mapRecursive(Entity entity, Selection filter, Selection expand, int depth) {
    if (depth > MAX_DEPTH) {
        throw new IllegalArgumentException("max_depth exceeded: " + depth);
    }
    EntityResponse.Builder builder = EntityResponse.builder();
    if (filter.hasItems()) {
        Map<String, Object> dataMap = new LinkedHashMap<>();
        stream(entity.getEntityType().getAtomicAttributes()).filter(attribute -> filter.hasItem(attribute.getName())).forEach(attribute -> dataMap.put(attribute.getName(), mapRecursive(entity, attribute, filter, expand, depth)));
        builder.setData(dataMap);
    }
    URI uri = createEntityResponseUri(entity);
    return builder.setLinks(LinksResponse.create(null, uri, null)).build();
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) AttributeType(org.molgenis.data.meta.AttributeType) Builder(org.molgenis.api.data.v3.model.EntitiesResponse.Builder) MolgenisServletUriComponentsBuilder(org.molgenis.api.support.MolgenisServletUriComponentsBuilder) IllegalAttributeTypeException(org.molgenis.data.meta.IllegalAttributeTypeException) Selection(org.molgenis.api.model.Selection) LinksUtils(org.molgenis.api.support.LinksUtils) LinksResponse(org.molgenis.api.model.response.LinksResponse) Attribute(org.molgenis.data.meta.model.Attribute) EntitiesResponse(org.molgenis.api.data.v3.model.EntitiesResponse) Streams.stream(com.google.common.collect.Streams.stream) LinkedHashMap(java.util.LinkedHashMap) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Component(org.springframework.stereotype.Component) EntityResponse(org.molgenis.api.data.v3.model.EntityResponse) Map(java.util.Map) PageUtils.getPageResponse(org.molgenis.api.support.PageUtils.getPageResponse) URI(java.net.URI) CheckForNull(javax.annotation.CheckForNull) Nullable(javax.annotation.Nullable) Page(org.molgenis.api.data.v3.EntityCollection.Page) Entity(org.molgenis.data.Entity) EntityResponse(org.molgenis.api.data.v3.model.EntityResponse) URI(java.net.URI) LinkedHashMap(java.util.LinkedHashMap)

Example 14 with Selection

use of org.molgenis.api.model.Selection in project molgenis by molgenis.

the class FetchMapper method toFetch.

@CheckForNull
@Nullable
public Fetch toFetch(EntityType entityType, Selection filter, Selection expand) {
    if (!filter.hasItems()) {
        return null;
    }
    Fetch fetch = new Fetch();
    Iterable<Attribute> attributes = entityType.getAtomicAttributes();
    attributes.forEach(attribute -> {
        String attributeName = attribute.getName();
        if (filter.hasItem(attributeName)) {
            Fetch subFetch;
            if (expand.hasItem(attributeName) && EntityTypeUtils.isReferenceType(attribute)) {
                Selection subFilter = filter.getSelection(attributeName).orElse(EMPTY_SELECTION);
                Selection subExpand = expand.getSelection(attributeName).orElse(EMPTY_SELECTION);
                subFetch = toFetch(attribute.getRefEntity(), subFilter, subExpand);
            } else {
                subFetch = null;
            }
            fetch.field(attributeName, subFetch);
        }
    });
    return fetch;
}
Also used : Fetch(org.molgenis.data.Fetch) Attribute(org.molgenis.data.meta.model.Attribute) Selection(org.molgenis.api.model.Selection) CheckForNull(javax.annotation.CheckForNull) Nullable(javax.annotation.Nullable)

Example 15 with Selection

use of org.molgenis.api.model.Selection in project molgenis by molgenis.

the class SelectionParserTest method testMultipleItemsSubSelection.

@Test
void testMultipleItemsSubSelection() throws ParseException {
    Map<String, Selection> itemSelections = new HashMap<>();
    itemSelections.put("abc", new Selection(Collections.singletonMap("def", null)));
    itemSelections.put("ghi", new Selection(Collections.singletonMap("jkl", null)));
    assertEquals(new Selection(itemSelections), new SelectionParser("abc(def),ghi(jkl)").parse());
}
Also used : HashMap(java.util.HashMap) Selection(org.molgenis.api.model.Selection) Test(org.junit.jupiter.api.Test)

Aggregations

Selection (org.molgenis.api.model.Selection)32 Test (org.junit.jupiter.api.Test)24 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)19 Entity (org.molgenis.data.Entity)18 Attribute (org.molgenis.data.meta.model.Attribute)15 EntityType (org.molgenis.data.meta.model.EntityType)13 Fetch (org.molgenis.data.Fetch)12 Sort (org.molgenis.api.model.Sort)11 Query (org.molgenis.api.model.Query)9 QueryImpl (org.molgenis.data.support.QueryImpl)7 URI (java.net.URI)5 EntityResponse (org.molgenis.api.data.v3.model.EntityResponse)5 CheckForNull (javax.annotation.CheckForNull)4 Nullable (javax.annotation.Nullable)4 EntitiesResponse (org.molgenis.api.data.v3.model.EntitiesResponse)4 HashMap (java.util.HashMap)3 Transactional (org.springframework.transaction.annotation.Transactional)3 GetMapping (org.springframework.web.bind.annotation.GetMapping)3 Streams.stream (com.google.common.collect.Streams.stream)2 LinkedHashMap (java.util.LinkedHashMap)2