Search in sources :

Example 1 with JobDefinitionEntity

use of org.finra.herd.model.jpa.JobDefinitionEntity 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 2 with JobDefinitionEntity

use of org.finra.herd.model.jpa.JobDefinitionEntity 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)

Example 3 with JobDefinitionEntity

use of org.finra.herd.model.jpa.JobDefinitionEntity in project herd by FINRAOS.

the class JobDefinitionDaoTest method testGetJobDefinitionByAltKey.

@Test
public void testGetJobDefinitionByAltKey() {
    // Create a namespace database entity.
    NamespaceEntity namespaceEntity = namespaceDaoTestHelper.createNamespaceEntity(NAMESPACE);
    // Create and persist a job definition entity.
    JobDefinitionEntity jobDefinitionEntity = jobDefinitionDaoTestHelper.createJobDefinitionEntity(namespaceEntity, JOB_NAME, JOB_DESCRIPTION, ACTIVITI_ID);
    // Retrieve the job definition by its key.
    assertEquals(jobDefinitionEntity, jobDefinitionDao.getJobDefinitionByAltKey(NAMESPACE, JOB_NAME));
    // Retrieve the job definition by its key in uppercase.
    assertEquals(jobDefinitionEntity, jobDefinitionDao.getJobDefinitionByAltKey(NAMESPACE.toUpperCase(), JOB_NAME.toUpperCase()));
    // Retrieve the job definition by its key in lowercase.
    assertEquals(jobDefinitionEntity, jobDefinitionDao.getJobDefinitionByAltKey(NAMESPACE.toLowerCase(), JOB_NAME.toLowerCase()));
    // Try to retrieve a job definition for a non-existing namespace.
    assertNull(jobDefinitionDao.getJobDefinitionByAltKey("I_DO_NOT_EXIST", JOB_NAME));
    // Try to retrieve a job definition for a non-existing job name.
    assertNull(jobDefinitionDao.getJobDefinitionByAltKey(NAMESPACE, "I_DO_NOT_EXIST"));
}
Also used : JobDefinitionEntity(org.finra.herd.model.jpa.JobDefinitionEntity) NamespaceEntity(org.finra.herd.model.jpa.NamespaceEntity) Test(org.junit.Test)

Example 4 with JobDefinitionEntity

use of org.finra.herd.model.jpa.JobDefinitionEntity in project herd by FINRAOS.

the class NotificationRegistrationDaoTestHelper method createStorageUnitNotificationRegistrationEntity.

/**
 * Creates and persists a storage unit notification registration entity.
 *
 * @param notificationRegistrationKey the business object data notification registration key
 * @param notificationEventTypeCode the notification event type
 * @param businessObjectDefinitionNamespace the business object definition namespace
 * @param businessObjectDefinitionName the business object definition name
 * @param businessObjectFormatUsage the business object usage
 * @param businessObjectFormatFileType the business object format file type
 * @param businessObjectFormatVersion the business object format version
 * @param storageName the storage name
 * @param newStorageUnitStatus the new storage unit status
 * @param oldStorageUnitStatus the old storage unit status
 * @param jobActions the list of job actions
 * @param notificationRegistrationStatus the notification registration status
 *
 * @return the newly created storage unit notification registration entity
 */
