nosi.core.webapp.bpmn.BPMNTimeLine Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of igrp-core Show documentation
Show all versions of igrp-core Show documentation
IGRP Framework is a powerful and highly customizable platform developed by the Operational Nucleus for the Information Society (NOSi) to create web applications, it provides out of box, several modules to make easy to create stand-alone, production-grade web applications: authentication and access-control, business processes automation, reporting, page builder with automatic code generation and incorporation of the Once-Only-Principle, written in Java. IGRP Framework WAR - Contains some keys resources that give UI to IGRP Framework and others supports files.
package nosi.core.webapp.bpmn;
/**
* Emanuel
* 9 Jul 2018
*/
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.model.SequenceFlow;
import org.activiti.bpmn.model.StartEvent;
import org.activiti.bpmn.model.UserTask;
import nosi.core.config.Config;
import nosi.core.gui.components.IGRPTable;
import nosi.core.gui.fields.Field;
import nosi.core.gui.fields.LinkField;
import nosi.core.gui.fields.TextField;
import nosi.core.webapp.Core;
import nosi.core.webapp.activit.rest.entities.ProcessDefinitionService;
import nosi.core.webapp.activit.rest.services.ProcessDefinitionServiceRest;
import nosi.core.webapp.activit.rest.services.ResourceServiceRest;
import nosi.core.webapp.activit.rest.services.TaskServiceRest;
import nosi.webapps.igrp.dao.TaskComponent;
public class BPMNTimeLine {
private int countTask=0;
private final RuntimeTask runtimeTask;
private int order = 0;
List taskTimeline;
public BPMNTimeLine() {
this.runtimeTask = RuntimeTask.getRuntimeTask();
}
public IGRPTable get() {
IGRPTable table = new IGRPTable("table");
if(this.runtimeTask.getShowTimeLine()) {
table.getProperties().put("type", "workflow");
Field title = new TextField(null,"title");
Field type = new TextField(null,"type");
Field link = new LinkField(null,"link");
table.addField(title);
table.addField(type);
table.addField(link);
List list = new ArrayList<>();
List tasks = getTasks();
this.countTask=0;
tasks.forEach(task->{
TimeLine t = new TimeLine();
t.setTitle((++countTask)+" - "+task.getName());
t.setType(task.getType());
t.setLink(task.getUrl());
list.add(t);
});
table.addData(list);
}
return table;
}
private void recursiveTask(List userTasks,Map refs,String taskSearch) {
final String next = refs.get(taskSearch);
userTasks.stream()
.filter(t-> (t.getId().compareTo(taskSearch) == 0) && next != null)
.forEach(t->{
TaskTimeLine tt = new TaskTimeLine();
tt.setTaskId(t.getId());
tt.setName(t.getName());
tt.setOrder((++order));
taskTimeline.add(tt);
});
refs.remove(taskSearch,next);
if(!refs.isEmpty()) {
if(next!=null)
recursiveTask(userTasks,refs,next);
else {
String next_ = refs.entrySet().stream().findFirst().get().getKey();
recursiveTask(userTasks,refs,next_);
}
}
}
public List getTasks() {
this.taskTimeline = new ArrayList<>();
Map tasksSQ = new HashMap<>();
String processDefinition = runtimeTask.getTask().getProcessDefinitionId();
TaskServiceRest taskSQ = new TaskServiceRest();
taskSQ.addFilterBody("finished", "true");
taskSQ.addFilterBody("processDefinitionId", processDefinition);
if(runtimeTask.getTask().getExecutionId() != null)
taskSQ.addFilterBody("executionId", runtimeTask.getTask().getExecutionId());
try {
taskSQ.queryHistoryTask()
.forEach(task->{
if(tasksSQ.isEmpty()) {
tasksSQ.put(task.getTaskDefinitionKey(), task.getId());
}else {
if(!tasksSQ.containsKey(task.getTaskDefinitionKey())) {
tasksSQ.put(task.getTaskDefinitionKey(), task.getId());
}
}
});
}catch(Exception e) {
e.printStackTrace();
}
ProcessDefinitionService p = new ProcessDefinitionServiceRest().getProcessDefinition(processDefinition);
String link = p.getResource().replace("/resources/", "/resourcedata/");
String resource = new ResourceServiceRest().getResourceData(link);
BpmnXMLConverter bpmn = new BpmnXMLConverter();
XMLInputFactory xif = XMLInputFactory.newInstance();
xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); // Disables DTDs entirely
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); // Disables external entities
try {
List startEvent = bpmn.convertToBpmnModel(xif.createXMLStreamReader(new StringReader(resource))).getMainProcess()
.findFlowElementsOfType(StartEvent.class);
List userTasks = bpmn.convertToBpmnModel(xif.createXMLStreamReader(new StringReader(resource))).getMainProcess()
.findFlowElementsOfType(UserTask.class);
List sequenceFlows = bpmn
.convertToBpmnModel(xif.createXMLStreamReader(new StringReader(resource))).getMainProcess()
.findFlowElementsOfType(SequenceFlow.class);
Map refs = new HashMap<>();
int i=0;
for(SequenceFlow sf:sequenceFlows) {
if(!refs.containsKey(sf.getSourceRef())) {
refs.put(sf.getSourceRef(), sf.getTargetRef());
}else {
refs.put(sf.getSourceRef()+"_"+(++i), sf.getTargetRef());
}
}
String start = startEvent.stream().findFirst().get().getId();
this.recursiveTask(userTasks,refs, start);
//See if there is a custom ordering for the timeline
List