Search in sources :

Example 26 with AttributeType

use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.

the class EntitySerializer method serialize.

@Override
public JsonElement serialize(Entity entity, Type type, JsonSerializationContext context) {
    JsonObject result = new JsonObject();
    result.addProperty("__entityTypeId", entity.getEntityType().getId());
    for (Attribute attr : entity.getEntityType().getAtomicAttributes()) {
        String attributeName = attr.getName();
        Object value = entity.get(attributeName);
        if (value != null) {
            AttributeType attrType = attr.getDataType();
            switch(attrType) {
                case BOOL:
                    result.addProperty(attributeName, entity.getBoolean(attributeName));
                    break;
                case CATEGORICAL:
                case XREF:
                case FILE:
                    Entity refEntity = entity.getEntity(attributeName);
                    result.add(attributeName, serializeReference(refEntity, context));
                    break;
                case CATEGORICAL_MREF:
                case MREF:
                case ONE_TO_MANY:
                    JsonArray jsonArray = new JsonArray();
                    entity.getEntities(attributeName).forEach(e -> jsonArray.add(serializeReference(e, context)));
                    result.add(attributeName, jsonArray);
                    break;
                case DATE:
                    LocalDate localDateValue = entity.getLocalDate(attributeName);
                    result.add(attributeName, context.serialize(localDateValue));
                    break;
                case DATE_TIME:
                    Instant instantValue = entity.getInstant(attributeName);
                    result.add(attributeName, context.serialize(instantValue));
                    break;
                case DECIMAL:
                    result.addProperty(attributeName, entity.getDouble(attributeName));
                    break;
                case INT:
                    result.addProperty(attributeName, entity.getInt(attributeName));
                    break;
                case LONG:
                    result.addProperty(attributeName, entity.getLong(attributeName));
                    break;
                case ENUM:
                case HTML:
                case HYPERLINK:
                case SCRIPT:
                case EMAIL:
                case STRING:
                case TEXT:
                    result.addProperty(attributeName, value.toString());
                    break;
                case COMPOUND:
                    throw new RuntimeException(format("Illegal attribute type [%s]", attrType.toString()));
                default:
                    throw new UnexpectedEnumException(attrType);
            }
        }
    }
    return result;
}
Also used : Entity(org.molgenis.data.Entity) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Attribute(org.molgenis.data.meta.model.Attribute) AttributeType(org.molgenis.data.meta.AttributeType) Instant(java.time.Instant) LocalDate(java.time.LocalDate)

Example 27 with AttributeType

use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.

the class PostgreSqlQueryGenerator method getSqlColumn.

private static String getSqlColumn(EntityType entityType, Attribute attr, ColumnMode columnMode) {
    StringBuilder sqlBuilder = new StringBuilder(getColumnName(attr)).append(' ');
    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:
            sqlBuilder.append(getPostgreSqlType(attr));
            break;
        case CATEGORICAL:
        case FILE:
        case XREF:
            sqlBuilder.append(getPostgreSqlType(attr.getRefEntity().getIdAttribute()));
            break;
        case ONE_TO_MANY:
        case COMPOUND:
        case CATEGORICAL_MREF:
        case MREF:
            throw new RuntimeException(format("Illegal attribute type [%s]", attrType.toString()));
        default:
            throw new UnexpectedEnumException(attrType);
    }
    String sqlColumnConstraints = getSqlColumnConstraints(entityType, attr, columnMode);
    if (!sqlColumnConstraints.isEmpty()) {
        sqlBuilder.append(' ').append(sqlColumnConstraints);
    }
    return sqlBuilder.toString();
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) AttributeType(org.molgenis.data.meta.AttributeType)

Example 28 with AttributeType

use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.

the class PostgreSqlRepository method handleBatch.

/**
 * Handles a batch of Entities. Looks up the values for MREF ID attributes and sets them as references in the
 * entities. Then feeds the entities to the {@link Consumer}
 *
 * @param consumer   {@link Consumer} to feed the batch to after setting the MREF ID values
 * @param entityType EntityType for the {@link Entity}s in the batch
 * @param batch      {@link Map} mapping entity ID to entity for all {@link Entity}s in the batch
 */
private void handleBatch(Consumer<List<Entity>> consumer, EntityType entityType, Map<Object, Entity> batch) {
    AttributeType idAttributeDataType = entityType.getIdAttribute().getDataType();
    LOG.debug("Select ID values for a batch of MREF attributes...");
    for (Attribute mrefAttr : entityType.getAtomicAttributes()) {
        if (mrefAttr.getExpression() == null && isMultipleReferenceType(mrefAttr) && !(mrefAttr.getDataType() == ONE_TO_MANY && mrefAttr.isMappedBy())) {
            EntityType refEntityType = mrefAttr.getRefEntity();
            Multimap<Object, Object> mrefIDs = selectMrefIDsForAttribute(entityType, idAttributeDataType, mrefAttr, batch.keySet(), refEntityType.getIdAttribute().getDataType());
            for (Map.Entry entry : batch.entrySet()) {
                batch.get(entry.getKey()).set(mrefAttr.getName(), postgreSqlEntityFactory.getReferences(refEntityType, newArrayList(mrefIDs.get(entry.getKey()))));
            }
        }
    }
    LOG.trace("Feeding batch of {} rows to consumer.", batch.size());
    consumer.accept(batch.values().stream().collect(toList()));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Attribute(org.molgenis.data.meta.model.Attribute) AttributeType(org.molgenis.data.meta.AttributeType) Maps.newHashMap(com.google.common.collect.Maps.newHashMap)

Example 29 with AttributeType

use of org.molgenis.data.meta.AttributeType 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 30 with AttributeType

use of org.molgenis.data.meta.AttributeType 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)

Aggregations

AttributeType (org.molgenis.data.meta.AttributeType)46 Attribute (org.molgenis.data.meta.model.Attribute)24 UnexpectedEnumException (org.molgenis.util.UnexpectedEnumException)24 LocalDate (java.time.LocalDate)11 Instant (java.time.Instant)10 Entity (org.molgenis.data.Entity)10 EntityType (org.molgenis.data.meta.model.EntityType)7 DataProvider (org.testng.annotations.DataProvider)7 ArrayList (java.util.ArrayList)5 MolgenisDataException (org.molgenis.data.MolgenisDataException)3 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)2 String.format (java.lang.String.format)2 Collectors.toList (java.util.stream.Collectors.toList)2 Test (org.testng.annotations.Test)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Maps.newLinkedHashMap (com.google.common.collect.Maps.newLinkedHashMap)1 UTC (java.time.ZoneOffset.UTC)1 ChronoUnit (java.time.temporal.ChronoUnit)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1