use of org.activityinfo.ui.client.component.importDialog.model.source.PastedTable in project activityinfo by bedatadriven.
the class InstanceScoreTest method adminEntityScoring.
@OnDataSet("/dbunit/nfi-import.db.xml")
@Test
public void adminEntityScoring() throws IOException {
setUser(3);
FormTree formTree = assertResolves(locator.getFormTree(ImportWithMultiClassRangeTest.SCHOOL_FORM_CLASS));
FormTreePrettyPrinter.print(formTree);
importModel = new ImportModel(formTree);
importer = new Importer(locator, formTree, FieldImportStrategies.get(JvmConverterFactory.get()));
// Step 1: User pastes in data to import
PastedTable source = new PastedTable(Resources.toString(getResource(getClass(), "school-import.csv"), Charsets.UTF_8));
importModel.setSource(source);
dumpList("COLUMNS", source.getColumns());
importModel.setColumnAction(columnIndex("School"), target("Name"));
// Province is at the root of both hierarchies
importModel.setColumnAction(columnIndex("Province"), target("Province Name"));
// Admin hierarchy
importModel.setColumnAction(columnIndex("District"), target("District Name"));
importModel.setColumnAction(columnIndex("Territoire"), target("Territoire Name"));
importModel.setColumnAction(columnIndex("Secteur"), target("Secteur Name"));
importModel.setColumnAction(columnIndex("Groupement"), target("Secteur Name"));
// health ministry hierarchy
importModel.setColumnAction(columnIndex("Zone de Santé"), target("Zone de Santé Name"));
FormTree.Node rootField = formTree.getRootField(ADMINISTRATIVE_UNIT_FIELD);
TargetCollector targetCollector = new TargetCollector(rootField);
Map<TargetSiteId, ColumnAccessor> mappedColumns = importModel.getMappedColumns(rootField.getFieldId());
List<ColumnAccessor> sourceColumns = Lists.newArrayList();
Map<FieldPath, Integer> referenceFields = targetCollector.getPathMap(mappedColumns, sourceColumns);
// Province level
ColumnSet columnSet = assertResolves(query(referenceFields, ImportWithMultiClassRangeTest.PROVINCE_LEVEL));
InstanceScoreSource scoreSource = new InstanceScoreSourceBuilder(CuidAdapter.adminLevelFormClass(ImportWithMultiClassRangeTest.PROVINCE_LEVEL), referenceFields, sourceColumns).build(columnSet);
InstanceScorer.Score score = score(source.getRows().get(0), scoreSource);
assertScore(score, "Katanga");
// District level
columnSet = assertResolves(query(referenceFields, ImportWithMultiClassRangeTest.DISTRICT_LEVEL));
scoreSource = new InstanceScoreSourceBuilder(CuidAdapter.adminLevelFormClass(ImportWithMultiClassRangeTest.DISTRICT_LEVEL), referenceFields, sourceColumns).build(columnSet);
score = score(source.getRows().get(1), scoreSource);
assertScore(score, "Katanga");
assertScore(score, "Tanganika");
// Territoire level
columnSet = assertResolves(query(referenceFields, ImportWithMultiClassRangeTest.TERRITOIRE_LEVEL));
scoreSource = new InstanceScoreSourceBuilder(CuidAdapter.adminLevelFormClass(ImportWithMultiClassRangeTest.TERRITOIRE_LEVEL), referenceFields, sourceColumns).build(columnSet);
score = score(source.getRows().get(2), scoreSource);
assertScore(score, "Katanga");
assertScore(score, "Tanganika");
assertScore(score, "Kalemie");
assertThat(scoreSource.getReferenceInstanceIds().get(score.getBestMatchIndex()), equalTo(ImportWithMultiClassRangeTest.TERRITOIRE_KALEMIE));
}
use of org.activityinfo.ui.client.component.importDialog.model.source.PastedTable in project activityinfo by bedatadriven.
the class PastedTableTest method columnTypeGuesser.
@Test
public void columnTypeGuesser() throws IOException {
PastedTable pastedTable = new PastedTable(Resources.toString(getResource(getClass(), "qis.csv"), Charsets.UTF_8));
// guess column types
pastedTable.parseAllRows();
pastedTable.guessColumnsType(JvmConverterFactory.get());
Assert.assertEquals(column(pastedTable, "Partner").getGuessedType(), FieldTypeClass.FREE_TEXT);
// Assert.assertEquals(column(pastedTable, "_CREATION_DATE").getGuessedType(), FormFieldType.LOCAL_DATE);
Assert.assertEquals(column(pastedTable, "_MODEL_VERSION").getGuessedType(), FieldTypeClass.QUANTITY);
}
use of org.activityinfo.ui.client.component.importDialog.model.source.PastedTable in project activityinfo by bedatadriven.
the class PastedTableTest method nfiParsingPerformance.
@Test
public void nfiParsingPerformance() throws IOException {
long start = System.currentTimeMillis();
PastedTable pastedTable = new PastedTable(Resources.toString(getResource(getClass(), "nfi-import-test.csv"), Charsets.UTF_8));
pastedTable.parseAllRows();
final List<SourceColumn> columns = pastedTable.getColumns();
final List<? extends SourceRow> rows = pastedTable.getRows();
long end = System.currentTimeMillis();
System.out.println("Done in " + (end - start) + "ms , rows count=" + rows.size() + ", columns count=" + columns.size());
}
use of org.activityinfo.ui.client.component.importDialog.model.source.PastedTable in project activityinfo by bedatadriven.
the class RowParserTest method qisCsvColumns.
@Test
public void qisCsvColumns() throws IOException {
PastedTable source = new PastedTable(Resources.toString(getResource(getClass(), "qis.csv"), Charsets.UTF_8));
final List<SourceColumn> columns = source.getColumns();
System.out.println(Joiner.on('\n').join(columns));
assertThat(columns.size(), equalTo(47));
}
use of org.activityinfo.ui.client.component.importDialog.model.source.PastedTable in project activityinfo by bedatadriven.
the class ChooseSourcePage method fireStateChanged.
@Override
public void fireStateChanged() {
// validate
boolean valid = false;
PastedTable pastedTable = null;
try {
pastedTable = new PastedTable(textArea.getValue());
valid = !pastedTable.getRows().isEmpty();
if (valid) {
boolean isFirstColumnHeaderBlank = Strings.isNullOrEmpty(pastedTable.getColumnHeader(0));
if (isFirstColumnHeaderBlank) {
// first row may be occupied by attribute group name, so we just cut it
valid = false;
String source = textArea.getValue().substring(textArea.getValue().indexOf("\n") + 1);
pastedTable = new PastedTable(source);
valid = !pastedTable.getRows().isEmpty();
}
}
} catch (Exception e) {
// ignore : text is not valid
}
if (valid) {
pastedTable.guessColumnsType(JsConverterFactory.get());
model.setSource(pastedTable);
eventBus.fireEvent(new PageChangedEvent(true, ""));
} else {
eventBus.fireEvent(new PageChangedEvent(false, errorMessage(pastedTable)));
}
}
Aggregations