001: package com.technoetic.xplanner.actions;
002:
003: import java.util.Collection;
004: import java.util.Iterator;
005: import java.util.ArrayList;
006: import javax.servlet.http.HttpServletRequest;
007: import javax.servlet.http.HttpServletResponse;
008:
009: import net.sf.hibernate.Session;
010: import org.apache.struts.action.ActionForm;
011: import org.apache.struts.action.ActionForward;
012: import org.apache.struts.action.ActionMapping;
013:
014: import com.technoetic.xplanner.charts.DataSampler;
015: import com.technoetic.xplanner.domain.Iteration;
016: import com.technoetic.xplanner.domain.Project;
017: import com.technoetic.xplanner.domain.repository.ObjectRepository;
018: import com.technoetic.xplanner.domain.repository.RepositoryException;
019: import com.technoetic.xplanner.forms.IterationStatusEditorForm;
020: import com.technoetic.xplanner.forms.TimeEditorForm;
021: import com.technoetic.xplanner.history.HistoricalEvent;
022: import com.technoetic.xplanner.history.HistorySupport;
023: import com.technoetic.xplanner.security.SecurityHelper;
024: import com.technoetic.xplanner.util.RequestUtils;
025: import com.technoetic.xplanner.util.TimeGenerator;
026:
027: public class StartIterationAction extends AbstractIterationAction {
028: private TimeGenerator timeGenerator;
029:
030: public void setTimeGenerator(TimeGenerator timeGenerator) {
031: this .timeGenerator = timeGenerator;
032: }
033:
034: public void beforeObjectCommit(Object object, Session session,
035: ActionMapping actionMapping, ActionForm actionForm,
036: HttpServletRequest request, HttpServletResponse reply)
037: throws Exception {
038: Iteration iteration = (Iteration) object;
039: iteration.start();
040: dataSampler.generateOpeningDataSamples(iteration);
041: HistorySupport.saveEvent(session, iteration,
042: HistoricalEvent.ITERATION_STARTED, null, SecurityHelper
043: .getRemoteUserId(request), timeGenerator
044: .getCurrentTime());
045: }
046:
047: protected ActionForward doExecute(ActionMapping mapping,
048: ActionForm actionForm, HttpServletRequest request,
049: HttpServletResponse response) throws Exception {
050: IterationStatusEditorForm form = (IterationStatusEditorForm) actionForm;
051: ObjectRepository iterationRepository = getRepository(Iteration.class);
052: ObjectRepository projectRepository = getRepository(Project.class);
053: Iteration iteration;
054:
055: ActionForward nextPage = mapping.getInputForward();
056: if (!form.isIterationStartConfirmed()) {
057: if (UpdateTimeAction.isFromUpdateTime(request)) {
058: //DEBT Extract constants for all these strings. from_edit/time -> UpdateTimeAction, edit/iteration -> this class
059: iteration = (Iteration) request
060: .getAttribute("edit/iteration");
061: } else {
062: String iterationId = form.getOid();
063: iteration = (Iteration) iterationRepository
064: .load(Integer.parseInt(iterationId));
065: }
066: if (iteration != null) {
067: request.setAttribute("edit/iteration", iteration);
068: Project project = (Project) projectRepository
069: .load(iteration.getProjectId());
070: Collection startedIterationsList = getStartedIterations(project);
071: int startedIterationsCount = startedIterationsList
072: .size();
073: request.setAttribute("startedIterationsNr",
074: new Integer(startedIterationsCount));
075: if (startedIterationsCount > 0) {
076: //DEBT These should not be strings but constant used both in java code and jsp.
077: nextPage = mapping.findForward("start/iteration");
078: } else if (!UpdateTimeAction.isFromUpdateTime(request)) {
079: iteration.close();
080: iterationRepository.update(iteration);
081: setTargetObject(request, iteration);
082: String returnto = request
083: .getParameter(EditObjectAction.RETURNTO_PARAM);
084: nextPage = returnto != null ? new ActionForward(
085: returnto, true) : mapping
086: .findForward("view/projects");
087: }
088: }
089: } else {
090: String iterationId = form.getOid();
091: iteration = (Iteration) iterationRepository.load(Integer
092: .parseInt(iterationId));
093: if (form.isCloseIterations()) {
094: Project project = (Project) projectRepository
095: .load(iteration.getProjectId());
096: closeStartedIterations(project, iterationRepository);
097: }
098: iteration.close();
099: iterationRepository.update(iteration);
100:
101: setTargetObject(request, iteration);
102:
103: // DEBT: Use the normal returnto mechanism to send the control back to edit/time w/o embedding knowledge of it inside this action
104: if (RequestUtils.isParameterTrue(request,
105: IterationStatusEditorForm.SAVE_TIME_ATTR)) {
106: request.setAttribute(TimeEditorForm.WIZARD_MODE_ATTR,
107: Boolean.TRUE);
108: nextPage = mapping.findForward("edit/time");
109: } else {
110: String returnto = request
111: .getParameter(EditObjectAction.RETURNTO_PARAM);
112: nextPage = returnto != null ? new ActionForward(
113: returnto, true) : mapping
114: .findForward("view/projects");
115: }
116: }
117: return nextPage;
118: }
119:
120: private void closeStartedIterations(Project project,
121: ObjectRepository iterationObjectRepository)
122: throws RepositoryException {
123: Collection startedIterationsList = getStartedIterations(project);
124: Iterator iterator = startedIterationsList.iterator();
125: while (iterator.hasNext()) {
126: Iteration iteration = (Iteration) iterator.next();
127: iteration.close();
128: iterationObjectRepository.update(iteration);
129: }
130: }
131:
132: private Collection getStartedIterations(Project project) {
133: Collection startedIterationList = new ArrayList();
134: Collection iterationList = project.getIterations();
135: Iterator iterator = iterationList.iterator();
136: while (iterator.hasNext()) {
137: Iteration iteration = (Iteration) iterator.next();
138: if (iteration.isActive()) {
139: startedIterationList.add(iteration);
140: }
141: }
142: return startedIterationList;
143: }
144:
145: public void setDataSampler(DataSampler dataSampler) {
146: this .dataSampler = dataSampler;
147: }
148:
149: public DataSampler getDataSampler() {
150: return dataSampler;
151: }
152: }
|