RegisterFileDAO { public RegisterFileInfo registerFile(RegisterFileInfo registerFileInfo) { Session session = sessionFactory.openSession(); long autoGenBatchId; try { session.beginTransaction(); Batch batchObj = new Batch(); if (registerFileInfo.getBatchId() == null) { batchObj.setBatchType("Type1"); batchObj.setInstanceExec(null); session.save(batchObj); autoGenBatchId = batchObj.getBatchId(); LOGGER.info("Auto generated BatchId" + autoGenBatchId); } else { autoGenBatchId = registerFileInfo.getBatchId(); batchObj.setBatchId(autoGenBatchId); } registerFileInfo.setBatchId(autoGenBatchId); Servers servers = new Servers(); servers.setServerId(registerFileInfo.getServerId()); FileId fileId = new FileId(); fileId.setPath(registerFileInfo.getPath()); fileId.setFileHash(registerFileInfo.getFileHash()); fileId.setBatchId(registerFileInfo.getBatchId()); fileId.setServerId(registerFileInfo.getServerId()); fileId.setFileSize(registerFileInfo.getFileSize()); fileId.setCreationTs(registerFileInfo.getCreationTs()); Criteria fileCriteria = session.createCriteria(File.class).add(Restrictions.eq("servers", servers)).add(Restrictions.eq("id.fileHash", fileId.getFileHash())).add(Restrictions.eq("id.path", fileId.getPath())); LOGGER.info("matched file count" + fileCriteria.list().size()); if (!fileCriteria.list().isEmpty()) { LOGGER.error("File Already exists exception"); throw new MetadataException("File Already exists exception"); } else { File newFile = new File(); newFile.setId(fileId); newFile.setServers(servers); newFile.setBatch(batchObj); session.save(newFile); BatchStatus batchStatusObj = new BatchStatus(); batchStatusObj.setBatchStateId(1); Process subProcess = (Process) session.get(Process.class, registerFileInfo.getSubProcessId()); } session.getTransaction().commit(); return registerFileInfo; } catch (MetadataException e) { session.getTransaction().rollback(); throw e; } finally { session.close(); } }  RegisterFileInfo registerFile(RegisterFileInfo registerFileInfo);  }
@Test public void registerFileTest() { com.wipro.ats.bdre.md.dao.jpa.Process parentProcess = new Process(); Process childProcess = new Process(); WorkflowType workflowType = workflowTypeDAO.get(1); BusDomain busDomain = busDomainDAO.get(1); ProcessTemplate processTemplate = processTemplateDAO.get(0); ProcessType processType = processTypeDAO.get(1); parentProcess.setProcess(null); parentProcess.setDescription("Test Parent Process"); parentProcess.setAddTs(new Date()); parentProcess.setProcessName("Test parent process name"); parentProcess.setBusDomain(busDomain); parentProcess.setProcessType(processType); parentProcess.setCanRecover(false); parentProcess.setEnqueuingProcessId("0"); parentProcess.setNextProcessId(" "); parentProcess.setWorkflowType(workflowType); parentProcess.setDeleteFlag(false); parentProcess.setProcessTemplate(processTemplate); parentProcess.setEditTs(new Date()); Integer parentProcessId = processDAO.insert(parentProcess); LOGGER.info("prnt proc id" + parentProcessId); processType = processTypeDAO.get(12); childProcess.setProcess(processDAO.get(parentProcessId)); childProcess.setDescription("Test Child Process"); childProcess.setAddTs(new Date()); childProcess.setProcessName("Test child process name"); childProcess.setBusDomain(busDomain); childProcess.setProcessType(processType); childProcess.setCanRecover(false); childProcess.setEnqueuingProcessId("0"); childProcess.setNextProcessId("1"); childProcess.setWorkflowType(workflowType); childProcess.setDeleteFlag(false); childProcess.setProcessTemplate(processTemplate); childProcess.setEditTs(new Date()); Integer childProcessId = processDAO.insert(childProcess); LOGGER.info("chld process id" + childProcessId); parentProcess = processDAO.get(parentProcessId); String cpid = childProcessId.toString(); LOGGER.info("child process id " + cpid); parentProcess.setNextProcessId(cpid); processDAO.update(parentProcess); LOGGER.info("child and parent inserted successfully"); RegisterFileInfo registerFileInfo = new RegisterFileInfo(); registerFileInfo.setSubProcessId(childProcessId); registerFileInfo.setServerId(123461); registerFileInfo.setPath("test path"); registerFileInfo.setFileSize(Long.valueOf(1)); registerFileInfo.setFileHash("12345"); Calendar calendar = Calendar.getInstance(); java.util.Date now = calendar.getTime(); registerFileInfo.setCreationTs(new Timestamp(now.getTime())); registerFileInfo.setBatchId(Long.valueOf(0)); registerFileDAO.registerFile(registerFileInfo); processDAO.testDelete(childProcessId); processDAO.testDelete(parentProcessId); }