use of org.finra.herd.model.dto.JobDefinitionAlternateKeyDto in project herd by FINRAOS.
the class JobServiceImpl method getJobs.
@Override
public JobSummaries getJobs(String namespace, String jobName, JobStatusEnum jobStatus, DateTime startTime, DateTime endTime) throws Exception {
// Trim the parameters.
String namespaceTrimmed = namespace == null ? null : namespace.trim();
String jobNameTrimmed = jobName == null ? null : jobName.trim();
// Construct the list of job summaries to return.
JobSummaries jobSummaries = new JobSummaries();
/*
* Get the namespaces which the current user is authorized to READ.
* If a specific namespace was requested, and the current user is authorized to read the namespace, include ONLY the requested namespace.
* If a specific namespace was requested, but the current user is not authorized to read the namespace, clear all namespaces.
* Otherwise, include all authorized namespaces.
*
* This ensures that only authorized namespaces are queried from the database and that
* an unauthorized user cannot determine if he specified an existing namespace or not.
*/
Set<String> authorizedNamespaces = namespaceSecurityHelper.getAuthorizedNamespaces(NamespacePermissionEnum.READ);
if (StringUtils.isNotBlank(namespaceTrimmed)) {
NamespaceEntity namespaceEntity = namespaceDao.getNamespaceByCd(namespaceTrimmed);
if (namespaceEntity != null && authorizedNamespaces.contains(namespaceEntity.getCode())) {
authorizedNamespaces.clear();
authorizedNamespaces.add(namespaceEntity.getCode());
} else {
authorizedNamespaces.clear();
}
}
// Continue the processing only when the list of authorized namespaces is not empty.
if (!authorizedNamespaces.isEmpty()) {
// Build a set of Activiti process definition keys for each job definition that was filtered.
Set<String> processDefinitionIds = new HashSet<>();
for (JobDefinitionEntity jobDefinitionEntity : jobDefinitionDao.getJobDefinitionsByFilter(authorizedNamespaces, jobNameTrimmed)) {
processDefinitionIds.add(jobDefinitionEntity.getActivitiId());
}
// Continue the processing only when the list of filtered job definitions is not empty.
if (!processDefinitionIds.isEmpty()) {
/*
* Keep a mapping of definition ID to its key.
* Activiti API does not provide an easy way to get the key without doing extra queries for each definition.
*/
Map<String, String> processDefinitionIdToKeyMap = new HashMap<>();
for (ProcessDefinition processDefinition : activitiService.getProcessDefinitionsByIds(processDefinitionIds)) {
processDefinitionIdToKeyMap.put(processDefinition.getId(), processDefinition.getKey());
}
List<HistoricProcessInstance> historicProcessInstances = getHistoricProcessInstances(processDefinitionIdToKeyMap.values(), jobStatus, startTime, endTime);
// Get a set of all runtime suspended process instance ids.
Set<String> suspendedProcessInstanceIds = getSuspendedProcessInstanceIds();
// Compile the Regex pattern.
Pattern pattern = jobDefinitionHelper.getNamespaceAndJobNameRegexPattern();
// Loop over the process instances and build a list of job summaries to return.
for (HistoricProcessInstance historicProcessInstance : historicProcessInstances) {
// Exclude all process instances started using older versions of the process definition.
if (processDefinitionIdToKeyMap.containsKey(historicProcessInstance.getProcessDefinitionId())) {
// Set a flag if this job is SUSPENDED.
boolean suspended = suspendedProcessInstanceIds.contains(historicProcessInstance.getId());
// getHistoricProcessInstances() returns both RUNNING and SUSPENDED process instances.
if (!(JobStatusEnum.SUSPENDED.equals(jobStatus) && !suspended) && !(JobStatusEnum.RUNNING.equals(jobStatus) && suspended)) {
// Create a new job summary.
JobSummary jobSummary = new JobSummary();
jobSummary.setId(historicProcessInstance.getId());
// Get the job definition key.
JobDefinitionAlternateKeyDto jobDefinitionKey = jobDefinitionHelper.getJobDefinitionKey(processDefinitionIdToKeyMap.get(historicProcessInstance.getProcessDefinitionId()), pattern);
// Set the namespace and job name on the job summary.
jobSummary.setNamespace(jobDefinitionKey.getNamespace());
jobSummary.setJobName(jobDefinitionKey.getJobName());
// Set the start time always since all jobs will have a start time.
jobSummary.setStartTime(HerdDateUtils.getXMLGregorianCalendarValue(historicProcessInstance.getStartTime()));
if (historicProcessInstance.getEndTime() == null) {
// Since there is no end time, the job is running or suspended.
jobSummary.setStatus(suspended ? JobStatusEnum.SUSPENDED : JobStatusEnum.RUNNING);
// If the end time is null, then determine the status based on the presence of any exceptions.
jobSummary.setTotalExceptions(activitiService.getJobsWithExceptionCountByProcessInstanceId(historicProcessInstance.getId()));
} else {
// If the end time is set, then the job has finished so set the end time and the status to completed.
jobSummary.setEndTime(HerdDateUtils.getXMLGregorianCalendarValue(historicProcessInstance.getEndTime()));
jobSummary.setStatus(JobStatusEnum.COMPLETED);
}
// Add the new summary to the list.
jobSummaries.getJobSummaries().add(jobSummary);
}
}
}
}
}
// Return the list of job summaries.
return jobSummaries;
}
Aggregations