Search in sources :

Example 21 with UnexpectedEnumException

use of org.molgenis.util.UnexpectedEnumException in project molgenis by molgenis.

the class PostgreSqlUtils method getPostgreSqlQueryValue.

/**
 * Returns the PostgreSQL query value for the given entity attribute. For query operators requiring a list of
 * values (e.g. IN or RANGE) this method must be called for each individual query value.
 *
 * @param queryValue value of the type that matches the attribute type
 * @param attr       attribute
 * @return PostgreSQL value
 */
static Object getPostgreSqlQueryValue(Object queryValue, Attribute attr) {
    while (true) {
        String attrName = attr.getName();
        AttributeType attrType = attr.getDataType();
        switch(attrType) {
            case BOOL:
                if (queryValue != null && !(queryValue instanceof Boolean)) {
                    throw new MolgenisDataException(format("Attribute [%s] query value is of type [%s] instead of [%s]", attrName, queryValue.getClass().getSimpleName(), Boolean.class.getSimpleName()));
                }
                return queryValue;
            case CATEGORICAL:
            // one query value
            case CATEGORICAL_MREF:
            case FILE:
            // one query value
            case MREF:
            case XREF:
            case ONE_TO_MANY:
                // queries values referencing an entity can either be the entity itself or the entity id
                if (queryValue != null) {
                    if (queryValue instanceof Entity) {
                        queryValue = ((Entity) queryValue).getIdValue();
                    }
                    attr = attr.getRefEntity().getIdAttribute();
                    continue;
                } else {
                    return null;
                }
            case DATE:
                if (queryValue != null && !(queryValue instanceof LocalDate)) {
                    throw new MolgenisDataException(format("Attribute [%s] query value is of type [%s] instead of [%s]", attrName, queryValue.getClass().getSimpleName(), LocalDate.class.getSimpleName()));
                }
                return queryValue;
            case DATE_TIME:
                if (queryValue == null) {
                    return null;
                }
                if (!(queryValue instanceof Instant)) {
                    throw new MolgenisDataException(format("Attribute [%s] query value is of type [%s] instead of [%s]", attrName, queryValue.getClass().getSimpleName(), Instant.class.getSimpleName()));
                }
                return ((Instant) queryValue).atOffset(UTC);
            case DECIMAL:
                if (queryValue != null && !(queryValue instanceof Double)) {
                    throw new MolgenisDataException(format("Attribute [%s] query value is of type [%s] instead of [%s]", attrName, queryValue.getClass().getSimpleName(), Double.class.getSimpleName()));
                }
                return queryValue;
            case ENUM:
                // enum query values can be an enum or enum string
                if (queryValue != null) {
                    if (queryValue instanceof String) {
                        return queryValue;
                    } else if (queryValue instanceof Enum<?>) {
                        return queryValue.toString();
                    } else {
                        throw new MolgenisDataException(format("Attribute [%s] query value is of type [%s] instead of [%s] or [%s]", attrName, queryValue.getClass().getSimpleName(), String.class.getSimpleName(), Enum.class.getSimpleName()));
                    }
                } else {
                    return null;
                }
            case EMAIL:
            case HTML:
            case HYPERLINK:
            case SCRIPT:
            case STRING:
            case TEXT:
                if (queryValue != null && !(queryValue instanceof String)) {
                    throw new MolgenisDataException(format("Attribute [%s] query value is of type [%s] instead of [%s]", attrName, queryValue.getClass().getSimpleName(), String.class.getSimpleName()));
                }
                return queryValue;
            case INT:
                if (queryValue != null && !(queryValue instanceof Integer)) {
                    throw new MolgenisDataException(format("Attribute [%s] query value is of type [%s] instead of [%s]", attrName, queryValue.getClass().getSimpleName(), Integer.class.getSimpleName()));
                }
                return queryValue;
            case LONG:
                if (queryValue != null && !(queryValue instanceof Long)) {
                    throw new MolgenisDataException(format("Attribute [%s] query value is of type [%s] instead of [%s]", attrName, queryValue.getClass().getSimpleName(), Long.class.getSimpleName()));
                }
                return queryValue;
            case COMPOUND:
                throw new RuntimeException(format("Illegal attribute type [%s]", attrType.toString()));
            default:
                throw new UnexpectedEnumException(attrType);
        }
    }
}
Also used : Entity(org.molgenis.data.Entity) Instant(java.time.Instant) LocalDate(java.time.LocalDate) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) MolgenisDataException(org.molgenis.data.MolgenisDataException) AttributeType(org.molgenis.data.meta.AttributeType)

Example 22 with UnexpectedEnumException

use of org.molgenis.util.UnexpectedEnumException in project molgenis by molgenis.

the class EmxDataProvider method toEntity.

