use of org.activityinfo.shared.dto.SiteDTO in project activityinfo by bedatadriven.
the class GetSitesHandler method doQuery.
private void doQuery(final GetSites command, final ExecutionContext context, final AsyncCallback<SiteResult> callback) {
// in order to pull in the linked queries, we want to
// to create two queries that we union together.
// for performance reasons, we want to apply all of the joins
// and filters on both parts of the union query
SqlQuery unioned = unionedQuery(context, command);
unioned.appendAllColumns();
if (isMySql()) {
// with this feature, MySQL will keep track of the total
// number of rows regardless of our limit statement.
// This way we don't have to execute the query twice to
// get the total count
//
// unfortunately, this is not available on sqlite
unioned.appendKeyword("SQL_CALC_FOUND_ROWS");
}
applySort(unioned, command.getSortInfo());
applyPaging(unioned, command);
final Multimap<Integer, SiteDTO> siteMap = HashMultimap.create();
final List<SiteDTO> sites = new ArrayList<SiteDTO>();
final SiteResult result = new SiteResult(sites);
result.setOffset(command.getOffset());
Log.trace("About to execute primary query");
unioned.execute(context.getTransaction(), new SqlResultCallback() {
@Override
public void onSuccess(SqlTransaction tx, SqlResultSet results) {
if (command.getLimit() <= 0) {
result.setTotalLength(results.getRows().size());
} else {
queryTotalLength(tx, command, context, result);
}
Log.trace("Primary query returned, starting to add to map");
for (SqlResultSetRow row : results.getRows()) {
SiteDTO site = toSite(row);
sites.add(site);
siteMap.put(site.getId(), site);
}
Log.trace("Finished adding to map");
if (!sites.isEmpty()) {
if (command.isFetchAdminEntities()) {
joinEntities(tx, siteMap);
}
if (command.fetchAnyIndicators()) {
joinIndicatorValues(command, tx, siteMap);
}
if (command.isFetchAttributes()) {
joinAttributeValues(tx, siteMap);
}
}
callback.onSuccess(result);
}
});
}
use of org.activityinfo.shared.dto.SiteDTO in project activityinfo by bedatadriven.
the class GetSitesHandler method joinAttributeValues.
private void joinAttributeValues(SqlTransaction tx, final Multimap<Integer, SiteDTO> siteMap) {
Log.trace("Starting joinAttributeValues() ");
SqlQuery.select().appendColumn("v.AttributeId", "attributeId").appendColumn("a.Name", "attributeName").appendColumn("v.Value", "value").appendColumn("v.SiteId", "siteId").appendColumn("g.name", "groupName").from(Tables.ATTRIBUTE_VALUE, "v").leftJoin(Tables.ATTRIBUTE, "a").on("v.AttributeId = a.AttributeId").leftJoin(Tables.ATTRIBUTE_GROUP, "g").on("a.AttributeGroupId=g.AttributeGroupId").where("v.SiteId").in(siteMap.keySet()).orderBy("groupName, attributeName").execute(tx, new SqlResultCallback() {
@Override
public void onSuccess(SqlTransaction tx, SqlResultSet results) {
Log.trace("Received results for joinAttributeValues() ");
for (SqlResultSetRow row : results.getRows()) {
int attributeId = row.getInt("attributeId");
boolean value = row.getBoolean("value");
String groupName = row.getString("groupName");
String attributeName = row.getString("attributeName");
for (SiteDTO site : siteMap.get(row.getInt("siteId"))) {
site.setAttributeValue(attributeId, value);
if (value) {
site.addDisplayAttribute(groupName, attributeName);
}
}
}
Log.trace("Done populating results for joinAttributeValues()");
}
});
}
use of org.activityinfo.shared.dto.SiteDTO in project activityinfo by bedatadriven.
the class GetSitesHandler method toSite.
private SiteDTO toSite(SqlResultSetRow row) {
SiteDTO model = new SiteDTO();
model.setId(row.getInt("SiteId"));
model.setLinked(row.getBoolean("Linked"));
model.setActivityId(row.getInt("ActivityId"));
model.setDate1(row.getDate("Date1"));
model.setDate2(row.getDate("Date2"));
model.setDateCreated(row.getDate("DateCreated"));
model.setTimeEdited(row.getDouble("TimeEdited"));
model.setLocationId(row.getInt("LocationId"));
model.setLocationName(row.getString("LocationName"));
model.setLocationAxe(row.getString("LocationAxe"));
if (!row.isNull("x") && !row.isNull("y")) {
model.setX(row.getDouble("x"));
model.setY(row.getDouble("y"));
}
model.setComments(row.getString("Comments"));
PartnerDTO partner = new PartnerDTO();
partner.setId(row.getInt("PartnerId"));
partner.setName(row.getString("PartnerName"));
if (!row.isNull("ProjectId") && row.isNull("ProjectDateDeleted")) {
ProjectDTO project = new ProjectDTO();
project.setId(row.getInt("ProjectId"));
project.setName(row.getString("ProjectName"));
model.setProject(project);
}
model.setPartner(partner);
return model;
}
use of org.activityinfo.shared.dto.SiteDTO in project activityinfo by bedatadriven.
the class DeleteTest method testDeleteSite.
@Test
public void testDeleteSite() throws CommandException {
PagingResult<SiteDTO> sites = execute(GetSites.byId(3));
execute(new Delete(sites.getData().get(0)));
sites = execute(GetSites.byId(3));
Assert.assertEquals(0, sites.getData().size());
sites = execute(new GetSites());
Assert.assertNull(getById(sites.getData(), 3));
}
use of org.activityinfo.shared.dto.SiteDTO in project activityinfo by bedatadriven.
the class CreateSiteTest method testAdminBoundCreate.
@Test
@Ignore("WIP")
public void testAdminBoundCreate() throws CommandException {
// create a new detached, client model
SiteDTO newSite = new SiteDTO();
newSite.setActivityId(4);
newSite.setPartner(new PartnerDTO(1, "Foobar"));
newSite.setDate1((new GregorianCalendar(2008, 12, 1)).getTime());
newSite.setDate2((new GregorianCalendar(2009, 1, 3)).getTime());
newSite.setAdminEntity(1, new AdminEntityDTO(1, 2, "Sud Kivu"));
newSite.setAdminEntity(2, new AdminEntityDTO(2, 11, "Walungu"));
newSite.setAdminEntity(3, null);
newSite.setX(27.432);
newSite.setY(1.23);
newSite.setComments("huba huba");
newSite.setProject(new ProjectDTO(1, "SomeProject"));
// create command
CreateSite cmd = new CreateSite(newSite);
// execute the command
setUser(1);
newSite.setProject(new ProjectDTO(1, "SomeProject"));
CreateResult result = execute(cmd);
newSite.setId(result.getNewId());
// try to retrieve what we've created
PagingLoadResult<SiteDTO> loadResult = execute(GetSites.byId(newSite.getId()));
Assert.assertEquals(1, loadResult.getData().size());
SiteDTO secondRead = loadResult.getData().get(0);
// confirm that the changes are there
Assert.assertEquals("site.location.name", "Walungu", secondRead.getLocationName());
}
Aggregations