Search in sources :

Example 1 with LocationResult

use of org.activityinfo.legacy.shared.command.result.LocationResult in project activityinfo by bedatadriven.

the class LocationFilterPanel method applyInternalValue.

private Promise<LocationResult> applyInternalValue() {
    Promise<LocationResult> promise = new Promise<>();
    promise.then(new Function<LocationResult, Object>() {

        @Override
        public Object apply(LocationResult input) {
            filterToolBar.setApplyFilterEnabled(false);
            filterToolBar.setRemoveFilterEnabled(value.isRestricted(DimensionType.Location));
            store.removeAll();
            store.add(input.getData());
            return null;
        }
    });
    dispatcher.execute(new GetLocations(new ArrayList<>(value.getRestrictions(DimensionType.Location))), promise);
    return promise;
}
Also used : Promise(org.activityinfo.promise.Promise) GetLocations(org.activityinfo.legacy.shared.command.GetLocations) ArrayList(java.util.ArrayList) LocationResult(org.activityinfo.legacy.shared.command.result.LocationResult)

Example 2 with LocationResult

use of org.activityinfo.legacy.shared.command.result.LocationResult in project activityinfo by bedatadriven.

the class LocationFilterPanel method setValue.

@Override
public void setValue(final Filter value, final boolean fireEvents) {
    Filter newValue = new Filter();
    newValue.addRestriction(DimensionType.Location, value.getRestrictions(DimensionType.Location));
    if (newValue.equals(this.value)) {
        return;
    }
    this.value = newValue;
    store.removeAll();
    if (!this.value.isRestricted(DimensionType.Location)) {
        return;
    }
    applyInternalValue().then(new Function<LocationResult, Object>() {

        @Nullable
        @Override
        public Object apply(@Nullable LocationResult input) {
            if (fireEvents) {
                ValueChangeEvent.fire(LocationFilterPanel.this, value);
            }
            return null;
        }
    });
}
Also used : Filter(org.activityinfo.legacy.shared.command.Filter) Nullable(javax.annotation.Nullable) LocationResult(org.activityinfo.legacy.shared.command.result.LocationResult)

Example 3 with LocationResult

use of org.activityinfo.legacy.shared.command.result.LocationResult in project activityinfo by bedatadriven.

the class SearchLocationsHandler method retrieveLocations.

private void retrieveLocations(final SearchLocations command, final ExecutionContext context, final AsyncCallback<LocationResult> callback, final int limit, final int totalCount) {
    SqlQuery query = baseQuery(command).appendColumns("LocationId", "Name", "Axe", "X", "Y", "LocationTypeId").setLimitClause(dialect.limitClause(0, limit));
    query.execute(context.getTransaction(), new SqlResultCallback() {

        @Override
        public void onSuccess(SqlTransaction tx, SqlResultSet results) {
            // Create a list of locations from query result
            List<LocationDTO> locations = Lists.newArrayList();
            Map<Integer, LocationDTO> locationsById = Maps.newHashMap();
            for (SqlResultSetRow row : results.getRows()) {
                LocationDTO location = LocationDTO.fromSqlRow(row);
                locations.add(location);
                locationsById.put(location.getId(), location);
            }
            LocationResult result = new LocationResult(locations);
            result.setOffset(0);
            result.setTotalLength(totalCount > results.getRows().size() ? totalCount : results.getRows().size());
            callback.onSuccess(result);
        }
    });
}
Also used : SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) SqlQuery(com.bedatadriven.rebar.sql.client.query.SqlQuery) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) List(java.util.List) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow) Map(java.util.Map) LocationDTO(org.activityinfo.legacy.shared.model.LocationDTO) LocationResult(org.activityinfo.legacy.shared.command.result.LocationResult)

Example 4 with LocationResult

use of org.activityinfo.legacy.shared.command.result.LocationResult in project activityinfo by bedatadriven.

the class GetLocationsHandler method execute.

