001: /*
002: * Wilos Is a cLever process Orchestration Software - http://www.wilos-project.org
003: * Copyright (C) 2006-2007 Paul Sabatier University, IUP ISI (Toulouse, France) <massie@irit.fr>
004: * Copyright (C) 2007 Sebastien BALARD <sbalard@wilos-project.org>
005: * Copyright (C) 2007-2008 Paul Sabatier University, IUP ISI (Toulouse, France) <massie@irit.fr>
006: *
007: * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
008: * General Public License as published by the Free Software Foundation; either version 2 of the License,
009: * or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
012: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License along with this program; if not,
016: * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
017: */
018:
019: package wilos.presentation.web.tree;
020:
021: import java.sql.Time;
022: import java.util.ArrayList;
023: import java.util.Comparator;
024: import java.util.Enumeration;
025: import java.util.HashMap;
026: import java.util.List;
027: import java.util.Map;
028: import java.util.TreeSet;
029:
030: import javax.faces.context.FacesContext;
031: import javax.faces.event.ActionEvent;
032: import javax.faces.event.ValueChangeEvent;
033: import javax.faces.model.SelectItem;
034: import javax.swing.tree.DefaultMutableTreeNode;
035: import javax.swing.tree.DefaultTreeModel;
036:
037: import org.apache.commons.logging.Log;
038: import org.apache.commons.logging.LogFactory;
039:
040: import wilos.business.services.misc.project.ProjectService;
041: import wilos.business.services.misc.wilosuser.LoginService;
042: import wilos.business.services.misc.wilosuser.ParticipantService;
043: import wilos.business.services.misc.wilosuser.ProjectDirectorService;
044: import wilos.business.services.spem2.process.ProcessService;
045: import wilos.model.misc.concreteactivity.ConcreteActivity;
046: import wilos.model.misc.concreteiteration.ConcreteIteration;
047: import wilos.model.misc.concretemilestone.ConcreteMilestone;
048: import wilos.model.misc.concretephase.ConcretePhase;
049: import wilos.model.misc.concreterole.ConcreteRoleDescriptor;
050: import wilos.model.misc.concretetask.ConcreteTaskDescriptor;
051: import wilos.model.misc.concreteworkproduct.ConcreteWorkProductDescriptor;
052: import wilos.model.misc.project.Project;
053: import wilos.model.misc.wilosuser.Participant;
054: import wilos.model.misc.wilosuser.ProjectDirector;
055: import wilos.model.spem2.process.Process;
056: import wilos.presentation.web.expandabletable.RolesExpTableBean;
057: import wilos.presentation.web.expandabletable.TasksExpTableBean;
058: import wilos.presentation.web.expandabletable.WorkProductsExpTableBean;
059: import wilos.presentation.web.process.ProcessBean;
060: import wilos.presentation.web.utils.WebCommonService;
061: import wilos.presentation.web.utils.WebSessionService;
062: import wilos.presentation.web.viewer.ConcreteActivityViewerBean;
063: import wilos.presentation.web.viewer.ConcreteIterationViewerBean;
064: import wilos.presentation.web.viewer.ConcreteMilestoneViewerBean;
065: import wilos.presentation.web.viewer.ConcretePhaseViewerBean;
066: import wilos.presentation.web.viewer.ConcreteRoleViewerBean;
067: import wilos.presentation.web.viewer.ConcreteTaskViewerBean;
068: import wilos.presentation.web.viewer.ConcreteWorkProductViewerBean;
069: import wilos.presentation.web.viewer.ProjectViewerBean;
070: import wilos.resources.WilosBundle;
071:
072: /**
073: * A basic backing bean for a ice:tree component. The only instance variable
074: * needed is a DefaultTreeModel Object which is bound to the icefaces tree
075: * component in the jspx code. The tree created by this backing bean is used to
076: * control the selected panel in a ice:panelStack.
077: */
078: public class TreeBean {
079:
080: /* Services */
081:
082: private ProjectService projectService;
083:
084: private LoginService loginService;
085:
086: private ParticipantService participantService;
087:
088: private ProcessService processService;
089:
090: private ProjectDirectorService projectDirectorService;
091:
092: /* Simple fields */
093:
094: private Project project;
095:
096: private static final String DEFAULT_PROJECT_ID = "default";
097:
098: private String projectId = DEFAULT_PROJECT_ID;
099:
100: private boolean loadTree;
101:
102: private boolean hideRadio;
103:
104: private String selectedMode = TASKS_MODE;
105:
106: public static final String TASKS_MODE = "tasksMode";
107:
108: public static final String ROLES_MODE = "rolesMode";
109:
110: public static final String WORKPRODUCTS_MODE = "workProductsMode";
111:
112: // tree default model, used as a value for the tree component
113: private DefaultTreeModel model = null;
114:
115: protected final Log logger = LogFactory.getLog(this .getClass());
116:
117: // HashMap which contains the object and his id
118: private HashMap<String, Object> treeMap = new HashMap<String, Object>();
119:
120: /**
121: * Comparator used to sort the components of the treeBean
122: */
123: class WilosMutableTreeNodeComparator implements
124: Comparator<DefaultMutableTreeNode> {
125:
126: public int compare(DefaultMutableTreeNode o1,
127: DefaultMutableTreeNode o2) {
128: if (((WilosObjectNode) o1.getUserObject())
129: .getDisplayOrder().compareTo(
130: ((WilosObjectNode) o2.getUserObject())
131: .getDisplayOrder()) > 0)
132: // o1 preceeds o2
133: return 1;
134: else
135: // o2 preceeds o1
136: return -1;
137: }
138:
139: public WilosMutableTreeNodeComparator() {
140: }
141:
142: }
143:
144: public TreeBean() {
145: this .model = new DefaultTreeModel(this .getDefaultTree());
146: }
147:
148: /**
149: * Gets the default tree which is a node asking for the user to select a
150: * project
151: *
152: * @return the default tree
153: */
154: public DefaultMutableTreeNode getDefaultTree() {
155: DefaultMutableTreeNode defaultTree = new DefaultMutableTreeNode();
156: WilosObjectNode iceUserObject = new WilosObjectNode(defaultTree);
157:
158: iceUserObject.setText(WilosBundle
159: .getText("navigation.tree.defaulttreenodetext"));
160: defaultTree.setUserObject(iceUserObject);
161: return defaultTree;
162: }
163:
164: /**
165: * Refreshes the tree by calling the method buildTreeModel
166: */
167: public void refreshProjectTree() {
168: this .buildTreeModel();
169: }
170:
171: /**
172: * Rebuilds the tree by reloading the project and calling the method
173: * buildTreeModel
174: */
175: public void rebuildProjectTree() {
176: String prId = (String) WebSessionService
177: .getAttribute(WebSessionService.PROJECT_ID);
178: this .project = this .projectService.getProject(prId);
179: this .buildTreeModel();
180: }
181:
182: /**
183: * Cleans the tree, it resets the PROJECT_ID in the Session to default an
184: * calls the method buildTreeModel This method has to be called on
185: * participant log out
186: */
187: public void cleanTreeDisplay() {
188: WebSessionService.setAttribute(WebSessionService.PROJECT_ID,
189: DEFAULT_PROJECT_ID);
190: this .projectId = DEFAULT_PROJECT_ID;
191: this .buildTreeModel();
192: }
193:
194: /**
195: * Builds the tree Model depending on the type of tree selected If the tree
196: * type is TASK_MODE the nodes are sorted before being displayed
197: */
198: private void buildTreeModel() {
199: if (this .projectId != null
200: && !this .projectId.equals(DEFAULT_PROJECT_ID)) {
201:
202: ProjectNode projectNode;
203: if (this .selectedMode.equals(TASKS_MODE)) {
204: projectNode = new ProjectNode(this .project, 1, treeMap);
205: WebSessionService.setAttribute(
206: WebSessionService.TREE_MODE, TASKS_MODE);
207: this .sortModel(projectNode);
208:
209: } else {
210: if (this .selectedMode.equals(ROLES_MODE)) {
211: projectNode = new ProjectNode(this .project, 2,
212: treeMap);
213: WebSessionService.setAttribute(
214: WebSessionService.TREE_MODE, ROLES_MODE);
215: } else {
216: projectNode = new ProjectNode(this .project, 3,
217: treeMap);
218: WebSessionService.setAttribute(
219: WebSessionService.TREE_MODE,
220: WORKPRODUCTS_MODE);
221: }
222: }
223: this .model = new DefaultTreeModel(projectNode);
224: } else {
225: // Build the default tree.
226: this .model = new DefaultTreeModel(this .getDefaultTree());
227: }
228: }
229:
230: /**
231: * Sorts the tree, it uses a WilosMutableTreeNodeComparator
232: *
233: * @param _node
234: * the nodes being sorted
235: */
236: public void sortModel(DefaultMutableTreeNode _node) {
237: TreeSet<DefaultMutableTreeNode> treeSet = new TreeSet<DefaultMutableTreeNode>(
238: new WilosMutableTreeNodeComparator());
239: Enumeration children = _node.children();
240: while (children.hasMoreElements()) {
241: DefaultMutableTreeNode child = (DefaultMutableTreeNode) children
242: .nextElement();
243: if (child.children().hasMoreElements()) {
244: this .sortModel(child);
245: }
246: treeSet.add(child);
247: }
248: for (DefaultMutableTreeNode node : treeSet) {
249: _node.add(node);
250: }
251: }
252:
253: /**
254: * Action listener on the tree
255: *
256: * @param evt
257: * event occurring
258: */
259: public void changeTreeActionListener(ValueChangeEvent evt) {
260: String nodeTypeToShow = DEFAULT_PROJECT_ID;
261:
262: this .projectId = (String) evt.getNewValue();
263: // Put into the session the current project used.
264: WebSessionService.setAttribute(WebSessionService.PROJECT_ID,
265: this .projectId);
266:
267: if (!this .projectId.equals(DEFAULT_PROJECT_ID)) {
268:
269: // Retrieve the entire project.
270: this .project = this .projectService
271: .getProject(this .projectId);
272:
273: nodeTypeToShow = WilosObjectNode.PROJECTNODE;
274:
275: // masquage de la exptable d'instanciation
276: // String projectId = (String)
277: // WebSessionService.getAttribute(WebSessionService.PROJECT_ID);
278: // Project project = this.projectService.getProject(projectId);
279:
280: ProcessBean processBean = (ProcessBean) WebCommonService
281: .getBean("ProcessBean");
282: processBean
283: .setInstanciationDependenciesView("view_instanciation_panelGroup");
284:
285: TasksExpTableBean tasksExpTableBean = (TasksExpTableBean) WebCommonService
286: .getBean("TasksExpTableBean");
287:
288: RolesExpTableBean rolesExpTableBean = (RolesExpTableBean) WebCommonService
289: .getBean("RolesExpTableBean");
290:
291: WorkProductsExpTableBean workProductsExpTableBean = (WorkProductsExpTableBean) WebCommonService
292: .getBean("WorkProductsExpTableBean");
293:
294: if (this .project.getProcess() == null) {
295: processBean.setSelectedProcessId(DEFAULT_PROJECT_ID);
296: tasksExpTableBean
297: .setSelectedProcessId(DEFAULT_PROJECT_ID);
298: rolesExpTableBean
299: .setSelectedProcessId(DEFAULT_PROJECT_ID);
300: workProductsExpTableBean
301: .setSelectedProcessId(DEFAULT_PROJECT_ID);
302: processBean.setIsVisibleExpTable(false);
303: tasksExpTableBean.setIsInstanciedProject(false);
304: tasksExpTableBean.getExpTableContent().clear();
305: } else {
306: Process process = this .projectService
307: .getProcessFromProject(this .project);
308: processBean.setSelectedProcessId(process.getId());
309: processBean
310: .setInstanciationDependenciesView("view_instanciation_panelGroup");
311: tasksExpTableBean.setSelectedProcessId(process.getId());
312: rolesExpTableBean.setSelectedProcessId(process.getId());
313: workProductsExpTableBean.setSelectedProcessId(process
314: .getId());
315: processBean.setIsVisibleExpTable(true);
316: tasksExpTableBean.setIsInstanciedProject(true);
317: }
318: }
319:
320: this .buildTreeModel();
321:
322: if (this .projectId.length() > 0)
323: this .selectNodeToShow(this .projectId, nodeTypeToShow);
324:
325: }
326:
327: /**
328: * Action listener on a node of the tree
329: *
330: * @param evt
331: * event occurring
332: */
333: public void selectNodeActionListener(ActionEvent evt) {
334: FacesContext context = FacesContext.getCurrentInstance();
335: Map map = context.getExternalContext().getRequestParameterMap();
336:
337: String nodeId = (String) map.get("nodeId");
338: String pageId = (String) map.get("pageId");
339:
340: this .selectNodeToShow(nodeId, pageId);
341: }
342:
343: /**
344: * Action listener on the radio button which allows to change the type of
345: * tree ( tasks, roles, work products)
346: *
347: * @param evt
348: * event occurring
349: */
350: public void changeModeActionListener(ValueChangeEvent evt) {
351: this .selectedMode = (String) evt.getNewValue();
352: this .buildTreeModel();
353: }
354:
355: /**
356: * Gets a list of the types of tree ( tasks, roles, work products)
357: *
358: * @return a list of SelectItem
359: */
360: public List<SelectItem> getModesList() {
361: ArrayList<SelectItem> modesList = new ArrayList<SelectItem>();
362:
363: modesList.add(new SelectItem(TASKS_MODE, WilosBundle
364: .getText("navigation.tree.checkboxlabel.tasks")));
365: modesList.add(new SelectItem(ROLES_MODE, WilosBundle
366: .getText("navigation.tree.checkboxlabel.roles")));
367: modesList
368: .add(new SelectItem(
369: WORKPRODUCTS_MODE,
370: WilosBundle
371: .getText("navigation.tree.checkboxlabel.workproducts")));
372:
373: return modesList;
374: }
375:
376: /**
377: * Getter for the combobox representing the projects to which the
378: * participant is affected
379: *
380: * @return a list of SelectItem
381: */
382:
383: public List<SelectItem> getProjects() {
384: List<SelectItem> projectsList = new ArrayList<SelectItem>();
385: // projectDirector
386: String role = (String) WebSessionService
387: .getAttribute(WebSessionService.ROLE_TYPE);
388: // if ((_applicationRole == "participant_role") ||
389: // (_applicationRole.equals("projectDirector_role")))
390: if (role.equals("participant")) {
391:
392: String wilosUserId = (String) WebSessionService
393: .getAttribute(WebSessionService.WILOS_USER_ID);
394: Participant participant = this .participantService
395: .getParticipant(wilosUserId);
396:
397: if (participant != null) {
398: HashMap<Project, Boolean> projects = this .participantService
399: .getProjectsForAParticipant(participant);
400: for (Project project : projects.keySet()) {
401: if (projects.get(project)) {
402: this .addSelectItemToList(projectsList,
403: new SelectItem(project.getId(), project
404: .getConcreteName()));
405: }
406: }
407: }
408: }
409: if (role.equals("projectDirector")) {
410:
411: String wilosUserId = (String) WebSessionService
412: .getAttribute(WebSessionService.WILOS_USER_ID);
413: ProjectDirector pd = new ProjectDirector();
414: pd = this .projectDirectorService
415: .getProjectDirector(wilosUserId);
416: for (Project project : projectService
417: .getAllSortedProjects()) {
418: if (project.getProjectDirector().getId().equals(
419: pd.getId())) {
420: this .addSelectItemToList(projectsList,
421: new SelectItem(project.getId(), project
422: .getConcreteName()));
423: }
424: }
425:
426: }
427: projectsList
428: .add(
429: 0,
430: new SelectItem(
431: DEFAULT_PROJECT_ID,
432: WilosBundle
433: .getText("navigation.tree.defaulttreenodetext")));
434: return projectsList;
435: }
436:
437: /**
438: * Inserts the SelectItem _si representing a project into the list used by
439: * the combobox
440: *
441: * @param _projectsList
442: * the projectsList
443: * @param _si
444: * the SelectItem
445: */
446: private void addSelectItemToList(List<SelectItem> _projectsList,
447: SelectItem _si) {
448: if (_projectsList.size() == 0)
449: _projectsList.add(_si);
450: else {
451: int i;
452: // inserting the project in an alphabetically ordered list
453: for (i = 0; i < _projectsList.size()
454: && _si.getLabel().compareTo(
455: _projectsList.get(i).getLabel()) > 0; i++) {
456: }
457: _projectsList.add(i, _si);
458: }
459: }
460:
461: /**
462: * Selects to node to show depending on the id of the object and the page id
463: * @param _objectId id of the object being shown
464: * @param _pageId id of the page to be displayed in the browser
465: */
466: private void selectNodeToShow(String _objectId, String _pageId) {
467: if (_objectId != null && _pageId != null) {
468: if (_pageId.equals(WilosObjectNode.ACTIVITYNODE)) {
469: ConcreteActivityViewerBean av = (ConcreteActivityViewerBean) WebCommonService
470: .getBean(WilosObjectNode.ACTIVITYNODE + "Bean");
471:
472: // recovers the object in the HashMap for the viewer
473: ConcreteActivity ca = (ConcreteActivity) treeMap
474: .get(_objectId);
475: av.setConcreteActivity(ca);
476:
477: WebCommonService.changeContentPage(_pageId);
478: } else if (_pageId.equals(WilosObjectNode.CONCRETETASKNODE)) {
479: ConcreteTaskViewerBean ctv = (ConcreteTaskViewerBean) WebCommonService
480: .getBean(WilosObjectNode.CONCRETETASKNODE
481: + "Bean");
482:
483: // recovers the object in the HashMap for the viewer
484: ConcreteTaskDescriptor ctd = (ConcreteTaskDescriptor) treeMap
485: .get(_objectId);
486: ctv.setConcreteTaskDescriptor(ctd);
487:
488: WebCommonService.changeContentPage(_pageId);
489: } else if (_pageId
490: .equals(WilosObjectNode.CONCRETEMILESTONENODE)) {
491: ConcreteMilestoneViewerBean cmiv = (ConcreteMilestoneViewerBean) WebCommonService
492: .getBean(WilosObjectNode.CONCRETEMILESTONENODE
493: + "Bean");
494:
495: // recovers the object in the HashMap for the viewer
496: ConcreteMilestone cmi = (ConcreteMilestone) treeMap
497: .get(_objectId);
498: cmiv.setConcreteMilestone(cmi);
499:
500: WebCommonService.changeContentPage(_pageId);
501: } else if (_pageId.equals(WilosObjectNode.CONCRETEROLENODE)) {
502: ConcreteRoleViewerBean crv = (ConcreteRoleViewerBean) WebCommonService
503: .getBean(WilosObjectNode.CONCRETEROLENODE
504: + "Bean");
505:
506: // recovers the object in the HashMap for the viewer
507: ConcreteRoleDescriptor crd = (ConcreteRoleDescriptor) treeMap
508: .get(_objectId);
509: crv.setConcreteRoleDescriptor(crd);
510:
511: WebCommonService.changeContentPage(_pageId);
512:
513: } else if (_pageId
514: .equals(WilosObjectNode.CONCRETEWORKPRODUCTNODE)) {
515:
516: ConcreteWorkProductViewerBean cwpv = (ConcreteWorkProductViewerBean) WebCommonService
517: .getBean(WilosObjectNode.CONCRETEWORKPRODUCTNODE
518: + "Bean");
519:
520: // recovers the object in the HashMap for the viewer
521: ConcreteWorkProductDescriptor cwpd = (ConcreteWorkProductDescriptor) treeMap
522: .get(_objectId);
523: cwpv.setConcreteWorkProductDescriptor(cwpd);
524:
525: WebCommonService.changeContentPage(_pageId);
526:
527: } else if (_pageId.equals(WilosObjectNode.ITERATIONNODE)) {
528: ConcreteIterationViewerBean iv = (ConcreteIterationViewerBean) WebCommonService
529: .getBean(WilosObjectNode.ITERATIONNODE + "Bean");
530:
531: // recovers the object in the HashMap for the viewer
532: ConcreteIteration ci = (ConcreteIteration) treeMap
533: .get(_objectId);
534: iv.setConcreteIteration(ci);
535:
536: WebCommonService.changeContentPage(_pageId);
537: } else if (_pageId.equals(WilosObjectNode.PHASENODE)) {
538: ConcretePhaseViewerBean pb = (ConcretePhaseViewerBean) WebCommonService
539: .getBean(WilosObjectNode.PHASENODE + "Bean");
540:
541: // recovers the object in the HashMap for the viewer
542: ConcretePhase cp = (ConcretePhase) treeMap
543: .get(_objectId);
544: pb.setConcretePhase(cp);
545:
546: WebCommonService.changeContentPage(_pageId);
547: } else if (_pageId.equals(WilosObjectNode.PROJECTNODE)) {
548: ProjectViewerBean p = (ProjectViewerBean) WebCommonService
549: .getBean(WilosObjectNode.PROJECTNODE + "Bean");
550:
551: // recovers the object in the HashMap for the viewer
552: Project proj = (Project) treeMap.get(_objectId);
553: p.setProject(proj);
554:
555: WebCommonService.changeContentPage(_pageId);
556: } else {
557: // displays blank page
558: WebCommonService.changeContentPage("wilos");
559: }
560: }
561: }
562:
563: /* Getters & Setters */
564:
565: public DefaultTreeModel getModel() {
566: return this .model;
567: }
568:
569: public String getProjectId() {
570: return this .projectId;
571: }
572:
573: public void setProjectId(String _processId) {
574: this .projectId = _processId;
575: }
576:
577: public ProjectService getProjectService() {
578: return projectService;
579: }
580:
581: public void setProjectService(ProjectService projectService) {
582: this .projectService = projectService;
583: }
584:
585: public Boolean getLoadTree() {
586: if (this .projectId != null
587: && !this .projectId.equals(DEFAULT_PROJECT_ID))
588: this .loadTree = false;
589: else
590: this .loadTree = true;
591: return this .loadTree;
592: }
593:
594: public void setLoadTree(Boolean loadTree) {
595: this .loadTree = loadTree;
596: }
597:
598: public LoginService getLoginService() {
599: return loginService;
600: }
601:
602: public void setLoginService(LoginService loginService) {
603: this .loginService = loginService;
604: }
605:
606: public ParticipantService getParticipantService() {
607: return participantService;
608: }
609:
610: public void setParticipantService(
611: ParticipantService participantService) {
612: this .participantService = participantService;
613: }
614:
615: public ProcessService getProcessService() {
616: return processService;
617: }
618:
619: public void setProcessService(ProcessService processService) {
620: this .processService = processService;
621: }
622:
623: /**
624: * Gets the tree map
625: * @return the HashMap
626: */
627: public HashMap<String, Object> getTreeMap() {
628: return treeMap;
629: }
630:
631: /**
632: * Sets the tree map
633: * @param _treeMap the new HashMap
634: */
635: public void setTreeMap(HashMap<String, Object> _treeMap) {
636: this .treeMap = _treeMap;
637: }
638:
639: /**
640: * Gets the selected mode
641: * @return the selectedMode
642: */
643: public String getSelectedMode() {
644: return this .selectedMode;
645: }
646:
647: /**
648: * Sets the select mode
649: * @param selectedMode the selectedMode to set
650: */
651: public void setSelectedMode(String _selectedMode) {
652: this .selectedMode = _selectedMode;
653: }
654:
655: /**
656: * Gets the hidden state of the radio buttons
657: * @return
658: */
659: public boolean getHideRadio() {
660: if (this .projectId != null
661: && !this .projectId.equals(DEFAULT_PROJECT_ID))
662: this .hideRadio = true;
663: else
664: this .hideRadio = false;
665: return this .hideRadio;
666: }
667:
668: /**
669: * Hides/shows the radio buttons
670: * @param _hideRadio
671: */
672: public void setHideRadio(boolean _hideRadio) {
673: this .hideRadio = _hideRadio;
674: }
675:
676: /**
677: * Gets the projectDirectorService
678: * @return the projectDirectorService
679: */
680: public ProjectDirectorService getProjectDirectorService() {
681: return projectDirectorService;
682: }
683:
684: /**
685: * Sets the projectDirectorService
686: * @param _projectDirectorService the new value
687: */
688: public void setProjectDirectorService(
689: ProjectDirectorService _projectDirectorService) {
690: projectDirectorService = _projectDirectorService;
691: }
692: }
|