Search in sources :

Example 86 with EntityType

use of org.molgenis.data.meta.model.EntityType in project molgenis by molgenis.

the class RestController method retrieveEntityAttributeInternal.

private Object retrieveEntityAttributeInternal(String entityTypeId, String untypedId, String refAttributeName, EntityCollectionRequest request, Set<String> attributesSet, Map<String, Set<String>> attributeExpandSet) {
    EntityType meta = dataService.getEntityType(entityTypeId);
    if (meta == null) {
        throw new UnknownEntityTypeException(entityTypeId);
    }
    Object id = getTypedValue(untypedId, meta.getIdAttribute());
    // Check if the entity has an attribute with name refAttributeName
    Attribute attr = meta.getAttribute(refAttributeName);
    if (attr == null) {
        throw new UnknownAttributeException(meta, refAttributeName);
    }
    // Get the entity
    Entity entity = dataService.findOneById(entityTypeId, id);
    if (entity == null) {
        throw new UnknownEntityException(entityTypeId + " " + id + " not found");
    }
    String attrHref = Href.concatAttributeHref(RestController.BASE_URI, meta.getId(), entity.getIdValue(), refAttributeName);
    switch(attr.getDataType()) {
        case COMPOUND:
            Map<String, Object> entityHasAttributeMap = new LinkedHashMap<>();
            entityHasAttributeMap.put("href", attrHref);
            @SuppressWarnings("unchecked") Iterable<Attribute> attributeParts = (Iterable<Attribute>) entity.get(refAttributeName);
            for (Attribute attribute : attributeParts) {
                String attrName = attribute.getName();
                entityHasAttributeMap.put(attrName, entity.get(attrName));
            }
            return entityHasAttributeMap;
        case CATEGORICAL_MREF:
        case MREF:
        case ONE_TO_MANY:
            List<Entity> mrefEntities = new ArrayList<>();
            for (Entity e : entity.getEntities((attr.getName()))) mrefEntities.add(e);
            int count = mrefEntities.size();
            int toIndex = request.getStart() + request.getNum();
            mrefEntities = mrefEntities.subList(request.getStart(), toIndex > count ? count : toIndex);
            List<Map<String, Object>> refEntityMaps = new ArrayList<>();
            for (Entity refEntity : mrefEntities) {
                Map<String, Object> refEntityMap = getEntityAsMap(refEntity, attr.getRefEntity(), attributesSet, attributeExpandSet);
                refEntityMaps.add(refEntityMap);
            }
            EntityPager pager = new EntityPager(request.getStart(), request.getNum(), (long) count, mrefEntities);
            return new EntityCollectionResponse(pager, refEntityMaps, attrHref, null, permissionService, dataService);
        case CATEGORICAL:
        case XREF:
            Map<String, Object> entityXrefAttributeMap = getEntityAsMap((Entity) entity.get(refAttributeName), attr.getRefEntity(), attributesSet, attributeExpandSet);
            entityXrefAttributeMap.put("href", attrHref);
            return entityXrefAttributeMap;
        default:
            Map<String, Object> entityAttributeMap = new LinkedHashMap<>();
            entityAttributeMap.put("href", attrHref);
            entityAttributeMap.put(refAttributeName, entity.get(refAttributeName));
            return entityAttributeMap;
    }
}
Also used : Attribute(org.molgenis.data.meta.model.Attribute) EntityType(org.molgenis.data.meta.model.EntityType)

Example 87 with EntityType

use of org.molgenis.data.meta.model.EntityType in project molgenis by molgenis.

the class RestController method updateInternal.

private void updateInternal(String entityTypeId, String untypedId, Map<String, Object> entityMap) {
    EntityType meta = dataService.getEntityType(entityTypeId);
    if (meta.getIdAttribute() == null) {
        throw new IllegalArgumentException(entityTypeId + " does not have an id attribute");
    }
    Object id = getTypedValue(untypedId, meta.getIdAttribute());
    Entity existing = dataService.findOneById(entityTypeId, id, new Fetch().field(meta.getIdAttribute().getName()));
    if (existing == null) {
        throw new UnknownEntityException("Entity of type " + entityTypeId + " with id " + id + " not found");
    }
    Entity entity = this.restService.toEntity(meta, entityMap);
    dataService.update(entityTypeId, entity);
    restService.updateMappedByEntities(entity, existing);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType)

Example 88 with EntityType

use of org.molgenis.data.meta.model.EntityType in project molgenis by molgenis.

the class RestController method retrieveEntity.

/**
 * Same as retrieveEntity (GET) only tunneled through POST.
 */
