001: package com.technoetic.xplanner.history;
002:
003: import java.util.Date;
004: import java.util.List;
005:
006: import net.sf.hibernate.Hibernate;
007: import net.sf.hibernate.HibernateException;
008: import net.sf.hibernate.Session;
009: import org.apache.commons.beanutils.PropertyUtils;
010: import org.apache.commons.lang.StringUtils;
011: import org.apache.log4j.Logger;
012:
013: import com.technoetic.xplanner.db.hibernate.ThreadSession;
014: import com.technoetic.xplanner.domain.DomainMetaDataRepository;
015: import com.technoetic.xplanner.domain.DomainObject;
016:
017: public class HistorySupport {
018: private static Logger log = Logger.getLogger(HistorySupport.class);
019: private static long FIFTEEN_MINUTES = 3600000 * 15;
020:
021: //DEBT should be spring loaded (metadatarepository should be passed in and we should not have private methods
022: private static void saveEvent(Session session, Class objectClass,
023: int containerOid, int oid, String action,
024: String description, int personId, Date when) {
025: try {
026: HistoricalEvent historicalEvent = new HistoricalEvent(
027: containerOid, oid,
028: DomainMetaDataRepository.getInstance()
029: .classToTypeName(objectClass), action,
030: description, personId, when);
031: if (!isEventThrottled(session, historicalEvent)) {
032: session.save(historicalEvent);
033: }
034: if (action.equals(HistoricalEvent.DELETED)) {
035: // Set name in event descriptions for deleted objects
036: List events = getEvents(oid);
037: for (int i = 0; i < events.size(); i++) {
038: HistoricalEvent event = (HistoricalEvent) events
039: .get(i);
040: if (StringUtils.isEmpty(event.getDescription())) {
041: event.setDescription(description);
042: }
043: }
044: }
045: } catch (HibernateException e) {
046: log.error("history error", e);
047: }
048: }
049:
050: private static boolean isEventThrottled(Session session,
051: HistoricalEvent event) throws HibernateException {
052: if (event.getAction().equals(HistoricalEvent.UPDATED)) {
053: HistoricalEvent previousEvent = (HistoricalEvent) session
054: .createQuery(
055: "from event in "
056: + HistoricalEvent.class
057: + " where event.targetObjectId = :oid and event.action = :action order by event.when desc")
058: .setInteger("oid", event.getTargetObjectId())
059: .setString("action", HistoricalEvent.UPDATED)
060: .setMaxResults(1).uniqueResult();
061: return previousEvent != null
062: && (event.getWhen().getTime() - previousEvent
063: .getWhen().getTime()) < FIFTEEN_MINUTES;
064: } else {
065: return false;
066: }
067: }
068:
069: public static void saveEvent(Session session, DomainObject object,
070: String action, String description, int personId, Date when) {
071: try {
072: Integer id = (Integer) PropertyUtils.getProperty(object,
073: "id");
074: saveEvent(session, object.getClass(),
075: DomainMetaDataRepository.getInstance().getParentId(
076: object), id.intValue(), action,
077: description, personId, when);
078: } catch (Exception e) {
079: log.error("history error", e);
080: }
081: }
082:
083: public static List getEvents(int oid) throws HibernateException {
084: //TODO externalize these queries into mapping file
085: return ThreadSession
086: .get()
087: .find(
088: "from event in "
089: + HistoricalEvent.class
090: + " where event.targetObjectId = ? order by event.when desc",
091: new Integer(oid), Hibernate.INTEGER);
092: }
093:
094: public static List getContainerEvents(int oid) throws Exception {
095: return ThreadSession.get().find(
096: "from event in " + HistoricalEvent.class
097: + " where event.containerId = ? "
098: + " and event.action in ('"
099: + HistoricalEvent.CREATED + "','"
100: + HistoricalEvent.DELETED
101: + "') order by event.when desc",
102: new Integer(oid), Hibernate.INTEGER);
103: }
104:
105: public static Object getHistoricalObject(HistoricalEvent event)
106: throws HibernateException {
107: if (event.getAction().equals(HistoricalEvent.DELETED)) {
108: return null;
109: }
110: return DomainMetaDataRepository.getInstance().getObject(
111: event.getObjectType(), event.getTargetObjectId());
112: }
113:
114: }
|