001: package org.emforge.jbpm;
002:
003: import java.util.Date;
004:
005: import org.apache.commons.collections.Transformer;
006: import org.apache.commons.lang.StringUtils;
007: import org.emforge.jbpm.web.bean.WorkflowController;
008: import org.emforge.xfer.WorkflowTO;
009: import org.jbpm.JbpmContext;
010: import org.jbpm.graph.def.ProcessDefinition;
011:
012: import ru.emdev.EmForge.wiki.WikiTextToXHTMLConverter;
013:
014: import com.ecyrd.jspwiki.WikiEngine;
015:
016: /** Transformer for converting jBPM Process Definitions to Workflows
017: *
018: * @author akakunin
019: *
020: */
021: public class ProcessDefTransformer implements Transformer {
022: private WikiEngine wikiEngine;
023: private JbpmContext jbpmContext;
024:
025: public ProcessDefTransformer(WikiEngine wikiEngine,
026: JbpmContext jbpmContext) {
027: this .wikiEngine = wikiEngine;
028: this .jbpmContext = jbpmContext;
029: }
030:
031: public WorkflowTO transform(Object i_input) {
032: ProcessDefinition processDef = (ProcessDefinition) i_input;
033:
034: WorkflowTO workflow = new WorkflowTO();
035: workflow.setId(processDef.getId());
036: workflow.setName(processDef.getName());
037: workflow.setVersion(processDef.getVersion());
038: workflow.setStartName(processDef.getStartState().getName());
039: workflow.setIconLink(getWorkflowIconLink(processDef));
040:
041: String comment = null;
042: Date date = null;
043: String userName = null;
044: String commentText = null;
045:
046: try {
047: comment = new String(processDef.getFileDefinition()
048: .getBytes(BpmServiceImpl.COMMENT_FILENAME));
049: date = new Date(Long.parseLong(StringUtils
050: .substringBetween(comment,
051: BpmServiceImpl.DATE_SEPARATOR,
052: BpmServiceImpl.USER_SEPARATOR)));
053: userName = StringUtils.substringBetween(comment,
054: BpmServiceImpl.USER_SEPARATOR,
055: BpmServiceImpl.MESSAGE_SEPARATOR);
056: commentText = StringUtils.substringAfter(comment,
057: BpmServiceImpl.MESSAGE_SEPARATOR);
058: } catch (Exception ex) {
059: // just ignore it here - comment not specified
060: }
061:
062: workflow.setDeployedBy(userName);
063: workflow.setDeployedAt(date);
064:
065: // convert comment to html
066: if (commentText != null) {
067: WikiTextToXHTMLConverter converter = new WikiTextToXHTMLConverter(
068: wikiEngine, processDef.getName());
069:
070: // convert page to HTML
071: String htmlComment = converter.getAsString(null, null,
072: commentText);
073: workflow.setDeployComment(htmlComment);
074: }
075:
076: if (jbpmContext.getGraphSession().findLatestProcessDefinition(
077: processDef.getName()).getId() == processDef.getId()) {
078: workflow.setObsolete(false);
079: } else {
080: workflow.setObsolete(true);
081: }
082:
083: return workflow;
084: }
085:
086: /** Returns contents of process icon (as byte array)
087: *
088: * @param processDef
089: * @return
090: */
091: protected static byte[] getWorkflowIcon(ProcessDefinition processDef) {
092: try {
093: byte[] bytes = processDef.getFileDefinition().getBytes(
094: "processicon.png");
095:
096: return bytes;
097: } catch (RuntimeException e) {
098: return null;
099: }
100:
101: }
102:
103: /** Returns link to process definition icon
104: *
105: * @param processDef
106: * @return
107: */
108: protected static String getWorkflowIconLink(
109: ProcessDefinition processDef) {
110: try {
111: byte[] bytes = getWorkflowIcon(processDef);
112: if (bytes == null || bytes.length == 0) {
113: return null;
114: }
115: } catch (RuntimeException e) {
116: return null;
117: }
118:
119: return "processicon?" + WorkflowController.WORKFLOW_ID_ATTR
120: + "=" + processDef.getId();
121: }
122:
123: /** Returns contents of process image as byte array (in jpeg format)
124: *
125: * @param processDef
126: * @return
127: */
128: protected static byte[] getWorkflowImage(
129: ProcessDefinition processDef) {
130: try {
131: byte[] bytes = processDef.getFileDefinition().getBytes(
132: "processimage.jpg");
133:
134: return bytes;
135: } catch (RuntimeException e) {
136: return null;
137: }
138:
139: }
140: }
|