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