use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.
the class AggregationGenerator method createAggregations.
List<AggregationBuilder> createAggregations(Attribute aggAttr1, Attribute aggAttr2, Attribute aggAttrDistinct) {
// validate request
if (aggAttr1 == null) {
throw new IllegalArgumentException("Aggregation requires at least one isAggregatable attribute");
}
if (!aggAttr1.isAggregatable()) {
throw new IllegalArgumentException("Attribute is not isAggregatable [ " + aggAttr1.getName() + "]");
}
if (aggAttr2 != null && !aggAttr2.isAggregatable()) {
throw new IllegalArgumentException("Attribute is not isAggregatable [ " + aggAttr2.getName() + "]");
}
if (aggAttrDistinct != null && aggAttrDistinct.isNillable()) {
// see: https://github.com/molgenis/molgenis/issues/1938
throw new IllegalArgumentException("Distinct isAggregatable attribute cannot be nillable");
}
AttributeType dataType1 = aggAttr1.getDataType();
if (aggAttr1.isNillable() && isReferenceType(aggAttr1)) {
// see: https://github.com/molgenis/molgenis/issues/1937
throw new IllegalArgumentException("Aggregatable attribute of type [" + dataType1 + "] cannot be nillable");
}
if (aggAttr2 != null) {
// see: https://github.com/molgenis/molgenis/issues/1937
AttributeType dataType2 = aggAttr2.getDataType();
if (aggAttr2.isNillable() && isReferenceType(aggAttr2)) {
throw new IllegalArgumentException("Aggregatable attribute of type [" + dataType2 + "] cannot be nillable");
}
}
LinkedList<Attribute> aggAttrs = new LinkedList<>();
aggAttrs.add(aggAttr1);
if (aggAttr2 != null) {
aggAttrs.add(aggAttr2);
}
return createAggregations(aggAttrs, null, aggAttrDistinct);
}
use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.
the class DocumentContentBuilder method createRec.
private void createRec(Entity entity, Attribute attr, XContentGenerator generator, int depth, int maxDepth) throws IOException {
String attrName = attr.getName();
AttributeType attrType = attr.getDataType();
switch(attrType) {
case BOOL:
Boolean boolValue = entity.getBoolean(attrName);
if (boolValue != null) {
generator.writeBoolean(boolValue);
} else {
generator.writeNull();
}
break;
case DECIMAL:
Double doubleValue = entity.getDouble(attrName);
if (doubleValue != null) {
generator.writeNumber(doubleValue);
} else {
generator.writeNull();
}
break;
case INT:
Integer intValue = entity.getInt(attrName);
if (intValue != null) {
generator.writeNumber(intValue);
} else {
generator.writeNull();
}
break;
case LONG:
Long longValue = entity.getLong(attrName);
if (longValue != null) {
generator.writeNumber(longValue);
} else {
generator.writeNull();
}
break;
case EMAIL:
case ENUM:
case HTML:
case HYPERLINK:
case SCRIPT:
case STRING:
case TEXT:
String strValue = entity.getString(attrName);
if (strValue != null) {
generator.writeString(strValue);
} else {
generator.writeNull();
}
break;
case DATE:
LocalDate date = entity.getLocalDate(attrName);
if (date != null) {
generator.writeString(date.toString());
} else {
generator.writeNull();
}
break;
case DATE_TIME:
Instant dateTime = entity.getInstant(attrName);
if (dateTime != null) {
generator.writeString(dateTime.toString());
} else {
generator.writeNull();
}
break;
case CATEGORICAL:
case XREF:
case FILE:
{
Entity xrefEntity = entity.getEntity(attrName);
if (xrefEntity != null) {
createRecReferenceAttribute(generator, depth, maxDepth, xrefEntity);
} else {
generator.writeNull();
}
break;
}
case CATEGORICAL_MREF:
case MREF:
case ONE_TO_MANY:
{
Iterable<Entity> mrefEntities = entity.getEntities(attrName);
if (!Iterables.isEmpty(mrefEntities)) {
generator.writeStartArray();
for (Entity mrefEntity : mrefEntities) {
createRecReferenceAttribute(generator, depth, maxDepth, mrefEntity);
}
generator.writeEndArray();
} else {
generator.writeNull();
}
break;
}
case COMPOUND:
throw new RuntimeException(format("Illegal attribute type [%s]", attrType.toString()));
default:
throw new UnexpectedEnumException(attrType);
}
}
use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.
the class SortGenerator method getSortField.
private String getSortField(Attribute attr) {
String sortField;
String fieldName = documentIdGenerator.generateId(attr);
AttributeType dataType = attr.getDataType();
switch(dataType) {
case BOOL:
case DATE:
case DATE_TIME:
case DECIMAL:
case INT:
case LONG:
// use indexed field for sorting
sortField = fieldName;
break;
case EMAIL:
case ENUM:
case HTML:
case HYPERLINK:
case SCRIPT:
case STRING:
case TEXT:
// use raw field for sorting
sortField = fieldName + '.' + FIELD_NOT_ANALYZED;
break;
case CATEGORICAL:
case CATEGORICAL_MREF:
case FILE:
case MREF:
case ONE_TO_MANY:
case XREF:
// use nested field for sorting
String refSortField = getSortField(attr.getRefEntity().getLabelAttribute());
sortField = fieldName + '.' + refSortField;
break;
case COMPOUND:
throw new UnsupportedOperationException();
default:
throw new UnexpectedEnumException(dataType);
}
return sortField;
}
use of org.molgenis.data.meta.AttributeType 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.meta.AttributeType in project molgenis by molgenis.
the class RestController method getEntityAsMap.
// Transforms an entity to a Map so it can be transformed to json
private Map<String, Object> getEntityAsMap(Entity entity, EntityType meta, Set<String> attributesSet, Map<String, Set<String>> attributeExpandsSet) {
if (null == entity)
throw new IllegalArgumentException("entity is null");
if (null == meta)
throw new IllegalArgumentException("meta is null");
Map<String, Object> entityMap = new LinkedHashMap<>();
entityMap.put("href", Href.concatEntityHref(RestController.BASE_URI, meta.getId(), entity.getIdValue()));
for (Attribute attr : meta.getAtomicAttributes()) {
// filter fields
if (attributesSet != null && !attributesSet.contains(attr.getName().toLowerCase()))
continue;
String attrName = attr.getName();
AttributeType attrType = attr.getDataType();
if (attrType == COMPOUND) {
if (attributeExpandsSet != null && attributeExpandsSet.containsKey(attrName.toLowerCase())) {
Set<String> subAttributesSet = attributeExpandsSet.get(attrName.toLowerCase());
entityMap.put(attrName, new AttributeResponse(meta.getId(), meta, attr, subAttributesSet, null, permissionService, dataService));
} else {
entityMap.put(attrName, Collections.singletonMap("href", Href.concatAttributeHref(RestController.BASE_URI, meta.getId(), entity.getIdValue(), attrName)));
}
} else if (attrType == DATE) {
LocalDate date = entity.getLocalDate(attrName);
entityMap.put(attrName, date != null ? date.toString() : null);
} else if (attrType == DATE_TIME) {
Instant date = entity.getInstant(attrName);
entityMap.put(attrName, date != null ? date.toString() : null);
} else if (attrType != XREF && attrType != CATEGORICAL && attrType != MREF && attrType != CATEGORICAL_MREF && attrType != ONE_TO_MANY && attrType != FILE) {
entityMap.put(attrName, entity.get(attr.getName()));
} else if ((attrType == XREF || attrType == CATEGORICAL || attrType == FILE) && attributeExpandsSet != null && attributeExpandsSet.containsKey(attrName.toLowerCase())) {
Entity refEntity = entity.getEntity(attr.getName());
if (refEntity != null) {
Set<String> subAttributesSet = attributeExpandsSet.get(attrName.toLowerCase());
EntityType refEntityType = dataService.getEntityType(attr.getRefEntity().getId());
Map<String, Object> refEntityMap = getEntityAsMap(refEntity, refEntityType, subAttributesSet, null);
entityMap.put(attrName, refEntityMap);
}
} else if ((attrType == MREF || attrType == CATEGORICAL_MREF || attrType == ONE_TO_MANY) && attributeExpandsSet != null && attributeExpandsSet.containsKey(attrName.toLowerCase())) {
EntityType refEntityType = dataService.getEntityType(attr.getRefEntity().getId());
Iterable<Entity> mrefEntities = entity.getEntities(attr.getName());
Set<String> subAttributesSet = attributeExpandsSet.get(attrName.toLowerCase());
List<Map<String, Object>> refEntityMaps = new ArrayList<>();
for (Entity refEntity : mrefEntities) {
Map<String, Object> refEntityMap = getEntityAsMap(refEntity, refEntityType, subAttributesSet, null);
refEntityMaps.add(refEntityMap);
}
EntityPager pager = new EntityPager(0, new EntityCollectionRequest().getNum(), (long) refEntityMaps.size(), mrefEntities);
EntityCollectionResponse ecr = new EntityCollectionResponse(pager, refEntityMaps, Href.concatAttributeHref(RestController.BASE_URI, meta.getId(), entity.getIdValue(), attrName), null, permissionService, dataService);
entityMap.put(attrName, ecr);
} else if ((attrType == XREF && entity.get(attr.getName()) != null) || (attrType == CATEGORICAL && entity.get(attr.getName()) != null) || (attrType == FILE && entity.get(attr.getName()) != null) || attrType == MREF || attrType == CATEGORICAL_MREF || attrType == ONE_TO_MANY) {
// Add href to ref field
Map<String, String> ref = new LinkedHashMap<>();
ref.put("href", Href.concatAttributeHref(RestController.BASE_URI, meta.getId(), entity.getIdValue(), attrName));
entityMap.put(attrName, ref);
}
}
return entityMap;
}
Aggregations