Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.iqiny.silly.activiti.BaseSillyActivitiEngineService Maven / Gradle / Ivy
/*
* Copyright iqiny.com
*
* https://gitee.com/iqiny/silly
*
* project name:silly-activiti-52
* project description:top silly project pom.xml file
*/
package com.iqiny.silly.activiti;
import com.iqiny.silly.common.SillyConstant;
import com.iqiny.silly.common.exception.SillyException;
import com.iqiny.silly.common.util.SillyAssert;
import com.iqiny.silly.common.util.SillyIoUtil;
import com.iqiny.silly.common.util.SillyObjectUtil;
import com.iqiny.silly.common.util.StringUtils;
import com.iqiny.silly.core.engine.*;
import com.iqiny.silly.core.group.BaseSillyTaskGroupHandle;
import org.activiti.engine.*;
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricProcessInstanceQuery;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.bpmn.behavior.UserTaskActivityBehavior;
import org.activiti.engine.impl.db.IbatisVariableTypeHandler;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.ReadOnlyProcessDefinition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.util.ReflectUtil;
import org.activiti.engine.impl.variable.VariableType;
import org.activiti.engine.repository.Model;
import org.activiti.engine.repository.ModelQuery;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.apache.ibatis.builder.xml.XMLConfigBuilder;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.type.JdbcType;
import javax.sql.DataSource;
import java.io.*;
import java.util.*;
import static org.activiti.engine.delegate.event.ActivitiEventType.ACTIVITY_STARTED;
/**
* 集成activiti 工作流引擎服务
*/
public abstract class BaseSillyActivitiEngineService implements SillyEngineService, SillyEngineRepositoryService {
private boolean initialized = false;
protected ManagementService managementService;
protected RuntimeService runtimeService;
protected HistoryService historyService;
protected TaskService taskService;
protected RepositoryService repositoryService;
protected SqlSessionFactory sqlSessionFactory;
protected final String DEFAULT_MYBATIS_MAPPING_FILE = "iqiny/silly/mappings.xml";
public void otherInit(EngineServices engineServices) {
this.managementService = engineServices.getManagementService();
this.runtimeService = engineServices.getRuntimeService();
this.historyService = engineServices.getHistoryService();
this.taskService = engineServices.getTaskService();
this.repositoryService = engineServices.getRepositoryService();
// 添加activiti监听器
this.runtimeService.removeEventListener(SillySetBusinessKeyListener.INSTANCE);
this.runtimeService.addEventListener(SillySetBusinessKeyListener.INSTANCE, ACTIVITY_STARTED);
}
protected void initSqlSessionFactory(DataSource dataSource, TransactionFactory transactionFactory) {
try (InputStream inputStream = ReflectUtil.getResourceAsStream(DEFAULT_MYBATIS_MAPPING_FILE)) {
Environment environment = new Environment("default", transactionFactory, dataSource);
Reader reader = new InputStreamReader(inputStream);
Properties properties = new Properties();
XMLConfigBuilder parser = new XMLConfigBuilder(reader, null, properties);
Configuration configuration = parser.getConfiguration();
configuration.setEnvironment(environment);
configuration.getTypeHandlerRegistry().register(VariableType.class, JdbcType.VARCHAR, new IbatisVariableTypeHandler());
configuration = parser.parse();
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
} catch (Exception e) {
throw new SillyException("Error while building ibatis SqlSessionFactory: " + e.getMessage(), e);
}
}
/**
* 获取流程引擎服务
*/
protected abstract EngineServices getEngineServices();
/**
* 获取数据源
*/
protected abstract DataSource getDataSource();
/**
* 获取事务工厂
*/
protected abstract TransactionFactory getTransactionFactory();
@Override
public synchronized void init() {
if (initialized) {
return;
}
initSqlSessionFactory(getDataSource(), getTransactionFactory());
otherInit(getEngineServices());
initialized = true;
}
@Override
public List getDoingMasterTask(Set processKeys, String userId, Set allGroupId) {
return getMyDoingMasterTaskId(processKeys, userId, null, allGroupId);
}
@Override
public List getHistoryMasterTask(Set processKeys, String userId) {
Map param = new HashMap<>();
param.put("processKeys", processKeys);
param.put("userId", userId);
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
return sqlSession.selectList(namespace() + "getHistoryMasterTask", param);
} catch (Exception e) {
throw new SillyException(e.getMessage(), e);
}
}
@Override
public List getMyDoingMasterTaskId(Set processKeys, String userId, String masterId, Set allGroupId) {
Map param = new HashMap<>();
param.put("allGroupId", allGroupId);
param.put("processKeys", processKeys);
param.put("businessKey", masterId);
param.put("userId", userId);
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
return sqlSession.selectList(namespace() + "getDoingMasterTask", param);
} catch (Exception e) {
throw new SillyException(e.getMessage(), e);
}
}
private String namespace() {
return "com.iqiny.silly.core.engine.SillyMasterTask.";
}
public SillyTask convertor(Task task) {
if (task == null) {
return null;
}
SillyTask sillyTask = new SillyTask();
sillyTask.setId(task.getId());
sillyTask.setExecutionId(task.getExecutionId());
sillyTask.setProcessInstanceId(task.getProcessInstanceId());
sillyTask.setName(task.getName());
sillyTask.setTaskDefinitionKey(task.getTaskDefinitionKey());
sillyTask.setAssignee(task.getAssignee());
sillyTask.setNativeTask(task);
if (sillyTask.getUserIds() == null) {
sillyTask.setUserIds(getTaskUserIds(task.getId()));
}
return sillyTask;
}
public SillyTask convertor(HistoricTaskInstance task) {
if (task == null) {
return null;
}
SillyTask sillyTask = new SillyTask();
sillyTask.setId(task.getId());
sillyTask.setExecutionId(task.getExecutionId());
sillyTask.setProcessInstanceId(task.getProcessInstanceId());
sillyTask.setName(task.getName());
sillyTask.setTaskDefinitionKey(task.getTaskDefinitionKey());
sillyTask.setAssignee(task.getAssignee());
sillyTask.setNativeTask(task);
return sillyTask;
}
public List convertor(List tasks) {
if (tasks == null) {
return null;
}
List taskList = new ArrayList<>();
for (Task task : tasks) {
SillyAssert.notNull(task, "任务对象不可为空");
taskList.add(convertor(task));
}
return taskList;
}
@Override
public String start(String processDefinitionKey, String businessKey, Map variableMap) {
SillyAssert.notEmpty(processDefinitionKey, "流程启动时流程KEY 不可为空!");
SillyAssert.notEmpty(processDefinitionKey, "流程启动时流程KEY 不可为空!");
if (variableMap == null) {
variableMap = new HashMap<>();
}
final ExecutionEntity processInstance = (ExecutionEntity) runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey, variableMap);
return processInstance.getId();
}
@Override
public void complete(String taskId, String userId, Map variableMap) {
SillyTask task = getTask(taskId);
SillyAssert.notNull(task, "当前任务获取失败");
if (task.getAssignee() == null) {
// 认领任务
taskService.claim(taskId, userId);
}
// 完成任务
taskService.complete(taskId, variableMap);
}
@Override
public void changeTask(String taskId, String nodeKey, String userId, boolean deleteOtherTaskFlag, Map valueMap) {
if (valueMap == null) {
valueMap = new HashMap<>();
}
SillyTask currentTask = getTask(taskId);
SillyAssert.notEmpty(taskId, "当前任务获取失败");
ProcessDefinitionEntity processDefinition = findProcessDefinitionEntityByTaskId(taskId);
// 目标节点
ActivityImpl pointActivity = findActivitiImpl(nodeKey, processDefinition);
// 流程变量信息
if (StringUtils.isNotEmpty(userId)) {
Map userVarMap = makeUserVarMap(pointActivity, userId);
valueMap.putAll(userVarMap);
}
// 当前节点
ActivityImpl currActivity = findActivitiImpl(currentTask.getTaskDefinitionKey(), processDefinition);
managementService.executeCommand(
new RejectToAnyWhereTaskCmd(currentTask, pointActivity, valueMap, currActivity, deleteOtherTaskFlag)
);
}
@Override
public List findTaskByProcessInstanceId(String processInstanceId) {
if (StringUtils.isEmpty(processInstanceId)) {
throw new SillyException("查询任务列表,流程实例ID不可为空!");
}
return convertor(taskService.createTaskQuery().processInstanceId(processInstanceId).list());
}
@Override
public SillyTask getTask(String taskId) {
if (StringUtils.isEmpty(taskId)) {
return null;
}
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
if (task != null) {
return convertor(task);
}
return convertor(historyService.createHistoricTaskInstanceQuery().taskId(taskId).singleResult());
}
@Override
public String getBusinessKey(String processInstanceId) {
final HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
return historicProcessInstance.getBusinessKey();
}
@Override
public String getBusinessKeyByTaskId(String taskId) {
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
return getBusinessKey(task.getProcessInstanceId());
}
@Override
public String getBusinessKeyByExecutionId(String executionId) {
final ExecutionEntity execution = (ExecutionEntity) runtimeService.createExecutionQuery().executionId(executionId).singleResult();
return execution.getBusinessKey();
}
@Override
public Long getTaskDueTime(String taskId) {
if (StringUtils.isEmpty(taskId)) {
return 0L;
}
HistoricTaskInstance historicTaskInstance = historyService.createHistoricTaskInstanceQuery().taskId(taskId).singleResult();
return historicTaskInstance.getDurationInMillis();
}
@Override
public List getTaskUserIds(String taskId) {
List ids = new ArrayList<>();
if (StringUtils.isNotEmpty(taskId)) {
List identityLinkList = taskService.getIdentityLinksForTask(taskId);
for (IdentityLink link : identityLinkList) {
String groupId = link.getGroupId();
String userId = link.getUserId();
if (StringUtils.isNotEmpty(groupId)) {
ids.add(BaseSillyTaskGroupHandle.GROUP_USER_ID_PREFIX + groupId);
} else if (StringUtils.isNotEmpty(userId)) {
ids.add(userId);
}
}
}
return ids;
}
@Override
public String getActKeyNameByProcessInstanceId(String processInstanceId) {
final String processDefinitionId = getProcessDefinitionIdByProcessInstanceId(processInstanceId);
if (StringUtils.isEmpty(processDefinitionId)) {
return null;
}
final ReadOnlyProcessDefinition deployedProcessDefinition = ((RepositoryServiceImpl) repositoryService)
.getDeployedProcessDefinition(processDefinitionId);
if (deployedProcessDefinition != null) {
return deployedProcessDefinition.getKey();
}
return null;
}
// ======================================= 工作流服务 内部方法 =============================================
private ActivityImpl findActivitiImpl(String nodeKey, ProcessDefinitionEntity processDefinition) {
// 根据节点ID,获取对应的活动节点
return processDefinition.findActivity(nodeKey);
}
private ProcessDefinitionEntity findProcessDefinitionEntityByTaskId(String taskId) {
// 取得流程定义
TaskQuery taskQuery = taskService.createTaskQuery();
TaskQuery query = taskQuery.taskId(taskId);
return (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
.getDeployedProcessDefinition(query.singleResult().getProcessDefinitionId());
}
private Map makeUserVarMap(ActivityImpl pointActivity, String userId) {
if (pointActivity != null && pointActivity.getActivityBehavior() instanceof UserTaskActivityBehavior) {
// 设置节点处置人信息
UserTaskActivityBehavior behavior = (UserTaskActivityBehavior) pointActivity.getActivityBehavior();
Set set = behavior.getTaskDefinition().getCandidateUserIdExpressions();
Map value = new HashMap<>();
for (Expression expression : set) {
String et = expression.getExpressionText();
if (et.length() > 3) {
// 设置下一步操作人
String userKey = et.substring(2, et.length() - 1);
value.put(userKey, userId);
}
}
return value;
}
return new HashMap<>();
}
private String getProcessDefinitionIdByProcessInstanceId(String processInstanceId) {
if (StringUtils.isEmpty(processInstanceId)) {
return null;
}
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
String processDefinitionId = "";
if (processInstance == null) {
HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
processDefinitionId = historicProcessInstance.getProcessDefinitionId();
} else {
processDefinitionId = processInstance.getProcessDefinitionId();
}
return processDefinitionId;
}
@Override
public void deleteProcessInstance(String processInstanceId, String deleteReason) {
final ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
if (processInstance != null) {
runtimeService.deleteProcessInstance(processInstanceId, deleteReason);
}
}
public List findProcessInstanceByMasterId(String masterId) {
return historyService.createHistoricProcessInstanceQuery().processInstanceBusinessKey(masterId).list();
}
@Override
public List findMyTaskByMasterId(Set processKeys, String userId, String masterId, Set allGroupId) {
return getMyDoingMasterTaskId(processKeys, userId, masterId, allGroupId);
}
@Override
public List findTaskByMasterId(String masterId) {
final List processInstances = findProcessInstanceByMasterId(masterId);
List taskList = new ArrayList<>();
for (HistoricProcessInstance processInstance : processInstances) {
taskList.addAll(findTaskByProcessInstanceId(processInstance.getId()));
}
return taskList;
}
@Override
public void changeAssigneeUser(String taskId, String userId) {
taskService.setAssignee(taskId, userId);
}
@Override
public void addUser(String taskId, String userId) {
taskService.addCandidateUser(taskId, userId);
}
@Override
public void deleteUser(String taskId, String userId) {
taskService.deleteCandidateUser(taskId, userId);
}
@Override
public void deleteTask(String taskId) {
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
if (task.getExecutionId() != null) {
// 运行中的任务无法删除,将其完成
taskService.complete(taskId);
} else {
taskService.deleteTask(taskId);
}
}
@Override
public SillyTask newTask(String taskId) {
Task task = taskService.newTask(taskId);
return convertor(task);
}
@Override
public void setVariables(String taskId, Map variables) {
taskService.setVariables(taskId, variables);
}
@Override
public String getModelSource(String processId) {
final HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processId).singleResult();
final String processDefinitionId = historicProcessInstance.getProcessDefinitionId();
return getModelSourceByProcessDefinitionId(processDefinitionId);
}
@Override
public String getModelSourceByProcessKey(String processKey) {
final ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
.processDefinitionKey(processKey).active().latestVersion().singleResult();
final String processDefinitionId = processDefinition.getId();
return getModelSourceByProcessDefinitionId(processDefinitionId);
}
@Override
public String getModelSourceByProcessDefinitionId(String processDefinitionId) {
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processDefinitionId).singleResult();
try (InputStream inputStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), processDefinition.getResourceName());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
byte[] b = new byte[1024];
int n;
while ((n = inputStream.read(b)) != -1) {
outputStream.write(b, 0, n);
}
return outputStream.toString("UTF-8");
} catch (IOException e) {
throw new SillyException("流程图获取异常," + e.getMessage(), e);
}
}
@Override
public List getActiveNodeKey(String processId) {
List activeNodeKeys = new ArrayList<>();
List historicActivityInstanceList = historyService.createHistoricActivityInstanceQuery()
.processInstanceId(processId).orderByHistoricActivityInstanceEndTime().asc()
.orderByHistoricActivityInstanceStartTime().asc().orderByActivityId().asc().list();
for (HistoricActivityInstance activityInstance : historicActivityInstanceList) {
activeNodeKeys.add(activityInstance.getActivityId());
}
final HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processId).singleResult();
final String processDefinitionId = historicProcessInstance.getProcessDefinitionId();
ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService).getDeployedProcessDefinition(processDefinitionId);
List activities = processDefinition.getActivities();
final List usedActivities = usedActivitis(activeNodeKeys, activities);
List activeFlows = new ArrayList<>();
this.traceLine(usedActivities, activeFlows, activeNodeKeys);
activeNodeKeys.addAll(activeFlows);
return activeNodeKeys;
}
/**
* 获取已执行的节点
*/
protected List usedActivitis(List activeNodeKeys, List activities) {
List usedActivities = new ArrayList<>();
// 按执行顺序排序
for (String s : activeNodeKeys) {
for (ActivityImpl activity : activities) {
if (s.equals(activity.getId())) {
usedActivities.add(activity);
break;
}
}
}
return usedActivities;
}
/**
* 获取流程线条
*/
protected void traceLine(List activities, List activeFlows, List activeNodeKeys) {
if (activities == null || activities.isEmpty()) {
return;
}
for (int i = 0; i < activities.size(); i++) {
ActivityImpl activity = activities.get(i);
ActivityImpl nextActivity = null;
if (i + 1 < activities.size()) {
nextActivity = activities.get(i + 1);
} else {
break;
}
if (activeNodeKeys.contains(activity.getId())) {
if (activity.getActivities() != null && !activity.getActivities().isEmpty()) {
final List usedActivities = usedActivitis(activeNodeKeys, activity.getActivities());
traceLine(usedActivities, activeFlows, activeNodeKeys);
}
List pvmTransitionList = nextActivity.getIncomingTransitions();
for (PvmTransition pvmTransition : pvmTransitionList) {
boolean addFlag = (nextActivity.getId().equals(pvmTransition.getDestination().getId())
&& activity.getId().equals(pvmTransition.getSource().getId()));
if (addFlag) {
activeFlows.add(pvmTransition.getId());
}
}
}
}
}
@Override
public List getCurrentNodeKey(String processId) {
List activeNodeKeys = new ArrayList<>();
final List taskList = taskService.createTaskQuery().processInstanceId(processId).list();
for (Task task : taskList) {
activeNodeKeys.add(task.getTaskDefinitionKey());
}
return activeNodeKeys;
}
// ====================================== SillyEngineRepositoryService ===========================================
public List convertorModels(List models) {
if (models == null) {
return null;
}
List modelList = new ArrayList<>();
for (Model model : models) {
modelList.add(convertor(model));
}
return modelList;
}
public SillyProcessModel convertor(Model model) {
if (model == null) {
return null;
}
SillyProcessModel sillyModel = new SillyProcessModel();
sillyModel.setId(model.getId());
sillyModel.setKey(model.getKey());
sillyModel.setName(model.getName());
sillyModel.setCategory(model.getCategory());
sillyModel.setVersion(model.getVersion());
sillyModel.setDeploymentId(model.getDeploymentId());
sillyModel.setCreateTime(model.getCreateTime());
sillyModel.setLastUpdateTime(model.getLastUpdateTime());
return sillyModel;
}
public List convertorDefinitions(List definitions) {
if (definitions == null) {
return null;
}
List definitionList = new ArrayList<>();
for (ProcessDefinition definition : definitions) {
definitionList.add(convertor(definition));
}
return definitionList;
}
public SillyProcessDefinition convertor(ProcessDefinition definition) {
if (definition == null) {
return null;
}
SillyProcessDefinition sillyDefinition = new SillyProcessDefinition();
sillyDefinition.setId(definition.getId());
sillyDefinition.setKey(definition.getKey());
sillyDefinition.setName(definition.getName());
sillyDefinition.setCategory(definition.getCategory());
sillyDefinition.setVersion(definition.getVersion());
sillyDefinition.setDescription(definition.getDescription());
sillyDefinition.setDeploymentId(definition.getDeploymentId());
sillyDefinition.setSuspendFlag(definition.isSuspended() ? SillyConstant.YesOrNo.YES : SillyConstant.YesOrNo.NO);
sillyDefinition.setResourceName(definition.getResourceName());
return sillyDefinition;
}
public List convertorInstances(List instances) {
if (instances == null) {
return null;
}
List instanceList = new ArrayList<>();
for (HistoricProcessInstance instance : instances) {
instanceList.add(convertor(instance));
}
return instanceList;
}
public SillyProcessInstance convertor(HistoricProcessInstance instance) {
if (instance == null) {
return null;
}
SillyProcessInstance sillyProcessInstance = new SillyProcessInstance();
sillyProcessInstance.setId(instance.getId());
sillyProcessInstance.setBusinessKey(instance.getBusinessKey());
sillyProcessInstance.setProcessDefinitionId(instance.getProcessDefinitionId());
sillyProcessInstance.setFinishFlag(instance.getEndTime() != null ? SillyConstant.YesOrNo.YES : SillyConstant.YesOrNo.NO);
sillyProcessInstance.setStartTime(instance.getStartTime());
sillyProcessInstance.setEndTime(instance.getEndTime());
sillyProcessInstance.setParentId(instance.getSuperProcessInstanceId());
return sillyProcessInstance;
}
protected ModelQuery createModelQuery(SillyProcessModel whereModel) {
final ModelQuery modelQuery = repositoryService.createModelQuery();
if (StringUtils.isNotBlank(whereModel.getKey())) {
modelQuery.modelKey(whereModel.getKey());
}
if (StringUtils.isNotBlank(whereModel.getName())) {
modelQuery.modelNameLike("%" + whereModel.getName() + "%");
}
if (StringUtils.isNotBlank(whereModel.getCategory())) {
modelQuery.modelCategory(whereModel.getCategory());
}
return modelQuery.orderByCreateTime().desc();
}
protected ProcessDefinitionQuery createDefinitionQuery(SillyProcessDefinition whereDefinition) {
final ProcessDefinitionQuery definitionQuery = repositoryService.createProcessDefinitionQuery();
if (StringUtils.isNotBlank(whereDefinition.getKey())) {
definitionQuery.processDefinitionKey(whereDefinition.getKey());
}
if (StringUtils.isNotBlank(whereDefinition.getName())) {
definitionQuery.processDefinitionName("%" + whereDefinition.getName() + "%");
}
if (StringUtils.isNotBlank(whereDefinition.getCategory())) {
definitionQuery.processDefinitionCategory(whereDefinition.getCategory());
}
if (StringUtils.isNotBlank(whereDefinition.getSuspendFlag())) {
if ("1".equals(whereDefinition.getSuspendFlag())) {
definitionQuery.suspended();
} else {
definitionQuery.active();
}
}
return definitionQuery
.orderByProcessDefinitionCategory().asc()
.orderByProcessDefinitionKey().asc()
.orderByProcessDefinitionVersion().desc();
}
protected HistoricProcessInstanceQuery createInstanceQuery(SillyProcessInstance whereInstance) {
final HistoricProcessInstanceQuery instanceQuery = historyService.createHistoricProcessInstanceQuery();
if (StringUtils.isNotBlank(whereInstance.getId())) {
instanceQuery.processInstanceId(whereInstance.getId());
}
if (StringUtils.isNotBlank(whereInstance.getBusinessKey())) {
instanceQuery.processInstanceBusinessKey(whereInstance.getBusinessKey());
}
if (StringUtils.isNotBlank(whereInstance.getProcessDefinitionId())) {
instanceQuery.processDefinitionId(whereInstance.getProcessDefinitionId());
}
if (StringUtils.isNotBlank(whereInstance.getParentId())) {
instanceQuery.superProcessInstanceId(whereInstance.getParentId());
}
if (StringUtils.isNotBlank(whereInstance.getParentId())) {
instanceQuery.superProcessInstanceId(whereInstance.getParentId());
}
if (SillyConstant.YesOrNo.YES.equals(whereInstance.getFinishFlag())) {
instanceQuery.finished();
} else if (SillyConstant.YesOrNo.NO.equals(whereInstance.getFinishFlag())) {
instanceQuery.unfinished();
}
return instanceQuery.orderByProcessInstanceStartTime().desc();
}
@Override
public void deploy(String resourceName, String resourceText) {
repositoryService.createDeployment().addString(resourceName, resourceText).disableSchemaValidation().deploy();
}
@Override
public void deleteDeployment(String deploymentId) {
repositoryService.deleteDeployment(deploymentId, true);
}
static final String MODEL_NAME = "name";
static final String MODEL_REVISION = "revision";
static final String MODEL_DESCRIPTION = "description";
@Override
public SillyProcessModel definitionToModel(String definitionId) {
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(definitionId).singleResult();
InputStream bpmnStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(),
processDefinition.getResourceName());
Model modelData = repositoryService.newModel();
modelData.setKey(processDefinition.getKey());
modelData.setName(processDefinition.getResourceName());
modelData.setCategory(processDefinition.getCategory());
modelData.setDeploymentId(processDefinition.getDeploymentId());
modelData.setVersion(Integer.parseInt(String.valueOf(repositoryService.createModelQuery().modelKey(modelData.getKey()).count() + 1)));
Map metaInfo = new HashMap<>();
metaInfo.put(MODEL_NAME, processDefinition.getName());
metaInfo.put(MODEL_REVISION, modelData.getVersion());
metaInfo.put(MODEL_DESCRIPTION, processDefinition.getDescription());
modelData.setMetaInfo(SillyObjectUtil.objectToJsonString(metaInfo));
repositoryService.saveModel(modelData);
repositoryService.addModelEditorSource(modelData.getId(), SillyIoUtil.toByteArray(bpmnStream));
return convertor(modelData);
}
@Override
public void saveModel(SillyProcessModel model, String resourceText) {
Model modelData;
if (StringUtils.isNotEmpty(model.getId())) {
modelData = repositoryService.getModel(model.getId());
SillyAssert.notNull(modelData, "此模型Id数据不存在" + model.getId());
}else {
modelData = repositoryService.newModel();
}
modelData.setKey(model.getKey());
modelData.setName(model.getName());
modelData.setCategory(model.getCategory());
modelData.setDeploymentId(model.getDeploymentId());
modelData.setVersion(Integer.parseInt(String.valueOf(repositoryService.createModelQuery().modelKey(modelData.getKey()).count() + 1)));
Map metaInfo = new HashMap<>();
metaInfo.put(MODEL_NAME, model.getName());
metaInfo.put(MODEL_REVISION, modelData.getVersion());
modelData.setMetaInfo(SillyObjectUtil.objectToJsonString(metaInfo));
repositoryService.saveModel(modelData);
repositoryService.addModelEditorSource(modelData.getId(), SillyIoUtil.toByteArray(resourceText));
}
@Override
public String getModelEditorSource(String modelId) {
final byte[] modelEditorSource = repositoryService.getModelEditorSource(modelId);
return (modelEditorSource == null || modelEditorSource.length == 0) ? null : new String(modelEditorSource);
}
@Override
public List queryModelList(SillyProcessModel whereModel, int pageNo, int pageSize) {
final ModelQuery modelQuery = createModelQuery(whereModel);
List models;
if (pageNo < 1 || pageSize < 1) {
models = modelQuery.list();
} else {
models = modelQuery.listPage((pageNo - 1) * pageSize, pageSize);
}
return convertorModels(models);
}
@Override
public long queryModelCount(SillyProcessModel whereModel) {
return createModelQuery(whereModel).count();
}
@Override
public List queryDefinitionList(SillyProcessDefinition whereDefinition, int pageNo, int pageSize) {
final ProcessDefinitionQuery query = createDefinitionQuery(whereDefinition);
List definitions;
if (pageNo < 1 || pageSize < 1) {
definitions = query.list();
} else {
definitions = query.listPage((pageNo - 1) * pageSize, pageSize);
}
return convertorDefinitions(definitions);
}
@Override
public long queryDefinitionCount(SillyProcessDefinition whereDefinition) {
return createDefinitionQuery(whereDefinition).count();
}
@Override
public List queryInstanceList(SillyProcessInstance whereInstance, int pageNo, int pageSize) {
final HistoricProcessInstanceQuery query = createInstanceQuery(whereInstance);
List instances;
if (pageNo < 1 || pageSize < 1) {
instances = query.list();
} else {
instances = query.listPage((pageNo - 1) * pageSize, pageSize);
}
return convertorInstances(instances);
}
@Override
public long queryInstanceCount(SillyProcessInstance whereInstance) {
return createInstanceQuery(whereInstance).count();
}
}