CopyFileDAO { public FileInfo copyFile(CopyFileInfo copyFileInfo) { Session session = sessionFactory.openSession(); try { session.beginTransaction(); if (copyFileInfo.getDestBatchId() == null || copyFileInfo.getDestServerId() == null || copyFileInfo.getSourceBatchId() == null) { LOGGER.info("Null params except for prefix not allowed"); throw new MetadataException("Null params except for prefix not allowed"); } Criteria deletingFileCriteria = session.createCriteria(File.class).add(Restrictions.eq("id.serverId", copyFileInfo.getDestServerId())).add(Restrictions.eq("id.batchId", copyFileInfo.getDestBatchId())); List<File> deletingFiles = deletingFileCriteria.list(); if (deletingFiles != null) { for (File deletingFile : deletingFiles) { session.delete(deletingFile); LOGGER.info("creation time of the file deleted is " + deletingFile.getId().getCreationTs()); } } Batch sourceBatch = (Batch) session.get(Batch.class, copyFileInfo.getSourceBatchId()); LOGGER.info("sourceBatch" + sourceBatch.getBatchId()); Servers destServer = (Servers) session.get(Servers.class, copyFileInfo.getDestServerId()); Batch destBatch = (Batch) session.get(Batch.class, copyFileInfo.getDestBatchId()); Criteria addingFileCriteria = session.createCriteria(File.class).add(Restrictions.eq("id.batchId", copyFileInfo.getSourceBatchId())); LOGGER.info("files count:" + addingFileCriteria.list().size()); List<File> addingFiles = addingFileCriteria.list(); for (File addingFile : addingFiles) { LOGGER.info(" IDPath" + addingFile.getId().getPath() + "Batchid" + addingFile.getBatch().getBatchId()); FileId addingFileId = addingFile.getId(); addingFileId.setPath(copyFileInfo.getDestPrefix() + addingFileId.getPath()); addingFileId.setBatchId(destBatch.getBatchId()); addingFileId.setServerId(destServer.getServerId()); File file = new File(); file.setServers(destServer); file.setBatch(destBatch); file.setId(addingFileId); LOGGER.info("adding file id is " + file.getId().getBatchId()); LOGGER.info("adding file dest batch id : " + addingFile.getBatch().getBatchId()); LOGGER.info("adding file server id" + addingFile.getServers().getServerId()); session.save(file); } LOGGER.info("return dest server id is " + destServer.getServerId()); LOGGER.info("return batch id is " + destBatch.getBatchId()); Criteria returningFilesCriteria = session.createCriteria(File.class).add(Restrictions.eq("batch", destBatch)).add(Restrictions.eq("servers", destServer)); returningFilesCriteria.setMaxResults(1); List<File> returningFiles = returningFilesCriteria.list(); LOGGER.info(returningFiles.size() + " return file size"); File returningFile = new File(); if (!returningFiles.isEmpty()) returningFile = returningFiles.get(0); LOGGER.info("Returning:" + returningFile); FileInfo fileInfo = new FileInfo(); if (returningFile.getBatch() != null && returningFile != null) fileInfo.setBatchId(returningFile.getBatch().getBatchId()); if (returningFile.getId() != null && returningFile != null) { fileInfo.setFileHash(returningFile.getId().getFileHash()); fileInfo.setFilePath(returningFile.getId().getPath()); fileInfo.setFileSize(returningFile.getId().getFileSize()); } destServer = (Servers) session.get(Servers.class, copyFileInfo.getDestServerId()); if (destServer != null) { fileInfo.setServerIP(destServer.getServerIp()); fileInfo.setServerName(destServer.getServerName()); fileInfo.setServerType(destServer.getServerType()); fileInfo.setSshPrivateKey(destServer.getSshPrivateKey()); fileInfo.setUsername(destServer.getLoginUser()); fileInfo.setPassword(destServer.getLoginPassword()); } session.getTransaction().commit(); return fileInfo; } catch (MetadataException e) { session.getTransaction().rollback(); throw new MetadataException(e); } finally { session.close(); } }  FileInfo copyFile(CopyFileInfo copyFileInfo);  }
@Test public void copyFileTest() { CopyFileInfo copyFileInfo = new CopyFileInfo(); copyFileInfo.setDestBatchId(Long.valueOf(0)); copyFileInfo.setDestPrefix("Test123"); copyFileInfo.setDestServerId(123461); copyFileInfo.setSourceBatchId(Long.valueOf(0)); FileInfo fileInfo = copyFileDAO.copyFile(copyFileInfo); LOGGER.info("fileInfo is " + fileInfo); }