Search in sources :

Example 6 with AdminEntityDTO

use of org.activityinfo.shared.dto.AdminEntityDTO in project activityinfo by bedatadriven.

the class GetAdminEntitiesHandler method execute.

@Override
public void execute(GetAdminEntities cmd, ExecutionContext context, final AsyncCallback<AdminEntityResult> callback) {
    SqlQuery query = SqlQuery.select("AdminEntity.adminEntityId", "AdminEntity.name", "AdminEntity.adminLevelId", "AdminEntity.adminEntityParentId", "x1", "y1", "x2", "y2").from(Tables.ADMIN_ENTITY, "AdminEntity").whereTrue("not AdminEntity.deleted");
    if (CollectionUtil.isNotEmpty(cmd.getCountryIds())) {
        query.leftJoin(Tables.ADMIN_LEVEL, "AdminLevel").on("AdminLevel.AdminLevelId=AdminEntity.AdminLevelId");
        query.where("AdminLevel.CountryId").in(cmd.getCountryIds());
        if (cmd.getParentId() == null && cmd.getLevelId() == null) {
            query.where("AdminLevel.ParentId is null");
        }
        query.orderBy("AdminLevel.CountryId");
    }
    query.orderBy("AdminEntity.name");
    if (cmd.getLevelId() != null) {
        query.where("AdminEntity.AdminLevelId").equalTo(cmd.getLevelId());
    }
    if (cmd.getParentId() != null) {
        query.where("AdminEntity.AdminEntityParentId").equalTo(cmd.getParentId());
    }
    if (cmd.getFilter() != null && cmd.getFilter().isRestricted(DimensionType.Activity)) {
        SqlQuery subQuery = SqlQuery.select("link.AdminEntityId").from(Tables.SITE, "site").leftJoin(Tables.LOCATION, "Location").on("Location.LocationId = site.LocationId").leftJoin(Tables.LOCATION_ADMIN_LINK, "link").on("link.LocationId = Location.LocationId").where("site.ActivityId").in(cmd.getFilter().getRestrictions(DimensionType.Activity));
        query.where("AdminEntity.AdminEntityId").in(subQuery);
    }
    if (cmd.getFilter() != null && cmd.getFilter().isRestricted(DimensionType.AdminLevel)) {
        if (cmd.getLevelId() == null) {
            query.where("AdminEntityId").in(cmd.getFilter().getRestrictions(DimensionType.AdminLevel));
        } else {
            SqlQuery subQuery = SqlQuery.select("adminEntityId").from(Tables.ADMIN_ENTITY, "AdminEntity").where("AdminLevelId").equalTo(cmd.getLevelId()).where("AdminEntityId").in(cmd.getFilter().getRestrictions(DimensionType.AdminLevel));
            query.where("AdminEntity.AdminEntityId").in(subQuery);
        }
    }
    query.execute(context.getTransaction(), new SqlResultCallback() {

        @Override
        public void onSuccess(SqlTransaction tx, SqlResultSet results) {
            final List<AdminEntityDTO> entities = new ArrayList<AdminEntityDTO>();
            for (SqlResultSetRow row : results.getRows()) {
                entities.add(toEntity(row));
            }
            callback.onSuccess(new AdminEntityResult(entities));
        }
    });
}
Also used : SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) SqlQuery(com.bedatadriven.rebar.sql.client.query.SqlQuery) AdminEntityDTO(org.activityinfo.shared.dto.AdminEntityDTO) AdminEntityResult(org.activityinfo.shared.command.result.AdminEntityResult) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) ArrayList(java.util.ArrayList) List(java.util.List) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow)

Example 7 with AdminEntityDTO

use of org.activityinfo.shared.dto.AdminEntityDTO in project activityinfo by bedatadriven.

the class GetAdminEntitiesHandler method toEntity.

