Search in sources :

Example 1 with IllegalAttributeTypeException

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

the class EntityModelWriter method addRelationForAttribute.

private void addRelationForAttribute(Model model, Resource subject, IRI predicate, Entity objectEntity, Attribute objectAttribute) {
    String name = objectAttribute.getName();
    AttributeType attributeType = objectAttribute.getDataType();
    switch(attributeType) {
        case MREF:
        case CATEGORICAL_MREF:
            addRelationForMrefTypeAttribute(model, subject, predicate, objectEntity.getEntities(name));
            break;
        case BOOL:
            model.add(subject, predicate, valueFactory.createLiteral(objectEntity.getBoolean(name)));
            break;
        case DATE:
            XMLGregorianCalendar calendar = DATATYPE_FACTORY.newXMLGregorianCalendar(objectEntity.getLocalDate(name).toString());
            model.add(subject, predicate, valueFactory.createLiteral(calendar));
            break;
        case DATE_TIME:
            calendar = DATATYPE_FACTORY.newXMLGregorianCalendar(objectEntity.getInstant(name).toString());
            model.add(subject, predicate, valueFactory.createLiteral(calendar));
            break;
        case DECIMAL:
            model.add(subject, predicate, valueFactory.createLiteral(objectEntity.getDouble(name)));
            break;
        case LONG:
            model.add(subject, predicate, valueFactory.createLiteral(objectEntity.getLong(name)));
            break;
        case INT:
            model.add(subject, predicate, valueFactory.createLiteral(objectEntity.getInt(name)));
            break;
        case ENUM:
        case EMAIL:
        case HTML:
        case TEXT:
        case SCRIPT:
        case STRING:
            addRelationForStringTypeAttribute(model, subject, predicate, objectEntity.getString(name));
            break;
        case HYPERLINK:
            model.add(subject, predicate, valueFactory.createIRI(objectEntity.getString(name)));
            break;
        case XREF:
        case CATEGORICAL:
        case FILE:
            addRelationForXrefTypeAttribute(model, subject, predicate, objectEntity.getEntity(name));
            break;
        default:
            throw new IllegalAttributeTypeException(attributeType);
    }
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) AttributeType(org.molgenis.data.meta.AttributeType) IllegalAttributeTypeException(org.molgenis.data.meta.IllegalAttributeTypeException)

Example 2 with IllegalAttributeTypeException

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

the class EntityMapperImpl method mapRecursive.

private Object mapRecursive(Entity entity, Attribute attribute, Selection filter, Selection expand, int depth) {
    Object value;
    String attributeName = attribute.getName();
    AttributeType attributeType = attribute.getDataType();
    switch(attributeType) {
        case BOOL:
            value = entity.getBoolean(attributeName);
            break;
        case CATEGORICAL:
        case FILE:
        case XREF:
            value = mapReference(entity, attribute, filter, expand, depth + 1);
            break;
        case CATEGORICAL_MREF:
        case MREF:
        case ONE_TO_MANY:
            value = mapReferences(entity, attribute, filter, expand, depth + 1);
            break;
        case DATE:
            value = entity.getLocalDate(attributeName);
            break;
        case DATE_TIME:
            value = entity.getInstant(attributeName);
            break;
        case DECIMAL:
            value = entity.getDouble(attributeName);
            break;
        case EMAIL:
        case ENUM:
        case HTML:
        case HYPERLINK:
        case SCRIPT:
        case STRING:
        case TEXT:
            value = entity.getString(attributeName);
            break;
        case INT:
            value = entity.getInt(attributeName);
            break;
        case LONG:
            value = entity.getLong(attributeName);
            break;
        case COMPOUND:
            throw new IllegalAttributeTypeException(attributeType);
        default:
            throw new UnexpectedEnumException(attributeType);
    }
    return value;
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) AttributeType(org.molgenis.data.meta.AttributeType) IllegalAttributeTypeException(org.molgenis.data.meta.IllegalAttributeTypeException)

Example 3 with IllegalAttributeTypeException

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

the class RestService method toEntityValue.

