001: package org.emforge.jbpm.web.bean;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Date;
006: import java.util.LinkedList;
007:
008: import javax.faces.model.SelectItem;
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.EmForgeException;
015: import org.emforge.projectmanager.ProjectService;
016: import org.emforge.projectmanager.base.ProjectDO;
017: import org.emforge.xfer.PriorityTO;
018: import org.emforge.xfer.StepTO;
019: import org.emforge.xfer.TransitionTO;
020: import org.emforge.xfer.VariableTO;
021: import org.emforge.xfer.WorkflowTO;
022:
023: import ru.emdev.EmForge.security.EmForgeUserDetails;
024: import ru.emdev.EmForge.security.UserFactory;
025: import ru.emdev.EmForge.security.dao.Role;
026:
027: /**
028: * Bean to represent Step in GUI
029: */
030: public class StepBean {
031:
032: protected final Log logger = LogFactory.getLog(getClass());
033:
034: private Integer num;
035: private StepTO step;
036: private PriorityTO priority;
037:
038: private static final String NEXT_ACTION_DEFAULT_TRANSITION = "__default_transition__";
039: private static final String NEXT_ACTION_SUBPROCESS = "__subprocess__";
040:
041: /** Next Action Name */
042: private String nextAction = NEXT_ACTION_DEFAULT_TRANSITION;
043:
044: /** Process Definition to create subprocess */
045: private WorkflowTO subWorkflow;
046: private String stepMessage;
047: private String reassignTo;
048:
049: private TaskController taskController;
050: private ProjectService projectService;
051: private UserFactory userFactory;
052: private BpmService bpmService;
053: private NewTaskController newTaskController;
054:
055: /**
056: * @param taskController
057: * @param num
058: * @param globalselectItems
059: * @param projectService
060: * @param userFactory
061: * @param bpmService
062: * @param newTaskController
063: * @param step
064: */
065: public StepBean(TaskController taskController, Integer num,
066: ProjectService projectService, UserFactory userFactory,
067: BpmService bpmService, NewTaskController newTaskController,
068: StepTO step) {
069:
070: this .num = num;
071: this .taskController = taskController;
072: this .projectService = projectService;
073: this .userFactory = userFactory;
074: this .bpmService = bpmService;
075: this .newTaskController = newTaskController;
076:
077: this .step = step;
078:
079: this .priority = step.getPriority();
080: }
081:
082: /**
083: * @return
084: */
085: public Integer getNum() {
086:
087: return num;
088: }
089:
090: /**
091: * @return
092: */
093: public Long getId() {
094:
095: return step.getId();
096: }
097:
098: /**
099: * @return
100: */
101: public PriorityTO getPriority() {
102:
103: return priority;
104: }
105:
106: /**
107: * @param i_priority
108: */
109: public void setPriority(PriorityTO i_priority) {
110:
111: priority = i_priority;
112: }
113:
114: /**
115: * @return
116: */
117: public String getOwner() {
118:
119: return step.getActor();
120: }
121:
122: /**
123: * @return
124: */
125: public Date getStartTime() {
126:
127: return step.getActualStartTime();
128: }
129:
130: /**
131: * @return
132: */
133: public String getName() {
134:
135: return step.getName();
136: }
137:
138: /**
139: * @return
140: */
141: public Date getDueDate() {
142:
143: return step.getDueDate();
144: }
145:
146: /**
147: * @return
148: */
149: public String getStepMessage() {
150:
151: return stepMessage;
152: }
153:
154: /**
155: * Is current user can work with this step?
156: *
157: * @return
158: */
159: public boolean isCanSubmit() {
160: return bpmService.canCompleteStep(step);
161: }
162:
163: public boolean isCanRequestStatus() throws EmForgeException {
164: return bpmService.isPossibleRequestStatus(step.getId());
165: }
166:
167: /**
168: * In case then step is assigned to role - return it. Othervise return null
169: *
170: * @return
171: */
172: public Role getAssignedRole() {
173:
174: if (step == null) {
175: return null;
176: }
177:
178: // Is task owner - role name?
179: Role role = userFactory.getRole(step.getActor());
180: return role;
181: }
182:
183: /**
184: * @return
185: */
186: public VariableTO[] getVariables() {
187: return step.getWritableVariables();
188: }
189:
190: // actions
191:
192: /**
193: * @return
194: */
195: public String getNextAction() {
196:
197: return nextAction;
198: }
199:
200: /**
201: * @param nextAction
202: */
203: public void setNextAction(String nextAction) {
204:
205: this .nextAction = nextAction;
206: }
207:
208: /**
209: * Return List of Next Options
210: *
211: * @return
212: */
213: public Collection<SelectItem> getNextActions() {
214:
215: Collection<SelectItem> result = new LinkedList<SelectItem>();
216:
217: if (userFactory.getCurrentUser().isAnonymous()) {
218: return result;
219: }
220:
221: TransitionTO[] transitions = step.getTransitions();
222:
223: for (TransitionTO transition : transitions) {
224: String transitionName = transition.getName();
225: if (StringUtils.isEmpty(transitionName)) {
226: transitionName = NEXT_ACTION_DEFAULT_TRANSITION;
227: }
228:
229: result.add(new SelectItem(transitionName, transition
230: .getLabel()));
231: }
232:
233: result.add(new SelectItem(NEXT_ACTION_SUBPROCESS,
234: "Create a new subtask"));
235:
236: return result;
237: }
238:
239: /**
240: * @return
241: */
242: public WorkflowTO getSubWorkflow() {
243:
244: return subWorkflow;
245: }
246:
247: /**
248: * @param i_subProcessDef
249: */
250: public void setSubWorkflow(WorkflowTO i_subWorkflow) {
251:
252: subWorkflow = i_subWorkflow;
253: }
254:
255: /**
256: * @return
257: */
258: public String getReassignTo() {
259:
260: return reassignTo;
261: }
262:
263: /**
264: * @param i_reassignTo
265: */
266: public void setReassignTo(String i_reassignTo) {
267:
268: reassignTo = i_reassignTo;
269: }
270:
271: /**
272: * @return
273: */
274: public Collection<SelectItem> getWorkflows()
275: throws EmForgeException {
276:
277: Collection<SelectItem> result = new ArrayList<SelectItem>();
278:
279: result.add(new SelectItem("", ""));
280:
281: WorkflowTO[] workflows = bpmService.getWorkflows();
282: for (WorkflowTO workflow : workflows) {
283: result
284: .add(new SelectItem(workflow, workflow
285: .getStartName()));
286: }
287:
288: return result;
289: }
290:
291: /**
292: * @return
293: */
294: public String submit() {
295:
296: if (!isCanSubmit()) {
297: return null;
298: }
299:
300: if (nextAction.equals(NEXT_ACTION_SUBPROCESS)) {
301: // add comment
302: taskController.addComment();
303:
304: if (subWorkflow != null) {
305: // start new process wizard for subprocess
306: newTaskController.clean();
307: newTaskController.setTaskProject(projectService
308: .getProject(step.getProjectName()));
309: newTaskController.setWorkflow(subWorkflow);
310: newTaskController.setParentStep(step);
311: // skip workflow selection
312: newTaskController.next();
313: return "newtask";
314: }
315:
316: return null;
317: } else {
318: // complete task with specified transition
319: String transitionName = nextAction;
320: if (transitionName.equals(NEXT_ACTION_DEFAULT_TRANSITION)) {
321: transitionName = "";
322: }
323:
324: try {
325: bpmService.completeStep(step, transitionName,
326: taskController.getComment());
327: taskController.clearComment();
328: } catch (EmForgeException ex) {
329: logger.warn("Cannot close task:", ex);
330:
331: taskController.addErrorMessage(ex.getMessage(), ex
332: .getMessage());
333: return null;
334: }
335:
336: // always stay in the process - this method will refresh all
337: // data in controller
338: taskController.setTaskId(step.getTaskId());
339: return null;
340: }
341: }
342:
343: /**
344: * @return
345: */
346: public String changeStepPriority() {
347: try {
348: step = bpmService.changeStepPriority(step, priority,
349: taskController.getComment());
350: taskController.clearComment();
351: } catch (EmForgeException ex) {
352: stepMessage = "Cannot change priority: " + ex.getMessage();
353: return null;
354: }
355:
356: stepMessage = "Individual Step Priority changed to "
357: + priority.getName();
358: return null;
359: }
360:
361: /**
362: * @return
363: */
364: public String assignToMe() {
365: String userName = userFactory.getCurrentUser().getUsername();
366:
367: try {
368: // save new task information into database
369: step = bpmService.assignStep(step, userName, taskController
370: .getComment());
371: taskController.clearComment();
372: } catch (EmForgeException ex) {
373: taskController.addErrorMessage("Cannot reassign step", ex
374: .getMessage());
375: }
376:
377: return null;
378: }
379:
380: public Collection<SelectItem> getReassignToUserNames()
381: throws Exception {
382:
383: Collection<SelectItem> result = new ArrayList<SelectItem>();
384:
385: String swimlaneName = step.getSwimlane();
386: Role role = null;
387: if (swimlaneName != null) {
388: role = userFactory.getRole(swimlaneName);
389: if (role != null) {
390: result.add(new SelectItem(swimlaneName, role
391: .getDecoratedName()));
392: }
393: }
394:
395: fillUsersCollection(role, result);
396: return result;
397: }
398:
399: /**
400: * Fills specified collection with users of specified role
401: *
402: * @param role a role to get users
403: * @param result a collection to return
404: * @author szakusov, 21.02.2008: Implemented for request [^74641]
405: */
406: protected void fillUsersCollection(Role role,
407: Collection<SelectItem> result) {
408:
409: ProjectDO project = projectService.getProject(step
410: .getProjectName());
411: Collection<EmForgeUserDetails> users = null;
412:
413: if (role != null) {
414: users = projectService.getUsers(project, role);
415: } else {
416: users = projectService.getAllProjectUsers(project);
417: }
418:
419: for (EmForgeUserDetails user : users) {
420: result.add(new SelectItem(user.getUsername(), user
421: .getDisplayName()));
422: }
423: }
424:
425: /**
426: * @return
427: */
428: public String reassignStep() {
429:
430: if (StringUtils.isNotEmpty(reassignTo)) {
431: try {
432: step = bpmService.assignStep(step, reassignTo,
433: taskController.getComment());
434:
435: taskController.clearComment();
436: } catch (EmForgeException ex) {
437: stepMessage = "Cannot reassing step:" + ex.getMessage();
438: }
439: }
440:
441: return null;
442: }
443:
444: /**
445: * @todo This code is dublicated with BpmServiceImpl - need to be refactored
446: * @return
447: */
448: public boolean isGroupMember() {
449: ProjectDO project = projectService.getProject(step
450: .getProjectName());
451: if (project == null) {
452: return false;
453: }
454:
455: // Is task owner - role name?
456: Role role = getAssignedRole(step);
457: if (role == null) {
458: return false;
459: }
460:
461: return projectService.hasRole(project, userFactory
462: .getCurrentUser(), role);
463: }
464:
465: /**
466: * @todo This code is dublicated with BpmServiceImpl - need to be refactored
467: * @return
468: */
469: private Role getAssignedRole(StepTO i_step) {
470: // Is task owner - role name?
471: Role role = userFactory.getRole(i_step.getActor());
472: return role;
473: }
474: }
|