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;
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations