JobDAO { public void haltJob(Integer processId, String batchMarking) { Session session = sessionFactory.openSession(); try { session.beginTransaction(); Process parentProcessId = new Process(); parentProcessId.setProcessId(processId); List<Process> listOfSubProcesses = new ArrayList<Process>(); Criteria listOfSubProcessCriteria = session.createCriteria(Process.class).add(Restrictions.eq(PROCESS, parentProcessId)).add(Restrictions.eq(DELETEFLAG, false)); for (Object subProcessObject : listOfSubProcessCriteria.list()) { Process subProcess = (Process) subProcessObject; listOfSubProcesses.add(subProcess); } List<Process> listOfDownStreamSubProcessesWithEnqID = new ArrayList<Process>(); Criteria listOfDownStreamSubProcessesWithEnqIDCriteria = session.createCriteria(Process.class).add(Restrictions.eq(DELETEFLAG, false)) .add(Restrictions.or(Restrictions.eq(ENQUEUINGPROCESSID, parentProcessId.getProcessId().toString()),Restrictions.like(ENQUEUINGPROCESSID,"%,"+parentProcessId.getProcessId().toString()),Restrictions.like(ENQUEUINGPROCESSID,parentProcessId.getProcessId().toString()+",%"),Restrictions.like(ENQUEUINGPROCESSID,"%,"+parentProcessId.getProcessId().toString()+",%"))); LOGGER.info("total subproesses "+listOfDownStreamSubProcessesWithEnqIDCriteria.list()); for (Object subProcessObject : listOfDownStreamSubProcessesWithEnqIDCriteria.list()) { Process subProcess = (Process) subProcessObject; listOfDownStreamSubProcessesWithEnqID.add(subProcess); } LOGGER.info("listOfDownStreamSubProcessesWithEnqID is "+listOfDownStreamSubProcessesWithEnqID.toString()); ExecStatus runningExecStatus = new ExecStatus(); runningExecStatus.setExecStateId(2); ExecStatus failedExecStatus = new ExecStatus(); failedExecStatus.setExecStateId(6); ExecStatus successExecStatus = new ExecStatus(); successExecStatus.setExecStateId(3); BatchStatus newBatchStatus = new BatchStatus(); newBatchStatus.setBatchStateId(0); BatchStatus processedBatchStatus = new BatchStatus(); processedBatchStatus.setBatchStateId(1); if ("null,".equals(batchMarking)) { batchMarking = null; } Criteria validProcessCriteria = session.createCriteria(Process.class).add(Restrictions.eq(PROCESSID, processId)).add(Restrictions.eq(DELETEFLAG, false)); if (validProcessCriteria.list().isEmpty()) { LOGGER.error(INVALIDPARENTPROCESS + processId); throw new MetadataException(INVALIDPARENTPROCESS + processId); } else { Process validProcess = (Process) validProcessCriteria.list().get(0); if (validProcess.getProcess() != null) { LOGGER.error(INVALIDPARENTPROCESS + processId); throw new MetadataException(INVALIDPARENTPROCESS + processId); } } Criteria runningProcessCriteria = session.createCriteria(InstanceExec.class).add(Restrictions.eq(PROCESS, parentProcessId)) .add(Restrictions.eq(EXECSTATUS, runningExecStatus)); if (runningProcessCriteria.list().size() != 1) { LOGGER.error(NOTUNDEREXECUTIONPROCESS + processId); throw new MetadataException(NOTUNDEREXECUTIONPROCESS + processId); } if (!listOfSubProcesses.isEmpty()) { Criteria runningSubProcessCriteria = session.createCriteria(InstanceExec.class).add(Restrictions.in(PROCESS, listOfSubProcesses)) .add(Restrictions.eq(EXECSTATUS, runningExecStatus)); if (!runningSubProcessCriteria.list().isEmpty()) { LOGGER.error("sub process in running state "); throw new MetadataException("sub process in running state"); } } List<Long> maxInstanceExecIds = new ArrayList<Long>(); for (Process subProcess : listOfSubProcesses) { Criteria maxInstanceExecCriteria = session.createCriteria(InstanceExec.class).add(Restrictions.eq(PROCESS, subProcess)) .addOrder(Order.desc("instanceExecId")).setMaxResults(1); if (maxInstanceExecCriteria.list().size() == 1) { InstanceExec maxInstanceExecId = (InstanceExec) maxInstanceExecCriteria.list().get(0); maxInstanceExecIds.add(maxInstanceExecId.getInstanceExecId()); } } if (!maxInstanceExecIds.isEmpty()) { Criteria failedSubProcessCriteria = session.createCriteria(InstanceExec.class).add(Restrictions.in("instanceExecId", maxInstanceExecIds)) .add(Restrictions.eq(EXECSTATUS, failedExecStatus)); if (!failedSubProcessCriteria.list().isEmpty()) { LOGGER.error("sub process in failed state"); throw new MetadataException("sub process in failed state"); } } Criteria updateInstanceExecCriteria = session.createCriteria(InstanceExec.class).add(Restrictions.eq(PROCESS, parentProcessId)) .add(Restrictions.eq(EXECSTATUS, runningExecStatus)); InstanceExec updateInstanceExec = (InstanceExec) updateInstanceExecCriteria.list().get(0); updateInstanceExec.setEndTs(new Date()); updateInstanceExec.setExecStatus(successExecStatus); session.update(updateInstanceExec); String batchMarkingPassed; if (batchMarking == null) { batchMarking = "0"; batchMarkingPassed = null; } else { batchMarkingPassed = batchMarking; } Batch targetBatchId = null; if (!listOfSubProcesses.isEmpty()) { Criteria targetBatchIdCriteria = session.createCriteria(BatchConsumpQueue.class).add(Restrictions.in(PROCESS, listOfSubProcesses)) .setMaxResults(1); if (targetBatchIdCriteria.list().size() == 1) { BatchConsumpQueue batchConsumpQueue = (BatchConsumpQueue) targetBatchIdCriteria.list().get(0); targetBatchId = batchConsumpQueue.getBatchByTargetBatchId(); } } Criteria deleteBCQCriteria = session.createCriteria(BatchConsumpQueue.class).add(Restrictions.eq("sourceProcessId", processId)); List<BatchConsumpQueue> deleteBCQList=deleteBCQCriteria.list(); for(int i=0;i<deleteBCQList.size();i++) { BatchConsumpQueue batchConsumpQueue=deleteBCQList.get(i); session.delete(batchConsumpQueue); } for (Process subProcess : listOfDownStreamSubProcessesWithEnqID) { if (batchMarkingPassed == null) { BatchConsumpQueue insertBatchConsumpQueue = new BatchConsumpQueue(); insertBatchConsumpQueue.setBatchBySourceBatchId(targetBatchId); insertBatchConsumpQueue.setInsertTs(new Date()); insertBatchConsumpQueue.setSourceProcessId(parentProcessId.getProcessId()); insertBatchConsumpQueue.setBatchStatus(newBatchStatus); insertBatchConsumpQueue.setBatchMarking(null); insertBatchConsumpQueue.setProcess(subProcess); session.save(insertBatchConsumpQueue); } else { BatchConsumpQueue insertBatchConsumpQueue = new BatchConsumpQueue(); insertBatchConsumpQueue.setBatchBySourceBatchId(targetBatchId); insertBatchConsumpQueue.setInsertTs(new Date()); insertBatchConsumpQueue.setSourceProcessId(parentProcessId.getProcessId()); insertBatchConsumpQueue.setBatchStatus(newBatchStatus); insertBatchConsumpQueue.setBatchMarking(batchMarking); insertBatchConsumpQueue.setProcess(subProcess); session.save(insertBatchConsumpQueue); } } if (!listOfSubProcesses.isEmpty()) { Criteria insertACQCriteria = session.createCriteria(BatchConsumpQueue.class).add(Restrictions.in(PROCESS, listOfSubProcesses)) .add(Restrictions.eq("batchStatus", processedBatchStatus)); for (Object batchConsumpQueueObject : insertACQCriteria.list()) { BatchConsumpQueue batchConsumpQueue = (BatchConsumpQueue) batchConsumpQueueObject; ArchiveConsumpQueue archiveConsumpQueue = new ArchiveConsumpQueue(); archiveConsumpQueue.setProcess(batchConsumpQueue.getProcess()); archiveConsumpQueue.setBatchByTargetBatchId(batchConsumpQueue.getBatchByTargetBatchId()); archiveConsumpQueue.setBatchBySourceBatchId(batchConsumpQueue.getBatchBySourceBatchId()); archiveConsumpQueue.setBatchStatus(batchConsumpQueue.getBatchStatus()); archiveConsumpQueue.setInsertTs(batchConsumpQueue.getInsertTs()); archiveConsumpQueue.setSourceProcessId(batchConsumpQueue.getSourceProcessId()); archiveConsumpQueue.setStartTs(batchConsumpQueue.getStartTs()); archiveConsumpQueue.setEndTs(batchConsumpQueue.getEndTs()); archiveConsumpQueue.setBatchMarking(batchConsumpQueue.getBatchMarking()); session.save(archiveConsumpQueue); session.delete(batchConsumpQueue); } } session.getTransaction().commit(); } catch (MetadataException e) { session.getTransaction().rollback(); LOGGER.error(e); throw e; } finally { session.close(); } }  List<InitJobRowInfo> initJob(Integer processId, Integer maxBatch); void haltJob(Integer processId, String batchMarking); void termJob(Integer processId);  }
@Ignore @Test public void testHaltJob() throws Exception { jobDAO.haltJob(1, "@a"); }