package org.activiti.monitor.dao;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.monitor.data.Instance;
import org.springframework.stereotype.Component;

@Component
public class InstanceDAO {
	@Inject
	HistoryService historyService;

	@Inject
	RuntimeService runtimeService;

	@Inject
	DefinitionDAO definitionDAO;

	public boolean hasEnded(String parentId) {
		ProcessInstance processInstance = runtimeService
				.createProcessInstanceQuery().processInstanceId(parentId)
				.singleResult();

		return processInstance == null || processInstance.isEnded();
	}

	private Instance copyInstanceDAO(HistoricProcessInstance h) {
		Instance p = new Instance();
		p.setId(h.getId());
		p.setName(definitionDAO.getProcessDefinitionName(h
				.getProcessDefinitionId()) + " - " + h.getId());
		p.setStartDate(h.getStartTime());
		p.setEndDate(h.getEndTime());
		p.setBusinessKey(h.getBusinessKey());
		p.setEndStatus(h.getEndActivityId());

		return p;

	}

	public List<Instance> recursiveSubprocesses(String parentProcessId) {
		List<Instance> subprocesses = new ArrayList<Instance>();
		for (HistoricProcessInstance historicProcessInstance : historyService
				.createHistoricProcessInstanceQuery()
				.superProcessInstanceId(parentProcessId).list()) {

			subprocesses.add(copyInstanceDAO(historicProcessInstance));
			subprocesses.addAll(recursiveSubprocesses(historicProcessInstance
					.getId()));
		}
		return subprocesses;
	}

}

@@ -56,16 +56,41 @@
 ayList;%0A
+import java.util.Arrays;%0A
 import j
@@ -104,16 +104,16 @@
 l.List;%0A
-
 %0Aimport 
@@ -212,32 +212,159 @@
 RuntimeService;%0A
+import org.activiti.engine.history.HistoricActivityInstance;%0Aimport org.activiti.engine.history.HistoricActivityInstanceQuery;%0A
 import org.activ
@@ -960,16 +960,977 @@
 ();%0A%09%7D%0A%0A
+%09private String endStatusName(String processInstanceId) %7B%0A%09%09try %7B%0A%09        String result = null;%0A%09        List%3CString%3E types = Arrays.asList(%22endEvent%22, %22errorEndEvent%22);%0A%09        HistoricActivityInstanceQuery query = historyService.createHistoricActivityInstanceQuery().processInstanceId(processInstanceId)%0A%09                .finished();%0A%09        for (HistoricActivityInstance historicActivityInstance : query.list()) %7B%0A%09            String activityName = historicActivityInstance.getActivityName();%0A%09            if (types.contains(historicActivityInstance.getActivityType())) %7B%0A%09                if (result == null)%0A%09                    result = activityName;%0A%09                else%0A%09                %09// multiple results. show none%0A%09                    return %22%22;%0A%09            %7D%0A%09        %7D%0A%09        if (result == null)%0A%09        %09// no result%0A%09            return %22%22;%0A%09        else%0A%09            return result;%0A%09%09%7D %0A%09%09catch (Exception e) %7B%0A%09%09%09return %22%22;%0A%09%09%7D%0A    %7D%0A%09%0A
 %09private
@@ -2268,16 +2268,30 @@
 dStatus(
+endStatusName(
 h.getEnd
@@ -2303,16 +2303,17 @@
 ityId())
+)
 ;%0A%0A%09%09ret

change endActivitiId to endActivitiLabel