Java tutorial
package edu.byu.softwareDist.manager.impl; import edu.byu.softwareDist.dao.*; import edu.byu.softwareDist.domain.*; import edu.byu.softwareDist.helper.FileLicenseSet; import edu.byu.softwareDist.helper.FileSetWithFiles; import edu.byu.softwareDist.manager.FileSetManager; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.io.File; import java.util.*; import static org.springframework.transaction.annotation.Isolation.DEFAULT; import static org.springframework.transaction.annotation.Propagation.REQUIRED; /** * Created by wct5 on 2010-10-06 17:23 * * @author wct5 */ @Service("fileSetManager") @SuppressWarnings("unused") public class FileSetManagerImpl implements FileSetManager { private static final Logger LOG = Logger.getLogger(FileSetManagerImpl.class); private FileSetDao fsdao; private FilesDao fdao; private FileSetFilesDao fsfdao; private LicenseSetDao lsdao; private LicenseSetFileSetDao lsfsdao; @Autowired public FileSetManagerImpl(FileSetDao fsdao, FilesDao fdao, FileSetFilesDao fsfdao, LicenseSetDao lsdao, LicenseSetFileSetDao lsfsdao) { this.fsdao = fsdao; this.fdao = fdao; this.fsfdao = fsfdao; this.lsdao = lsdao; this.lsfsdao = lsfsdao; } @Override @Transactional(readOnly = false, isolation = DEFAULT, propagation = REQUIRED) public FileSetWithFiles createNewFileSet(FileSet fileSet, List<Files> files) { final FileSet sfs = fsdao.save(fileSet); final List<Files> rf = new ArrayList<Files>(files.size()); for (Files file : files) { Files dbf = fdao.findByFullPath(file.getDiskLocation()); if (dbf == null) { dbf = addFileFromFileSystem(file); } FileSetFiles assoc = new FileSetFiles(sfs.getFileSetId(), dbf.getFileId(), null, null); fsfdao.save(assoc); rf.add(dbf); } return new FileSetWithFiles(sfs, rf); } private Files findFiles(String input) { Files files = fdao.findByFullPath(input); if (files != null) { return files; } return createFile(input); } @Override @Transactional(readOnly = false, isolation = DEFAULT, propagation = REQUIRED) public FileSet saveFileSet(FileSet fileSet, Collection<String> files, Collection<LicenseSet> licenseSets) { FileSet savedFileSet; if (fileSet.getFileSetId() == null) { savedFileSet = createNewFileSet(fileSet, loadFilesFromNames(files)).getFileSet(); } else { savedFileSet = fsdao.update(fileSet); files = new TreeSet<String>(files); //go through all the old files in this file set //if the incoming list of files does not include it, delete it List<Files> oldFiles = fdao.findAllByFileSet(fileSet); for (Files file : oldFiles) { String diskLocation = file.getDiskLocation(); if (files.contains(diskLocation)) { files.remove(diskLocation); } else { fdao.delete(file); } } //save all the new files for (String diskLocation : files) { Files file = findFiles(diskLocation); FileSetFiles fsf = new FileSetFiles(); fsf.setFileId(file.getFileId()); fsf.setFileSetId(fileSet.getFileSetId()); fsfdao.save(fsf); } } verifyKeyAssociations(savedFileSet.getFileSetId(), extractLicenseSetIds(licenseSets)); return savedFileSet; } private List<Files> loadFilesFromNames(final Collection<String> files) { if (files == null || files.isEmpty()) return Collections.emptyList(); final List<Files> result = new ArrayList<Files>(files.size()); for (final String f : files) { result.add(findFiles(f)); } return result; } private List<Integer> extractLicenseSetIds(final Collection<LicenseSet> licenseSets) { if (licenseSets == null || licenseSets.isEmpty()) return Collections.emptyList(); final List<Integer> result = new ArrayList<Integer>(licenseSets.size()); for (final LicenseSet ls : licenseSets) { result.add(ls.getLicenseSetId()); } return result; } @Override public FileSetWithFiles findFileSetWithFilesById(int fileSetId) { final FileSetWithFiles result = new FileSetWithFiles(); FileSet fs = fsdao.findById(fileSetId); if (fs == null) { throw new IllegalArgumentException("No file set found for id " + fileSetId); } result.setFileSet(fs); List<Files> files = fsfdao.findAllFilesForFileSet(fileSetId); result.setFiles(files); return result; } @Override public Files getFileByFullPath(String path) { return fdao.findByFullPath(path); } private Files createFile(String diskLocation) { Files files = new Files(); files.setDiskLocation(diskLocation); return addFileFromFileSystem(files); } @Override @Transactional(readOnly = false, propagation = REQUIRED) public Files addFileFromFileSystem(Files file) { if (file.getDiskLocation() == null || file.getDiskLocation().isEmpty()) { throw new IllegalArgumentException("The file must have a valid disk location."); } File f = new File(file.getDiskLocation()); if (!f.exists()) { throw new IllegalStateException("The file specified " + file.getDiskLocation() + " does not exist."); } if (file.getFileName() == null || file.getFileName().isEmpty()) { file.setFileName(f.getName()); } file.setSizeInBytes(f.length()); if (file.getContentType() == null || file.getContentType().isEmpty()) { file.setContentType(determineContentType(f.getName())); } return fdao.save(file); } @Override @Transactional(readOnly = false, propagation = REQUIRED) public Files updateFileFromFileSystem(Files file) { if (file.getDiskLocation() == null || file.getDiskLocation().isEmpty()) { throw new IllegalArgumentException("The file must have a valid disk location."); } File f = new File(file.getDiskLocation()); if (!f.exists()) { throw new IllegalStateException("The file specified " + file.getDiskLocation() + " does not exist."); } file.setSizeInBytes(f.length()); if (file.getContentType() == null || file.getContentType().isEmpty()) { file.setContentType(determineContentType(f.getName())); } return fdao.update(file); } @Override @Transactional(readOnly = false, isolation = DEFAULT, propagation = REQUIRED) public FileSetWithFiles editFileSetWithFiles(FileSet newValues, int fileSetId, List<String> existingFiles, List<String> newFiles, List<Integer> removeFiles) { FileSetWithFiles result = new FileSetWithFiles(); if (newValues == null) { throw new IllegalArgumentException("Please include a FileSet object that contains values."); } final FileSet fs = fsdao.findById(fileSetId); if (fs == null) { throw new IllegalArgumentException("FileSetId " + fileSetId + " is not valid or does not exist."); } if (newValues.hasNewValues(fs)) { fs.setName(newValues.getName()); fs.setDescription(newValues.getDescription()); fs.setUserUrl(newValues.getUserUrl()); fs.setSendEmailTo(newValues.getSendEmailTo()); fs.setEmailSubject(newValues.getEmailSubject()); fs.setEmailContent(newValues.getEmailContent()); result.setFileSet(fsdao.update(fs)); } else { result.setFileSet(fs); } for (Integer fileId : removeFiles) { fsfdao.deleteByFileIdAndFileSetId(fileId, fileSetId); } for (String nfn : newFiles) { final String fn = cleanFileName(nfn); Files f = fdao.findByFullPath(fn); if (f == null) { f = addFileFromFileSystem(f); } fsfdao.save(new FileSetFiles(fileSetId, f.getFileId(), null, null)); } result.setFiles(fsfdao.findAllFilesForFileSet(fileSetId)); return result; } private String determineContentType(String fileName) { if (fileName == null) { throw new IllegalArgumentException("You must provide a file name to determine content type."); } int pos = fileName.lastIndexOf("."); if (pos < 0) { return "application/octet-stream"; } if (fileName.endsWith(".")) { throw new IllegalStateException("File names must not end in '.'."); } String extension = fileName.substring(pos + 1); if (extension.equalsIgnoreCase("exe")) { return "application/octet-stream"; } else if (extension.equalsIgnoreCase("zip")) { return "application/zip"; } else if (extension.equalsIgnoreCase("rar")) { return "application/zip"; } else if (extension.equalsIgnoreCase("iso")) { return "application/zip"; } else { return "application/octet-stream"; } } private static String cleanFileName(String path) { return path.replaceAll("\\\\", "/").replaceAll("//", "/"); } @Override public FileLicenseSet findAssociatedLicenseSetsByFileSetId(Integer fileSetId) { List<String> nonAssociated = new ArrayList<String>(); List<String> associated = new ArrayList<String>(); final List<LicenseSet> nonAssociatedLists = lsdao.findAll(); for (LicenseSet ls : nonAssociatedLists) { nonAssociated.add(ls.getName()); } final List<LicenseSet> associatedLists = lsdao.findAllByFileSetId(fileSetId); for (LicenseSet lsn : associatedLists) { associated.add(lsn.getName()); } nonAssociated.removeAll(associated); return new FileLicenseSet(fileSetId, associated, nonAssociated); } @Transactional(readOnly = false, isolation = DEFAULT, propagation = REQUIRED) @Override public void verifyKeyAssociations(Integer fileSetId, List<Integer> keyIds) { final List<LicenseSetFileSet> associated = lsfsdao.findByFileSet(fileSetId); List<Integer> associatedKeys = new LinkedList<Integer>(); for (LicenseSetFileSet lset : associated) { associatedKeys.add(lset.getLicenseSetId()); } for (Integer s : keyIds) { if (s != null) { if (!associatedKeys.contains(s)) { LOG.info("Saving " + s); lsfsdao.save(new LicenseSetFileSet(s, fileSetId, new Date(), "TEST")); } } } LOG.info(associatedKeys.size()); for (LicenseSetFileSet c : associated) { LOG.info("Check for Delete " + c); if (!keyIds.contains(c.getLicenseSetId())) { LOG.info("Deleting " + c); lsfsdao.delete(c); } } } }