/**
 * Converts a HTTP request parameter to a entity value of which the type is defined by the
 * attribute. For file attributes persists the file in the file store and persist a file meta data
 * entity.
 *
 * @param attr attribute
 * @param paramValue HTTP parameter value
 * @return Object
 */
public Object toEntityValue(Attribute attr, Object paramValue, Object id) {
    // Treat empty strings as null
    if ((paramValue instanceof String) && ((String) paramValue).isEmpty()) {
        paramValue = null;
    }
    Object value;
    AttributeType attrType = attr.getDataType();
    switch(attrType) {
        case BOOL:
            value = convertBool(attr, paramValue);
            break;
        case EMAIL:
        case ENUM:
        case HTML:
        case HYPERLINK:
        case SCRIPT:
        case STRING:
        case TEXT:
            value = convertString(attr, paramValue);
            break;
        case CATEGORICAL:
        case XREF:
            value = convertRef(attr, paramValue);
            break;
        case CATEGORICAL_MREF:
        case MREF:
        case ONE_TO_MANY:
            value = convertMref(attr, paramValue);
            break;
        case DATE:
            value = convertDate(attr, paramValue);
            break;
        case DATE_TIME:
            value = convertDateTime(attr, paramValue);
            break;
        case DECIMAL:
            value = convertDecimal(attr, paramValue);
            break;
        case FILE:
            value = convertFile(attr, paramValue, id);
            break;
        case INT:
            value = convertInt(attr, paramValue);
            break;
        case LONG:
            value = convertLong(attr, paramValue);
            break;
        case COMPOUND:
            throw new IllegalAttributeTypeException(attrType);
        default:
            throw new UnexpectedEnumException(attrType);
    }
    return value;
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) AttributeType(org.molgenis.data.meta.AttributeType) IllegalAttributeTypeException(org.molgenis.data.meta.IllegalAttributeTypeException)

Example 4 with IllegalAttributeTypeException

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

the class PostgreSqlUtils method getPostgreSqlValue.

/**
 * Returns the PostgreSQL value for the given entity attribute
 *
 * @param entity entity
 * @param attr attribute
 * @return PostgreSQL value
 */
static Object getPostgreSqlValue(Entity entity, Attribute attr) {
    String attrName = attr.getName();
    AttributeType attrType = attr.getDataType();
    switch(attrType) {
        case BOOL:
            return entity.getBoolean(attrName);
        case CATEGORICAL:
        case XREF:
            Entity xrefEntity = entity.getEntity(attrName);
            return xrefEntity != null ? getPostgreSqlValue(xrefEntity, xrefEntity.getEntityType().getIdAttribute()) : null;
        case CATEGORICAL_MREF:
        case MREF:
        case ONE_TO_MANY:
            Iterable<Entity> entities = entity.getEntities(attrName);
            return stream(entities).map(mrefEntity -> getPostgreSqlValue(mrefEntity, mrefEntity.getEntityType().getIdAttribute())).collect(toList());
        case DATE:
            return entity.getLocalDate(attrName);
        case DATE_TIME:
            // As a workaround for #5674, we don't store milliseconds
            Instant instant = entity.getInstant(attrName);
            return instant != null ? instant.truncatedTo(ChronoUnit.SECONDS).atOffset(UTC) : null;
        case DECIMAL:
            return entity.getDouble(attrName);
        case EMAIL:
        case ENUM:
        case HTML:
        case HYPERLINK:
        case SCRIPT:
        case STRING:
        case TEXT:
            return entity.getString(attrName);
        case FILE:
            FileMeta fileEntity = entity.getEntity(attrName, FileMeta.class);
            return fileEntity != null ? getPostgreSqlValue(fileEntity, fileEntity.getEntityType().getIdAttribute()) : null;
        case INT:
            return entity.getInt(attrName);
        case LONG:
            return entity.getLong(attrName);
        case COMPOUND:
            throw new IllegalAttributeTypeException(attrType);
        default:
            throw new UnexpectedEnumException(attrType);
    }
}
Also used : AttributeType(org.molgenis.data.meta.AttributeType) IllegalAttributeTypeException(org.molgenis.data.meta.IllegalAttributeTypeException) Instant(java.time.Instant) Attribute(org.molgenis.data.meta.model.Attribute) String.format(java.lang.String.format) Streams.stream(com.google.common.collect.Streams.stream) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) FileMeta(org.molgenis.data.file.model.FileMeta) Collectors.toList(java.util.stream.Collectors.toList) ChronoUnit(java.time.temporal.ChronoUnit) LocalDate(java.time.LocalDate) UTC(java.time.ZoneOffset.UTC) MolgenisDataException(org.molgenis.data.MolgenisDataException) Entity(org.molgenis.data.Entity) Entity(org.molgenis.data.Entity) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) AttributeType(org.molgenis.data.meta.AttributeType) Instant(java.time.Instant) IllegalAttributeTypeException(org.molgenis.data.meta.IllegalAttributeTypeException) FileMeta(org.molgenis.data.file.model.FileMeta)

