001: package com.technoetic.xplanner.actions;
002:
003: import java.lang.reflect.InvocationTargetException;
004: import java.util.Collection;
005: import java.util.Date;
006: import java.util.Iterator;
007: import javax.servlet.ServletException;
008: import javax.servlet.http.HttpServletRequest;
009: import javax.servlet.http.HttpServletResponse;
010:
011: import net.sf.hibernate.HibernateException;
012: import net.sf.hibernate.Session;
013: import org.apache.commons.beanutils.PropertyUtils;
014: import org.apache.struts.action.ActionForm;
015: import org.apache.struts.action.ActionForward;
016: import org.apache.struts.action.ActionMapping;
017:
018: import com.technoetic.xplanner.domain.Iteration;
019: import com.technoetic.xplanner.domain.RelationshipConvertor;
020: import com.technoetic.xplanner.domain.RelationshipMappingRegistry;
021: import com.technoetic.xplanner.domain.UserStory;
022: import com.technoetic.xplanner.domain.repository.RepositoryException;
023: import com.technoetic.xplanner.forms.MoveContinueStoryForm;
024: import com.technoetic.xplanner.history.Historian;
025: import com.technoetic.xplanner.history.HistoricalEvent;
026: import com.technoetic.xplanner.security.AuthenticationException;
027: import com.technoetic.xplanner.security.SecurityHelper;
028:
029: public class MoveContinueStoryAction extends EditObjectAction {
030: public static final String CONTINUE_ACTION = "Continue";
031: public static final String MOVE_ACTION = "Move";
032:
033: private StoryContinuer storyContinuer;
034:
035: public void setStoryContinuer(StoryContinuer storyContinuer) {
036: this .storyContinuer = storyContinuer;
037: }
038:
039: public StoryContinuer getStoryContinuer() {
040: return storyContinuer;
041: }
042:
043: public ActionForward doExecute(ActionMapping actionMapping,
044: ActionForm actionForm, HttpServletRequest request,
045: HttpServletResponse reply) throws Exception {
046: Session session = getSession(request);
047: MoveContinueStoryForm storyForm = (MoveContinueStoryForm) actionForm;
048: if (storyForm.isSubmitted()) {
049: saveForm(storyForm, session, request);
050: String returnto = request.getParameter(RETURNTO_PARAM);
051: if (returnto != null) {
052: return new ActionForward(returnto, true);
053: } else {
054: return actionMapping.findForward("view/projects");
055: }
056: } else {
057: populateForm(storyForm, session);
058: return new ActionForward(actionMapping.getInput());
059: }
060: }
061:
062: private void saveForm(MoveContinueStoryForm storyForm,
063: Session session, HttpServletRequest request)
064: throws Exception {
065: UserStory story = getStory(Integer.parseInt(storyForm.getOid()));
066: populateObject(request, story, storyForm);
067: Iteration originalIteration = getIteration(story
068: .getIterationId());
069: Iteration targetIteration = getIteration(storyForm
070: .getTargetIterationId());
071:
072: if (storyForm.getAction().equals(MOVE_ACTION)) {
073: moveStory(story, targetIteration, originalIteration,
074: request, session);
075: } else if (storyForm.getAction().equals(CONTINUE_ACTION)) {
076: continueStory(story, originalIteration, targetIteration,
077: request, session);
078: } else {
079: throw new ServletException("Unknown editor action: "
080: + storyForm.getAction());
081: }
082: reorderIterationStories(originalIteration);
083: reorderIterationStories(targetIteration);
084: storyForm.setAction(null);
085: }
086:
087: private void continueStory(UserStory story,
088: Iteration originalIteration, Iteration targetIteration,
089: HttpServletRequest request, Session session)
090: throws AuthenticationException, HibernateException {
091: storyContinuer.init(session, request);
092: UserStory targetStory = (UserStory) storyContinuer
093: .continueObject(story, originalIteration,
094: targetIteration);
095: updateStoryOrderNoInTargetIteration(targetStory,
096: originalIteration, targetIteration);
097: }
098:
099: private void moveStory(UserStory story, Iteration targetIteration,
100: Iteration originalIteration, HttpServletRequest request,
101: Session session) throws AuthenticationException {
102: originalIteration.getUserStories().remove(story);
103: story.moveTo(targetIteration);
104: updateStoryOrderNoInTargetIteration(story, originalIteration,
105: targetIteration);
106: saveMoveHistory(story, originalIteration, targetIteration,
107: session, SecurityHelper.getRemoteUserId(request));
108: }
109:
110: private UserStory getStory(int id) throws RepositoryException {
111: return (UserStory) getRepository(UserStory.class).load(id);
112: }
113:
114: private void reorderIterationStories(Iteration iteration)
115: throws Exception {
116: Collection stories = iteration.getUserStories();
117: iteration.modifyStoryOrder(DomainOrderer
118: .buildStoryIdNewOrderArray(stories));
119: }
120:
121: private void saveMoveHistory(UserStory story,
122: Iteration originIteration, Iteration targetIteration,
123: Session session, int currentUserId) {
124: Historian historian = new Historian(session, currentUserId);
125:
126: Date now = new Date();
127: historian.saveEvent(story, HistoricalEvent.MOVED, "from "
128: + originIteration.getName() + " to "
129: + targetIteration.getName(), now);
130: historian.saveEvent(originIteration, HistoricalEvent.MOVED_OUT,
131: story.getName() + " to " + targetIteration.getName(),
132: now);
133: historian.saveEvent(targetIteration, HistoricalEvent.MOVED_IN,
134: story.getName() + " from " + originIteration.getName(),
135: now);
136: }
137:
138: private void updateStoryOrderNoInTargetIteration(UserStory story,
139: Iteration originalIteration, Iteration targetIteration) {
140: if (originalIteration.getStartDate().compareTo(
141: targetIteration.getStartDate()) <= 0) {
142: story.setOrderNo(0);
143: } else {
144: story.setOrderNo(Integer.MAX_VALUE);
145: }
146: }
147:
148: private void populateForm(MoveContinueStoryForm storyForm,
149: Session session) throws Exception {
150: String oid = storyForm.getOid();
151: if (oid != null) {
152: UserStory story = (UserStory) session.load(UserStory.class,
153: new Integer(oid));
154: Iteration iteration = (Iteration) session.load(
155: Iteration.class,
156: new Integer(story.getIterationId()));
157: storyForm.setFutureIteration(iteration.isFuture());
158: PropertyUtils.copyProperties(storyForm, story);
159: populateManyToOneIds(storyForm, story);
160: }
161: }
162:
163: private void populateManyToOneIds(ActionForm form, UserStory story)
164: throws IllegalAccessException, NoSuchMethodException,
165: InvocationTargetException {
166: Collection mappings = RelationshipMappingRegistry.getInstance()
167: .getRelationshipMappings(story);
168: for (Iterator iterator = mappings.iterator(); iterator
169: .hasNext();) {
170: RelationshipConvertor convertor = (RelationshipConvertor) iterator
171: .next();
172: convertor.populateAdapter(form, story);
173: }
174: }
175:
176: }
|