use of org.molgenis.data.meta.model.Attribute in project molgenis by molgenis.
the class AttributeReferenceMapperTest method testToAttributeReference.
@Test
public void testToAttributeReference() {
String id = "id";
String label = "label";
EditorAttributeIdentifier editorAttributeIdentifier = EditorAttributeIdentifier.create(id, label);
Attribute attribute = attributeReferenceMapper.toAttributeReference(editorAttributeIdentifier);
assertEquals(attribute.getIdValue(), id);
}
use of org.molgenis.data.meta.model.Attribute in project molgenis by molgenis.
the class MolgenisRSQLVisitor method getAttribute.
private Attribute getAttribute(ComparisonNode node) {
String attrName = node.getSelector();
String[] attrTokens = attrName.split("\\.");
Attribute attr = entityType.getAttribute(attrTokens[0]);
if (attr == null) {
throw new UnknownAttributeException(entityType, attrName);
}
EntityType entityTypeAtDepth;
for (int i = 1; i < attrTokens.length; ++i) {
entityTypeAtDepth = attr.getRefEntity();
attr = entityTypeAtDepth.getAttribute(attrTokens[i]);
if (attr == null) {
throw new UnknownAttributeException(entityTypeAtDepth, attrName);
}
}
return attr;
}
use of org.molgenis.data.meta.model.Attribute in project molgenis by molgenis.
the class DefaultEntityValidator method checkNotNull.
private Set<ConstraintViolation> checkNotNull(Iterable<? extends Entity> entities, EntityType meta) {
Set<ConstraintViolation> violations = Sets.newLinkedHashSet();
for (Attribute attr : meta.getAtomicAttributes()) {
if (!attr.isNillable() && !attr.equals(meta.getIdAttribute()) && !attr.isAuto()) {
long rowNr = 0;
for (Entity entity : entities) {
rowNr++;
if (mustDoNotNullCheck(meta, attr, entity) && entity.get(attr.getName()) == null) {
String message = String.format("The attribute '%s' of entity '%s' with key '%s' can not be null.", attr.getName(), meta.getId(), entity.getString(meta.getLabelAttribute().getName()));
violations.add(new ConstraintViolation(message, rowNr));
}
}
}
}
return violations;
}
use of org.molgenis.data.meta.model.Attribute 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.Attribute 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);
}
}
Aggregations