Search in sources :

Example 1 with MatchLocation

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;
}
Also used : AdminEntity(org.activityinfo.server.database.hibernate.entity.AdminEntity) AdminLevel(org.activityinfo.server.database.hibernate.entity.AdminLevel) AdminEntityDTO(org.activityinfo.shared.dto.AdminEntityDTO) KeyGenerator(org.activityinfo.client.local.command.handler.KeyGenerator) LocationDTO(org.activityinfo.shared.dto.LocationDTO) MatchLocation(org.activityinfo.shared.command.MatchLocation) Location(org.activityinfo.server.database.hibernate.entity.Location)

Example 2 with MatchLocation

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;
}
Also used : AdminEntity(org.activityinfo.server.database.hibernate.entity.AdminEntity) MatchLocation(org.activityinfo.shared.command.MatchLocation) Location(org.activityinfo.server.database.hibernate.entity.Location)

Example 3 with MatchLocation

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);
        }
    });
}
Also used : ColumnBinding(org.activityinfo.client.importer.column.ColumnBinding) BatchResult(org.activityinfo.shared.command.result.BatchResult) MatchLocation(org.activityinfo.shared.command.MatchLocation) Command(org.activityinfo.shared.command.Command) BatchCommand(org.activityinfo.shared.command.BatchCommand) BatchCommand(org.activityinfo.shared.command.BatchCommand) List(java.util.List) KeyGenerator(org.activityinfo.client.local.command.handler.KeyGenerator)

Aggregations

MatchLocation (org.activityinfo.shared.command.MatchLocation)3 KeyGenerator (org.activityinfo.client.local.command.handler.KeyGenerator)2 AdminEntity (org.activityinfo.server.database.hibernate.entity.AdminEntity)2 Location (org.activityinfo.server.database.hibernate.entity.Location)2 List (java.util.List)1 ColumnBinding (org.activityinfo.client.importer.column.ColumnBinding)1 AdminLevel (org.activityinfo.server.database.hibernate.entity.AdminLevel)1 BatchCommand (org.activityinfo.shared.command.BatchCommand)1 Command (org.activityinfo.shared.command.Command)1 BatchResult (org.activityinfo.shared.command.result.BatchResult)1 AdminEntityDTO (org.activityinfo.shared.dto.AdminEntityDTO)1 LocationDTO (org.activityinfo.shared.dto.LocationDTO)1