001: /*
002: * Created by IntelliJ IDEA.
003: * User: Jacques
004: * Date: May 28, 2005
005: * Time: 8:52:29 PM
006: */
007: package com.technoetic.xplanner.actions;
008:
009: import java.text.MessageFormat;
010: import java.util.Date;
011: import java.util.Iterator;
012:
013: import net.sf.hibernate.HibernateException;
014: import net.sf.hibernate.Query;
015: import net.sf.hibernate.Session;
016: import org.apache.commons.beanutils.BeanUtils;
017: import org.apache.log4j.Logger;
018: import org.apache.struts.util.MessageResources;
019:
020: import com.technoetic.xplanner.db.hibernate.ThreadSession;
021: import com.technoetic.xplanner.domain.*;
022: import com.technoetic.xplanner.history.Historian;
023: import com.technoetic.xplanner.history.HistoricalEvent;
024: import com.technoetic.xplanner.util.LogUtil;
025:
026: public abstract class Continuer {
027: protected static final Logger LOG = LogUtil.getLogger();
028:
029: public static final String CONTINUE_ACTION = "Continue";
030: public static final String MOVE_ACTION = "Move";
031: protected MessageFormat toText;
032: protected MessageFormat fromText;
033: protected MessageFormat fromParentText;
034: protected MessageFormat toParentText;
035:
036: protected DomainObjectWikiLinkFormatter linkFormatter;
037: protected int currentUserId;
038: private Session hibernateSession;
039: protected Date when = new Date();
040: protected Historian historian;
041: public static final String CONTINUED_FROM_DESCRIPTION_KEY = "continue.description.from";
042: public static final String CONTINUED_AS_DESCRIPTION_KEY = "continue.description.to";
043: public static final String CONTINUE_DESCRIPTION_TO_PARENT_KEY = "continue.description.from_parent";
044: public static final String CONTINUE_DESCRIPTION_FROM_PARENT_KEY = "continue.description.to_parent";
045: protected DomainMetaDataRepository metaDataRepository;
046:
047: protected Continuer() {
048: }
049:
050: public Continuer(Session session, int currentUserId,
051: MessageResources messageResources) {
052: init(session, messageResources, currentUserId);
053: }
054:
055: public final void init(Session session,
056: MessageResources messageResources, int currentUserId) {
057: setHibernateSession(session);
058: setLinkFormatter(new DomainObjectWikiLinkFormatter());
059: setMessageResources(messageResources);
060: setCurrentUserId(currentUserId);
061: }
062:
063: public void setMessageResources(MessageResources messageResources) {
064: toText = new MessageFormat(messageResources
065: .getMessage(CONTINUED_FROM_DESCRIPTION_KEY));
066: fromText = new MessageFormat(messageResources
067: .getMessage(CONTINUED_AS_DESCRIPTION_KEY));
068: fromParentText = new MessageFormat(messageResources
069: .getMessage(CONTINUE_DESCRIPTION_FROM_PARENT_KEY));
070: toParentText = new MessageFormat(messageResources
071: .getMessage(CONTINUE_DESCRIPTION_TO_PARENT_KEY));
072: }
073:
074: public DomainObject continueObject(DomainObject fromObject,
075: DomainObject fromParent, DomainObject toParent)
076: throws HibernateException {
077:
078: LOG.debug("hibernateSession used=" + getHibernateSession());
079: DomainObject toObject = createContinuingObject(fromObject,
080: toParent);
081:
082: doContinueObject(fromObject, toParent, toObject);
083:
084: // FIXME The original iteration should not have container events of created action when continue a story in target targetIteration
085:
086: updateDescriptionAndHistory(fromObject, fromParent, toObject,
087: toParent);
088:
089: continueNotes((NoteAttachable) fromObject,
090: (NoteAttachable) toObject);
091:
092: getHibernateSession().update(fromObject);
093: getHibernateSession().update(toParent);
094: getHibernateSession().update(fromParent);
095:
096: return toObject;
097:
098: }
099:
100: public void setMetaDataRepository(
101: DomainMetaDataRepository metaDataRepository) {
102: this .metaDataRepository = metaDataRepository;
103: }
104:
105: private DomainObject createContinuingObject(
106: DomainObject fromObject, DomainObject toParent)
107: throws HibernateException {
108: DomainObject toObject = cloneObject(fromObject);
109: getHibernateSession().save(toObject);
110: metaDataRepository.setParent(toObject, toParent);
111: return toObject;
112: }
113:
114: private void updateDescriptionAndHistory(DomainObject fromObject,
115: DomainObject fromParent, DomainObject toObject,
116: DomainObject toParent) {
117: Object[] parentDescriptionParams = new Object[] {
118: linkFormatter.format(fromObject),
119: linkFormatter.format(fromParent),
120: linkFormatter.format(toObject),
121: linkFormatter.format(toParent) };
122: updateDescription((Describable) toObject, fromText,
123: parentDescriptionParams);
124: updateDescription((Describable) fromObject, toText,
125: parentDescriptionParams);
126:
127: addHistoryEvent(HistoricalEvent.CONTINUED, fromObject, toText
128: .format(parentDescriptionParams));
129: addHistoryEvent(HistoricalEvent.CONTINUED, toObject, fromText
130: .format(parentDescriptionParams));
131: addHistoryEvent(HistoricalEvent.CONTINUED, fromParent,
132: toParentText.format(parentDescriptionParams));
133: addHistoryEvent(HistoricalEvent.CONTINUED, toParent,
134: fromParentText.format(parentDescriptionParams));
135: }
136:
137: private DomainObject cloneObject(DomainObject fromObject) {
138: try {
139: DomainObject toObject = createInstance(fromObject);
140: BeanUtils.copyProperties(toObject, fromObject);
141: toObject.setId(0);
142: return toObject;
143:
144: } catch (Exception e) {
145: LOG.error(e);
146: }
147: return null;
148: }
149:
150: protected DomainObject createInstance(DomainObject fromObject)
151: throws InstantiationException, IllegalAccessException {
152: return (DomainObject) fromObject.getClass().newInstance();
153: }
154:
155: protected abstract void doContinueObject(DomainObject fromObject,
156: DomainObject toParent, DomainObject toObject)
157: throws HibernateException;
158:
159: protected void addHistoryEvent(String type, DomainObject target,
160: String description) {
161: historian = new Historian(getHibernateSession(), currentUserId);
162: historian.saveEvent(target, type, description, when);
163: }
164:
165: private void updateDescription(Describable describable,
166: MessageFormat direction, Object[] params) {
167: describable.setDescription(direction.format(params) + "\n\n"
168: + describable.getDescription());
169: }
170:
171: public void continueNotes(NoteAttachable fromObject,
172: NoteAttachable toObject) throws HibernateException {
173: Query query = getHibernateSession().getNamedQuery(
174: Note.class.getName() + Note.ATTACHED_NOTES_QUERY);
175: query.setString("attachedToId", String.valueOf(fromObject
176: .getId()));
177: Iterator iterator = query.iterate();
178: while (iterator.hasNext()) {
179: Note note = (Note) iterator.next();
180: Note newNote = (Note) cloneObject(note);
181: newNote.setAttachedToId(toObject.getId());
182: newNote.setId(0);
183: getHibernateSession().save(newNote);
184: }
185: }
186:
187: public Session getHibernateSession() {
188: if (hibernateSession != ThreadSession.get()) {
189: LOG.error("hibernateSession mismatch " + hibernateSession
190: + " and " + ThreadSession.get());
191: }
192: return hibernateSession;
193: }
194:
195: public void setHibernateSession(Session hibernateSession) {
196: this .hibernateSession = hibernateSession;
197: }
198:
199: public void setCurrentUserId(int currentUserId) {
200: this .currentUserId = currentUserId;
201: }
202:
203: public void setLinkFormatter(
204: DomainObjectWikiLinkFormatter linkFormatter) {
205: this.linkFormatter = linkFormatter;
206: }
207: }
|