Search in sources :

Example 11 with AttributeType

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

the class QueryGenerator method createQueryClauseLike.

private QueryBuilder createQueryClauseLike(QueryRule queryRule, EntityType entityType) {
    List<Attribute> attributePath = getAttributePath(queryRule.getField(), entityType);
    Attribute attr = attributePath.get(attributePath.size() - 1);
    Object queryValue = getQueryValue(attr, queryRule.getValue());
    String fieldName = getQueryFieldName(attributePath);
    AttributeType attrType = attr.getDataType();
    switch(attrType) {
        case EMAIL:
        case ENUM:
        case HYPERLINK:
        case STRING:
            return nestedQueryBuilder(attributePath, QueryBuilders.matchPhrasePrefixQuery(fieldName, queryValue).maxExpansions(50).slop(10).analyzer(DEFAULT_ANALYZER));
        case BOOL:
        case COMPOUND:
        case DATE:
        case DATE_TIME:
        case DECIMAL:
        case INT:
        case LONG:
            throw new MolgenisQueryException(format("Illegal data type [%s] for operator [%s]", attrType, LIKE));
        case CATEGORICAL:
        case CATEGORICAL_MREF:
        case FILE:
        // due to size would result in large amount of ngrams
        case HTML:
        case MREF:
        case ONE_TO_MANY:
        // due to size would result in large amount of ngrams
        case SCRIPT:
        // due to size would result in large amount of ngrams
        case TEXT:
        case XREF:
            throw new UnsupportedOperationException(format("Unsupported data type [%s] for operator [%s]", attrType, LIKE));
        default:
            throw new UnexpectedEnumException(attrType);
    }
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Attribute(org.molgenis.data.meta.model.Attribute) AttributeType(org.molgenis.data.meta.AttributeType)

Example 12 with AttributeType

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

the class AggregationGenerator method getAggregateFieldName.

private String getAggregateFieldName(Attribute attr) {
    String fieldName = documentIdGenerator.generateId(attr);
    AttributeType dataType = attr.getDataType();
    switch(dataType) {
        case BOOL:
        case INT:
        case LONG:
        case DECIMAL:
            return fieldName;
        case DATE:
        case DATE_TIME:
        case EMAIL:
        case ENUM:
        case HTML:
        case HYPERLINK:
        case SCRIPT:
        case STRING:
        case TEXT:
            // use non-analyzed field
            return fieldName + '.' + FieldConstants.FIELD_NOT_ANALYZED;
        case CATEGORICAL:
        case CATEGORICAL_MREF:
        case XREF:
        case MREF:
        case FILE:
        case ONE_TO_MANY:
            // use id attribute of nested field
            return fieldName + '.' + getAggregateFieldName(attr.getRefEntity().getIdAttribute());
        case COMPOUND:
            throw new UnsupportedOperationException();
        default:
            throw new UnexpectedEnumException(dataType);
    }
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) AttributeType(org.molgenis.data.meta.AttributeType)

Example 13 with AttributeType

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

the class OneClickImporterServiceTest method testCastType.

@Test
public void testCastType() {
    Object value = 1;
    AttributeType type = INT;
    Object casted = oneClickImporterService.castValueAsAttributeType(value, type);
    assertTrue(casted instanceof Integer);
    value = 1.0;
    type = INT;
    casted = oneClickImporterService.castValueAsAttributeType(value, type);
    assertTrue(casted instanceof Integer);
    value = "1";
    type = INT;
    casted = oneClickImporterService.castValueAsAttributeType(value, type);
    assertTrue(casted instanceof Integer);
    value = "1";
    type = STRING;
    casted = oneClickImporterService.castValueAsAttributeType(value, type);
    assertTrue(casted instanceof String);
    Long myLong = (long) Integer.MAX_VALUE + 1;
    value = myLong;
    type = LONG;
    casted = oneClickImporterService.castValueAsAttributeType(value, type);
    assertTrue(casted instanceof Long);
    Double myDouble = (double) Long.MAX_VALUE + 1;
    value = myDouble;
    type = DECIMAL;
    casted = oneClickImporterService.castValueAsAttributeType(value, type);
    assertTrue(casted instanceof Double);
    value = "1.1";
    type = DECIMAL;
    casted = oneClickImporterService.castValueAsAttributeType(value, type);
    assertTrue(casted instanceof Double);
    value = 1.1;
    type = STRING;
    casted = oneClickImporterService.castValueAsAttributeType(value, type);
    assertTrue(casted instanceof String);
    value = 1L;
    type = STRING;
    casted = oneClickImporterService.castValueAsAttributeType(value, type);
    assertTrue(casted instanceof String);
    value = 1.1D;
    type = STRING;
    casted = oneClickImporterService.castValueAsAttributeType(value, type);
    assertTrue(casted instanceof String);
    value = true;
    type = STRING;
    casted = oneClickImporterService.castValueAsAttributeType(value, type);
    assertTrue(casted instanceof String);
    value = "2018-01-03T00:00";
    type = DATE;
    casted = oneClickImporterService.castValueAsAttributeType(value, type);
    assertTrue(casted instanceof LocalDate);
}
Also used : AttributeType(org.molgenis.data.meta.AttributeType) LocalDate(java.time.LocalDate) Test(org.testng.annotations.Test)

Example 14 with AttributeType

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

the class AttributeTypeServiceImpl method guessAttributeType.

@Override
public AttributeType guessAttributeType(List<Object> dataValues) {
    boolean guessCompleted = false;
    int rowCount = dataValues.size();
    int currentRowIndex = 0;
    AttributeType currentGuess = getBasicAttributeType(dataValues.get(0));
    currentGuess = getEnrichedType(currentGuess, dataValues.get(currentRowIndex));
    while (currentRowIndex < rowCount && !guessCompleted) {
        Object value = dataValues.get(currentRowIndex);
        AttributeType basicType = getBasicAttributeType(value);
        AttributeType basicTypeGuess = getCommonType(currentGuess, basicType);
        AttributeType enrichedTypeGuess = getEnrichedType(basicTypeGuess, value);
        // e.g. a long does not fit into an integer
        if (isBroader(enrichedTypeGuess, currentGuess)) {
            currentGuess = enrichedTypeGuess;
        }
        // If a guess is TEXT, there is no other type option suitable
        if (TEXT.equals(currentGuess)) {
            guessCompleted = true;
        }
        currentRowIndex++;
    }
    if (currentGuess == null) {
        currentGuess = STRING;
    }
    return currentGuess;
}
Also used : AttributeType(org.molgenis.data.meta.AttributeType)

Example 15 with AttributeType

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

the class EntityServiceImpl method createEntityType.

@Override
public EntityType createEntityType(DataCollection dataCollection, String packageName) {
    String entityTypeId = idGenerator.generateId();
    // Create a dataTable
    EntityType entityType = entityTypeFactory.create();
    org.molgenis.data.meta.model.Package package_ = metaDataService.getPackage(packageName);
    if (package_ == null) {
        package_ = packageFactory.create(packageName);
        package_.setLabel(packageName);
        metaDataService.addPackage(package_);
    }
    entityType.setPackage(package_);
    entityType.setId(entityTypeId);
    entityType.setLabel(oneClickImporterNamingService.getLabelWithPostFix(dataCollection.getName()));
    // Check if first column can be used as id ( has unique values )
    List<Column> columns = dataCollection.getColumns();
    Column firstColumn = columns.get(0);
    final boolean isFirstColumnUnique = oneClickImporterService.hasUniqueValues(firstColumn);
    AttributeType type = attributeTypeService.guessAttributeType(firstColumn.getDataValues());
    final boolean isValidAttributeType = getValidIdAttributeTypes().contains(type);
    final boolean useAutoId = !isFirstColumnUnique || !isValidAttributeType;
    Attribute idAttribute = useAutoId ? createIdAttribute() : createAttribute(firstColumn);
    entityType.addAttribute(idAttribute, ROLE_ID);
    // Add all columns to the dataTable
    columns.forEach(column -> {
        if (useAutoId || column != firstColumn) {
            Attribute attribute = createAttribute(column);
            entityType.addAttribute(attribute);
        }
    });
    // Store the dataTable (metadata only)
    metaDataService.addEntityType(entityType);
    // TODO: the user who adds/owns should get WRITE META always.
    permissionSystemService.giveUserWriteMetaPermissions(entityType);
    List<Entity> rows = newArrayList();
    int numberOfRows = dataCollection.getColumns().get(0).getDataValues().size();
    for (int index = 0; index < numberOfRows; index++) {
        Entity row = entityManager.create(entityType, NO_POPULATE);
        if (useAutoId) {
            row.setIdValue(idGenerator.generateId());
        }
        for (Column column : columns) {
            setRowValueForAttribute(row, index, column);
        }
        rows.add(row);
    }
    dataService.add(entityType.getId(), rows.stream());
    return entityType;
}
Also used : Entity(org.molgenis.data.Entity) Column(org.molgenis.oneclickimporter.model.Column) AttributeType(org.molgenis.data.meta.AttributeType) org.molgenis.data.meta.model(org.molgenis.data.meta.model)

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