001: package org.emforge.jbpm.web.bean;
002:
003: import javax.faces.context.ExternalContext;
004: import javax.faces.context.FacesContext;
005: import javax.servlet.http.HttpServletRequest;
006:
007: import org.apache.commons.lang.StringUtils;
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010: import org.emforge.BpmService;
011: import org.emforge.EmForgeException;
012: import org.emforge.xfer.AttachmentTO;
013: import org.emforge.xfer.HistoryTO;
014: import org.emforge.xfer.WorkflowTO;
015:
016: import ru.emdev.EmForge.security.web.SiteRole;
017: import ru.emdev.EmForge.web.bean.BaseControllerImpl;
018: import ru.emdev.EmForge.web.bean.MainMenuController.MainMenuItem;
019: import ru.emdev.EmForge.wiki.web.bean.Crumb;
020:
021: import com.ecyrd.jspwiki.WikiPage;
022:
023: /** Workflow controller is used for processing workflow page
024: *
025: */
026: public class WorkflowController extends BaseControllerImpl {
027: public static final String WORKFLOW_PAGE_NAME = "workflow";
028: public static final String WORKFLOW_ID_ATTR = "workflowid";
029: public static final String WORKFLOW_PAGE_ATTR = "page";
030: public static final String WORKFLOW_VERSION_ATTR = "version";
031:
032: protected final Log logger = LogFactory.getLog(getClass());
033:
034: /** Processed Workflow*/
035: private WorkflowTO m_workflow;
036:
037: /** BPM Context */
038: BpmService m_bpmService;
039:
040: /** Task Controller
041: *
042: * Used to start new process
043: */
044: NewTaskController m_newTaskController;
045:
046: private String m_pageName;
047: private WikiPage m_wikiPage;
048:
049: public void setBpmService(BpmService i_bpmService) {
050: m_bpmService = i_bpmService;
051: }
052:
053: public void setNewTaskController(
054: NewTaskController i_newTaskController) {
055: m_newTaskController = i_newTaskController;
056: }
057:
058: @Override
059: public MainMenuItem getSelectionItemOnMainMenu() {
060: return MainMenuItem.TASKS;
061: }
062:
063: @Override
064: public Crumb getTrailCrumbInfo() {
065: if (m_workflow == null) {
066: return null;
067: }
068: String displayName;
069: String url;
070: try {
071: displayName = m_workflow.getName();
072: url = WORKFLOW_PAGE_NAME + "/" + displayName;
073: } catch (Exception ex) {
074: logger.debug("Cannot get trail crumb info: ", ex);
075: return null;
076: }
077:
078: return new Crumb(displayName, url);
079: }
080:
081: public String getTitleImpl() {
082: if (m_workflow != null) {
083: return "Workflow: " + m_workflow.getName();
084: } else {
085: return "Unknown Workflow";
086: }
087: }
088:
089: public void init() {
090: try {
091: if (m_bpmService != null) {
092: //get milestone by id
093: // moved initialization here - so, hibernate session is available
094: ExternalContext context = FacesContext
095: .getCurrentInstance().getExternalContext();
096: HttpServletRequest request = (HttpServletRequest) context
097: .getRequest();
098:
099: // get id
100: String idStr = request.getParameter(WORKFLOW_ID_ATTR);
101: if (StringUtils.isNotEmpty(idStr)) {
102: long id = Long.valueOf(idStr).longValue();
103:
104: // get process definition by id
105: WorkflowTO workflow = m_bpmService
106: .getWorkflowById(id);
107: setWorkflow(workflow);
108: m_pageName = workflow.getName();
109: } else {
110: m_pageName = request
111: .getParameter(WORKFLOW_PAGE_ATTR);
112:
113: String versionStr = request
114: .getParameter(WORKFLOW_VERSION_ATTR);
115: Integer version = null;
116: try {
117: version = Integer.valueOf(versionStr);
118: } catch (Exception ex) {
119: // ignore any exception here
120: }
121:
122: if (version != null) {
123: // read workflow by pageName and version
124: setWorkflow(m_bpmService.getWorkflowByName(
125: m_pageName, version));
126: } else {
127: // read workflow by pageName
128: setWorkflow(m_bpmService.getWorkflowByName(
129: m_pageName,
130: BpmService.WORKFLOW_LAST_VERSION));
131: }
132: }
133:
134: if (m_pageName != null) {
135: m_wikiPage = m_wikiEngine.getPage(m_pageName);
136: }
137:
138: }
139: } catch (EmForgeException ex) {
140: logger.error("Cannot read workflow", ex);
141: }
142: }
143:
144: public WorkflowTO getWorkflow() {
145: return m_workflow;
146: }
147:
148: public void setWorkflow(WorkflowTO i_workflow) {
149: m_workflow = i_workflow;
150: }
151:
152: /** Starts new process
153: *
154: * @return
155: */
156: public String startNewProcess() {
157: m_newTaskController.clean();
158:
159: m_newTaskController.setWorkflow(m_workflow);
160:
161: return "newtask";
162: }
163:
164: public String getPageName() {
165: return m_pageName;
166: }
167:
168: public void setPageName(String pageName) {
169: m_pageName = pageName;
170: }
171:
172: public boolean isPageExists() {
173: return m_wikiPage != null;
174: }
175:
176: @SuppressWarnings("unchecked")
177: public AttachmentTO[] getAttachments() {
178: if (m_wikiPage != null) {
179: try {
180: return m_attachmentService.getAttachments(m_wikiPage
181: .getName());
182: } catch (EmForgeException ex) {
183: logger.error("Cannot get attachments", ex);
184: }
185: }
186:
187: return new AttachmentTO[0];
188: }
189:
190: public HistoryTO[] getWorkflowHistory() throws EmForgeException {
191: if (m_workflow != null) {
192: return m_bpmService.getWorkflowHistory(
193: m_workflow.getName(), m_workflow.getVersion());
194: } else {
195: return new HistoryTO[0];
196: }
197: }
198:
199: public WorkflowTO[] getUsedWorkflows() throws EmForgeException {
200: if (m_workflow != null) {
201: return m_bpmService.getUsedWorkflows(m_workflow.getName(),
202: m_workflow.getVersion());
203: } else {
204: return new WorkflowTO[0];
205: }
206: }
207:
208: public boolean isCanStartProcess() {
209: return !getCurrentUser().isAnonymous();
210: }
211:
212: public boolean isCanAddAttachment() {
213: return getCurrentUser().hasRole(SiteRole.ADMIN.getId());
214: }
215:
216: public boolean isCanEditDescription() {
217: return getCurrentUser().hasRole(SiteRole.ADMIN.getId());
218: }
219: }
|