001: package com.technoetic.xplanner.actions;
002:
003: import java.lang.reflect.InvocationTargetException;
004: import java.util.Collection;
005: import java.util.Iterator;
006: import javax.servlet.ServletException;
007: import javax.servlet.http.HttpServletRequest;
008: import javax.servlet.http.HttpServletResponse;
009:
010: import net.sf.hibernate.HibernateException;
011: import net.sf.hibernate.Session;
012: import org.apache.commons.beanutils.PropertyUtils;
013: import org.apache.struts.action.ActionForm;
014: import org.apache.struts.action.ActionForward;
015: import org.apache.struts.action.ActionMapping;
016:
017: import com.technoetic.xplanner.domain.Iteration;
018: import com.technoetic.xplanner.domain.IterationStatus;
019: import com.technoetic.xplanner.domain.UserStory;
020: import com.technoetic.xplanner.forms.ContinueUnfinishedStoriesForm;
021:
022: public class ContinueUnfinishedStoriesAction extends EditObjectAction {
023: public static final String OK_ACTION = "Ok";
024: public static final String CANCEL_ACTION = "Cancel";
025: private StoryContinuer storyContinuer;
026:
027: protected ActionForward doExecute(ActionMapping mapping,
028: ActionForm form, HttpServletRequest request,
029: HttpServletResponse response) throws Exception {
030: ContinueUnfinishedStoriesForm continueUnfinishedStoriesForm;
031: continueUnfinishedStoriesForm = (ContinueUnfinishedStoriesForm) form;
032: Session session = getSession(request);
033: try {
034: if (continueUnfinishedStoriesForm.isSubmitted()) {
035: saveForm(continueUnfinishedStoriesForm, mapping,
036: session, request, form, response);
037: String returnto = request.getParameter(RETURNTO_PARAM);
038: if (returnto != null) {
039: return new ActionForward(returnto, true);
040: } else {
041: return mapping.findForward("view/projects");
042: }
043: } else {
044: populateForm(continueUnfinishedStoriesForm, session);
045: return new ActionForward(mapping.getInput());
046: }
047: } catch (Exception ex) {
048: session.connection().rollback();
049: throw new ServletException(ex);
050: }
051: }
052:
053: private void populateForm(
054: ContinueUnfinishedStoriesForm continueUnfinishedStoriesForm,
055: Session session) throws HibernateException,
056: NoSuchMethodException, IllegalAccessException,
057: InvocationTargetException {
058: int iterationId = continueUnfinishedStoriesForm
059: .getIterationId();
060: if (iterationId != 0) {
061: Iteration iteration = (Iteration) session.load(
062: Iteration.class, new Integer(iterationId));
063: PropertyUtils.copyProperties(continueUnfinishedStoriesForm,
064: iteration);
065: populateManyToOneIds(continueUnfinishedStoriesForm,
066: iteration);
067: }
068: }
069:
070: protected void saveForm(
071: ContinueUnfinishedStoriesForm selectionForm,
072: ActionMapping mapping, Session session,
073: HttpServletRequest request, ActionForm form,
074: HttpServletResponse response) throws Exception {
075: String action = selectionForm.getAction();
076: if (action.equals(OK_ACTION)) {
077: int currentIterationId = selectionForm.getIterationId();
078: int targetIterationId = selectionForm
079: .getTargetIterationId();
080: continueIteration(currentIterationId, targetIterationId,
081: request, session);
082: }
083: }
084:
085: private void continueIteration(int currentIterationId,
086: int targetIterationId, HttpServletRequest request,
087: Session session) throws Exception {
088: Iteration originalIteration = (Iteration) getRepository(
089: Iteration.class).load(currentIterationId);
090: Iteration targetIteration = (Iteration) getRepository(
091: Iteration.class).load(targetIterationId);
092: continueUnfinishedStoriesInIteration(request, session,
093: originalIteration, targetIteration);
094: }
095:
096: protected void continueUnfinishedStoriesInIteration(
097: HttpServletRequest request, Session session,
098: Iteration originalIteration, Iteration targetIteration)
099: throws Exception {
100: storyContinuer.init(session, request);
101: if (originalIteration != null) {
102: continueIteration(originalIteration, targetIteration);
103: }
104: }
105:
106: private void continueIteration(Iteration originalIteration,
107: Iteration targetIteration) throws HibernateException {
108: originalIteration.setStatusKey(IterationStatus.INACTIVE_KEY);
109: Collection stories = originalIteration.getUserStories();
110: if (targetIteration.getId() != 0) {
111: for (Iterator iterator = stories.iterator(); iterator
112: .hasNext();) {
113: continueCompleteStory((UserStory) iterator.next(),
114: originalIteration, targetIteration);
115: }
116: }
117: }
118:
119: private void continueCompleteStory(UserStory userStory,
120: Iteration originalIteration, Iteration targetIteration)
121: throws HibernateException {
122: if (!userStory.isCompleted()) {
123: continueStory(userStory, originalIteration, targetIteration);
124: }
125: }
126:
127: private void continueStory(UserStory userStory,
128: Iteration originalIteration, Iteration targetIteration)
129: throws HibernateException {
130: UserStory continuedUserStory = (UserStory) storyContinuer
131: .continueObject(userStory, originalIteration,
132: targetIteration);
133: Collection targetIterationStories = targetIteration
134: .getUserStories();
135: targetIterationStories.add(continuedUserStory);
136: targetIteration.setUserStories(targetIterationStories);
137: }
138:
139: public void setStoryContinuer(StoryContinuer storyContinuer) {
140: this.storyContinuer = storyContinuer;
141: }
142: }
|