Search in sources :

Example 1 with Site

use of org.activityinfo.server.database.hibernate.entity.Site 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);
    }
}
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) SiteResult(org.activityinfo.legacy.shared.command.result.SiteResult) SiteCommand(org.activityinfo.legacy.shared.command.SiteCommand) SiteDTO(org.activityinfo.legacy.shared.model.SiteDTO) SiteHistory(org.activityinfo.server.database.hibernate.entity.SiteHistory) Timed(org.activityinfo.server.util.monitoring.Timed)

Example 2 with Site

use of org.activityinfo.server.database.hibernate.entity.Site in project activityinfo by bedatadriven.

the class UpdateSiteTest method testUpdatePartner.

@Test
public void testUpdatePartner() throws CommandException {
    // define changes for site id=2
    Map<String, Object> changes = new HashMap<String, Object>();
    changes.put("partnerId", 2);
    execute(new UpdateSite(2, changes));
    // assure that the change has been effected
    Site site = em.find(Site.class, 2);
    Assert.assertEquals("partnerId", 2, site.getPartner().getId());
}
Also used : UpdateSite(org.activityinfo.legacy.shared.command.UpdateSite) Site(org.activityinfo.server.database.hibernate.entity.Site) HashMap(java.util.HashMap) UpdateSite(org.activityinfo.legacy.shared.command.UpdateSite) Test(org.junit.Test)

Example 3 with Site

use of org.activityinfo.server.database.hibernate.entity.Site in project activityinfo by bedatadriven.

the class DeleteSiteHandler method execute.

@Override
public VoidResult execute(DeleteSite cmd, User user) throws CommandException {
    Site site = entityManager.find(Site.class, cmd.getSiteId());
    permissionOracle.assertEditAllowed(site, user);
    site.setDateDeleted(new Date());
    site.setVersion(site.getActivity().incrementSiteVersion());
    entityManager.createNativeQuery("update reportingperiod set deleted = 1 WHERE siteId = ?").setParameter(1, site.getId()).executeUpdate();
    logHistory(user, site);
    return VoidResult.EMPTY;
}
Also used : Site(org.activityinfo.server.database.hibernate.entity.Site) DeleteSite(org.activityinfo.legacy.shared.command.DeleteSite) Date(java.util.Date)

Example 4 with Site

use of org.activityinfo.server.database.hibernate.entity.Site in project activityinfo by bedatadriven.

the class SiteUpdateBuilderTest method subsequentCallsAreUpToDate.

@Test
@OnDataSet("/dbunit/sites-simple1.db.xml")
public void subsequentCallsAreUpToDate() throws Exception {
    User user = new User();
    user.setId(1);
    // update one of the sites so we have a realistic nano value type stamp
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    Site site = em.find(Site.class, 1);
    site.setComments("I'm slightly new");
    site.setDateEdited(new Date());
    em.getTransaction().commit();
    em.close();
    SyncRegionUpdate initialUpdate = builder.get().build(user, new GetSyncRegionUpdates("sites/1", null));
    assertThat(initialUpdate.isComplete(), equalTo(true));
    assertThat(initialUpdate.getSql(), not(nullValue()));
    assertThat(initialUpdate.getSql(), containsString("slightly new"));
    System.out.println(initialUpdate.getSql());
    // nothing has changed!
    SyncRegionUpdate subsequentUpdate = builder.get().build(user, new GetSyncRegionUpdates("sites/1", initialUpdate.getVersion()));
    assertThat(subsequentUpdate.isComplete(), equalTo(true));
    assertThat(subsequentUpdate.getSql(), nullValue());
    assertThat(subsequentUpdate.getVersion(), equalTo(initialUpdate.getVersion()));
}
Also used : Site(org.activityinfo.server.database.hibernate.entity.Site) GetSyncRegionUpdates(org.activityinfo.legacy.shared.command.GetSyncRegionUpdates) EntityManager(javax.persistence.EntityManager) User(org.activityinfo.server.database.hibernate.entity.User) SyncRegionUpdate(org.activityinfo.legacy.shared.command.result.SyncRegionUpdate) Date(java.util.Date) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Aggregations

Site (org.activityinfo.server.database.hibernate.entity.Site)4 Date (java.util.Date)3 EntityManager (javax.persistence.EntityManager)2 User (org.activityinfo.server.database.hibernate.entity.User)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 Query (javax.persistence.Query)1 DeleteSite (org.activityinfo.legacy.shared.command.DeleteSite)1 GetSyncRegionUpdates (org.activityinfo.legacy.shared.command.GetSyncRegionUpdates)1 SiteCommand (org.activityinfo.legacy.shared.command.SiteCommand)1 UpdateSite (org.activityinfo.legacy.shared.command.UpdateSite)1 SiteResult (org.activityinfo.legacy.shared.command.result.SiteResult)1 SyncRegionUpdate (org.activityinfo.legacy.shared.command.result.SyncRegionUpdate)1 SiteDTO (org.activityinfo.legacy.shared.model.SiteDTO)1 OnDataSet (org.activityinfo.server.database.OnDataSet)1 SiteHistory (org.activityinfo.server.database.hibernate.entity.SiteHistory)1 Timed (org.activityinfo.server.util.monitoring.Timed)1