Search in sources :

Example 1 with SiteHistory

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);
    }
}
Also used : JSONObject(org.json.JSONObject) SiteHistory(org.activityinfo.server.database.hibernate.entity.SiteHistory) CommandException(org.activityinfo.legacy.shared.exception.CommandException)

Example 2 with SiteHistory

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);
                    }
                }
            }
        }
    }
}
Also used : ActivityMap(org.activityinfo.server.digest.activity.ActivityDigestModel.ActivityMap) PartnerActivityModel(org.activityinfo.server.digest.activity.ActivityDigestModel.PartnerActivityModel) DatabaseModel(org.activityinfo.server.digest.activity.ActivityDigestModel.DatabaseModel) User(org.activityinfo.server.database.hibernate.entity.User) SiteHistory(org.activityinfo.server.database.hibernate.entity.SiteHistory) Partner(org.activityinfo.server.database.hibernate.entity.Partner)

Example 3 with SiteHistory

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());
}
Also used : Query(javax.persistence.Query) SiteHistory(org.activityinfo.server.database.hibernate.entity.SiteHistory) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with SiteHistory

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);
    }
}
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 5 with SiteHistory

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);
}
Also used : SiteHistory(org.activityinfo.server.database.hibernate.entity.SiteHistory) Date(java.util.Date)

Aggregations

SiteHistory (org.activityinfo.server.database.hibernate.entity.SiteHistory)7 Query (javax.persistence.Query)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Date (java.util.Date)2 SiteResult (org.activityinfo.legacy.shared.command.result.SiteResult)2 SiteDTO (org.activityinfo.legacy.shared.model.SiteDTO)2 User (org.activityinfo.server.database.hibernate.entity.User)2 EntityManager (javax.persistence.EntityManager)1 SiteCommand (org.activityinfo.legacy.shared.command.SiteCommand)1 CommandException (org.activityinfo.legacy.shared.exception.CommandException)1 ActivityDTO (org.activityinfo.legacy.shared.model.ActivityDTO)1 Partner (org.activityinfo.server.database.hibernate.entity.Partner)1 Site (org.activityinfo.server.database.hibernate.entity.Site)1 ActivityMap (org.activityinfo.server.digest.activity.ActivityDigestModel.ActivityMap)1 DatabaseModel (org.activityinfo.server.digest.activity.ActivityDigestModel.DatabaseModel)1 PartnerActivityModel (org.activityinfo.server.digest.activity.ActivityDigestModel.PartnerActivityModel)1 Timed (org.activityinfo.server.util.monitoring.Timed)1 JSONObject (org.json.JSONObject)1