Search in sources :

Example 1 with COMPOUND

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());
    }
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) ListEscapeUtils(org.molgenis.util.ListEscapeUtils) Iterables(com.google.common.collect.Iterables) Pair(org.molgenis.util.Pair) Attribute(org.molgenis.data.meta.model.Attribute) Tag(org.molgenis.data.meta.model.Tag) AttributeType(org.molgenis.data.meta.AttributeType) Iterator(java.util.Iterator) EntityTypeUtils(org.molgenis.data.support.EntityTypeUtils) MolgenisDateFormat(org.molgenis.data.util.MolgenisDateFormat) EntityType(org.molgenis.data.meta.model.EntityType) PACKAGE_SYSTEM(org.molgenis.data.system.model.RootSystemPackage.PACKAGE_SYSTEM) String.format(java.lang.String.format) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Objects(java.util.Objects) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) StreamSupport.stream(java.util.stream.StreamSupport.stream) COMPOUND(org.molgenis.data.meta.AttributeType.COMPOUND) DataService(org.molgenis.data.DataService) Package(org.molgenis.data.meta.model.Package) MolgenisDataException(org.molgenis.data.MolgenisDataException) EntityManager(org.molgenis.data.EntityManager) Entity(org.molgenis.data.Entity) DateTimeParseException(java.time.format.DateTimeParseException) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) MolgenisDataException(org.molgenis.data.MolgenisDataException)

Example 2 with COMPOUND

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);
}
Also used : AttributeFactory(org.molgenis.data.meta.model.AttributeFactory) RepositoryAnnotator(org.molgenis.data.annotation.core.RepositoryAnnotator) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) Attribute(org.molgenis.data.meta.model.Attribute) EntityType(org.molgenis.data.meta.model.EntityType) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) VcfAttributes(org.molgenis.data.vcf.model.VcfAttributes) COMPOUND(org.molgenis.data.meta.AttributeType.COMPOUND) Map(java.util.Map) MolgenisDataException(org.molgenis.data.MolgenisDataException) Collections(java.util.Collections) Attribute(org.molgenis.data.meta.model.Attribute)

Aggregations

List (java.util.List)2 Collectors.toList (java.util.stream.Collectors.toList)2 MolgenisDataException (org.molgenis.data.MolgenisDataException)2 COMPOUND (org.molgenis.data.meta.AttributeType.COMPOUND)2 Attribute (org.molgenis.data.meta.model.Attribute)2 EntityType (org.molgenis.data.meta.model.EntityType)2 Iterables (com.google.common.collect.Iterables)1 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 String.format (java.lang.String.format)1 DateTimeParseException (java.time.format.DateTimeParseException)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Stream (java.util.stream.Stream)1 StreamSupport.stream (java.util.stream.StreamSupport.stream)1 StringUtils (org.apache.commons.lang3.StringUtils)1 DataService (org.molgenis.data.DataService)1 Entity (org.molgenis.data.Entity)1