001: package com.technoetic.xplanner.actions;
002:
003: import java.util.Date;
004: import javax.servlet.ServletException;
005: import javax.servlet.http.HttpServletRequest;
006: import javax.servlet.http.HttpServletResponse;
007:
008: import net.sf.hibernate.Session;
009: import org.apache.log4j.Logger;
010: import org.apache.struts.Globals;
011: import org.apache.struts.action.ActionForm;
012: import org.apache.struts.action.ActionForward;
013: import org.apache.struts.action.ActionMapping;
014: import org.apache.struts.util.MessageResources;
015:
016: import com.technoetic.xplanner.domain.Iteration;
017: import com.technoetic.xplanner.domain.RelationshipConvertor;
018: import com.technoetic.xplanner.domain.StoryDisposition;
019: import com.technoetic.xplanner.domain.Task;
020: import com.technoetic.xplanner.domain.TaskDisposition;
021: import com.technoetic.xplanner.domain.UserStory;
022: import com.technoetic.xplanner.forms.MoveContinueTaskForm;
023: import com.technoetic.xplanner.history.HistoricalEvent;
024: import com.technoetic.xplanner.history.HistorySupport;
025: import com.technoetic.xplanner.security.SecurityHelper;
026:
027: public class MoveContinueTaskAction extends EditObjectAction {
028: private final Logger log = Logger.getLogger(getClass());
029: public static final RelationshipConvertor TARGET_STORY_ID_CONVERTOR = new RelationshipConvertor(
030: "targetStoryId", "story");
031: public static final String CONTINUE_ACTION = "Continue";
032: public static final String MOVE_ACTION = "Move";
033:
034: private TaskContinuer taskContinuer;
035:
036: public TaskContinuer getTaskContinuer() {
037: return taskContinuer;
038: }
039:
040: public void setTaskContinuer(TaskContinuer taskContinuer) {
041: this .taskContinuer = taskContinuer;
042: }
043:
044: public ActionForward execute(ActionMapping actionMapping,
045: ActionForm actionForm, HttpServletRequest request,
046: HttpServletResponse reply) throws Exception {
047: MoveContinueTaskForm taskForm = (MoveContinueTaskForm) actionForm;
048: Session session = getSession(request);
049: try {
050: if (taskForm.isSubmitted()) {
051: saveForm(taskForm, actionMapping, session, request,
052: actionForm, reply);
053: String returnto = request.getParameter(RETURNTO_PARAM);
054: if (returnto != null) {
055: return new ActionForward(returnto, true);
056: } else {
057: return actionMapping.findForward("view/projects");
058: }
059: } else {
060: populateForm(taskForm, session);
061: return new ActionForward(actionMapping.getInput());
062: }
063: } catch (Exception ex) {
064: log.error("error during task move/continue action", ex);
065: session.connection().rollback();
066: throw new ServletException(ex);
067: }
068: }
069:
070: private void saveForm(MoveContinueTaskForm taskForm,
071: ActionMapping actionMapping, Session session,
072: HttpServletRequest request, ActionForm actionForm,
073: HttpServletResponse reply) throws Exception {
074: MessageResources messageResources = (MessageResources) request
075: .getAttribute(Globals.MESSAGES_KEY);
076: String oid = taskForm.getOid();
077: int targetStoryId = taskForm.getTargetStoryId();
078: Class objectClass = getObjectType(actionMapping, request);
079: Task task;
080: if (taskForm.getAction().equals(CONTINUE_ACTION)) {
081: task = (Task) session.load(objectClass, new Integer(oid));
082: taskContinuer.init(session, (MessageResources) request
083: .getAttribute(Globals.MESSAGES_KEY), SecurityHelper
084: .getRemoteUserId(request));
085: UserStory fromStory = (UserStory) getRepository(
086: UserStory.class).load(task.getStory().getId());
087: UserStory toStory = (UserStory) getRepository(
088: UserStory.class).load(taskForm.getTargetStoryId());
089: taskContinuer.continueObject(task, fromStory, toStory);
090: } else if (taskForm.getAction().equals(MOVE_ACTION)) {
091: task = (Task) session.load(objectClass, new Integer(oid));
092: UserStory origStory = (UserStory) session.load(
093: UserStory.class, new Integer(task.getStory()
094: .getId()));
095: UserStory targetStory = (UserStory) session.load(
096: UserStory.class, new Integer(targetStoryId));
097: HistorySupport.saveEvent(session, task,
098: HistoricalEvent.MOVED, messageResources.getMessage(
099: "task.moved.from.to", origStory.getName(),
100: targetStory.getName()), SecurityHelper
101: .getRemoteUserId(request), new Date());
102: HistorySupport
103: .saveEvent(session, origStory,
104: HistoricalEvent.MOVED_OUT, messageResources
105: .getMessage("task.moved.out.to",
106: task.getName(), targetStory
107: .getName()),
108: SecurityHelper.getRemoteUserId(request),
109: new Date());
110:
111: Iteration targetIteration = (Iteration) session.load(
112: Iteration.class, new Integer(targetStory
113: .getIterationId()));
114:
115: boolean isTheSameIteration = (origStory.getIterationId() == targetStory
116: .getIterationId());
117:
118: if (!isTheSameIteration) {
119: if (targetIteration.isFuture()
120: && !targetIteration.isActive()) {
121: if (targetStory.getDisposition() == StoryDisposition.ADDED) {
122: task.setDisposition(TaskDisposition.DISCOVERED);
123: } else if (targetStory.getDisposition() == StoryDisposition.CARRIED_OVER) {
124: task
125: .setDisposition(TaskDisposition.CARRIED_OVER);
126: } else if (targetStory.getDisposition() == StoryDisposition.PLANNED) {
127: task.setDisposition(TaskDisposition.PLANNED);
128: }
129: } else {
130: task.setDisposition(TaskDisposition.ADDED);
131: }
132: }
133:
134: TARGET_STORY_ID_CONVERTOR.populateDomainObject(task,
135: taskForm);
136:
137: HistorySupport
138: .saveEvent(session, targetStory,
139: HistoricalEvent.MOVED_IN, messageResources
140: .getMessage("task.moved.in.from",
141: task.getName(), origStory
142: .getName()),
143: SecurityHelper.getRemoteUserId(request),
144: new Date());
145: } else {
146: throw new ServletException(
147: "Unknown task editor form action: "
148: + taskForm.getAction());
149: }
150: taskForm.setAction(null);
151: session.flush();
152: session.connection().commit();
153: afterObjectCommit(actionMapping, actionForm, request, reply);
154: }
155:
156: private void populateForm(MoveContinueTaskForm taskEditorForm,
157: Session session) throws Exception {
158: String oid = taskEditorForm.getOid();
159: if (oid != null) {
160: Task object = (Task) session.load(Task.class, new Integer(
161: oid));
162: populateForm(taskEditorForm, object);
163: }
164: }
165: }
|