use of org.molgenis.util.UnexpectedEnumException in project molgenis by molgenis.
the class ExcelSheetWriter method writeAttributeHeaders.
/**
* Write sheet column headers
*/
public void writeAttributeHeaders(Iterable<Attribute> attributes, AttributeWriteMode attributeWriteMode) {
if (attributes == null)
throw new IllegalArgumentException("Attributes cannot be null");
if (attributeWriteMode == null)
throw new IllegalArgumentException("AttributeWriteMode cannot be null");
if (cachedAttributes == null) {
Row poiRow = sheet.createRow(row++);
// write header
int i = 0;
for (Attribute attribute : attributes) {
Cell cell = poiRow.createCell(i++, CellType.STRING);
switch(attributeWriteMode) {
case ATTRIBUTE_LABELS:
cell.setCellValue(AbstractCellProcessor.processCell(attribute.getLabel(), true, cellProcessors));
break;
case ATTRIBUTE_NAMES:
cell.setCellValue(AbstractCellProcessor.processCell(attribute.getName(), true, cellProcessors));
break;
default:
throw new UnexpectedEnumException(attributeWriteMode);
}
}
// store header
this.cachedAttributes = attributes;
}
}
use of org.molgenis.util.UnexpectedEnumException in project molgenis by molgenis.
the class CsvWriter method toValue.
private String toValue(Object obj) {
String value;
if (obj == null) {
value = null;
} else if (obj instanceof Entity) {
if (getEntityWriteMode() != null) {
switch(getEntityWriteMode()) {
case ENTITY_IDS:
value = ((Entity) obj).getIdValue().toString();
break;
case ENTITY_LABELS:
Object labelValue = ((Entity) obj).getLabelValue();
value = labelValue != null ? labelValue.toString() : null;
break;
default:
throw new UnexpectedEnumException(getEntityWriteMode());
}
} else {
Object labelValue = ((Entity) obj).getLabelValue();
value = labelValue != null ? labelValue.toString() : null;
}
} else if (obj instanceof Iterable<?>) {
StringBuilder strBuilder = new StringBuilder();
for (Object listItem : (Iterable<?>) obj) {
if (strBuilder.length() > 0)
strBuilder.append(',');
strBuilder.append(toValue(listItem));
}
// TODO apply cell processors to list elements?
value = strBuilder.toString();
} else {
value = obj.toString();
}
return AbstractCellProcessor.processCell(value, false, this.cellProcessors);
}
use of org.molgenis.util.UnexpectedEnumException in project molgenis by molgenis.
the class QueryGenerator method createQueryClauseFuzzyMatchNgram.
private QueryBuilder createQueryClauseFuzzyMatchNgram(QueryRule queryRule, EntityType entityType) {
String queryField = queryRule.getField();
Object queryValue = queryRule.getValue();
QueryBuilder queryBuilder;
if (queryValue == null)
throw new MolgenisQueryException("Query value cannot be null");
if (queryField == null) {
queryBuilder = QueryBuilders.matchQuery("_all", queryValue);
} else {
Attribute attr = entityType.getAttribute(queryField);
if (attr == null) {
throw new UnknownAttributeException(entityType, queryField);
}
// construct query part
AttributeType dataType = attr.getDataType();
switch(dataType) {
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:
queryField = getQueryFieldName(attr) + ".ngram";
queryBuilder = QueryBuilders.queryStringQuery(queryField + ":(" + queryValue + ")");
break;
case MREF:
case XREF:
queryField = getQueryFieldName(attr) + "." + getQueryFieldName(attr.getRefEntity().getLabelAttribute()) + ".ngram";
queryBuilder = QueryBuilders.nestedQuery(getQueryFieldName(attr), QueryBuilders.queryStringQuery(queryField + ":(" + queryValue + ")"), ScoreMode.Max);
break;
default:
throw new UnexpectedEnumException(dataType);
}
}
return queryBuilder;
}
use of org.molgenis.util.UnexpectedEnumException in project molgenis by molgenis.
the class QueryGenerator method createQueryClauseEqualsValue.
private QueryBuilder createQueryClauseEqualsValue(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 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:
if (useNotAnalyzedField(attr)) {
fieldName = fieldName + '.' + FIELD_NOT_ANALYZED;
}
return nestedQueryBuilder(attributePath, QueryBuilders.termQuery(fieldName, queryValue));
case CATEGORICAL:
case CATEGORICAL_MREF:
case XREF:
case MREF:
case FILE:
case ONE_TO_MANY:
if (attributePath.size() > 1) {
throw new MolgenisQueryException("Can not filter on references deeper than 1.");
}
Attribute refIdAttr = attr.getRefEntity().getIdAttribute();
List<Attribute> refAttributePath = concat(attributePath.stream(), of(refIdAttr)).collect(toList());
String indexFieldName = getQueryFieldName(refAttributePath);
if (useNotAnalyzedField(refIdAttr)) {
indexFieldName = indexFieldName + '.' + FIELD_NOT_ANALYZED;
}
return QueryBuilders.nestedQuery(fieldName, QueryBuilders.termQuery(indexFieldName, queryValue), ScoreMode.Avg);
case COMPOUND:
throw new MolgenisQueryException(format("Illegal attribute type [%s]", attrType.toString()));
default:
throw new UnexpectedEnumException(attrType);
}
}
use of org.molgenis.util.UnexpectedEnumException in project molgenis by molgenis.
the class QueryGenerator method createQueryClauseFuzzyMatch.
private QueryBuilder createQueryClauseFuzzyMatch(QueryRule queryRule, EntityType entityType) {
String queryField = queryRule.getField();
Object queryValue = queryRule.getValue();
QueryBuilder queryBuilder;
if (queryValue == null)
throw new MolgenisQueryException("Query value cannot be null");
if (queryField == null) {
queryBuilder = QueryBuilders.matchQuery("_all", queryValue);
} else {
Attribute attr = entityType.getAttribute(queryField);
if (attr == null) {
throw new UnknownAttributeException(entityType, queryField);
}
// construct query part
AttributeType dataType = attr.getDataType();
switch(dataType) {
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:
queryBuilder = QueryBuilders.queryStringQuery(getQueryFieldName(attr) + ":(" + queryValue + ")");
break;
case MREF:
case XREF:
case CATEGORICAL:
case CATEGORICAL_MREF:
case ONE_TO_MANY:
case FILE:
queryField = getQueryFieldName(attr) + "." + getQueryFieldName(attr.getRefEntity().getLabelAttribute());
queryBuilder = QueryBuilders.nestedQuery(getQueryFieldName(attr), QueryBuilders.queryStringQuery(queryField + ":(" + queryValue + ")"), ScoreMode.Max);
break;
case BOOL:
case COMPOUND:
throw new MolgenisQueryException("Illegal data type [" + dataType + "] for operator [" + QueryRule.Operator.FUZZY_MATCH + "]");
default:
throw new UnexpectedEnumException(dataType);
}
}
return queryBuilder;
}
Aggregations