Search in sources :

Example 1 with FieldValueParser

use of org.activityinfo.ui.client.component.importDialog.model.type.converter.FieldValueParser in project activityinfo by bedatadriven.

the class ColumnTypeGuesser method calculateTypeScores.

private void calculateTypeScores() {
    for (String value : columnValues) {
        final Map<FieldTypeClass, Integer> copyMap = Maps.newHashMap(typeMap);
        // we don't need to iterate over string types because input is always string
        copyMap.remove(FieldTypeClass.FREE_TEXT);
        copyMap.remove(FieldTypeClass.NARRATIVE);
        boolean hasMatch = false;
        for (Map.Entry<FieldTypeClass, Integer> entry : copyMap.entrySet()) {
            try {
                if (isBoolean(value) && entry.getKey().equals(BooleanType.TYPE_CLASS)) {
                    increaseValue(entry.getKey());
                    hasMatch = true;
                    break;
                }
                final FieldValueParser stringConverter = converterFactory.createStringConverter(entry.getKey().createType());
                final Object convertedValue = stringConverter.convert(value);
                // analyze converted value
                if (convertedValue != null) {
                    increaseValue(entry.getKey());
                    hasMatch = true;
                    break;
                }
            } catch (Exception e) {
            // ignore
            }
        }
        // if no match then we fallback to string type
        if (!hasMatch) {
            final int length = value.length();
            if (length < FormFieldType.FREE_TEXT_LENGTH) {
                increaseValue(FieldTypeClass.FREE_TEXT);
            } else {
                increaseValue(FieldTypeClass.NARRATIVE);
            }
        }
    }
}
Also used : FieldValueParser(org.activityinfo.ui.client.component.importDialog.model.type.converter.FieldValueParser) FieldTypeClass(org.activityinfo.model.type.FieldTypeClass) Map(java.util.Map)

Example 2 with FieldValueParser

use of org.activityinfo.ui.client.component.importDialog.model.type.converter.FieldValueParser in project activityinfo by bedatadriven.

the class DataFieldImportStrategy method createImporter.

@Override
public FieldImporter createImporter(FormTree.Node node, Map<TargetSiteId, ColumnAccessor> bindings, ImportModel model) {
    ImportTarget requiredTarget = target(node);
    ColumnAccessor column = bindings.get(VALUE);
    if (column == null) {
        column = MissingColumn.INSTANCE;
    }
    FieldValueParser converter = converterFactory.createStringConverter(node.getType());
    return new DataFieldImporter(column, requiredTarget, converter, node, model);
}
Also used : FieldValueParser(org.activityinfo.ui.client.component.importDialog.model.type.converter.FieldValueParser)

Aggregations

FieldValueParser (org.activityinfo.ui.client.component.importDialog.model.type.converter.FieldValueParser)2 Map (java.util.Map)1 FieldTypeClass (org.activityinfo.model.type.FieldTypeClass)1