use of org.molgenis.data.meta.AttributeType.COMPOUND in project molgenis by molgenis.
the class EntityUtils method getTypedValue.
/**
* Convert a string value to a typed value based on the attribute data type.
*
* @param valueStr string value
* @param attr attribute
* @param entityManager entity manager used to convert referenced entity values
* @return typed value
*/
public static Object getTypedValue(String valueStr, Attribute attr, EntityManager entityManager) {
if (valueStr == null)
return null;
switch(attr.getDataType()) {
case BOOL:
return Boolean.valueOf(valueStr);
case CATEGORICAL:
case FILE:
case XREF:
EntityType xrefEntity = attr.getRefEntity();
Object xrefIdValue = getTypedValue(valueStr, xrefEntity.getIdAttribute(), entityManager);
return entityManager.getReference(xrefEntity, xrefIdValue);
case CATEGORICAL_MREF:
case MREF:
case ONE_TO_MANY:
EntityType mrefEntity = attr.getRefEntity();
List<String> mrefIdStrValues = ListEscapeUtils.toList(valueStr);
return mrefIdStrValues.stream().map(mrefIdStrValue -> getTypedValue(mrefIdStrValue, mrefEntity.getIdAttribute(), entityManager)).map(mrefIdValue -> entityManager.getReference(mrefEntity, mrefIdValue)).collect(toList());
case COMPOUND:
throw new IllegalArgumentException("Compound attribute has no value");
case DATE:
try {
return parseLocalDate(valueStr);
} catch (DateTimeParseException e) {
throw new MolgenisDataException(format(FAILED_TO_PARSE_ATTRIBUTE_AS_DATE_MESSAGE, attr.getName(), valueStr), e);
}
case DATE_TIME:
try {
return parseInstant(valueStr);
} catch (DateTimeParseException e) {
throw new MolgenisDataException(format(FAILED_TO_PARSE_ATTRIBUTE_AS_DATETIME_MESSAGE, attr.getName(), valueStr), e);
}
case DECIMAL:
return Double.valueOf(valueStr);
case EMAIL:
case ENUM:
case HTML:
case HYPERLINK:
case SCRIPT:
case STRING:
case TEXT:
return valueStr;
case INT:
return Integer.valueOf(valueStr);
case LONG:
return Long.valueOf(valueStr);
default:
throw new UnexpectedEnumException(attr.getDataType());
}
}
use of org.molgenis.data.meta.AttributeType.COMPOUND in project molgenis by molgenis.
the class AnnotatorUtils method createCompoundForAnnotator.
private static void createCompoundForAnnotator(EntityType entityType, AttributeFactory attributeFactory, RepositoryAnnotator annotator, List<Attribute> attributes, String compoundName) {
Attribute compound;
compound = attributeFactory.create().setName(compoundName).setLabel(annotator.getFullName()).setDataType(COMPOUND).setLabel(annotator.getSimpleName());
attributes.stream().filter(part -> entityType.getAttribute(part.getName()) == null).forEachOrdered(part -> part.setParent(compound));
entityType.addAttribute(compound);
// Only add attributes that do not already exist. We assume existing attributes are added in a previous annotation run.
// This is a potential risk if an attribute with that name already exist that was not added by the annotator.
// This risk is relatively low since annotator attributes are prefixed.
attributes = attributes.stream().filter(attribute -> entityType.getAttribute(attribute.getName()) == null).collect(toList());
entityType.addAttributes(attributes);
}
Aggregations