Search in sources :

Example 1 with SiteCommand

use of org.activityinfo.shared.command.SiteCommand 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)

Aggregations

Date (java.util.Date)1 EntityManager (javax.persistence.EntityManager)1 Query (javax.persistence.Query)1 Site (org.activityinfo.server.database.hibernate.entity.Site)1 SiteHistory (org.activityinfo.server.database.hibernate.entity.SiteHistory)1 User (org.activityinfo.server.database.hibernate.entity.User)1 ChangeType (org.activityinfo.server.event.sitechange.ChangeType)1 SiteCommand (org.activityinfo.shared.command.SiteCommand)1 SiteResult (org.activityinfo.shared.command.result.SiteResult)1 SiteDTO (org.activityinfo.shared.dto.SiteDTO)1