001: package com.technoetic.xplanner.domain.repository;
002:
003: import java.util.Date;
004:
005: import net.sf.hibernate.Session;
006: import org.apache.log4j.Logger;
007: import org.springframework.orm.hibernate.HibernateCallback;
008: import org.springframework.orm.hibernate.support.HibernateDaoSupport;
009:
010: import com.technoetic.xplanner.domain.DomainObject;
011: import com.technoetic.xplanner.filters.ThreadServletRequest;
012: import com.technoetic.xplanner.history.HistoricalEvent;
013: import com.technoetic.xplanner.history.HistorySupport;
014: import com.technoetic.xplanner.security.AuthenticationException;
015: import com.technoetic.xplanner.security.SecurityHelper;
016:
017: public class RepositoryHistoryAdapter extends HibernateDaoSupport
018: implements ObjectRepository {
019: private Class objectClass;
020: private ObjectRepository delegate;
021:
022: public RepositoryHistoryAdapter(Class objectClass,
023: ObjectRepository delegate) {
024: this .objectClass = objectClass;
025: this .delegate = delegate;
026: }
027:
028: public void delete(final int objectIdentifier)
029: throws RepositoryException {
030: // todo How should the project ID be obtained with refs to Hibernate?
031:
032: final int remoteUserId = getRemoteUserId();
033: DomainObject object = (DomainObject) getHibernateTemplate()
034: .load(objectClass, new Integer(objectIdentifier));
035: saveHistoryEvent(object, HistoricalEvent.DELETED, object
036: .getName(), remoteUserId);
037: delegate.delete(objectIdentifier);
038: }
039:
040: public int insert(final DomainObject object)
041: throws RepositoryException {
042: int id = delegate.insert(object);
043: final int remoteUserId;
044: remoteUserId = getRemoteUserId();
045: saveHistoryEvent(object, HistoricalEvent.CREATED, object
046: .getName(), remoteUserId);
047: return id;
048: }
049:
050: public Object load(int objectIdentifier) throws RepositoryException {
051: // no load history
052: return delegate.load(objectIdentifier);
053: }
054:
055: public void update(final DomainObject object)
056: throws RepositoryException {
057: final int remoteUserId;
058: remoteUserId = getRemoteUserId();
059: saveHistoryEvent(object, HistoricalEvent.UPDATED, object
060: .getName(), remoteUserId);
061: delegate.update(object);
062: }
063:
064: private void saveHistoryEvent(final DomainObject object,
065: final String eventType, final String description,
066: final int remoteUserId) {
067: getHibernateTemplate().execute(
068: new SaveEventHibernateCallback(object, eventType,
069: description, remoteUserId));
070:
071: }
072:
073: private int getRemoteUserId() throws RepositoryException {
074: int remoteUserId;
075: try {
076: remoteUserId = SecurityHelper
077: .getRemoteUserId(ThreadServletRequest.get());
078: } catch (AuthenticationException e) {
079: throw new RepositoryException(e);
080: }
081: return remoteUserId;
082: }
083:
084: class SaveEventHibernateCallback implements HibernateCallback {
085: private final DomainObject object;
086: private final String eventType;
087: private final String description;
088: private final int remoteUserId;
089:
090: public SaveEventHibernateCallback(DomainObject object,
091: String eventType, String description, int remoteUserId) {
092: this .object = object;
093: this .eventType = eventType;
094: this .description = description;
095: this .remoteUserId = remoteUserId;
096: }
097:
098: public Object doInHibernate(Session session) {
099: HistorySupport.saveEvent(session, object, eventType,
100: description, remoteUserId, new Date());
101: return null;
102: }
103:
104: public boolean equals(Object o) {
105: if (this == o)
106: return true;
107: if (o == null || getClass() != o.getClass())
108: return false;
109:
110: final SaveEventHibernateCallback that = (SaveEventHibernateCallback) o;
111:
112: if (remoteUserId != that.remoteUserId)
113: return false;
114: if (description != null ? !description
115: .equals(that.description)
116: : that.description != null)
117: return false;
118: if (eventType != null ? !eventType.equals(that.eventType)
119: : that.eventType != null)
120: return false;
121: if (object != null ? !object.equals(that.object)
122: : that.object != null)
123: return false;
124:
125: return true;
126: }
127:
128: public int hashCode() {
129: return 0;
130: }
131: }
132: }
|