@Override
public void execute(final GetLocations command, final ExecutionContext context, final AsyncCallback<LocationResult> callback) {
    if (!command.hasLocationIds() && command.getLocationTypeId() == null) {
        callback.onSuccess(new LocationResult());
        return;
    }
    final Map<Integer, LocationDTO> dtos = new HashMap<>();
    SqlQuery query = SqlQuery.select("locationID", "name", "axe", "x", "y", "workflowStatusId", "LocationTypeId").from(Tables.LOCATION, "Location");
    if (!command.getLocationIds().isEmpty()) {
        query.where("LocationId").in(command.getLocationIds());
    }
    if (command.getLocationTypeId() != null) {
        query.where("locationTypeId").equalTo(command.getLocationTypeId());
    }
    query.where("workflowstatusid").equalTo("validated");
    query.execute(context.getTransaction(), new SqlResultCallback() {

        @Override
        public void onSuccess(SqlTransaction tx, SqlResultSet results) {
            for (SqlResultSetRow row : results.getRows()) {
                final LocationDTO dto = new LocationDTO();
                dto.setId(row.getInt("locationID"));
                dto.setName(row.getString("name"));
                dto.setAxe(row.getString("axe"));
                dto.setWorkflowStatusId(row.getString("workflowStatusId"));
                dto.setLocationTypeId(row.getInt("LocationTypeId"));
                if (!row.isNull("x") && !row.isNull("y")) {
                    dto.setLatitude(row.getDouble("y"));
                    dto.setLongitude(row.getDouble("x"));
                }
                dtos.put(dto.getId(), dto);
            }
            SqlQuery query = SqlQuery.select().appendColumn("AdminEntity.AdminEntityId", "adminEntityId").appendColumn("AdminEntity.Name", "name").appendColumn("AdminEntity.AdminLevelId", "levelId").appendColumn("AdminEntity.AdminEntityParentId", "parentId").appendColumn("link.LocationID", "locationId").from(Tables.LOCATION_ADMIN_LINK, "link").leftJoin(Tables.ADMIN_ENTITY, "AdminEntity").on("link.AdminEntityId=AdminEntity.AdminEntityId").whereTrue("AdminEntity.AdminEntityId is not null");
            if (!command.getLocationIds().isEmpty()) {
                query.where("link.LocationId").in(command.getLocationIds());
            }
            if (command.getLocationTypeId() != null) {
                query.leftJoin(Tables.LOCATION, "Location").on("link.LocationId=Location.LocationId");
                query.where("Location.LocationTypeId").equalTo(command.getLocationTypeId());
            }
            query.execute(context.getTransaction(), new SqlResultCallback() {

                @Override
                public void onSuccess(SqlTransaction tx, SqlResultSet results) {
                    for (SqlResultSetRow row : results.getRows()) {
                        AdminEntityDTO entity = new AdminEntityDTO();
                        entity.setId(row.getInt("adminEntityId"));
                        entity.setName(row.getString("name"));
                        entity.setLevelId(row.getInt("levelId"));
                        if (!row.isNull("parentId")) {
                            entity.setParentId(row.getInt("parentId"));
                        }
                        LocationDTO dto = dtos.get(row.getInt("locationId"));
                        if (dto != null) {
                            dto.setAdminEntity(entity.getLevelId(), entity);
                        }
                    }
                    List<LocationDTO> list = new ArrayList<>(dtos.values());
                    callback.onSuccess(new LocationResult(list));
                }
            });
        }
    });
}
Also used : SqlQuery(com.bedatadriven.rebar.sql.client.query.SqlQuery) HashMap(java.util.HashMap) AdminEntityDTO(org.activityinfo.legacy.shared.model.AdminEntityDTO) ArrayList(java.util.ArrayList) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow) LocationResult(org.activityinfo.legacy.shared.command.result.LocationResult) SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback) LocationDTO(org.activityinfo.legacy.shared.model.LocationDTO)

Example 5 with LocationResult

use of org.activityinfo.legacy.shared.command.result.LocationResult in project activityinfo by bedatadriven.

the class ResourceLocatorAdaptorTest method persistLocation.

@Test
public void persistLocation() {
    FormInstance instance = new FormInstance(newLegacyFormInstanceId(HEALTH_CENTER_CLASS), HEALTH_CENTER_CLASS);
    instance.set(field(HEALTH_CENTER_CLASS, NAME_FIELD), "CS Ubuntu");
    instance.set(field(HEALTH_CENTER_CLASS, GEOMETRY_FIELD), new GeoPoint(-1, 13));
    instance.set(field(HEALTH_CENTER_CLASS, ADMIN_FIELD), entityRef(TERRITOIRE, IRUMU));
    assertResolves(locator.persist(instance));
    // ensure that everything worked out
    GetLocations query = new GetLocations(getLegacyIdFromCuid(instance.getId()));
    LocationResult result = execute(query);
    LocationDTO location = result.getData().get(0);
    assertThat(location.getName(), equalTo("CS Ubuntu"));
    assertThat(location.getAdminEntity(1).getName(), equalTo("Ituri"));
    assertThat(location.getAdminEntity(2).getName(), equalTo("Irumu"));
    assertThat(location.getLatitude(), equalTo(-1d));
    assertThat(location.getLongitude(), equalTo(13d));
    // remove location
    assertResolves(locator.remove(HEALTH_CENTER_CLASS, instance.getId()));
    // check whether location is removed
    result = execute(query);
    assertThat(result.getData(), IsEmptyCollection.empty());
}
Also used : GeoPoint(org.activityinfo.model.type.geo.GeoPoint) GetLocations(org.activityinfo.legacy.shared.command.GetLocations) FormInstance(org.activityinfo.model.form.FormInstance) LocationDTO(org.activityinfo.legacy.shared.model.LocationDTO) LocationResult(org.activityinfo.legacy.shared.command.result.LocationResult) Test(org.junit.Test)

Aggregations

LocationResult (org.activityinfo.legacy.shared.command.result.LocationResult)9 LocationDTO (org.activityinfo.legacy.shared.model.LocationDTO)6 GetLocations (org.activityinfo.legacy.shared.command.GetLocations)4 Test (org.junit.Test)4 SqlResultCallback (com.bedatadriven.rebar.sql.client.SqlResultCallback)2 SqlResultSet (com.bedatadriven.rebar.sql.client.SqlResultSet)2 SqlResultSetRow (com.bedatadriven.rebar.sql.client.SqlResultSetRow)2 SqlTransaction (com.bedatadriven.rebar.sql.client.SqlTransaction)2 SqlQuery (com.bedatadriven.rebar.sql.client.query.SqlQuery)2 ArrayList (java.util.ArrayList)2 AdminEntityDTO (org.activityinfo.legacy.shared.model.AdminEntityDTO)2 FormInstance (org.activityinfo.model.form.FormInstance)2 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Nullable (javax.annotation.Nullable)1 Filter (org.activityinfo.legacy.shared.command.Filter)1 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)1 Promise (org.activityinfo.promise.Promise)1