public static AdminEntityDTO toEntity(SqlResultSetRow row) {
    AdminEntityDTO entity = new AdminEntityDTO();
    entity.setId(row.getInt("adminEntityId"));
    entity.setName(row.getString("name"));
    entity.setLevelId(row.getInt("adminLevelId"));
    if (!row.isNull("adminEntityParentId")) {
        entity.setParentId(row.getInt("adminEntityParentId"));
    }
    Extents bounds = Extents.empty();
    if (!row.isNull("x1")) {
        bounds.setMinLon(row.getDouble("x1"));
        bounds.setMinLat(row.getDouble("y1"));
        bounds.setMaxLon(row.getDouble("x2"));
        bounds.setMaxLat(row.getDouble("y2"));
        entity.setBounds(bounds);
    }
    return entity;
}
Also used : AdminEntityDTO(org.activityinfo.shared.dto.AdminEntityDTO) Extents(org.activityinfo.shared.util.mapping.Extents)

Example 8 with AdminEntityDTO

use of org.activityinfo.shared.dto.AdminEntityDTO in project activityinfo by bedatadriven.

the class GetLocationsHandler method execute.

@Override
public void execute(final GetLocations command, final ExecutionContext context, final AsyncCallback<GetLocationsResult> callback) {
    if (command.hasLocationIds()) {
        final Map<Integer, LocationDTO> dtos = new HashMap<Integer, LocationDTO>();
        SqlQuery.select("locationID", "name", "axe", "x", "y").from(Tables.LOCATION).where("locationId").in(command.getLocationIds()).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"));
                    if (!row.isNull("x") && !row.isNull("y")) {
                        dto.setLatitude(row.getDouble("y"));
                        dto.setLongitude(row.getDouble("x"));
                    }
                    dtos.put(dto.getId(), dto);
                }
                SqlQuery.select().appendColumn("AdminEntity.AdminEntityId", "adminEntityId").appendColumn("AdminEntity.Name", "name").appendColumn("AdminEntity.AdminLevelId", "levelId").appendColumn("link.LocationID", "locationId").from(Tables.LOCATION_ADMIN_LINK, "link").leftJoin(Tables.ADMIN_ENTITY, "AdminEntity").on("link.AdminEntityId=AdminEntity.AdminEntityId").where("link.LocationId").in(command.getLocationIds()).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"));
                            LocationDTO dto = dtos.get(row.getInt("locationId"));
                            if (dto != null) {
                                dto.setAdminEntity(entity.getLevelId(), entity);
                            }
                        }
                        List<LocationDTO> list = new ArrayList<LocationDTO>(dtos.values());
                        callback.onSuccess(new GetLocationsResult(list));
                    }
                });
            }
        });
    } else {
        callback.onSuccess(new GetLocationsResult());
    }
}
Also used : HashMap(java.util.HashMap) AdminEntityDTO(org.activityinfo.shared.dto.AdminEntityDTO) ArrayList(java.util.ArrayList) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow) GetLocationsResult(org.activityinfo.shared.command.GetLocations.GetLocationsResult) SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback) LocationDTO(org.activityinfo.shared.dto.LocationDTO)

Example 9 with AdminEntityDTO

use of org.activityinfo.shared.dto.AdminEntityDTO in project activityinfo by bedatadriven.

the class GetSitesHandler method joinEntities.

