use of org.activityinfo.shared.command.MatchLocation in project activityinfo by bedatadriven.
the class MatchLocationHandler method execute.
@Override
public CommandResult execute(MatchLocation cmd, User user) throws CommandException {
Map<Integer, AdminEntity> matched = Maps.newHashMap();
// now try and match against the text
for (Integer levelId : cmd.getAdminLevels().keySet()) {
matchEntity(matched, cmd.getAdminLevels(), em.find(AdminLevel.class, levelId));
}
Location matchedLocation = matchLocation(cmd.getLocationType(), cmd.getName(), matched.values());
LocationDTO location = new LocationDTO();
if (matchedLocation == null) {
// create a new location object
location.setId(new KeyGenerator().generateInt());
location.setName(cmd.getName());
location.setLatitude(cmd.getLatitude());
location.setLongitude(cmd.getLongitude());
location.setNew(true);
location.setLocationTypeId(cmd.getLocationType());
for (AdminEntity entity : matched.values()) {
AdminEntityDTO dto = new AdminEntityDTO();
dto.setId(entity.getId());
dto.setName(entity.getName());
dto.setLevelId(entity.getLevel().getId());
location.setAdminEntity(entity.getLevel().getId(), dto);
}
} else {
location.setNew(false);
location.setId(matchedLocation.getId());
location.setName(matchedLocation.getName());
location.setLatitude(matchedLocation.getY());
location.setLongitude(matchedLocation.getX());
location.setLocationTypeId(matchedLocation.getLocationType().getId());
for (AdminEntity entity : matchedLocation.getAdminEntities()) {
AdminEntityDTO dto = new AdminEntityDTO();
dto.setId(entity.getId());
dto.setName(entity.getName());
dto.setLevelId(entity.getLevel().getId());
location.setAdminEntity(entity.getLevel().getId(), dto);
}
}
return location;
}
use of org.activityinfo.shared.command.MatchLocation in project activityinfo by bedatadriven.
the class MatchLocationHandler method matchLocation.
/* try to match to an existing location, based on administrative entity
* membership and approximate name match
*/
private Location matchLocation(int locationTypeId, String name, Collection<AdminEntity> entities) {
// find the set of locations that is present in all matched admin
// entities
Set<Location> locations = null;
for (AdminEntity entity : entities) {
if (locations == null) {
locations = Sets.newHashSet(entity.getLocations());
} else {
locations.retainAll(entity.getLocations());
}
}
// anything?
if (locations == null) {
return null;
}
// now find the best matching location by name among this set
Location bestMatch = null;
double bestScore = 0;
for (Location location : locations) {
if (location.getLocationType().getId() == locationTypeId) {
double score = similarity(location.getName(), name);
if (score > bestScore) {
bestScore = score;
bestMatch = location;
}
}
}
return bestMatch;
}
use of org.activityinfo.shared.command.MatchLocation in project activityinfo by bedatadriven.
the class ImporterWizard method finish.
@Override
public void finish(final AsyncCallback<Void> callback) {
final KeyGenerator keyGenerator = new KeyGenerator();
int numColums = model.getData().getNumColumns();
ColumnBinding[] bindings = bindingsArray();
// do a first pass to match the location
List<Command> matchBatch = Lists.newArrayList();
for (ImportRowModel row : model.getData().getRowStore().getModels()) {
MatchLocation location = new MatchLocation();
location.setLocationType(model.getActivity().getLocationTypeId());
for (int i = 0; i != numColums; ++i) {
bindings[i].bindLocation(row.get(i), location);
}
matchBatch.add(location);
}
dispatcher.execute(new BatchCommand(matchBatch), new AsyncCallback<BatchResult>() {
@Override
public void onFailure(Throwable caught) {
MessageBox.alert("Match locations failed", "Exception", null);
}
@Override
public void onSuccess(BatchResult result) {
submitSites((List) result.getResults(), callback);
}
});
}
Aggregations