/**
 * Create an entity from the EMX entity
 *
 * @param entityType entity meta data
 * @param emxEntity  EMX entity
 * @return MOLGENIS entity
 */
private Entity toEntity(EntityType entityType, Entity emxEntity) {
    Entity entity = entityManager.create(entityType, POPULATE);
    for (Attribute attr : entityType.getAtomicAttributes()) {
        if (attr.getExpression() == null && !attr.isMappedBy()) {
            String attrName = attr.getName();
            Object emxValue = emxEntity.get(attrName);
            AttributeType attrType = attr.getDataType();
            switch(attrType) {
                case BOOL:
                case DATE:
                case DATE_TIME:
                case DECIMAL:
                case EMAIL:
                case ENUM:
                case HTML:
                case HYPERLINK:
                case INT:
                case LONG:
                case SCRIPT:
                case STRING:
                case TEXT:
                    Object value = emxValue != null ? DataConverter.convert(emxValue, attr) : null;
                    if ((!attr.isAuto() || value != null) && (!attr.hasDefaultValue() || value != null)) {
                        entity.set(attrName, value);
                    }
                    break;
                case CATEGORICAL:
                case FILE:
                case XREF:
                    // DataConverter.convert performs no conversion for reference types
                    Entity refEntity = toRefEntity(attr, emxValue);
                    // do not set generated auto refEntities to null
                    if ((!attr.isAuto() || refEntity != null) && (!attr.hasDefaultValue() || refEntity != null)) {
                        entity.set(attrName, refEntity);
                    }
                    break;
                case CATEGORICAL_MREF:
                case MREF:
                    // DataConverter.convert performs no conversion for reference types
                    List<Entity> refEntities = toRefEntities(attr, emxValue);
                    // do not set generated auto refEntities to null
                    if (!refEntities.isEmpty()) {
                        entity.set(attrName, refEntities);
                    }
                    break;
                case COMPOUND:
                    throw new RuntimeException(format("Illegal attribute type [%s]", attrType.toString()));
                default:
                    throw new UnexpectedEnumException(attrType);
            }
        }
    }
    return entity;
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Attribute(org.molgenis.data.meta.model.Attribute) AttributeType(org.molgenis.data.meta.AttributeType)

Example 23 with UnexpectedEnumException

use of org.molgenis.util.UnexpectedEnumException in project molgenis by molgenis.

the class VcfToEntity method vcfReaderFormatToMolgenisType.

private static AttributeType vcfReaderFormatToMolgenisType(VcfMetaInfo vcfMetaInfo) {
    String number = vcfMetaInfo.getNumber();
    boolean isListValue;
    try {
        isListValue = number.equals("A") || number.equals("R") || number.equals("G") || number.equals(".") || Integer.parseInt(number) > 1;
    } catch (NumberFormatException ex) {
        throw new GenotypeDataException("Error parsing length of vcf info field. " + number + " is not a valid int or expected preset (A, R, G, .)", ex);
    }
    switch(vcfMetaInfo.getType()) {
        case CHARACTER:
            if (isListValue) {
                // TODO support list of primitives datatype
                return STRING;
            }
            return STRING;
        case FLAG:
            return BOOL;
        case FLOAT:
            if (isListValue) {
                // TODO support list of primitives datatype
                return STRING;
            }
            return DECIMAL;
        case INTEGER:
            if (isListValue) {
                // TODO support list of primitives datatype
                return STRING;
            }
            return INT;
        case STRING:
            if (isListValue) {
                // TODO support list of primitives datatype
                return TEXT;
            }
            return TEXT;
        default:
            throw new UnexpectedEnumException(vcfMetaInfo.getType());
    }
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) GenotypeDataException(org.molgenis.genotype.GenotypeDataException)

Example 24 with UnexpectedEnumException

use of org.molgenis.util.UnexpectedEnumException in project molgenis by molgenis.

the class VcfToEntity method vcfFieldTypeToMolgenisFieldType.

private static AttributeType vcfFieldTypeToMolgenisFieldType(VcfMetaFormat format) {
    String number = format.getNumber();
    boolean isListValue;
    try {
        isListValue = number.equals("A") || number.equals("R") || number.equals("G") || number.equals(".") || Integer.parseInt(number) > 1;
    } catch (NumberFormatException ex) {
        throw new GenotypeDataException("Error parsing length of vcf info field. " + number + " is not a valid int or expected preset (A, R, G, .)", ex);
    }
    switch(format.getType()) {
        case CHARACTER:
            if (isListValue) {
                // TODO support list of primitives datatype
                return STRING;
            }
            return STRING;
        case FLOAT:
            if (isListValue) {
                // TODO support list of primitives datatype
                return STRING;
            }
            return DECIMAL;
        case INTEGER:
            if (isListValue) {
                // TODO support list of primitives datatype
                return STRING;
            }
            return INT;
        case STRING:
            if (isListValue) {
                // TODO support list of primitives datatype
                return STRING;
            }
            return STRING;
        default:
            throw new UnexpectedEnumException(format.getType());
    }
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) GenotypeDataException(org.molgenis.genotype.GenotypeDataException)

Example 25 with UnexpectedEnumException

use of org.molgenis.util.UnexpectedEnumException in project molgenis by molgenis.

the class EntityUtils method equals.

/**
 * Returns true if entity equals another entity. For referenced entities compares the referenced entity ids.
 *
 * @return true if entity equals another entity
 */
public static boolean equals(Entity entity, Entity otherEntity) {
    if (entity == null && otherEntity != null)
        return false;
    if (entity != null && otherEntity == null)
        return false;
    if (entity == null)
        return true;
    if (!entity.getEntityType().getId().equals(otherEntity.getEntityType().getId()))
        return false;
    for (Attribute attr : entity.getEntityType().getAtomicAttributes()) {
        String attrName = attr.getName();
        switch(attr.getDataType()) {
            case BOOL:
                if (!Objects.equals(entity.getBoolean(attrName), otherEntity.getBoolean(attrName)))
                    return false;
                break;
            case CATEGORICAL:
            case FILE:
            case XREF:
                Entity xrefValue = entity.getEntity(attrName);
                Entity otherXrefValue = otherEntity.getEntity(attrName);
                if (xrefValue == null && otherXrefValue != null)
                    return false;
                if (xrefValue != null && otherXrefValue == null)
                    return false;
                if (xrefValue != null && otherXrefValue != null && !xrefValue.getIdValue().equals(otherXrefValue.getIdValue()))
                    return false;
                break;
            case CATEGORICAL_MREF:
            case ONE_TO_MANY:
            case MREF:
                List<Entity> entities = newArrayList(entity.getEntities(attrName));
                List<Entity> otherEntities = newArrayList(otherEntity.getEntities(attrName));
                if (entities.size() != otherEntities.size())
                    return false;
                for (int i = 0; i < entities.size(); ++i) {
                    Entity mrefValue = entities.get(i);
                    Entity otherMrefValue = otherEntities.get(i);
                    if (mrefValue == null && otherMrefValue != null)
                        return false;
                    if (mrefValue != null && otherMrefValue == null)
                        return false;
                    if (mrefValue != null && otherMrefValue != null && !mrefValue.getIdValue().equals(otherMrefValue.getIdValue()))
                        return false;
                }
                break;
            case COMPOUND:
                throw new RuntimeException(format("Invalid data type [%s]", attr.getDataType()));
            case DATE:
                if (!Objects.equals(entity.getLocalDate(attrName), otherEntity.getLocalDate(attrName)))
                    return false;
                break;
            case DATE_TIME:
                if (!Objects.equals(entity.getInstant(attrName), otherEntity.getInstant(attrName)))
                    return false;
                break;
            case DECIMAL:
                if (!Objects.equals(entity.getDouble(attrName), otherEntity.getDouble(attrName)))
                    return false;
                break;
            case EMAIL:
            case ENUM:
            case HTML:
            case HYPERLINK:
            case SCRIPT:
            case STRING:
            case TEXT:
                if (!Objects.equals(entity.getString(attrName), otherEntity.getString(attrName)))
                    return false;
                break;
            case INT:
                if (!Objects.equals(entity.getInt(attrName), otherEntity.getInt(attrName)))
                    return false;
                break;
            case LONG:
                if (!Objects.equals(entity.getLong(attrName), otherEntity.getLong(attrName)))
                    return false;
                break;
            default:
                throw new UnexpectedEnumException(attr.getDataType());
        }
    }
    return true;
}
Also used : Entity(org.molgenis.data.Entity) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Attribute(org.molgenis.data.meta.model.Attribute)

Aggregations

UnexpectedEnumException (org.molgenis.util.UnexpectedEnumException)39 AttributeType (org.molgenis.data.meta.AttributeType)26 Attribute (org.molgenis.data.meta.model.Attribute)20 Entity (org.molgenis.data.Entity)13 Instant (java.time.Instant)10 LocalDate (java.time.LocalDate)10 String.format (java.lang.String.format)4 Collectors.toList (java.util.stream.Collectors.toList)4 MolgenisDataException (org.molgenis.data.MolgenisDataException)4 Iterator (java.util.Iterator)3 List (java.util.List)3 StreamSupport.stream (java.util.stream.StreamSupport.stream)3 EntityType (org.molgenis.data.meta.model.EntityType)3 UTC (java.time.ZoneOffset.UTC)2 ChronoUnit (java.time.temporal.ChronoUnit)2 Collection (java.util.Collection)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Collectors.joining (java.util.stream.Collectors.joining)2 Stream (java.util.stream.Stream)2