@PostMapping(value = "/{entityTypeId}/{id:.+}", params = "_method=GET", produces = APPLICATION_JSON_VALUE)
@ResponseBody
public Map<String, Object> retrieveEntity(@PathVariable("entityTypeId") String entityTypeId, @PathVariable("id") String untypedId, @Valid @RequestBody EntityTypeRequest request) {
    Set<String> attributesSet = toAttributeSet(request != null ? request.getAttributes() : null);
    Map<String, Set<String>> attributeExpandSet = toExpandMap(request != null ? request.getExpand() : null);
    EntityType meta = dataService.getEntityType(entityTypeId);
    Object id = getTypedValue(untypedId, meta.getIdAttribute());
    Entity entity = dataService.findOneById(entityTypeId, id);
    if (entity == null) {
        throw new UnknownEntityException(entityTypeId + " " + untypedId + " not found");
    }
    return getEntityAsMap(entity, meta, attributesSet, attributeExpandSet);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType)

Example 89 with EntityType

use of org.molgenis.data.meta.model.EntityType in project molgenis by molgenis.

the class RestController method retrieveEntityCollectionInternal.

// Handles a Query
@SuppressWarnings("deprecation")
private EntityCollectionResponse retrieveEntityCollectionInternal(String entityTypeId, EntityCollectionRequest request, Set<String> attributesSet, Map<String, Set<String>> attributeExpandsSet) {
    EntityType meta = dataService.getEntityType(entityTypeId);
    if (meta == null) {
        throw new UnknownEntityTypeException(entityTypeId);
    }
    Repository<Entity> repository = dataService.getRepository(entityTypeId);
    // convert sort
    Sort sort;
    SortV1 sortV1 = request.getSort();
    if (sortV1 != null) {
        sort = new Sort();
        for (SortV1.OrderV1 orderV1 : sortV1) {
            sort.on(orderV1.getProperty(), orderV1.getDirection() == SortV1.DirectionV1.ASC ? Sort.Direction.ASC : Sort.Direction.DESC);
        }
    } else {
        sort = null;
    }
    List<QueryRule> queryRules = request.getQ() == null ? Collections.emptyList() : request.getQ();
    Query<Entity> q = new QueryImpl<>(queryRules).pageSize(request.getNum()).offset(request.getStart()).sort(sort);
    Iterable<Entity> it = () -> dataService.findAll(entityTypeId, q).iterator();
    Long count = repository.count(new QueryImpl<>(q).setOffset(0).setPageSize(0));
    EntityPager pager = new EntityPager(request.getStart(), request.getNum(), count, it);
    List<Map<String, Object>> entities = new ArrayList<>();
    for (Entity entity : it) {
        entities.add(getEntityAsMap(entity, meta, attributesSet, attributeExpandsSet));
    }
    return new EntityCollectionResponse(pager, entities, BASE_URI + "/" + entityTypeId, meta, permissionService, dataService);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType)

Example 90 with EntityType

use of org.molgenis.data.meta.model.EntityType in project molgenis by molgenis.

the class RestController method retrieveEntity.

/**
 * Get's an entity by it's id
 * <p>
 * Examples:
 * <p>
 * /api/v1/person/99 Retrieves a person with id 99
 */
@GetMapping(value = "/{entityTypeId}/{id:.+}", produces = APPLICATION_JSON_VALUE)
public Map<String, Object> retrieveEntity(@PathVariable("entityTypeId") String entityTypeId, @PathVariable("id") String untypedId, @RequestParam(value = "attributes", required = false) String[] attributes, @RequestParam(value = "expand", required = false) String[] attributeExpands) {
    Set<String> attributesSet = toAttributeSet(attributes);
    Map<String, Set<String>> attributeExpandSet = toExpandMap(attributeExpands);
    EntityType meta = dataService.getEntityType(entityTypeId);
    if (meta == null) {
        throw new UnknownEntityTypeException(entityTypeId);
    }
    Object id = getTypedValue(untypedId, meta.getIdAttribute());
    Entity entity = dataService.findOneById(entityTypeId, id);
    if (entity == null) {
        throw new UnknownEntityException(entityTypeId + " " + untypedId + " not found");
    }
    return getEntityAsMap(entity, meta, attributesSet, attributeExpandSet);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType)

Aggregations

EntityType (org.molgenis.data.meta.model.EntityType)581 Test (org.testng.annotations.Test)367 Attribute (org.molgenis.data.meta.model.Attribute)315 Entity (org.molgenis.data.Entity)98 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)71 DynamicEntity (org.molgenis.data.support.DynamicEntity)61 Stream (java.util.stream.Stream)44 EntityTypeIdentity (org.molgenis.data.security.EntityTypeIdentity)43 WithMockUser (org.springframework.security.test.context.support.WithMockUser)40 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)36 QueryImpl (org.molgenis.data.support.QueryImpl)33 Package (org.molgenis.data.meta.model.Package)32 Objects.requireNonNull (java.util.Objects.requireNonNull)22 Collectors.toList (java.util.stream.Collectors.toList)22 BeforeMethod (org.testng.annotations.BeforeMethod)20 AttributeType (org.molgenis.data.meta.AttributeType)19 List (java.util.List)17 String.format (java.lang.String.format)16 ExplainedAttribute (org.molgenis.semanticsearch.explain.bean.ExplainedAttribute)16 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)15