use of org.activityinfo.shared.command.result.SiteResult in project activityinfo by bedatadriven.
the class SearchHandler method processFilter.
private void processFilter(final ExecutionContext context, final AsyncCallback<SearchResult> callback, final Filter resultFilter) {
final SearchResult searchResult = new SearchResult();
if (resultFilter.getRestrictedDimensions().size() > 0) {
// pivot data query
final PivotTableReportElement pivotTable = createSearchPivotTableElement();
pivotTable.setFilter(resultFilter);
GenerateElement<PivotContent> zmd = new GenerateElement<PivotContent>(pivotTable);
context.execute(zmd, new AsyncCallback<PivotContent>() {
@Override
public void onFailure(Throwable caught) {
callback.onFailure(caught);
}
@Override
public void onSuccess(PivotContent content) {
content.setEffectiveFilter(resultFilter);
searchResult.setPivotTabelData(content);
// recent sites query
GetSites getSites = createGetSitesCommand(resultFilter);
context.execute(getSites, new AsyncCallback<SiteResult>() {
@Override
public void onFailure(Throwable caught) {
callback.onFailure(caught);
}
@Override
public void onSuccess(SiteResult resultSites) {
searchResult.setRecentAdditions(resultSites.getData());
callback.onSuccess(searchResult);
}
});
}
});
} else {
// Return empty searchresult when no filtered entities found
callback.onSuccess(searchResult);
}
}
use of org.activityinfo.shared.command.result.SiteResult in project activityinfo by bedatadriven.
the class SiteChangeServlet method sendNotifications.
@VisibleForTesting
void sendNotifications(int editorUserId, int siteId, ChangeType type) {
User user = entityManager.get().find(User.class, editorUserId);
/*
* For our purposes, the user who initiated the change will be
* considered the authenticated user for this thread
*/
authProvider.set(user);
SiteResult siteResult = dispatcher.execute(GetSites.byId(siteId));
SiteDTO siteDTO = siteResult.getData().get(0);
SchemaDTO schemaDTO = dispatcher.execute(new GetSchema());
ActivityDTO activityDTO = schemaDTO.getActivityById(siteDTO.getActivityId());
UserDatabaseDTO userDatabaseDTO = activityDTO.getDatabase();
Date date = new Date();
List<User> recipients = findRecipients(userDatabaseDTO.getId());
for (User recipient : recipients) {
try {
// themselves!
if (recipient.getId() != editorUserId) {
LOGGER.info("sending sitechange notification email to " + recipient.getEmail());
UpdateMessageBuilder message = new UpdateMessageBuilder();
message.setDate(date);
message.setEditor(user);
message.setRecipient(recipient);
message.setUserDatabaseDTO(userDatabaseDTO);
message.setSiteDTO(siteDTO);
message.setActivityDTO(activityDTO);
message.setChangeType(type);
mailSender.get().send(message.build());
}
} catch (Throwable t) {
LOGGER.warning("failed sending notification email to " + recipient.getName() + " <" + recipient.getEmail() + ">: " + t.getMessage());
t.printStackTrace();
}
}
}
use of org.activityinfo.shared.command.result.SiteResult in project activityinfo by bedatadriven.
the class SiteHistoryProcessor method process.
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 getReference() 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 (StringUtils.isNotBlank(json)) {
persistHistory(site, user, type, json);
}
}
use of org.activityinfo.shared.command.result.SiteResult in project activityinfo by bedatadriven.
the class GetSitePointsHandler method execute.
@Override
public void execute(GetSitePoints command, ExecutionContext context, final AsyncCallback<SitePointList> callback) {
Filter filter = new Filter();
if (command.getActivityId() != 0) {
filter.addRestriction(DimensionType.Activity, command.getActivityId());
}
context.execute(new GetSites(filter), new AsyncCallback<SiteResult>() {
@Override
public void onFailure(Throwable caught) {
callback.onFailure(caught);
}
@Override
public void onSuccess(SiteResult result) {
callback.onSuccess(toPointList(result.getData()));
}
});
}
use of org.activityinfo.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 {
synchronizeFirstTime();
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 UserDatabaseId is not 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(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.getIndicatorValue(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.getIndicatorValue(1), equalTo(10000d));
}
Aggregations