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);
}
}
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);
}
}
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);
}
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;
}
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;
}
Aggregations