use of org.molgenis.data.meta.model.EntityType in project molgenis by molgenis.
the class DefaultValueReferenceValidatorImpl method getDefaultValueMap.
private Multimap<String, Attribute> getDefaultValueMap(EntityType entityType) {
Multimap<String, Attribute> defaultValueMultiMap = LinkedHashMultimap.create();
dataService.query(ATTRIBUTE_META_DATA, Attribute.class).eq(REF_ENTITY_TYPE, entityType.getIdValue()).and().not().eq(DEFAULT_VALUE, null).findAll().forEach(attribute -> {
if (EntityTypeUtils.isSingleReferenceType(attribute)) {
Entity defaultEntityValue = (Entity) getDefaultTypedValue(attribute);
defaultValueMultiMap.put(defaultEntityValue.getIdValue().toString(), attribute);
} else if (EntityTypeUtils.isMultipleReferenceType(attribute)) {
@SuppressWarnings("unchecked") Iterable<Entity> defaultEntitiesValue = (Iterable<Entity>) getDefaultTypedValue(attribute);
defaultEntitiesValue.forEach(defaultEntityValue -> defaultValueMultiMap.put(defaultEntityValue.getIdValue().toString(), attribute));
}
});
return defaultValueMultiMap;
}
use of org.molgenis.data.meta.model.EntityType in project molgenis by molgenis.
the class RestControllerV2 method getEntityResponse.
private Map<String, Object> getEntityResponse(String entityTypeId, String untypedId, AttributeFilter attributeFilter, boolean includeCategories) {
EntityType entityType = dataService.getEntityType(entityTypeId);
Object id = getTypedValue(untypedId, entityType.getIdAttribute());
Fetch fetch = AttributeFilterToFetchConverter.convert(attributeFilter, entityType, LanguageService.getCurrentUserLanguageCode());
Entity entity = dataService.findOneById(entityTypeId, id, fetch);
if (entity == null) {
throw new UnknownEntityException(entityTypeId + " [" + untypedId + "] not found");
}
return createEntityResponse(entity, fetch, true, includeCategories);
}
use of org.molgenis.data.meta.model.EntityType in project molgenis by molgenis.
the class RestControllerV2 method updateEntities.
/**
* Try to update 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
*/
@PutMapping("/{entityTypeId}")
public synchronized void updateEntities(@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 {
List<Entity> entities = request.getEntities().stream().map(e -> this.restService.toEntity(meta, e)).collect(toList());
// update all entities
this.dataService.update(entityTypeId, entities.stream());
entities.forEach(entity -> restService.updateMappedByEntities(entity, dataService.findOneById(entityTypeId, entity.getIdValue())));
response.setStatus(HttpServletResponse.SC_OK);
} catch (Exception e) {
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
throw e;
}
}
use of org.molgenis.data.meta.model.EntityType 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.meta.model.EntityType in project molgenis by molgenis.
the class RestServiceTest method toEntityValueMrefToIntAttr.
@Test(dataProvider = "toEntityValueMrefProvider")
public void toEntityValueMrefToIntAttr(AttributeType attrType) {
Entity entity0 = mock(Entity.class);
Entity entity1 = mock(Entity.class);
String refEntityName = "refEntity";
Attribute refIdAttr = mock(Attribute.class);
when(refIdAttr.getDataType()).thenReturn(INT);
EntityType refEntityType = mock(EntityType.class);
when(refEntityType.getId()).thenReturn(refEntityName);
when(refEntityType.getIdAttribute()).thenReturn(refIdAttr);
Attribute attr = mock(Attribute.class);
when(attr.getDataType()).thenReturn(attrType);
when(attr.getRefEntity()).thenReturn(refEntityType);
when(entityManager.getReference(refEntityType, 0)).thenReturn(entity0);
when(entityManager.getReference(refEntityType, 1)).thenReturn(entity1);
// string
Object entityValue = restService.toEntityValue(attr, "0,1", "test");
assertEquals(entityValue, Arrays.asList(entity0, entity1));
}
Aggregations