use of org.activiti.engine.repository.ProcessDefinition in project CzechIdMng by bcvsolutions.
the class DefaultWorkflowProcessInstanceService method searchInternal.
@Override
public ResourcesWrapper<WorkflowProcessInstanceDto> searchInternal(WorkflowFilterDto filter, boolean checkRight) {
String processDefinitionId = filter.getProcessDefinitionId();
Map<String, Object> equalsVariables = filter.getEqualsVariables();
ProcessInstanceQuery query = runtimeService.createProcessInstanceQuery();
query.active();
query.includeProcessVariables();
if (processDefinitionId != null) {
query.processDefinitionId(processDefinitionId);
}
if (filter.getProcessDefinitionKey() != null) {
query.processDefinitionKey(filter.getProcessDefinitionKey());
}
if (filter.getProcessInstanceId() != null) {
query.processInstanceId(filter.getProcessInstanceId());
}
if (filter.getCategory() != null) {
// Find definitions with this category (use double sided like)
// We have to find definitions first, because process instance can't
// be find by category.
ProcessDefinitionQuery queryDefinition = repositoryService.createProcessDefinitionQuery();
queryDefinition.active();
queryDefinition.latestVersion();
queryDefinition.processDefinitionCategoryLike(filter.getCategory() + "%");
List<ProcessDefinition> processDefinitions = queryDefinition.list();
Set<String> processDefinitionKeys = new HashSet<>();
processDefinitions.forEach(p -> processDefinitionKeys.add(p.getKey()));
if (processDefinitionKeys.isEmpty()) {
// We don`t have any definitions ... nothing must be returned
processDefinitionKeys.add("-1");
}
query.processDefinitionKeys(processDefinitionKeys);
}
if (equalsVariables != null) {
for (Entry<String, Object> entry : equalsVariables.entrySet()) {
query.variableValueEquals(entry.getKey(), entry.getValue());
}
}
// (subprocess) started. This modification allow not use OR clause.
if (checkRight && !securityService.isAdmin()) {
query.involvedUser(securityService.getCurrentId().toString());
}
query.orderByProcessDefinitionId();
query.desc();
long count = query.count();
List<ProcessInstance> processInstances = query.listPage((filter.getPageNumber()) * filter.getPageSize(), filter.getPageSize());
List<WorkflowProcessInstanceDto> dtos = new ArrayList<>();
if (processInstances != null) {
for (ProcessInstance instance : processInstances) {
dtos.add(toResource(instance));
}
}
double totalPageDouble = ((double) count / filter.getPageSize());
double totlaPageFlorred = Math.floor(totalPageDouble);
long totalPage = 0;
if (totalPageDouble > totlaPageFlorred) {
totalPage = (long) (totlaPageFlorred + 1);
}
return new ResourcesWrapper<>(dtos, count, totalPage, filter.getPageNumber(), filter.getPageSize());
}
use of org.activiti.engine.repository.ProcessDefinition in project herd by FINRAOS.
the class JobDefinitionServiceImpl method createJobDefinitionFromEntity.
/**
* Creates the job definition from the persisted entity.
*
* @param jobDefinitionEntity the newly persisted job definition entity.
*
* @return the job definition.
*/
private JobDefinition createJobDefinitionFromEntity(JobDefinitionEntity jobDefinitionEntity) throws IOException {
// Create the business object definition information.
JobDefinition jobDefinition = new JobDefinition();
jobDefinition.setId(jobDefinitionEntity.getId());
jobDefinition.setNamespace(jobDefinitionEntity.getNamespace().getCode());
jobDefinition.setJobName(jobDefinitionEntity.getName());
jobDefinition.setDescription(jobDefinitionEntity.getDescription());
String s3BucketName = jobDefinitionEntity.getS3BucketName();
String s3ObjectKey = jobDefinitionEntity.getS3ObjectKey();
if (s3BucketName != null && s3ObjectKey != null) {
S3PropertiesLocation s3PropertiesLocation = new S3PropertiesLocation();
s3PropertiesLocation.setBucketName(s3BucketName);
s3PropertiesLocation.setKey(s3ObjectKey);
jobDefinition.setS3PropertiesLocation(s3PropertiesLocation);
}
// Retrieve Activiti XML from activiti.
ProcessDefinition processDefinition = activitiRepositoryService.createProcessDefinitionQuery().processDefinitionId(jobDefinitionEntity.getActivitiId()).singleResult();
InputStream xmlStream = activitiRepositoryService.getResourceAsStream(processDefinition.getDeploymentId(), processDefinition.getResourceName());
jobDefinition.setActivitiJobXml(IOUtils.toString(xmlStream));
// Add in the parameters.
List<Parameter> parameters = new ArrayList<>();
jobDefinition.setParameters(parameters);
for (JobDefinitionParameterEntity parameterEntity : jobDefinitionEntity.getParameters()) {
Parameter parameter = new Parameter(parameterEntity.getName(), parameterEntity.getValue());
jobDefinitionHelper.maskPassword(parameter);
parameters.add(parameter);
}
// Populate the "last updated by" user ID.
jobDefinition.setLastUpdatedByUserId(jobDefinitionEntity.getUpdatedBy());
return jobDefinition;
}
use of org.activiti.engine.repository.ProcessDefinition in project herd by FINRAOS.
the class ActivitiServiceTest method testGetProcessDefinitionById.
@Test
public void testGetProcessDefinitionById() {
String processDefinitionId = "processDefinitionId";
ProcessDefinitionQuery processDefinitionQuery = mock(ProcessDefinitionQuery.class);
when(activitiRepositoryService.createProcessDefinitionQuery()).thenReturn(processDefinitionQuery);
when(processDefinitionQuery.processDefinitionId(processDefinitionId)).thenReturn(processDefinitionQuery);
ProcessDefinition expectedProcessDefinition = mock(ProcessDefinition.class);
when(processDefinitionQuery.singleResult()).thenReturn(expectedProcessDefinition);
ProcessDefinition actualProcessDefinition = activitiService.getProcessDefinitionById(processDefinitionId);
assertSame(expectedProcessDefinition, actualProcessDefinition);
InOrder inOrder = inOrder(processDefinitionQuery);
inOrder.verify(processDefinitionQuery).processDefinitionId(processDefinitionId);
inOrder.verify(processDefinitionQuery).singleResult();
inOrder.verifyNoMoreInteractions();
}
use of org.activiti.engine.repository.ProcessDefinition in project herd by FINRAOS.
the class JobServiceImpl method createAndStartJob.
@NamespacePermission(fields = "#request?.namespace", permissions = NamespacePermissionEnum.EXECUTE)
@Override
public Job createAndStartJob(JobCreateRequest request) throws Exception {
// Perform the validation.
validateJobCreateRequest(request);
// Get the namespace and ensure it exists.
NamespaceEntity namespaceEntity = namespaceDaoHelper.getNamespaceEntity(request.getNamespace());
// Get the job definition and ensure it exists.
JobDefinitionEntity jobDefinitionEntity = jobDefinitionDao.getJobDefinitionByAltKey(request.getNamespace(), request.getJobName());
if (jobDefinitionEntity == null) {
throw new ObjectNotFoundException("Job definition with name \"" + request.getJobName() + "\" doesn't exist for namespace \"" + request.getNamespace() + "\".");
}
// Build the parameters map
Map<String, Object> mergedParameters = getParameters(jobDefinitionEntity, request);
// Create a process instance holder to check for a handle to the process instance once it is created.
String processDefinitionId = jobDefinitionEntity.getActivitiId();
ProcessDefinition processDefinition = activitiService.getProcessDefinitionById(processDefinitionId);
Assert.notNull(processDefinition, "No process definition found for Id: " + processDefinitionId);
Assert.isTrue(!processDefinition.isSuspended(), "Cannot start process instance for process definition Id: " + processDefinitionId + " because it is suspended.");
ProcessInstance processInstance = activitiService.startProcessInstanceByProcessDefinitionId(processDefinitionId, mergedParameters);
// If we get here, we have a newly created process instance. Log to know it was created successfully.
LOGGER.info("Created process instance with Id: " + processInstance.getProcessInstanceId() + " for process definition Id: " + processDefinitionId + " with merged parameters: " + mergedParameters);
// Create and return the job object.
return createJobFromRequest(namespaceEntity.getCode(), jobDefinitionEntity.getName(), mergedParameters, processInstance.getProcessInstanceId());
}
use of org.activiti.engine.repository.ProcessDefinition in project herd by FINRAOS.
the class BaseJavaDelegate method setSecurityContext.
/**
* Sets the security context per last updater of the current process instance's job definition.
*
* @param execution the current execution context
*/
protected void setSecurityContext(DelegateExecution execution) {
String processDefinitionId = execution.getProcessDefinitionId();
// Get process definition by process definition ID from Activiti.
ProcessDefinition processDefinition = activitiService.getProcessDefinitionById(processDefinitionId);
// Validate that we retrieved the process definition from Activiti.
if (processDefinition == null) {
throw new ObjectNotFoundException(String.format("Failed to find Activiti process definition for processDefinitionId=\"%s\".", processDefinitionId));
}
// Retrieve the process definition key.
String processDefinitionKey = processDefinition.getKey();
// Get the job definition key.
JobDefinitionAlternateKeyDto jobDefinitionKey = jobDefinitionHelper.getJobDefinitionKey(processDefinitionKey);
// Get the job definition from the Herd repository and validate that it exists.
JobDefinitionEntity jobDefinitionEntity = jobDefinitionDaoHelper.getJobDefinitionEntity(jobDefinitionKey.getNamespace(), jobDefinitionKey.getJobName());
// Set the security context per last updater of the job definition.
String updatedByUserId = jobDefinitionEntity.getUpdatedBy();
ApplicationUser applicationUser = new ApplicationUser(getClass());
applicationUser.setUserId(updatedByUserId);
userNamespaceAuthorizationHelper.buildNamespaceAuthorizations(applicationUser);
SecurityContextHolder.getContext().setAuthentication(new PreAuthenticatedAuthenticationToken(new SecurityUserWrapper(updatedByUserId, "", true, true, true, true, Collections.emptyList(), applicationUser), null));
}
Aggregations