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);
}
}
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());
}
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;
}
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()));
}
Aggregations