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.TimeZone;
008:
009: import javax.faces.context.FacesContext;
010: import javax.servlet.http.HttpServletRequest;
011:
012: import org.apache.commons.lang.StringUtils;
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:
022: import ru.emdev.EmForge.web.bean.BaseControllerImpl;
023: import ru.emdev.EmForge.web.bean.EditTextController;
024: import ru.emdev.EmForge.web.bean.MainMenuController.MainMenuItem;
025: import ru.emdev.EmForge.wiki.web.bean.Crumb;
026:
027: import com.ecyrd.jspwiki.WikiContext;
028: import com.ecyrd.jspwiki.WikiPage;
029: import com.ecyrd.jspwiki.attachment.Attachment;
030: import com.ecyrd.jspwiki.providers.ProviderException;
031:
032: /**
033: * Controller for processing editing of project
034: *
035: * @author mchirkov
036: */
037: public class ProjectController extends BaseControllerImpl {
038: public static final String PROJECT_PAGE_NAME = "project";
039: public static final String PROJECT_ID_ATTR = "id";
040: public static final String PROJECT_PAGE_ATTR = "page";
041:
042: protected ProjectService m_projectService;
043:
044: private ProjectDO m_project;
045:
046: BpmService m_bpmService;
047:
048: CommentService m_commentService;
049: EditTextController m_editTextController;
050:
051: Collection<MilestoneData> m_openedMilestones;
052: Collection<MilestoneData> m_closedMilestones;
053:
054: public void setProjectService(ProjectService i_projectService) {
055: m_projectService = i_projectService;
056: }
057:
058: public void setBpmService(BpmService i_bpmService) {
059: m_bpmService = i_bpmService;
060: }
061:
062: public void setCommentService(CommentService i_commentService) {
063: m_commentService = i_commentService;
064: }
065:
066: public void setEditTextController(
067: EditTextController i_editTextController) {
068: m_editTextController = i_editTextController;
069: }
070:
071: @Override
072: public MainMenuItem getSelectionItemOnMainMenu() {
073: return MainMenuItem.PROJECTS;
074: }
075:
076: public String getTitleImpl() {
077: return m_project.getDisplayName();
078: }
079:
080: @Override
081: public Crumb getTrailCrumbInfo() {
082: if (m_project == null) {
083: return null;
084: }
085: String displayName;
086: String url;
087: try {
088: displayName = m_project.getName();
089: url = PROJECT_PAGE_NAME + "/" + m_project.getName();
090: } catch (Exception ex) {
091: logger.debug("Cannot get trail crumb info: ", ex);
092: return null;
093: }
094: return new Crumb(displayName, url);
095: }
096:
097: @Override
098: protected void init() {
099: if (m_appContext != null && m_projectService != null) {
100: HttpServletRequest request = (HttpServletRequest) FacesContext
101: .getCurrentInstance().getExternalContext()
102: .getRequest();
103:
104: String id = request.getParameter(PROJECT_ID_ATTR);
105: String name = null;
106:
107: if (StringUtils.isNotEmpty(id)) {
108: try {
109: m_project = m_projectService.getProject(Long
110: .valueOf(id));
111: } catch (Exception ex) {
112: // just ignore it here
113: }
114: }
115:
116: if (m_project != null) {
117: name = m_project.getName();
118: } else if (StringUtils.isNotEmpty(request
119: .getParameter(PROJECT_PAGE_ATTR))) {
120: name = request.getParameter(PROJECT_PAGE_ATTR);
121: m_project = m_projectService.getProject(name);
122:
123: } else {
124: m_project = new ProjectDO("");
125: }
126:
127: if (name != null) {
128: // set it into editTextController
129: m_editTextController.setPageName(name);
130: }
131: }
132: }
133:
134: public String save() {
135: Boolean wasNew = isNewProject();
136:
137: try {
138: m_projectService.saveProject(m_project);
139:
140: if (wasNew) {
141: addInfoMessage("New Project Created", null);
142: } else {
143: addInfoMessage("Project Changes Saved", null);
144: }
145:
146: // navigate to edit window - if it was new project
147: if (wasNew) {
148: redirect("edit/" + m_project.getName());
149: return null;
150: } else {
151: // returns to the project page
152: return "project";
153: }
154: } catch (Exception ex) {
155: logger.error("Cannot save project", ex);
156:
157: if (wasNew) {
158: addErrorMessage("Cannot Created Project", ex
159: .getMessage());
160: } else {
161: addErrorMessage("Cannot Save Changes", ex.getMessage());
162: }
163:
164: return null;
165: }
166: }
167:
168: public boolean isNewProject() {
169: if (m_project != null) {
170: return (m_project.getId() == null);
171: }
172: return false;
173: }
174:
175: public ProjectDO getProject() {
176: return m_project;
177: }
178:
179: @SuppressWarnings("unchecked")
180: public Collection getOpenedMilestones() throws EmForgeException {
181: if (m_openedMilestones == null) {
182: m_openedMilestones = new ArrayList<MilestoneData>();
183:
184: Collection<MilestoneDO> milestones = m_projectService
185: .getOpenedMilestones(m_project);
186: for (MilestoneDO milestone : milestones) {
187: int opened = m_bpmService.findTasks(
188: milestone.getName(), TaskStatus.OPENED, true)
189: .size();
190: int closed = m_bpmService.findTasks(
191: milestone.getName(), TaskStatus.CLOSED, true)
192: .size();
193:
194: m_openedMilestones.add(new MilestoneData(milestone,
195: opened, closed));
196: }
197: }
198:
199: return m_openedMilestones;
200: }
201:
202: @SuppressWarnings("unchecked")
203: public Collection getClosedMilestones() throws EmForgeException {
204: if (m_closedMilestones == null) {
205: m_closedMilestones = new ArrayList<MilestoneData>();
206:
207: Collection<MilestoneDO> milestones = m_projectService
208: .getClosedMilestones(m_project);
209: for (MilestoneDO milestone : milestones) {
210: int opened = m_bpmService.findTasks(
211: milestone.getName(), TaskStatus.OPENED, true)
212: .size();
213: int closed = m_bpmService.findTasks(
214: milestone.getName(), TaskStatus.CLOSED, true)
215: .size();
216:
217: m_closedMilestones.add(new MilestoneData(milestone,
218: opened, closed));
219: }
220: }
221:
222: return m_closedMilestones;
223: }
224:
225: /**
226: * @deprecated We should found some other solution to work with time-zones
227: * @return
228: */
229: public TimeZone getTimeZone() {
230: return new GregorianCalendar().getTimeZone();
231: }
232:
233: public Long getProjectId() {
234: if (m_project != null) {
235: return m_project.getId();
236: }
237:
238: return null;
239: }
240:
241: public void setProjectId(Long id) {
242: if (id != null) {
243: m_project = m_projectService.getProject(id);
244: } else {
245: m_project = new ProjectDO("");
246: }
247: }
248:
249: public boolean getCanAddMilestone() {
250: return m_projectService.canCreateMilestone(m_project);
251: }
252:
253: public boolean getCanAddProject() {
254: return m_projectService.canCreateProject();
255: }
256:
257: public boolean getCanSaveProject() {
258: return m_projectService.canChangeProject(m_project);
259: }
260:
261: public boolean getCanViewProjectRoles() {
262: return !getCurrentUser().isAnonymous();
263: }
264:
265: public class MilestoneData {
266:
267: MilestoneDO m_milestone;
268: int m_openedProcessesCount;
269: int m_closedProcessesCount;
270:
271: public MilestoneData(MilestoneDO i_milestone,
272: int i_openedProcessesCount, int i_closedProcessesCount) {
273: m_milestone = i_milestone;
274: m_openedProcessesCount = i_openedProcessesCount;
275: m_closedProcessesCount = i_closedProcessesCount;
276: }
277:
278: public int getClosedProcessesCount() {
279: return m_closedProcessesCount;
280: }
281:
282: public MilestoneDO getMilestone() {
283: return m_milestone;
284: }
285:
286: public int getOpenedProcessesCount() {
287: return m_openedProcessesCount;
288: }
289:
290: public int getOpenedProcessesPercentage() {
291: return 100 * m_openedProcessesCount
292: / (m_openedProcessesCount + m_closedProcessesCount);
293: }
294:
295: public int getClosedProcessesPercentage() {
296: return 100 - getOpenedProcessesPercentage();
297: }
298: }
299:
300: /** Returns Attachments for Project
301: *
302: * @return
303: */
304: @SuppressWarnings("unchecked")
305: public Collection<Attachment> getAttachments() {
306: if (isNewProject()) {
307: // return empty list
308: return new LinkedList<Attachment>();
309: }
310:
311: // attachments for project are stored in wiki-engine
312: Collection<Attachment> result = new ArrayList<Attachment>();
313:
314: WikiContext wikiContext = getWikiContext();
315: if (wikiContext != null) {
316: try {
317: WikiPage page = wikiContext.getPage();
318: if (page != null) {
319: result = wikiContext.getEngine()
320: .getAttachmentManager().listAttachments(
321: page);
322: }
323: } catch (ProviderException e) {
324: logger.error("Cannot get attachment list: ", e);
325: return result;
326: }
327: }
328: return result;
329:
330: }
331:
332: private WikiContext getWikiContext() {
333: if (StringUtils.isEmpty(getProject().getName())) {
334: return null;
335: }
336:
337: WikiPage page = m_wikiEngine
338: .getPage(getProject().getName(), -1);
339: if (page != null) {
340: return new WikiContext(m_wikiEngine, page);
341: }
342:
343: return null;
344: }
345:
346: public CommentTO[] getComments() throws EmForgeException {
347: return m_commentService.getComments(getProject().getName());
348: }
349:
350: /** Logged-In user can add comments for project
351: *
352: */
353: public boolean isCanAddComment() {
354: return !m_appContext.getUserService().getCurrentUser()
355: .isAnonymous()
356: && !isNewProject();
357: }
358:
359: public String addComment() {
360: String comment = m_editTextController.getWikiText();
361:
362: if (StringUtils.isNotEmpty(comment)) {
363: try {
364: m_commentService.addComment(getProject().getName(),
365: comment);
366: } catch (EmForgeException ex) {
367: logger.error("Cannot add comment for project: "
368: + getProject().getName(), ex);
369: addErrorMessage("Cannot add comment: "
370: + ex.getMessage(), "Cannot add comment: "
371: + ex.getMessage());
372: }
373:
374: }
375:
376: return null;
377: }
378: }
|