Search in sources :

Example 1 with SiteResult

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);
    }
}
Also used : SiteResult(org.activityinfo.shared.command.result.SiteResult) AsyncCallback(com.google.gwt.user.client.rpc.AsyncCallback) PivotContent(org.activityinfo.shared.report.content.PivotContent) GetSites(org.activityinfo.shared.command.GetSites) GenerateElement(org.activityinfo.shared.command.GenerateElement) SearchResult(org.activityinfo.shared.command.result.SearchResult) PivotTableReportElement(org.activityinfo.shared.report.model.PivotTableReportElement)

Example 2 with SiteResult

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();
        }
    }
}
Also used : User(org.activityinfo.server.database.hibernate.entity.User) SiteResult(org.activityinfo.shared.command.result.SiteResult) UserDatabaseDTO(org.activityinfo.shared.dto.UserDatabaseDTO) SiteDTO(org.activityinfo.shared.dto.SiteDTO) ActivityDTO(org.activityinfo.shared.dto.ActivityDTO) SchemaDTO(org.activityinfo.shared.dto.SchemaDTO) GetSchema(org.activityinfo.shared.command.GetSchema) Date(java.util.Date) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with SiteResult

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);
    }
}
Also used : Site(org.activityinfo.server.database.hibernate.entity.Site) User(org.activityinfo.server.database.hibernate.entity.User) Query(javax.persistence.Query) Date(java.util.Date) EntityManager(javax.persistence.EntityManager) ChangeType(org.activityinfo.server.event.sitechange.ChangeType) SiteResult(org.activityinfo.shared.command.result.SiteResult) SiteCommand(org.activityinfo.shared.command.SiteCommand) SiteDTO(org.activityinfo.shared.dto.SiteDTO) SiteHistory(org.activityinfo.server.database.hibernate.entity.SiteHistory)

Example 4 with SiteResult

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()));
        }
    });
}
Also used : Filter(org.activityinfo.shared.command.Filter) SiteResult(org.activityinfo.shared.command.result.SiteResult) GetSites(org.activityinfo.shared.command.GetSites)

Example 5 with SiteResult

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));
}
Also used : Delete(org.activityinfo.shared.command.Delete) SiteResult(org.activityinfo.shared.command.result.SiteResult) SiteDTO(org.activityinfo.shared.dto.SiteDTO) UpdateSite(org.activityinfo.shared.command.UpdateSite) Date(java.util.Date) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Aggregations

SiteResult (org.activityinfo.shared.command.result.SiteResult)19 Test (org.junit.Test)12 SiteDTO (org.activityinfo.shared.dto.SiteDTO)11 GetSites (org.activityinfo.shared.command.GetSites)10 OnDataSet (org.activityinfo.server.database.OnDataSet)6 Date (java.util.Date)4 Filter (org.activityinfo.shared.command.Filter)4 SortInfo (com.extjs.gxt.ui.client.data.SortInfo)3 DispatcherSync (org.activityinfo.server.command.DispatcherSync)3 User (org.activityinfo.server.database.hibernate.entity.User)2 CreateSite (org.activityinfo.shared.command.CreateSite)2 GetSchema (org.activityinfo.shared.command.GetSchema)2 UpdateSite (org.activityinfo.shared.command.UpdateSite)2 CreateResult (org.activityinfo.shared.command.result.CreateResult)2 ActivityDTO (org.activityinfo.shared.dto.ActivityDTO)2 SchemaDTO (org.activityinfo.shared.dto.SchemaDTO)2 UserDatabaseDTO (org.activityinfo.shared.dto.UserDatabaseDTO)2 TableColumn (org.activityinfo.shared.report.model.TableColumn)2 SqlResultCallback (com.bedatadriven.rebar.sql.client.SqlResultCallback)1 SqlResultSet (com.bedatadriven.rebar.sql.client.SqlResultSet)1