use of org.activityinfo.geoadmin.merge2.view.profile.FormProfile in project activityinfo by bedatadriven.
the class FormMappingBuilder method buildReferenceMapping.
/**
* Builds a reference mapping for a given target node.
*
* <p>A reference field takes the value of a single {@code ResourceId}, but
* most of the time we don't have the actual id of the field in the dataset to import:
* we have to obtain the id by performing a look up against text fields in the
* </p>
*
* @param targetField the reference field to look up
*/
private void buildReferenceMapping(final FormField targetField) {
// In order to match against the ReferenceField, we need the actual data
// from the form that is being referenced.
//
// Example: If we have a "Location" field that references the "Province" form class,
// then we need then names and/or codes of the Province form class in order to lookup the
// ids, assuming that our source dataset has a "province name" column with the names of the provinces.
ReferenceType type = (ReferenceType) targetField.getType();
// Currently this only supports reference fields that reference exactly one form class.
if (type.getCardinality() == Cardinality.SINGLE && type.getRange().size() == 1) {
ResourceId referenceFormId = Iterables.getOnlyElement(type.getRange());
Observable<FormProfile> lookupForm = FormProfile.profile(resourceStore, referenceFormId);
Observable<FieldMapping> mapping = lookupForm.transform(new Function<FormProfile, FieldMapping>() {
@Override
public FieldMapping apply(FormProfile lookupForm) {
return new ReferenceFieldMapping(targetField, KeyFieldPairSet.matchKeys(source, lookupForm), referenceMatches);
}
});
mappings.add(mapping);
}
}
use of org.activityinfo.geoadmin.merge2.view.profile.FormProfile in project activityinfo by bedatadriven.
the class KeyFieldPairSet method matchKeys.
/**
* Constructs a {@code KeyFieldPairSet} from a source and target form by matching columns with similar
* sets of values.
*/
public static KeyFieldPairSet matchKeys(FormProfile source, FormProfile target) {
ScoreMatrix scoreMatrix = new FieldScoreMatrix(source.getFields(), target.getFields());
dumpScoreMatrix(source, target, scoreMatrix);
MatchGraph matchGraph = new MatchGraph(scoreMatrix);
matchGraph.build();
BiMap<FieldProfile, FieldProfile> targetToSource = HashBiMap.create();
for (int sourceColumnIndex = 0; sourceColumnIndex != scoreMatrix.getRowCount(); ++sourceColumnIndex) {
int targetColumnIndex = matchGraph.getBestMatchForSource(sourceColumnIndex);
if (targetColumnIndex != -1) {
FieldProfile sourceColumn = source.getFields().get(sourceColumnIndex);
FieldProfile targetColumn = target.getFields().get(targetColumnIndex);
targetToSource.put(targetColumn, sourceColumn);
System.out.println("Matching FIELD " + sourceColumn.getLabel() + " => " + targetColumn.getLabel());
}
}
return new KeyFieldPairSet(source, target, targetToSource);
}
Aggregations