001: package org.emforge.jbpm;
002:
003: import java.util.Collection;
004: import java.util.Date;
005: import java.util.LinkedList;
006: import java.util.List;
007: import java.util.Map;
008:
009: import org.apache.commons.collections.Transformer;
010: import org.emforge.projectmanager.ProjectService;
011: import org.emforge.projectmanager.base.MilestoneDO;
012: import org.emforge.projectmanager.base.ProjectDO;
013: import org.emforge.xfer.BlockerInfoTO;
014: import org.emforge.xfer.TaskTO;
015: import org.emforge.xfer.VariableTO;
016: import org.jbpm.context.exe.ContextInstance;
017: import org.jbpm.graph.exe.ProcessInstance;
018: import org.jbpm.graph.exe.Token;
019: import org.jbpm.taskmgmt.def.Task;
020: import org.jbpm.taskmgmt.exe.TaskInstance;
021: import org.jbpm.taskmgmt.exe.TaskMgmtInstance;
022:
023: import ru.emdev.EmForge.security.UserFactory;
024: import ru.emdev.EmForge.wiki.WikiTextToXHTMLConverter;
025:
026: import com.ecyrd.jspwiki.WikiEngine;
027: import com.ecyrd.jspwiki.WikiPage;
028:
029: /** Class for converting ProcessInstance into TaskTO
030: *
031: */
032: public class ProcessTransformer implements Transformer {
033: private BpmServiceImpl bpmService;
034: private ProjectService projectService;
035: private UserFactory userFactory;
036: private WikiEngine wikiEngine;
037: private boolean fillVars;
038:
039: public ProcessTransformer(BpmServiceImpl bpmService,
040: ProjectService projectService, UserFactory userFactory,
041: WikiEngine wikiEngine, boolean fillVars) {
042: this .bpmService = bpmService;
043: this .projectService = projectService;
044: this .userFactory = userFactory;
045: this .wikiEngine = wikiEngine;
046: this .fillVars = fillVars;
047: }
048:
049: public TaskTO transform(Object i_input) {
050: ProcessInstance process = (ProcessInstance) i_input;
051: TaskTO task = new TaskTO();
052:
053: task.setId(process.getId());
054: task.setTitle(getTitle(process));
055:
056: task.setDescription(getTaskDescription(process));
057:
058: task.setOwner(getOwner(process));
059:
060: task.setPriority(new PriorityTransformer()
061: .transform(getPriority(process)));
062:
063: ProcessInstance parentProcess = getParentProcess(process);
064: task.setParentTaskId(parentProcess != null ? parentProcess
065: .getId() : null);
066:
067: TaskInstance parentTask = bpmService.getParentTask(process);
068: task.setParentStepId(parentTask != null ? parentTask.getId()
069: : null);
070:
071: task.setWorkflowName(process.getProcessDefinition().getName());
072: task.setWorkflowVersion(process.getProcessDefinition()
073: .getVersion());
074: task.setWorkflowIconLink(ProcessDefTransformer
075: .getWorkflowIconLink(process.getProcessDefinition()));
076:
077: task.setStartTime(process.getStart());
078: task.setEndTime(process.getEnd());
079: task.setDueDate(getDueDate(process));
080: task.setLastChangedDate(bpmService
081: .getLastProcessChangesDate(process));
082:
083: task.setHasEnded(process.hasEnded());
084: // set vars only in case then it is required (for speeding up the displaying lists of tasks)
085: // null in vars will mean vars are not readed
086: if (fillVars) {
087: task.setVariables(getVariables(process));
088: }
089: ProjectDO project = getProject(process);
090: task.setProjectName(project != null ? project.getName() : null);
091:
092: if (project != null) {
093: MilestoneDO milestone = getMilestone(process);
094: task.setMilestoneName(milestone != null ? milestone
095: .getName() : null);
096: }
097:
098: task.setBlockers(getAllBlockers(process));
099: return task;
100: }
101:
102: /** Generates HTML-Description for Task
103: *
104: * @param i_process
105: * @return
106: */
107: private String getTaskDescription(ProcessInstance i_process) {
108: String description = null;
109: String wikiName = String.valueOf(i_process.getId());
110:
111: // get page
112: WikiPage page = wikiEngine.getPage(wikiName, -1);
113:
114: // page exist?
115: if (page != null) {
116: WikiTextToXHTMLConverter converter = new WikiTextToXHTMLConverter(
117: wikiEngine, wikiName);
118:
119: // convert page to HTML
120: description = converter.getAsString(null, null, page);
121: }
122:
123: return description;
124: }
125:
126: @SuppressWarnings("unchecked")
127: private String getTitle(ProcessInstance process) {
128:
129: // gets process variables
130: ContextInstance context = process.getContextInstance();
131: Map<String, Object> vars = context.getVariables();
132:
133: // [AKA] once I've got vars as null, I do not know why it happend
134: // but it is better to check vars for null too
135: if (vars != null
136: && vars.containsKey(BpmVariable.TITLE.getVariable())) {
137: return (String) vars.get(BpmVariable.TITLE.getVariable());
138: }
139:
140: return null;
141: }
142:
143: protected static String getOwner(ProcessInstance process) {
144:
145: ContextInstance context = process.getContextInstance();
146: assert context != null;
147:
148: if (context.hasVariable(BpmVariable.OWNER.getVariable())
149: && context.getVariable(BpmVariable.OWNER.getVariable()) != null) {
150: return context.getVariable(BpmVariable.OWNER.getVariable())
151: .toString();
152: } else {
153: return null;
154: }
155: }
156:
157: private ProcessInstance getParentProcess(ProcessInstance process) {
158:
159: Token token = process.getSuperProcessToken();
160: if (token != null) {
161: return token.getProcessInstance();
162: } else {
163: TaskInstance parentTask = bpmService.getParentTask(process);
164:
165: if (parentTask != null) {
166: return parentTask.getToken().getProcessInstance();
167: }
168: }
169:
170: return null;
171: }
172:
173: public static Date getDueDate(ProcessInstance process) {
174:
175: ContextInstance context = process.getContextInstance();
176: assert context != null;
177:
178: Date dueDate = null;
179: if (context.hasVariable(BpmVariable.DUEDATE.getVariable())
180: && context.getVariable(BpmVariable.DUEDATE
181: .getVariable()) != null) {
182: dueDate = (Date) context.getVariable(BpmVariable.DUEDATE
183: .getVariable());
184: }
185:
186: return dueDate;
187: }
188:
189: @SuppressWarnings("unchecked")
190: public static ProjectDO getProject(ProcessInstance i_process) {
191: // gets process variables
192: ContextInstance context = i_process.getContextInstance();
193: Map<String, Object> vars = context.getVariables();
194:
195: // [AKA] once I've got vars as null, I do not know why it happend
196: // but it is better to check vars for null too
197: if (vars != null
198: && vars.containsKey(BpmVariable.PROJECT.getVariable())
199: && vars.get(BpmVariable.PROJECT.getVariable()) instanceof ProjectDO) {
200: return (ProjectDO) vars.get(BpmVariable.PROJECT
201: .getVariable());
202: }
203:
204: return null;
205:
206: }
207:
208: @SuppressWarnings("unchecked")
209: private MilestoneDO getMilestone(ProcessInstance process) {
210:
211: // gets process variables
212: Map<String, Object> vars = process.getContextInstance()
213: .getVariables();
214:
215: if (vars != null
216: && vars
217: .containsKey(BpmVariable.MILESTONE
218: .getVariable())
219: && vars.get(BpmVariable.MILESTONE.getVariable()) instanceof MilestoneDO) {
220: return (MilestoneDO) vars.get(BpmVariable.MILESTONE
221: .getVariable());
222: }
223:
224: return null;
225: }
226:
227: @SuppressWarnings("unchecked")
228: private VariableTO[] getVariables(ProcessInstance process) {
229:
230: List<VariableTO> processVars = new LinkedList<VariableTO>();
231:
232: // gets process variables
233: ContextInstance context = process.getContextInstance();
234: Map<String, Object> vars = context.getVariables();
235:
236: // end now, make a list from this map
237: VariableTransformer varTransformer = new VariableTransformer(
238: projectService, userFactory, process, null);
239:
240: for (String key : vars.keySet()) {
241: // skip internal process variables
242: if (!key.equals(BpmVariable.OWNER.getVariable())
243: && !key.startsWith("_")) {
244: Object value = vars.get(key);
245: VariableTO var = varTransformer.transform(key, value);
246:
247: // add variable into list
248: processVars.add(var);
249: }
250: }
251:
252: return processVars.toArray(new VariableTO[processVars.size()]);
253: }
254:
255: static protected void setPriority(ProcessInstance process,
256: int priority) {
257: ContextInstance context = process.getContextInstance();
258: assert context != null;
259:
260: context.setVariable(BpmVariable.PRIORITY.getVariable(),
261: priority);
262: }
263:
264: @SuppressWarnings("unchecked")
265: public static Integer getPriority(ProcessInstance process) {
266:
267: // first, try to get it from variable
268:
269: //
270: ContextInstance context = process.getContextInstance();
271: assert context != null;
272:
273: if (context.hasVariable(BpmVariable.PRIORITY.getVariable())
274: && context.getVariable(BpmVariable.PRIORITY
275: .getVariable()) != null) {
276: Integer priority = Integer.valueOf(context.getVariable(
277: BpmVariable.PRIORITY.getVariable()).toString());
278: assert priority != null;
279:
280: return priority;
281: }
282:
283: // ok, we do not found this variable - probably because it is new
284: // process
285: // or it is process created by previous version
286: // in this case get priority from active activity and do not forget to
287: // set it into variable
288: TaskMgmtInstance tmi = process.getTaskMgmtInstance();
289: assert tmi != null;
290:
291: Collection<TaskInstance> unfinishedTasks = BpmServiceImpl
292: .getBlockingTasks(process);
293: assert unfinishedTasks != null;
294:
295: // default is Normal priority
296: Integer priority = Task.PRIORITY_NORMAL;
297:
298: // get first task - if exist, othervise returns null
299: if (unfinishedTasks.size() > 0) {
300: TaskInstance task = unfinishedTasks.iterator().next();
301: priority = task.getPriority();
302: } else {
303: // ok, process is finished and here is no unfinished tasks
304: // in this case we get priority from the first task
305:
306: Collection<TaskInstance> tasks = tmi.getTaskInstances();
307: if (tasks.size() > 0) {
308: TaskInstance task = tasks.iterator().next();
309: priority = task.getPriority();
310: }
311: }
312:
313: assert priority != null;
314:
315: // set this priority into variable
316: context.setVariable(BpmVariable.PRIORITY.getVariable(),
317: priority);
318:
319: return priority;
320: }
321:
322: private BlockerInfoTO[] getAllBlockers(ProcessInstance process) {
323: Collection<TaskInstance> blockers = bpmService
324: .getAllBlockers(process);
325:
326: BlockerInfoTO[] result = new BlockerInfoTO[blockers.size()];
327: int i = 0;
328: for (TaskInstance blockingTask : blockers) {
329: BlockerInfoTO blocker = new BlockerInfoTO();
330: blocker.setName(blockingTask.getName());
331: blocker.setActor(blockingTask.getActorId());
332: blocker.setTaskId(blockingTask.getToken()
333: .getProcessInstance().getId());
334:
335: result[i++] = blocker;
336: }
337:
338: return result;
339: }
340: }
|