Example 5 with IllegalAttributeTypeException

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

the class PostgreSqlQueryGenerator method getSqlCreateJunctionTable.

static String getSqlCreateJunctionTable(EntityType entityType, Attribute attr) {
    Attribute idAttr = entityType.getIdAttribute();
    StringBuilder sql = new StringBuilder("CREATE TABLE ").append(getJunctionTableName(entityType, attr)).append(" (").append(getJunctionTableOrderColumnName()).append(" INT,").append(getColumnName(idAttr)).append(' ').append(getPostgreSqlType(idAttr)).append(" NOT NULL, ").append(getColumnName(attr)).append(' ').append(getPostgreSqlType(attr.getRefEntity().getIdAttribute())).append(" NOT NULL").append(", FOREIGN KEY (").append(getColumnName(idAttr)).append(") REFERENCES ").append(getTableName(entityType)).append('(').append(getColumnName(idAttr)).append(") ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED");
    if (isPersistedInPostgreSql(attr.getRefEntity())) {
        sql.append(", FOREIGN KEY (").append(getColumnName(attr)).append(") REFERENCES ").append(getTableName(attr.getRefEntity())).append('(').append(getColumnName(attr.getRefEntity().getIdAttribute())).append(") DEFERRABLE INITIALLY DEFERRED");
    }
    AttributeType attrType = attr.getDataType();
    switch(attrType) {
        case CATEGORICAL_MREF:
        case MREF:
            sql.append(", UNIQUE (").append(getColumnName(idAttr)).append(',').append(getColumnName(attr)).append(')');
            break;
        default:
            throw new IllegalAttributeTypeException(attrType);
    }
    sql.append(", UNIQUE (").append(getJunctionTableOrderColumnName()).append(',').append(getColumnName(idAttr)).append(')');
    sql.append(')');
    return sql.toString();
}
Also used : Attribute(org.molgenis.data.meta.model.Attribute) AttributeType(org.molgenis.data.meta.AttributeType) IllegalAttributeTypeException(org.molgenis.data.meta.IllegalAttributeTypeException)

Aggregations

IllegalAttributeTypeException (org.molgenis.data.meta.IllegalAttributeTypeException)15 AttributeType (org.molgenis.data.meta.AttributeType)14 UnexpectedEnumException (org.molgenis.util.UnexpectedEnumException)13 Entity (org.molgenis.data.Entity)7 Instant (java.time.Instant)5 LocalDate (java.time.LocalDate)5 Attribute (org.molgenis.data.meta.model.Attribute)5 MolgenisDataException (org.molgenis.data.MolgenisDataException)2 Streams.stream (com.google.common.collect.Streams.stream)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 String.format (java.lang.String.format)1 UTC (java.time.ZoneOffset.UTC)1 ChronoUnit (java.time.temporal.ChronoUnit)1 Collection (java.util.Collection)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors.toList (java.util.stream.Collectors.toList)1 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 MolgenisQueryException (org.molgenis.data.MolgenisQueryException)1