private void joinEntities(SqlTransaction tx, final Multimap<Integer, SiteDTO> siteMap) {
    Log.trace("Starting joinEntities()");
    SqlQuery.select("site.SiteId", "Link.adminEntityId", "e.name", "e.adminLevelId", "e.adminEntityParentId", "x1", "y1", "x2", "y2").from(Tables.SITE).innerJoin(Tables.LOCATION).on("location.LocationId = site.LocationId").innerJoin(Tables.LOCATION_ADMIN_LINK, "Link").on("Link.LocationId = location.LocationId").innerJoin(Tables.ADMIN_ENTITY, "e").on("Link.AdminEntityId = e.AdminEntityId").where("site.SiteId").in(siteMap.keySet()).execute(tx, new SqlResultCallback() {

        @Override
        public void onSuccess(SqlTransaction tx, SqlResultSet results) {
            Log.trace("Received results for joinEntities()");
            Map<Integer, AdminEntityDTO> entities = Maps.newHashMap();
            for (SqlResultSetRow row : results.getRows()) {
                int adminEntityId = row.getInt("adminEntityId");
                AdminEntityDTO entity = entities.get(adminEntityId);
                if (entity == null) {
                    entity = GetAdminEntitiesHandler.toEntity(row);
                    entities.put(adminEntityId, entity);
                }
                for (SiteDTO site : siteMap.get(row.getInt("SiteId"))) {
                    site.setAdminEntity(entity.getLevelId(), entity);
                }
            }
            Log.trace("Done populating results for joinEntities");
        }
    });
}
Also used : SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) AdminEntityDTO(org.activityinfo.shared.dto.AdminEntityDTO) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) SiteDTO(org.activityinfo.shared.dto.SiteDTO) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow) Map(java.util.Map)

Example 10 with AdminEntityDTO

use of org.activityinfo.shared.dto.AdminEntityDTO in project activityinfo by bedatadriven.

the class GetAdminEntitiesHandlerTest method testChildQuery.

@Test
public void testChildQuery() throws Exception {
    GetAdminEntities cmd = new GetAdminEntities(2, 2);
    AdminEntityResult result = execute(cmd);
    assertThat(result.getData().size(), equalTo(3));
    AdminEntityDTO kalehe = result.getData().get(0);
    assertThat(kalehe.getName(), equalTo("Kalehe"));
    assertThat(kalehe.getBounds(), is(not(nullValue())));
    assertThat(kalehe.getBounds().getMinLon(), equalTo(-44d));
    assertThat(kalehe.getBounds().getMinLat(), equalTo(-22d));
    assertThat(kalehe.getBounds().getMaxLon(), equalTo(33.5d));
    assertThat(kalehe.getBounds().getMaxLat(), equalTo(40d));
}
Also used : AdminEntityDTO(org.activityinfo.shared.dto.AdminEntityDTO) AdminEntityResult(org.activityinfo.shared.command.result.AdminEntityResult) GetAdminEntities(org.activityinfo.shared.command.GetAdminEntities) Test(org.junit.Test)

Aggregations

AdminEntityDTO (org.activityinfo.shared.dto.AdminEntityDTO)21 ArrayList (java.util.ArrayList)4 AdminEntityResult (org.activityinfo.shared.command.result.AdminEntityResult)4 SqlResultCallback (com.bedatadriven.rebar.sql.client.SqlResultCallback)3 SqlResultSet (com.bedatadriven.rebar.sql.client.SqlResultSet)3 SqlResultSetRow (com.bedatadriven.rebar.sql.client.SqlResultSetRow)3 SqlTransaction (com.bedatadriven.rebar.sql.client.SqlTransaction)3 GetAdminEntities (org.activityinfo.shared.command.GetAdminEntities)3 AdminLevelDTO (org.activityinfo.shared.dto.AdminLevelDTO)3 SiteDTO (org.activityinfo.shared.dto.SiteDTO)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 AdminEntity (org.activityinfo.server.database.hibernate.entity.AdminEntity)2 AdminLevel (org.activityinfo.server.database.hibernate.entity.AdminLevel)2 Filter (org.activityinfo.shared.command.Filter)2 LocationDTO (org.activityinfo.shared.dto.LocationDTO)2 SqlQuery (com.bedatadriven.rebar.sql.client.query.SqlQuery)1 LabelField (com.extjs.gxt.ui.client.widget.form.LabelField)1 FitLayout (com.extjs.gxt.ui.client.widget.layout.FitLayout)1 SafeHtmlBuilder (com.google.gwt.safehtml.shared.SafeHtmlBuilder)1