use of org.activityinfo.legacy.shared.command.result.SiteResult in project activityinfo by bedatadriven.
the class SiteExporter method querySites.
private SiteResult querySites(ActivityFormDTO activity, Filter filter, int offset) {
Filter effectiveFilter = new Filter(filter);
effectiveFilter.addRestriction(DimensionType.Activity, activity.getId());
GetSites query = new GetSites();
query.setFilter(effectiveFilter);
query.setSortInfo(new SortInfo("date2", SortDir.DESC));
query.setOffset(offset);
query.setLimit(SITE_BATCH_SIZE);
if (activity.getReportingFrequency() == ActivityFormDTO.REPORT_MONTHLY) {
query.setFetchAllReportingPeriods(true);
query.setFetchLinks(false);
}
SiteResult result = context.execute(query);
if (result.getTotalLength() > 0) {
context.updateProgress(Math.min(1.0, (double) offset) / ((double) result.getTotalLength()));
}
return result;
}
use of org.activityinfo.legacy.shared.command.result.SiteResult in project activityinfo by bedatadriven.
the class SiteHistoryProcessor method process.
@Timed(name = "updates.site_history")
public void process(Command<?> cmd, final int userId, final int siteId) {
assert (cmd instanceof SiteCommand);
LOGGER.fine("persisting site history (site: " + siteId + ", user: " + userId + ")");
EntityManager em = entityManager.get();
// It's important to use getOnlyReference() here rather
// than find() becuase the site might not actually have
// been sent to the database at this point
Site site = em.getReference(Site.class, siteId);
User user = em.getReference(User.class, userId);
ChangeType type = ChangeType.getType(cmd);
if (!type.isNew()) {
Query q = em.createQuery("select count(*) from SiteHistory where site = :site");
q.setParameter("site", site);
Long count = (Long) q.getSingleResult();
if (count == 0) {
// update, but first entry -> repair history by adding baseline
// record with complete site json
LOGGER.fine("site is not new, but history was empty. Adding baseline record..");
SiteResult siteResult = dispatcher.execute(GetSites.byId(siteId));
SiteDTO siteDTO = siteResult.getData().get(0);
String fulljson = JsonUtil.encodeMap(siteDTO.getProperties()).toString();
SiteHistory baseline = new SiteHistory();
baseline.setSite(site);
baseline.setUser(user);
baseline.setJson(fulljson);
baseline.setTimeCreated(new Date().getTime());
baseline.setInitial(false);
persist(baseline);
}
}
String json = null;
if (type.isNewOrUpdate()) {
Map<String, Object> changeMap = ((SiteCommand) cmd).getProperties().getTransientMap();
if (!changeMap.isEmpty()) {
json = JsonUtil.encodeMap(changeMap).toString();
}
} else if (type.isDelete()) {
json = JSON_DELETE;
}
if (!Strings.isNullOrEmpty(json)) {
persistHistory(site, user, type, json);
}
}
use of org.activityinfo.legacy.shared.command.result.SiteResult in project activityinfo by bedatadriven.
the class ImportSimpleTest method test.
@Test
public void test() throws IOException {
FormTree formTree = assertResolves(locator.getFormTree(HOUSEHOLD_SURVEY_FORM_CLASS));
FormTreePrettyPrinter.print(formTree);
importModel = new ImportModel(formTree);
// Step 1: User pastes in data to import
PastedTable source = new PastedTable(Resources.toString(getResource(getClass(), "qis.csv"), Charsets.UTF_8));
source.parseAllRows();
assertThat(source.getRows().size(), equalTo(63));
importModel.setSource(source);
importer = new Importer(locator, formTree, FieldImportStrategies.get(JvmConverterFactory.get()));
dumpList("COLUMNS", source.getColumns());
// Step 2: User maps imported columns to FormFields
dumpList("FIELDS", importer.getImportTargets());
importModel.setColumnAction(columnIndex("MEMBER_NO_ADULT_FEMALE"), target("NumAdultMale"));
importModel.setColumnAction(columnIndex("MEMBER_NO_ADULT_FEMALE"), target("NumAdultFemale"));
importModel.setColumnAction(columnIndex("_CREATION_DATE"), target("Start Date"));
importModel.setColumnAction(columnIndex("_SUBMISSION_DATE"), target("End Date"));
importModel.setColumnAction(columnIndex("district name"), target("District Name"));
importModel.setColumnAction(columnIndex("upazila"), target("Upzilla Name"));
importModel.setColumnAction(columnIndex("Partner"), target("Partner Name"));
// Step 3: Validate for user
ValidatedRowTable validatedResult = assertResolves(importer.validateRows(importModel));
showValidationGrid(validatedResult);
assertResolves(importer.persist(importModel));
// VERIFY total count
SiteResult allResults = execute(new GetSites(Filter.filter().onActivity(1)));
assertThat(allResults.getData().size(), equalTo(63));
// AND... verify
Filter filter = new Filter();
filter.addRestriction(DimensionType.AdminLevel, MODHUPUR);
SiteResult sites = execute(new GetSites(filter));
assertThat(sites.getTotalLength(), equalTo(1));
SiteDTO site = sites.getData().get(0);
assertThat(site.getDate1(), equalTo(new LocalDate(2012, 12, 19)));
assertThat(site.getDate2(), equalTo(new LocalDate(2012, 12, 19)));
assertThat(site.getAdminEntity(3), not(nullValue()));
}
use of org.activityinfo.legacy.shared.command.result.SiteResult in project activityinfo by bedatadriven.
the class SyncIntegrationTest method run.
@Test
@OnDataSet("/dbunit/sites-simple1.db.xml")
public void run() throws SQLException, InterruptedException {
synchronize();
Collector<Date> lastUpdate = Collector.newCollector();
syncHistoryTable.get(lastUpdate);
assertThat(lastUpdate.getResult(), is(not(nullValue())));
assertThat(queryString("select Name from Indicator where IndicatorId=103"), equalTo("Nb. of distributions"));
assertThat(queryString("select Name from AdminLevel where AdminLevelId=1"), equalTo("Province"));
assertThat(queryString("select Name from AdminEntity where AdminEntityId=21"), equalTo("Irumu"));
assertThat(queryString("select Name from Location where LocationId=7"), equalTo("Shabunda"));
assertThat(queryInt("select value from IndicatorValue where ReportingPeriodId=601 and IndicatorId=6"), equalTo(35));
assertThat(queryInt("select PartnerId from partnerInDatabase where databaseid=2"), equalTo(1));
assertThat(queryInt("select AttributeGroupId from AttributeGroupInActivity where ActivityId=2"), equalTo(1));
assertThat(queryInt("select count(*) from LockedPeriod"), equalTo(4));
assertThat(queryInt("select count(*) from LockedPeriod where ProjectId is not null"), equalTo(1));
assertThat(queryInt("select count(*) from LockedPeriod where ActivityId is not null"), equalTo(1));
assertThat(queryInt("select count(*) from LockedPeriod where ProjectId is null and ActivityId is null"), equalTo(2));
// / now try updating a site remotely (from another client)
// and veryify that we get the update after we synchronized
Thread.sleep(1000);
SiteDTO s1 = executeLocally(GetSites.byId(1)).getData().get(0);
assertThat((Double) s1.getIndicatorValue(1), equalTo(1500d));
Map<String, Object> changes = Maps.newHashMap();
changes.put(AttributeDTO.getPropertyName(1), true);
changes.put("comments", "newComments");
executeRemotely(new UpdateSite(1, changes));
synchronize();
s1 = executeLocally(GetSites.byId(1)).getData().get(0);
assertThat(s1.getAttributeValue(1), equalTo(true));
assertThat(s1.getAttributeValue(2), equalTo(false));
assertThat(s1.getComments(), equalTo("newComments"));
// old values are preserved...
assertThat(s1.getIndicatorDoubleValue(1), equalTo(1500d));
// Try deleting a site
executeRemotely(new Delete("Site", 1));
synchronize();
SiteResult siteResult = executeLocally(GetSites.byId(1));
assertThat(siteResult.getData().size(), equalTo(0));
// Verify that we haven't toasted the other data
SiteDTO site = executeLocally(GetSites.byId(3)).getData().get(0);
assertThat(site.getIndicatorDoubleValue(1), equalTo(10000d));
}
use of org.activityinfo.legacy.shared.command.result.SiteResult in project activityinfo by bedatadriven.
the class GetSitesTest method duplicated.
@Test
@OnDataSet("/dbunit/sites-linked.db.xml")
public void duplicated() {
// A given site can be appear multiple times in the list if it is the
// source
// of one or more indicators
setUser(1);
GetSites cmd = new GetSites();
SiteResult result = execute(cmd);
assertThat(result.getData().size(), equalTo(3));
}
Aggregations