class OCRQueueJob implements AcmSchedulableBean {

    @Override
    public void executeTask()
    {
        try
        {
            getAuditPropertyEntityAdapter().setUserId(OCRConstants.OCR_SYSTEM_USER);

            OCRConfiguration configuration = getArkCaseOCRService().getConfiguration();
            List<OCR> processingOCRObjects = getArkCaseOCRService()
                    .getAllByStatus(OCRStatusType.PROCESSING.toString());
            List<OCR> processingOCRAutomaticObjects = processingOCRObjects.stream()
                    .filter(t -> OCRType.AUTOMATIC.toString().equalsIgnoreCase(t.getType())).collect(Collectors.toList());
            List<OCR> processingOCRObjectsDistinctByProcessId = processingOCRAutomaticObjects.stream()
                    .filter(OCRUtils.distinctByProperty(OCR::getProcessId)).collect(Collectors.toList());

            if (configuration.getNumberOfFilesForProcessing() > processingOCRObjectsDistinctByProcessId.size())
            {
                String key = OCRBusinessProcessVariableKey.STATUS.toString();
                String value = OCRStatusType.QUEUED.toString();
                List<ProcessInstance> processInstances = getActivitiRuntimeService().createProcessInstanceQuery().includeProcessVariables()
                        .variableValueEqualsIgnoreCase(key, value).list();

                if (processInstances != null && !processInstances.isEmpty())
                {
                    processInstances.sort(new OCRProcessInstanceCreatedDateComparator());
                    for (ProcessInstance processInstance : processInstances)
                    {
                        List<Long> ids = (List<Long>) processInstance.getProcessVariables().get("IDS");

                        if (ids != null && !ids.isEmpty())
                        {
                            OCR ocr = getArkCaseOCRService().get(ids.get(0));

                            AcmObjectLock lock = objectLockService.findLock(ocr.getEcmFileVersion().getFile().getId(), "FILE");
                            if (ocr != null && (lock == null || lock.getCreator().equalsIgnoreCase(OCRConstants.OCR_SYSTEM_USER)))
                            {
                                objectLockingManager.acquireObjectLock(ocr.getEcmFileVersion().getFile().getId(), "FILE", "WRITE",
                                        null,
                                        true,
                                        OCRConstants.OCR_SYSTEM_USER);
                                getArkCaseOCRService().getOCRServiceFactory().getService(configuration.getProvider()).create(ocr);
                                getArkCaseOCRService().save(ocr);
                                getArkCaseOCRService().signal(processInstance, OCRStatusType.PROCESSING.toString(),
                                        OCRActionType.PROCESSING.toString());
                                break;
                            }
                        }
                    }
                }
            }
        }
        catch (GetConfigurationException | SaveOCRException | GetOCRException | CreateOCRException | AcmObjectLockException
                | OCRServiceProviderNotFoundException e)
        {
            LOG.error("Could not move OCR from the queue. REASON=[{}]", e.getMessage(), e);
        }
    }

}