use of org.molgenis.data.rest.v2.RestControllerV2.BASE_URI in project molgenis by molgenis.
the class RestControllerV2 method createEntityCollectionResponse.
private EntityCollectionResponseV2 createEntityCollectionResponse(String entityTypeId, EntityCollectionRequestV2 request, HttpServletRequest httpRequest, boolean includeCategories) {
EntityType meta = dataService.getEntityType(entityTypeId);
Query<Entity> q = request.getQ() != null ? request.getQ().createQuery(meta) : new QueryImpl<>();
q.pageSize(request.getNum()).offset(request.getStart()).sort(request.getSort());
Fetch fetch = AttributeFilterToFetchConverter.convert(request.getAttrs(), meta, LocaleContextHolder.getLocale().getLanguage());
if (fetch != null) {
q.fetch(fetch);
}
if (request.getAggs() != null) {
// return aggregates for aggregate query
AggregateQuery aggsQ = request.getAggs().createAggregateQuery(meta, q);
Attribute xAttr = aggsQ.getAttributeX();
Attribute yAttr = aggsQ.getAttributeY();
if (xAttr == null && yAttr == null) {
throw new MolgenisQueryException("Aggregate query is missing 'x' or 'y' attribute");
}
AggregateResult aggs = dataService.aggregate(entityTypeId, aggsQ);
AttributeResponseV2 xAttrResponse = xAttr != null ? new AttributeResponseV2(entityTypeId, meta, xAttr, fetch, permissionService, dataService) : null;
AttributeResponseV2 yAttrResponse = yAttr != null ? new AttributeResponseV2(entityTypeId, meta, yAttr, fetch, permissionService, dataService) : null;
return new EntityAggregatesResponse(aggs, xAttrResponse, yAttrResponse, BASE_URI + '/' + entityTypeId);
} else {
Long count = dataService.count(entityTypeId, new QueryImpl<>(q).setOffset(0).setPageSize(0));
Iterable<Entity> it;
if (count > 0 && q.getPageSize() > 0) {
it = () -> dataService.findAll(entityTypeId, q).iterator();
} else {
it = Collections.emptyList();
}
EntityPager pager = new EntityPager(request.getStart(), request.getNum(), count, it);
List<Map<String, Object>> entities = new ArrayList<>();
for (Entity entity : it) {
Map<String, Object> responseData = new LinkedHashMap<>();
createEntityValuesResponse(entity, fetch, responseData);
entities.add(responseData);
}
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(getFullURL(httpRequest));
String prevHref = null;
if (pager.getPrevStart() != null) {
builder.replaceQueryParam("start", pager.getPrevStart());
prevHref = builder.build(false).toUriString();
}
String nextHref = null;
if (pager.getNextStart() != null) {
builder.replaceQueryParam("start", pager.getNextStart());
nextHref = builder.build(false).toUriString();
}
return new EntityCollectionResponseV2(pager, entities, fetch, BASE_URI + '/' + entityTypeId, meta, permissionService, dataService, prevHref, nextHref, includeCategories);
}
}
use of org.molgenis.data.rest.v2.RestControllerV2.BASE_URI in project molgenis by molgenis.
the class RestControllerV2 method createEntityValuesResponseRec.
private void createEntityValuesResponseRec(Entity entity, Iterable<Attribute> attrs, Fetch fetch, Map<String, Object> responseData) {
responseData.put("_href", Href.concatEntityHref(BASE_URI, entity.getEntityType().getId(), entity.getIdValue()));
for (// TODO performance use fetch instead of attrs
Attribute attr : // TODO performance use fetch instead of attrs
attrs) {
String attrName = attr.getName();
if (fetch == null || fetch.hasField(attr)) {
AttributeType dataType = attr.getDataType();
switch(dataType) {
case BOOL:
responseData.put(attrName, entity.getBoolean(attrName));
break;
case CATEGORICAL:
case XREF:
case FILE:
Entity refEntity = entity.getEntity(attrName);
Map<String, Object> refEntityResponse;
if (refEntity != null) {
Fetch refAttrFetch = fetch != null ? fetch.getFetch(attr) : createDefaultAttributeFetch(attr, LanguageService.getCurrentUserLanguageCode());
refEntityResponse = createEntityResponse(refEntity, refAttrFetch, false);
} else {
refEntityResponse = null;
}
responseData.put(attrName, refEntityResponse);
break;
case CATEGORICAL_MREF:
case MREF:
case ONE_TO_MANY:
Iterable<Entity> refEntities = entity.getEntities(attrName);
List<Map<String, Object>> refEntityResponses;
if (refEntities != null) {
refEntityResponses = new ArrayList<>();
Fetch refAttrFetch = fetch != null ? fetch.getFetch(attrName) : createDefaultAttributeFetch(attr, LanguageService.getCurrentUserLanguageCode());
for (Entity refEntitiesEntity : refEntities) {
refEntityResponses.add(createEntityResponse(refEntitiesEntity, refAttrFetch, false));
}
} else {
refEntityResponses = null;
}
responseData.put(attrName, refEntityResponses);
break;
case COMPOUND:
throw new RuntimeException("Invalid data type [" + dataType + "]");
case DATE:
LocalDate dateValue = entity.getLocalDate(attrName);
responseData.put(attrName, dateValue != null ? dateValue.toString() : null);
break;
case DATE_TIME:
Instant dateTimeValue = entity.getInstant(attrName);
responseData.put(attrName, dateTimeValue != null ? dateTimeValue.toString() : null);
break;
case DECIMAL:
responseData.put(attrName, entity.getDouble(attrName));
break;
case EMAIL:
case ENUM:
case HTML:
case HYPERLINK:
case SCRIPT:
case STRING:
case TEXT:
responseData.put(attrName, entity.getString(attrName));
break;
case INT:
responseData.put(attrName, entity.getInt(attrName));
break;
case LONG:
responseData.put(attrName, entity.getLong(attrName));
break;
default:
throw new UnexpectedEnumException(dataType);
}
}
}
}
use of org.molgenis.data.rest.v2.RestControllerV2.BASE_URI in project molgenis by molgenis.
the class RestControllerV2 method createEntities.
/**
* Try to create multiple entities in one transaction. If one fails all fails.
*
* @param entityTypeId name of the entity where the entities are going to be added.
* @param request EntityCollectionCreateRequestV2
* @param response HttpServletResponse
* @return EntityCollectionCreateResponseBodyV2
*/
@Transactional
@PostMapping(value = "/{entityTypeId}", produces = APPLICATION_JSON_VALUE)
public EntityCollectionBatchCreateResponseBodyV2 createEntities(@PathVariable("entityTypeId") String entityTypeId, @RequestBody @Valid EntityCollectionBatchRequestV2 request, HttpServletResponse response) throws Exception {
final EntityType meta = dataService.getEntityType(entityTypeId);
if (meta == null) {
throw createUnknownEntityException(entityTypeId);
}
try {
final List<Entity> entities = request.getEntities().stream().map(e -> this.restService.toEntity(meta, e)).collect(toList());
final EntityCollectionBatchCreateResponseBodyV2 responseBody = new EntityCollectionBatchCreateResponseBodyV2();
final List<String> ids = new ArrayList<>();
// Add all entities
if (ATTRIBUTE_META_DATA.equals(entityTypeId)) {
entities.stream().map(attribute -> (Attribute) attribute).forEach(attribute -> dataService.getMeta().addAttribute(attribute));
} else {
this.dataService.add(entityTypeId, entities.stream());
}
entities.forEach(entity -> {
restService.updateMappedByEntities(entity);
String id = entity.getIdValue().toString();
ids.add(id);
responseBody.getResources().add(new AutoValue_ResourcesResponseV2(Href.concatEntityHref(RestControllerV2.BASE_URI, entityTypeId, id)));
});
responseBody.setLocation(Href.concatEntityCollectionHref(RestControllerV2.BASE_URI, entityTypeId, meta.getIdAttribute().getName(), ids));
response.setStatus(HttpServletResponse.SC_CREATED);
return responseBody;
} catch (Exception e) {
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
throw e;
}
}
Aggregations