Search in sources :

Example 86 with ProcessDefinition

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());
}
Also used : ArrayList(java.util.ArrayList) ProcessDefinitionQuery(org.activiti.engine.repository.ProcessDefinitionQuery) ProcessDefinition(org.activiti.engine.repository.ProcessDefinition) ResourcesWrapper(eu.bcvsolutions.idm.core.api.rest.domain.ResourcesWrapper) ProcessInstanceQuery(org.activiti.engine.runtime.ProcessInstanceQuery) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) WorkflowProcessInstanceDto(eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowProcessInstanceDto) HashSet(java.util.HashSet)

Example 87 with ProcessDefinition

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;
}
Also used : S3PropertiesLocation(org.finra.herd.model.api.xml.S3PropertiesLocation) InputStream(java.io.InputStream) JobDefinitionParameterEntity(org.finra.herd.model.jpa.JobDefinitionParameterEntity) ArrayList(java.util.ArrayList) Parameter(org.finra.herd.model.api.xml.Parameter) ProcessDefinition(org.activiti.engine.repository.ProcessDefinition) JobDefinition(org.finra.herd.model.api.xml.JobDefinition)

Example 88 with ProcessDefinition

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();
}
Also used : InOrder(org.mockito.InOrder) ProcessDefinitionQuery(org.activiti.engine.repository.ProcessDefinitionQuery) ProcessDefinition(org.activiti.engine.repository.ProcessDefinition) Test(org.junit.Test)

Example 89 with ProcessDefinition

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());
}
Also used : JobDefinitionEntity(org.finra.herd.model.jpa.JobDefinitionEntity) NamespaceEntity(org.finra.herd.model.jpa.NamespaceEntity) ObjectNotFoundException(org.finra.herd.model.ObjectNotFoundException) ProcessDefinition(org.activiti.engine.repository.ProcessDefinition) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) NamespacePermission(org.finra.herd.model.annotation.NamespacePermission)

Example 90 with ProcessDefinition

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));
}
Also used : ApplicationUser(org.finra.herd.model.dto.ApplicationUser) JobDefinitionEntity(org.finra.herd.model.jpa.JobDefinitionEntity) JobDefinitionAlternateKeyDto(org.finra.herd.model.dto.JobDefinitionAlternateKeyDto) ObjectNotFoundException(org.finra.herd.model.ObjectNotFoundException) SecurityUserWrapper(org.finra.herd.model.dto.SecurityUserWrapper) PreAuthenticatedAuthenticationToken(org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken) ProcessDefinition(org.activiti.engine.repository.ProcessDefinition)

Aggregations

ProcessDefinition (org.activiti.engine.repository.ProcessDefinition)211 Deployment (org.activiti.engine.test.Deployment)82 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)42 ArrayList (java.util.ArrayList)29 ActivitiException (org.activiti.engine.ActivitiException)25 HistoricProcessInstance (org.activiti.engine.history.HistoricProcessInstance)23 RepositoryService (org.activiti.engine.RepositoryService)20 ProcessDefinitionQuery (org.activiti.engine.repository.ProcessDefinitionQuery)19 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)19 Task (org.activiti.engine.task.Task)16 Date (java.util.Date)14 Test (org.junit.Test)14 JsonNode (com.fasterxml.jackson.databind.JsonNode)12 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)12 HashMap (java.util.HashMap)12 ProcessDefinitionEntity (org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity)11 JobDefinitionEntity (org.finra.herd.model.jpa.JobDefinitionEntity)11 InputStream (java.io.InputStream)10 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)10 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)9