use of org.activityinfo.server.database.hibernate.entity.SiteHistory in project activityinfo by bedatadriven.
the class DeleteSiteHandler method logHistory.
private void logHistory(User user, Site site) {
try {
JSONObject change = new JSONObject();
change.put("type", "Boolean");
change.put("value", true);
JSONObject changeSet = new JSONObject();
changeSet.put("_DELETE", change);
SiteHistory history = new SiteHistory();
history.setUser(user);
history.setSite(site);
history.setInitial(false);
history.setTimeCreated(System.currentTimeMillis());
history.setJson(changeSet.toString());
entityManager.persist(history);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.activityinfo.server.database.hibernate.entity.SiteHistory in project activityinfo by bedatadriven.
the class ActivityDigestModelBuilder method createDatabaseModel.
private void createDatabaseModel(ActivityDigestModel model, Database database) {
SiteHistory lastEdit = findLastEdit(database);
// only include databases that are known to be edited at least once
if (lastEdit != null) {
DatabaseModel databaseModel = new DatabaseModel(model, database, lastEdit);
List<SiteHistory> ownerHistories = findSiteHistory(databaseModel, database.getOwner());
ActivityMap ownerActivityMap = new ActivityMap(databaseModel, database.getOwner(), ownerHistories);
databaseModel.setOwnerActivityMap(ownerActivityMap);
List<Partner> partners = findPartners(databaseModel);
LOGGER.finest("building user activity digest for user " + model.getUserDigest().getUser().getId() + " and database " + database.getId() + " - found " + partners.size() + " partner(s)");
if (!partners.isEmpty()) {
for (Partner partner : partners) {
PartnerActivityModel partnerModel = new PartnerActivityModel(databaseModel, partner);
List<User> partnerUsers = findUsers(partnerModel);
LOGGER.finest("found users " + partnerUsers + " for partner " + partner.getName());
if (!partnerUsers.isEmpty()) {
for (User partnerUser : partnerUsers) {
List<SiteHistory> histories = findSiteHistory(databaseModel, partnerUser);
ActivityMap activityMap = new ActivityMap(databaseModel, partnerUser, histories);
partnerModel.addActivityMap(activityMap);
}
}
}
}
}
}
use of org.activityinfo.server.database.hibernate.entity.SiteHistory in project activityinfo by bedatadriven.
the class GeoDigestRenderer method findSiteHistory.
/**
* @param siteId
* @param from
* @param from
* @return the sitehistory edited since the specified timestamp (milliseconds) and linked to the specified database
* and user. The resulting list is grouped by user, keeping the last created sitehistory entry per user.
*/
@VisibleForTesting
@SuppressWarnings("unchecked")
List<SiteHistory> findSiteHistory(Integer siteId, long from) {
Query query = entityManager.get().createQuery("select distinct h from SiteHistory h " + "where h.site.id = :siteId and h.timeCreated >= :from " + "order by h.timeCreated");
query.setParameter("siteId", siteId);
query.setParameter("from", from);
List<SiteHistory> list = query.getResultList();
if (list.isEmpty()) {
return list;
}
Map<Integer, SiteHistory> map = new HashMap<Integer, SiteHistory>();
for (SiteHistory siteHistory : list) {
SiteHistory old = map.get(siteHistory.getUser().getId());
if (old == null || old.getTimeCreated() <= siteHistory.getTimeCreated()) {
map.put(siteHistory.getUser().getId(), siteHistory);
}
}
return new ArrayList<SiteHistory>(map.values());
}
use of org.activityinfo.server.database.hibernate.entity.SiteHistory 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.SiteHistory in project activityinfo by bedatadriven.
the class SiteHistoryProcessor method persistHistory.
public void persistHistory(Site site, User user, ChangeType type, String json) {
SiteHistory history = new SiteHistory();
history.setSite(site);
history.setUser(user);
history.setJson(json);
history.setTimeCreated(new Date().getTime());
history.setInitial(type.isNew());
persist(history);
}
Aggregations