Search in sources :

Example 1 with ImportTarget

use of org.activityinfo.ui.client.component.importDialog.model.strategy.ImportTarget in project activityinfo by bedatadriven.

the class ColumnActionSelector method updateTypeStyles.

public void updateTypeStyles(FieldTypeClass sourceType) {
    for (Map.Entry<ColumnAction, RadioButton> entry : buttons.entrySet()) {
        final ColumnAction columnAction = entry.getKey();
        if (columnAction instanceof MapExistingAction) {
            final ImportTarget target = ((MapExistingAction) columnAction).getTarget();
            final FieldTypeClass targetType = target.getFormField().getType().getTypeClass();
            final RadioButton button = entry.getValue();
            button.removeStyleName(ColumnMappingStyles.INSTANCE.typeNotMatched());
            button.removeStyleName(ColumnMappingStyles.INSTANCE.typeMatched());
            if (targetType == sourceType || (sourceType == FieldTypeClass.FREE_TEXT && targetType == FieldTypeClass.REFERENCE)) {
                button.addStyleName(ColumnMappingStyles.INSTANCE.typeMatched());
            } else {
                button.addStyleName(ColumnMappingStyles.INSTANCE.typeNotMatched());
            }
        }
    }
}
Also used : ColumnAction(org.activityinfo.ui.client.component.importDialog.model.ColumnAction) ImportTarget(org.activityinfo.ui.client.component.importDialog.model.strategy.ImportTarget) FieldTypeClass(org.activityinfo.model.type.FieldTypeClass) MapExistingAction(org.activityinfo.ui.client.component.importDialog.model.MapExistingAction) RadioButton(org.activityinfo.ui.client.widget.RadioButton) Map(java.util.Map)

Example 2 with ImportTarget

use of org.activityinfo.ui.client.component.importDialog.model.strategy.ImportTarget in project activityinfo by bedatadriven.

the class ColumnMappingPage method collectNotMappedMandatoryColumns.

/**
 * @param mandatory if true - then only mandatory, if false then ALL
 * @return
 */
public List<ImportTarget> collectNotMappedMandatoryColumns(boolean mandatory) {
    List<ImportTarget> list = Lists.newArrayList();
    for (final MapExistingAction action : actions) {
        final FormField formField = action.getTarget().getFormField();
        if (mandatory && !formField.isRequired()) {
            continue;
        }
        final Map<TargetSiteId, ColumnAccessor> mappedColumns = importModel.getMappedColumns(formField.getId());
        if (mappedColumns.isEmpty()) {
            list.add(action.getTarget());
        }
    }
    return list;
}
Also used : ImportTarget(org.activityinfo.ui.client.component.importDialog.model.strategy.ImportTarget) ColumnAccessor(org.activityinfo.ui.client.component.importDialog.model.strategy.ColumnAccessor) MapExistingAction(org.activityinfo.ui.client.component.importDialog.model.MapExistingAction) TargetSiteId(org.activityinfo.ui.client.component.importDialog.model.strategy.TargetSiteId) FormField(org.activityinfo.model.form.FormField)

Example 3 with ImportTarget

use of org.activityinfo.ui.client.component.importDialog.model.strategy.ImportTarget in project activityinfo by bedatadriven.

the class ColumnMappingGuesser method getDistanceMap.

public TreeMap<Integer, ImportTarget> getDistanceMap(String sourceLabel) {
    final TreeMap<Integer, ImportTarget> distanceMap = Maps.newTreeMap();
    for (ImportTarget target : importTargets) {
        String targetLabel = target.getLabel();
        if (target.getFormField().getType() instanceof EnumType) {
            EnumType enumType = (EnumType) target.getFormField().getType();
            if (enumType.getCardinality() == Cardinality.MULTIPLE) {
                int index = targetLabel.indexOf("-");
                if (index != -1) {
                    targetLabel = targetLabel.substring(0, index - 1);
                }
            }
        }
        final int distance = Levenshtein.getLevenshteinDistance(sourceLabel, targetLabel);
        distanceMap.put(distance, target);
    }
    return distanceMap;
}
Also used : ImportTarget(org.activityinfo.ui.client.component.importDialog.model.strategy.ImportTarget) EnumType(org.activityinfo.model.type.enumerated.EnumType)

