001: package org.emforge.projectmanager.web.bean;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.GregorianCalendar;
006: import java.util.LinkedList;
007: import java.util.List;
008: import java.util.TimeZone;
009:
010: import org.apache.commons.lang.StringUtils;
011: import org.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013: import org.emforge.BpmService;
014: import org.emforge.CommentService;
015: import org.emforge.EmForgeException;
016: import org.emforge.projectmanager.ProjectService;
017: import org.emforge.projectmanager.base.MilestoneDO;
018: import org.emforge.projectmanager.base.ProjectDO;
019: import org.emforge.xfer.CommentTO;
020: import org.emforge.xfer.TaskStatus;
021: import org.emforge.xfer.TaskTO;
022: import org.emforge.projectmanager.web.bean.MilestoneController;
023: import org.emforge.projectmanager.web.bean.ProjectController;
024:
025: import ru.emdev.EmForge.web.bean.BaseControllerImpl;
026: import ru.emdev.EmForge.web.bean.EditTextController;
027: import ru.emdev.EmForge.web.bean.MainMenuController.MainMenuItem;
028: import ru.emdev.EmForge.wiki.web.bean.Crumb;
029:
030: import com.ecyrd.jspwiki.WikiContext;
031: import com.ecyrd.jspwiki.WikiPage;
032: import com.ecyrd.jspwiki.attachment.Attachment;
033: import com.ecyrd.jspwiki.parser.MarkupParser;
034: import com.ecyrd.jspwiki.providers.ProviderException;
035:
036: public class MilestoneController extends BaseControllerImpl {
037: public static final String MILESTONE_PAGE_NAME = "milestone";
038: public static final String MILESTONE_ID_ATTR = "id";
039: public static final String MILESTONE_PROJECT_ATTR = "project";
040: public static final String MILESTONE_PAGE_ATTR = "page";
041:
042: protected final Log logger = LogFactory.getLog(getClass());
043:
044: ProjectService m_projectService;
045:
046: MilestoneDO m_milestone;
047:
048: boolean m_isNewMilestone = false;
049:
050: Integer m_openedTasksCount;
051: List<TaskTO> m_openedTasks;
052: Integer m_closedTasksCount;
053: List<TaskTO> m_closedTasks;
054:
055: boolean m_isShowSubprocesses = false;
056:
057: BpmService m_bpmService;
058:
059: CommentService m_commentService;
060: EditTextController m_editTextController;
061:
062: public void setProjectService(ProjectService i_projectService) {
063: m_projectService = i_projectService;
064: }
065:
066: public void setBpmService(BpmService i_bpmService) {
067: m_bpmService = i_bpmService;
068: }
069:
070: public void setCommentService(CommentService i_commentService) {
071: m_commentService = i_commentService;
072: }
073:
074: public void setEditTextController(
075: EditTextController i_editTextController) {
076: m_editTextController = i_editTextController;
077: }
078:
079: @Override
080: public MainMenuItem getSelectionItemOnMainMenu() {
081: return MainMenuItem.PROJECTS;
082: }
083:
084: public String getTitleImpl() {
085: if (m_isNewMilestone) {
086: return "Create New Milestone";
087: } else {
088: return "Milestone: " + m_milestone.getDisplayName();
089: }
090: }
091:
092: public String getDialogTitle() {
093: if (m_isNewMilestone) {
094: return "New Milestone Settings";
095: } else {
096: return "Milestone " + m_milestone.getDisplayName()
097: + " settings";
098: }
099: }
100:
101: @Override
102: public Crumb getTrailCrumbInfo() {
103: if (m_milestone == null) {
104: return null;
105: }
106:
107: String displayName = m_milestone.getName();
108: String url = getUrl();
109:
110: return new Crumb(displayName, url);
111: }
112:
113: @Override
114: protected void init() {
115:
116: if (m_appContext != null && m_projectService != null) {
117: //get milestone by id
118: // moved initialization here - so, hibernate session is available
119: // get id
120: String idStr = getParam(MILESTONE_ID_ATTR);
121: String wikiName = null;
122:
123: if (StringUtils.isNotEmpty(idStr)) {
124: try {
125: long id = Long.valueOf(idStr).longValue();
126:
127: m_milestone = m_projectService.getMilestone(id);
128: } catch (Exception ex) {
129: // just ignore it here
130: }
131: }
132:
133: if (m_milestone != null) {
134: wikiName = m_milestone.getName();
135: } else if (StringUtils
136: .isNotEmpty(getParam(MILESTONE_PAGE_ATTR))) {
137: wikiName = getParam(MILESTONE_PAGE_ATTR);
138: m_milestone = m_projectService.getMilestone(wikiName);
139:
140: } else {
141: String projectName = getParam(MILESTONE_PROJECT_ATTR);
142: if (StringUtils.isNotEmpty(projectName)) {
143: m_milestone = new MilestoneDO();
144: m_isNewMilestone = true;
145:
146: ProjectDO project = m_projectService
147: .getProject(projectName);
148:
149: m_milestone.setProject(project);
150: }
151: }
152: }
153: }
154:
155: /** This action is used for new milestone dialog
156: * to automatically generate unique milestoneName from milestone Display Name
157: *
158: * @return
159: */
160: public String changeMilestoneName() {
161: if (isNewMilestone()) {
162: // this is used only for new milestones
163: String displayName = getMilestone().getDisplayName();
164: String name = MarkupParser.cleanLink(displayName);
165: String milestoneName = name;
166: int i = 0;
167: // now, check - name should be unique
168: while (m_projectService.getMilestone(milestoneName) != null) {
169: i++;
170: milestoneName = name + i;
171: }
172:
173: getMilestone().setName(milestoneName);
174: }
175:
176: return null;
177: }
178:
179: /** Gets processed milestone
180: *
181: * @return
182: */
183: public MilestoneDO getMilestone() {
184: return m_milestone;
185: }
186:
187: /**
188: *
189: * @deprecated I do not know why it is here!
190: * @return
191: */
192: public TimeZone getTimeZone() {
193: return new GregorianCalendar().getTimeZone();
194: }
195:
196: /** Save new milestone data
197: *
198: * @return
199: */
200: public String save() {
201: assert m_milestone != null;
202:
203: Boolean wasNew = isNewMilestone();
204:
205: m_projectService.saveMilestone(m_milestone);
206:
207: if (wasNew) {
208: addInfoMessage("New Milestone Created", null);
209: } else {
210: addInfoMessage("Milestone Changes Saved", null);
211: }
212:
213: m_isNewMilestone = false;
214:
215: // navigate to edit window - if it was new project
216: if (wasNew) {
217: redirect("edit/" + m_milestone.getName());
218: } else {
219: // returns to the project page
220: redirect(getUrl());
221: }
222:
223: return null;
224: }
225:
226: public List<TaskTO> getOpenedTasks() throws EmForgeException {
227: if (m_openedTasks == null) {
228: m_openedTasks = m_bpmService
229: .findTasks(m_milestone.getName(),
230: TaskStatus.OPENED, m_isShowSubprocesses);
231: }
232:
233: return m_openedTasks;
234:
235: }
236:
237: public List<TaskTO> getClosedTasks() throws EmForgeException {
238: if (m_closedTasks == null) {
239: m_closedTasks = m_bpmService
240: .findTasks(m_milestone.getName(),
241: TaskStatus.CLOSED, m_isShowSubprocesses);
242: }
243:
244: return m_closedTasks;
245: }
246:
247: public boolean isNewMilestone() {
248: return m_isNewMilestone;
249: }
250:
251: public boolean isCanSaveMilestone() {
252: return m_projectService.canChangeMilestone(m_milestone);
253: }
254:
255: public String getUrl() {
256: return ProjectController.PROJECT_PAGE_NAME + "/"
257: + m_milestone.getProject().getName() + "/"
258: + MilestoneController.MILESTONE_PAGE_NAME + "/"
259: + m_milestone.getName();
260: }
261:
262: public int getOpenedProcessesCount() throws EmForgeException {
263: if (m_openedTasksCount == null) {
264: m_openedTasksCount = getOpenedTasks().size();
265: }
266: return m_openedTasksCount;
267: }
268:
269: public int getClosedProcessesCount() throws EmForgeException {
270: if (m_closedTasksCount == null) {
271: m_closedTasksCount = getClosedTasks().size();
272: }
273: return m_closedTasksCount;
274: }
275:
276: public int getOpenedProcessesPercentage() throws EmForgeException {
277: return 100
278: * getOpenedProcessesCount()
279: / (getOpenedProcessesCount() + getClosedProcessesCount());
280: }
281:
282: public int getClosedProcessesPercentage() throws EmForgeException {
283: return 100 - getOpenedProcessesPercentage();
284: }
285:
286: public boolean getShowSubprocesses() {
287: return m_isShowSubprocesses;
288: }
289:
290: public void setShowSubprocesses(boolean i_isShowSubprocesses) {
291: m_isShowSubprocesses = i_isShowSubprocesses;
292: }
293:
294: /** Returns Attachments for Project
295: *
296: * @return
297: */
298: @SuppressWarnings("unchecked")
299: public Collection<Attachment> getAttachments() {
300: if (isNewMilestone()) {
301: // return empty list
302: return new LinkedList<Attachment>();
303: }
304:
305: // attachments for project are stored in wiki-engine
306: Collection<Attachment> result = new ArrayList<Attachment>();
307:
308: WikiContext wikiContext = getWikiContext();
309: if (wikiContext != null) {
310: try {
311: WikiPage page = wikiContext.getPage();
312: if (page != null) {
313: result = wikiContext.getEngine()
314: .getAttachmentManager().listAttachments(
315: page);
316: }
317: } catch (ProviderException e) {
318: logger.error("Cannot get attachment list: ", e);
319: return result;
320: }
321: }
322: return result;
323:
324: }
325:
326: private WikiContext getWikiContext() {
327: if (StringUtils.isEmpty(getMilestone().getName())) {
328: return null;
329: }
330:
331: WikiPage page = m_wikiEngine.getPage(getMilestone().getName(),
332: -1);
333: if (page != null) {
334: return new WikiContext(m_wikiEngine, page);
335: }
336:
337: return null;
338: }
339:
340: public CommentTO[] getComments() throws EmForgeException {
341: return m_commentService.getComments(getMilestone().getName());
342: }
343:
344: /** Logged-In user can add comments for project
345: *
346: */
347: public boolean isCanAddComment() {
348: return !m_appContext.getUserService().getCurrentUser()
349: .isAnonymous()
350: && !isNewMilestone();
351: }
352:
353: public String addComment() {
354: String comment = m_editTextController.getWikiText();
355:
356: if (StringUtils.isNotEmpty(comment)) {
357: try {
358: m_commentService.addComment(getMilestone().getName(),
359: comment);
360: } catch (EmForgeException ex) {
361: logger.error("Cannot add comment for milestone: "
362: + getMilestone().getName(), ex);
363: addErrorMessage("Cannot add comment: "
364: + ex.getMessage(), "Cannot add comment: "
365: + ex.getMessage());
366: }
367:
368: }
369:
370: return null;
371: }
372:
373: public String cancel() {
374: if (m_trail.getCurrentUrl() != null) {
375: redirect(m_trail.getCurrentUrl());
376: }
377:
378: return null;
379: }
380: }
|