use of org.activityinfo.legacy.shared.model.LocationDTO 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));
}
});
}
});
}
use of org.activityinfo.legacy.shared.model.LocationDTO 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());
}
use of org.activityinfo.legacy.shared.model.LocationDTO in project activityinfo by bedatadriven.
the class CreateLocationHandlerTest method test.
@Test
public void test() throws CommandException {
LocationDTO location = LocationDTOs.newLocation();
execute(new CreateLocation(location));
SearchLocations getLocations = new SearchLocations().setName(location.getName());
LocationResult locations = execute(getLocations);
LocationDTO newLocation = locations.getData().get(0);
assertEquals(location.getName(), newLocation.getName());
assertEquals(location.getAxe(), newLocation.getAxe());
assertEquals(location.getLongitude(), newLocation.getLongitude());
assertEquals(location.getLatitude(), newLocation.getLatitude());
}
use of org.activityinfo.legacy.shared.model.LocationDTO in project activityinfo by bedatadriven.
the class LocationsResource method query.
@GET
@Timed(name = "api.rest.locations.get")
@Produces(MediaType.APPLICATION_JSON)
public Response query(@QueryParam("type") int typeId) throws IOException {
GetLocations query = new GetLocations();
query.setLocationTypeId(typeId);
LocationResult result = dispatcher.execute(query);
StringWriter writer = new StringWriter();
JsonGenerator json = Jackson.createJsonFactory(writer);
json.writeStartArray();
for (LocationDTO location : result.getData()) {
json.writeStartObject();
json.writeNumberField("id", location.getId());
json.writeStringField("name", location.getName());
if (location.hasAxe()) {
json.writeStringField("code", location.getAxe());
}
if (location.hasCoordinates()) {
json.writeNumberField("latitude", location.getLatitude());
json.writeNumberField("longitude", location.getLongitude());
}
if (!location.getAdminEntities().isEmpty()) {
json.writeObjectFieldStart("adminEntities");
for (AdminEntityDTO entity : location.getAdminEntities()) {
json.writeFieldName(Integer.toString(entity.getLevelId()));
json.writeStartObject();
json.writeNumberField("id", entity.getId());
json.writeStringField("name", entity.getName());
json.writeEndObject();
}
json.writeEndObject();
}
json.writeEndObject();
}
json.writeEndArray();
json.close();
return Response.ok(writer.toString()).type(MediaType.APPLICATION_JSON_TYPE).build();
}
use of org.activityinfo.legacy.shared.model.LocationDTO in project activityinfo by bedatadriven.
the class SiteDialog method showExisting.
public void showExisting(SiteDTO site, SiteDialogCallback callback) {
this.newSite = false;
this.site = site;
this.callback = callback;
LocationDTO location = site.getLocation();
location.setLocationTypeId(activity.getLocationTypeId());
locationForm.updateForm(location, false);
updateForms(site, false);
show();
}
Aggregations