public StorageUnitNotificationRegistrationEntity createStorageUnitNotificationRegistrationEntity(NotificationRegistrationKey notificationRegistrationKey, String notificationEventTypeCode, String businessObjectDefinitionNamespace, String businessObjectDefinitionName, String businessObjectFormatUsage, String businessObjectFormatFileType, Integer businessObjectFormatVersion, String storageName, String newStorageUnitStatus, String oldStorageUnitStatus, List<JobAction> jobActions, String notificationRegistrationStatus) {
    // Create a namespace entity if needed.
    NamespaceEntity namespaceEntity = namespaceDao.getNamespaceByCd(notificationRegistrationKey.getNamespace());
    if (namespaceEntity == null) {
        namespaceEntity = namespaceDaoTestHelper.createNamespaceEntity(notificationRegistrationKey.getNamespace());
    }
    // Create a notification event type entity if needed.
    NotificationEventTypeEntity notificationEventTypeEntity = notificationEventTypeDao.getNotificationEventTypeByCode(notificationEventTypeCode);
    if (notificationEventTypeEntity == null) {
        notificationEventTypeEntity = createNotificationEventTypeEntity(notificationEventTypeCode);
    }
    // Create a business object definition entity if needed.
    BusinessObjectDefinitionEntity businessObjectDefinitionEntity = businessObjectDefinitionDao.getBusinessObjectDefinitionByKey(new BusinessObjectDefinitionKey(businessObjectDefinitionNamespace, businessObjectDefinitionName));
    if (businessObjectDefinitionEntity == null) {
        businessObjectDefinitionEntity = businessObjectDefinitionDaoTestHelper.createBusinessObjectDefinitionEntity(businessObjectDefinitionNamespace, businessObjectDefinitionName, AbstractDaoTest.DATA_PROVIDER_NAME, AbstractDaoTest.BDEF_DESCRIPTION);
    }
    // Create a business object format file type entity if needed.
    FileTypeEntity fileTypeEntity = null;
    if (StringUtils.isNotBlank(businessObjectFormatFileType)) {
        fileTypeEntity = fileTypeDao.getFileTypeByCode(businessObjectFormatFileType);
        if (fileTypeEntity == null) {
            fileTypeEntity = fileTypeDaoTestHelper.createFileTypeEntity(businessObjectFormatFileType);
        }
    }
    // Create a storage entity.
    StorageEntity storageEntity = storageDao.getStorageByName(storageName);
    if (storageEntity == null) {
        storageEntity = storageDaoTestHelper.createStorageEntity(storageName, StoragePlatformEntity.S3);
    }
    // Create a business object status entity for new status if needed.
    StorageUnitStatusEntity newStorageUnitStatusEntity = null;
    if (StringUtils.isNotBlank(newStorageUnitStatus)) {
        newStorageUnitStatusEntity = storageUnitStatusDao.getStorageUnitStatusByCode(newStorageUnitStatus);
        if (newStorageUnitStatusEntity == null) {
            newStorageUnitStatusEntity = storageUnitStatusDaoTestHelper.createStorageUnitStatusEntity(newStorageUnitStatus);
        }
    }
    // Create a business object status entity for new status if needed.
    StorageUnitStatusEntity oldStorageUnitStatusEntity = null;
    if (StringUtils.isNotBlank(oldStorageUnitStatus)) {
        oldStorageUnitStatusEntity = storageUnitStatusDao.getStorageUnitStatusByCode(oldStorageUnitStatus);
        if (oldStorageUnitStatusEntity == null) {
            oldStorageUnitStatusEntity = storageUnitStatusDaoTestHelper.createStorageUnitStatusEntity(oldStorageUnitStatus);
        }
    }
    // Create a notification registration status entity if needed.
    NotificationRegistrationStatusEntity notificationRegistrationStatusEntity = notificationRegistrationStatusDao.getNotificationRegistrationStatus(notificationRegistrationStatus);
    if (notificationRegistrationStatusEntity == null) {
        notificationRegistrationStatusEntity = createNotificationRegistrationStatusEntity(notificationRegistrationStatus);
    }
    // Create a business object data notification registration entity.
    StorageUnitNotificationRegistrationEntity storageUnitNotificationRegistrationEntity = new StorageUnitNotificationRegistrationEntity();
    storageUnitNotificationRegistrationEntity.setNamespace(namespaceEntity);
    storageUnitNotificationRegistrationEntity.setName(notificationRegistrationKey.getNotificationName());
    storageUnitNotificationRegistrationEntity.setNotificationEventType(notificationEventTypeEntity);
    storageUnitNotificationRegistrationEntity.setBusinessObjectDefinition(businessObjectDefinitionEntity);
    storageUnitNotificationRegistrationEntity.setUsage(businessObjectFormatUsage);
    storageUnitNotificationRegistrationEntity.setFileType(fileTypeEntity);
    storageUnitNotificationRegistrationEntity.setBusinessObjectFormatVersion(businessObjectFormatVersion);
    storageUnitNotificationRegistrationEntity.setStorage(storageEntity);
    storageUnitNotificationRegistrationEntity.setNewStorageUnitStatus(newStorageUnitStatusEntity);
    storageUnitNotificationRegistrationEntity.setOldStorageUnitStatus(oldStorageUnitStatusEntity);
    storageUnitNotificationRegistrationEntity.setNotificationRegistrationStatus(notificationRegistrationStatusEntity);
    if (!CollectionUtils.isEmpty(jobActions)) {
        List<NotificationActionEntity> notificationActionEntities = new ArrayList<>();
        storageUnitNotificationRegistrationEntity.setNotificationActions(notificationActionEntities);
        for (JobAction jobAction : jobActions) {
            // Create a job definition entity if needed.
            JobDefinitionEntity jobDefinitionEntity = jobDefinitionDao.getJobDefinitionByAltKey(jobAction.getNamespace(), jobAction.getJobName());
            if (jobDefinitionEntity == null) {
                jobDefinitionEntity = jobDefinitionDaoTestHelper.createJobDefinitionEntity(jobAction.getNamespace(), jobAction.getJobName(), String.format("Description of \"%s.%s\" job definition.", jobAction.getNamespace(), jobAction.getJobName()), String.format("%s.%s.%s", jobAction.getNamespace(), jobAction.getJobName(), AbstractDaoTest.ACTIVITI_ID));
            }
            NotificationJobActionEntity notificationJobActionEntity = new NotificationJobActionEntity();
            notificationActionEntities.add(notificationJobActionEntity);
            notificationJobActionEntity.setNotificationRegistration(storageUnitNotificationRegistrationEntity);
            notificationJobActionEntity.setJobDefinition(jobDefinitionEntity);
            notificationJobActionEntity.setCorrelationData(jobAction.getCorrelationData());
        }
    }
    return storageUnitNotificationRegistrationDao.saveAndRefresh(storageUnitNotificationRegistrationEntity);
}
Also used : NamespaceEntity(org.finra.herd.model.jpa.NamespaceEntity) BusinessObjectDefinitionKey(org.finra.herd.model.api.xml.BusinessObjectDefinitionKey) FileTypeEntity(org.finra.herd.model.jpa.FileTypeEntity) ArrayList(java.util.ArrayList) StorageEntity(org.finra.herd.model.jpa.StorageEntity) NotificationJobActionEntity(org.finra.herd.model.jpa.NotificationJobActionEntity) NotificationEventTypeEntity(org.finra.herd.model.jpa.NotificationEventTypeEntity) JobDefinitionEntity(org.finra.herd.model.jpa.JobDefinitionEntity) JobAction(org.finra.herd.model.api.xml.JobAction) BusinessObjectDefinitionEntity(org.finra.herd.model.jpa.BusinessObjectDefinitionEntity) StorageUnitStatusEntity(org.finra.herd.model.jpa.StorageUnitStatusEntity) NotificationRegistrationStatusEntity(org.finra.herd.model.jpa.NotificationRegistrationStatusEntity) StorageUnitNotificationRegistrationEntity(org.finra.herd.model.jpa.StorageUnitNotificationRegistrationEntity) NotificationActionEntity(org.finra.herd.model.jpa.NotificationActionEntity)