Example 4 with ImportTarget

use of org.activityinfo.ui.client.component.importDialog.model.strategy.ImportTarget in project activityinfo by bedatadriven.

the class ColumnMappingGuesser method getMapping.

public Map<SourceColumn, ImportTarget> getMapping() {
    // lower distance between maps
    Map<SourceColumn, TreeMap<Integer, ImportTarget>> distanceWithinTargetMaps = Maps.newHashMap();
    for (SourceColumn sourceColumn : importModel.getSource().getColumns()) {
        final String sourceColumnHeader = sourceColumn.getHeader();
        final TreeMap<Integer, ImportTarget> distanceMap = getDistanceMap(sourceColumnHeader);
        if (!distanceMap.isEmpty()) {
            final Map.Entry<Integer, ImportTarget> lowerDistanceEntry = distanceMap.entrySet().iterator().next();
            // if number of transformation operations are higher then label length then ignore such mapping
            final Integer transformationOperations = lowerDistanceEntry.getKey();
            // if (transformationOperations < sourceColumnHeader.length() && transformationOperations < lowerDistanceEntry.getValue().getLabel().length()) {
            TreeMap<Integer, ImportTarget> valueMap = distanceWithinTargetMaps.get(sourceColumn);
            if (valueMap == null) {
                valueMap = Maps.newTreeMap();
                distanceWithinTargetMaps.put(sourceColumn, valueMap);
            }
            valueMap.put(transformationOperations, lowerDistanceEntry.getValue());
        // }
        }
    }
    // now re-iterate for target (different source columns may get the same target column as best match (lower distance))
    Map<ImportTarget, TreeMap<Integer, SourceColumn>> targetToSource = Maps.newHashMap();
    for (Map.Entry<SourceColumn, TreeMap<Integer, ImportTarget>> entry : distanceWithinTargetMaps.entrySet()) {
        final TreeMap<Integer, ImportTarget> value = entry.getValue();
        if (!value.isEmpty()) {
            // entry with lowest distance
            final Map.Entry<Integer, ImportTarget> bestEntry = value.entrySet().iterator().next();
            TreeMap<Integer, SourceColumn> distanceForSourceMap = targetToSource.get(bestEntry.getValue());
            if (distanceForSourceMap == null) {
                distanceForSourceMap = Maps.newTreeMap();
                targetToSource.put(bestEntry.getValue(), distanceForSourceMap);
            }
            distanceForSourceMap.put(bestEntry.getKey(), entry.getKey());
        }
    }
    // finally build mapping
    Map<SourceColumn, ImportTarget> mapping = Maps.newHashMap();
    for (Map.Entry<ImportTarget, TreeMap<Integer, SourceColumn>> entry : targetToSource.entrySet()) {
        final TreeMap<Integer, SourceColumn> map = entry.getValue();
        if (!map.isEmpty()) {
            mapping.put(map.entrySet().iterator().next().getValue(), entry.getKey());
        }
    }
    return mapping;
}
Also used : ImportTarget(org.activityinfo.ui.client.component.importDialog.model.strategy.ImportTarget) SourceColumn(org.activityinfo.ui.client.component.importDialog.model.source.SourceColumn) TreeMap(java.util.TreeMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 5 with ImportTarget

use of org.activityinfo.ui.client.component.importDialog.model.strategy.ImportTarget in project activityinfo by bedatadriven.

the class ImportSimpleTest method locationWithMissingAdminLevel.

@Test
public void locationWithMissingAdminLevel() throws IOException {
    FormTree formTree = assertResolves(locator.getFormTree(VILLAGE_FORM_ID));
    FormTreePrettyPrinter.print(formTree);
    importModel = new ImportModel(formTree);
    // Step 1: User pastes in data to import
    PastedTable source = new PastedTable(Resources.toString(getResource(getClass(), "qis-villages.csv"), Charsets.UTF_8));
    source.parseAllRows();
    assertThat(source.getRows().size(), equalTo(1));
    importModel.setSource(source);
    importer = new Importer(locator, formTree, FieldImportStrategies.get(JvmConverterFactory.get()));
    dumpList("COLUMNS", source.getColumns());
    // Step 2: User maps imported columns to FormFields
    List<ImportTarget> targets = importer.getImportTargets();
    dumpList("FIELDS", targets);
    importModel.setColumnAction(columnIndex("Name"), target("Name"));
    importModel.setColumnAction(columnIndex("District"), target("District Name"));
    // Step 3: Validate for user
    ValidatedRowTable validatedResult = assertResolves(importer.validateRows(importModel));
    showValidationGrid(validatedResult);
    assertResolves(importer.persist(importModel));
    // AND... verify
    QueryModel queryModel = new QueryModel(VILLAGE_FORM_ID);
    queryModel.selectExpr("Name").as("name");
    queryModel.selectField(CuidAdapter.field(VILLAGE_FORM_ID, CuidAdapter.ADMIN_FIELD)).as("admin");
    ColumnSet columnSet = assertResolves(locator.queryTable(queryModel));
    assertThat(columnSet.getNumRows(), equalTo(1));
    assertThat(columnSet.getColumnView("name").getString(0), equalTo("Village 1"));
    assertThat(columnSet.getColumnView("admin").getString(0), equalTo(CuidAdapter.cuid(CuidAdapter.ADMIN_ENTITY_DOMAIN, 2).asString()));
}
Also used : FormTree(org.activityinfo.model.formTree.FormTree) PastedTable(org.activityinfo.ui.client.component.importDialog.model.source.PastedTable) ImportTarget(org.activityinfo.ui.client.component.importDialog.model.strategy.ImportTarget) ValidatedRowTable(org.activityinfo.ui.client.component.importDialog.model.validation.ValidatedRowTable) ColumnSet(org.activityinfo.model.query.ColumnSet) ImportModel(org.activityinfo.ui.client.component.importDialog.model.ImportModel) QueryModel(org.activityinfo.model.query.QueryModel) Test(org.junit.Test)

Aggregations

ImportTarget (org.activityinfo.ui.client.component.importDialog.model.strategy.ImportTarget)7 MapExistingAction (org.activityinfo.ui.client.component.importDialog.model.MapExistingAction)4 Map (java.util.Map)3 ColumnAction (org.activityinfo.ui.client.component.importDialog.model.ColumnAction)2 RadioButton (org.activityinfo.ui.client.widget.RadioButton)2 TreeMap (java.util.TreeMap)1 FormField (org.activityinfo.model.form.FormField)1 FormTree (org.activityinfo.model.formTree.FormTree)1 ColumnSet (org.activityinfo.model.query.ColumnSet)1 QueryModel (org.activityinfo.model.query.QueryModel)1 FieldTypeClass (org.activityinfo.model.type.FieldTypeClass)1 EnumType (org.activityinfo.model.type.enumerated.EnumType)1 ImportModel (org.activityinfo.ui.client.component.importDialog.model.ImportModel)1 PastedTable (org.activityinfo.ui.client.component.importDialog.model.source.PastedTable)1 SourceColumn (org.activityinfo.ui.client.component.importDialog.model.source.SourceColumn)1 ColumnAccessor (org.activityinfo.ui.client.component.importDialog.model.strategy.ColumnAccessor)1 TargetSiteId (org.activityinfo.ui.client.component.importDialog.model.strategy.TargetSiteId)1 ValidatedRowTable (org.activityinfo.ui.client.component.importDialog.model.validation.ValidatedRowTable)1 Test (org.junit.Test)1