Example 5 with JobDefinitionEntity

use of org.finra.herd.model.jpa.JobDefinitionEntity in project herd by FINRAOS.

the class ActivitiDelegateTest method testCreateProcessCommandProcessSuspended.

/**
 * This method tests the scenario where a process definition is suspended in Activiti.
 */
@Test(expected = IllegalArgumentException.class)
public void testCreateProcessCommandProcessSuspended() throws Exception {
    // Define the job definition
    JobDefinition jobDefinition = jobDefinitionServiceTestHelper.createJobDefinition(ACTIVITI_XML_HERD_WORKFLOW);
    JobDefinitionEntity jobDefinitionEntity = jobDefinitionDao.getJobDefinitionByAltKey(jobDefinition.getNamespace(), jobDefinition.getJobName());
    // Suspend the job definition with activiti api
    activitiRepositoryService.suspendProcessDefinitionById(jobDefinitionEntity.getActivitiId());
    jobService.createAndStartJob(jobServiceTestHelper.createJobCreateRequest(TEST_ACTIVITI_NAMESPACE_CD, TEST_ACTIVITI_JOB_NAME));
}
Also used : JobDefinitionEntity(org.finra.herd.model.jpa.JobDefinitionEntity) JobDefinition(org.finra.herd.model.api.xml.JobDefinition) Test(org.junit.Test) HerdActivitiServiceTaskTest(org.finra.herd.service.activiti.task.HerdActivitiServiceTaskTest)

Aggregations

JobDefinitionEntity (org.finra.herd.model.jpa.JobDefinitionEntity)36 NamespaceEntity (org.finra.herd.model.jpa.NamespaceEntity)18 Test (org.junit.Test)17 ArrayList (java.util.ArrayList)12 ProcessDefinition (org.activiti.engine.repository.ProcessDefinition)11 HistoricProcessInstance (org.activiti.engine.history.HistoricProcessInstance)7 Collection (java.util.Collection)6 HashSet (java.util.HashSet)6 JobDefinition (org.finra.herd.model.api.xml.JobDefinition)5 JobStatusEnum (org.finra.herd.model.api.xml.JobStatusEnum)5 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 Predicate (javax.persistence.criteria.Predicate)4 NamespacePermission (org.finra.herd.model.annotation.NamespacePermission)4 JobAction (org.finra.herd.model.api.xml.JobAction)4 JobDefinitionCreateRequest (org.finra.herd.model.api.xml.JobDefinitionCreateRequest)4 JobSummaries (org.finra.herd.model.api.xml.JobSummaries)4 JobSummary (org.finra.herd.model.api.xml.JobSummary)4 NotificationActionEntity (org.finra.herd.model.jpa.NotificationActionEntity)4 NotificationJobActionEntity (org.finra.herd.model.jpa.NotificationJobActionEntity)4 Date (java.util.Date)3