1 package ch.busyboxes.agoo.dao.impl;
2
3 import java.util.LinkedList;
4 import java.util.List;
5
6 import org.apache.log4j.Logger;
7 import org.springframework.transaction.annotation.Transactional;
8
9 import ch.busyboxes.agoo.dao.WatchedFolderDAO;
10 import ch.busyboxes.agoo.model.WatchedFile;
11 import ch.busyboxes.agoo.model.WatchedFolder;
12 import ch.busyboxes.agoo.model.enumeration.WatchedFileState;
13
14
15
16
17
18
19 @Transactional
20 public class WatchedFolderDAOImpl extends AbstractHibernateDAO implements WatchedFolderDAO {
21
22
23 private Logger logger = Logger.getLogger(WatchedFolderDAOImpl.class);
24
25
26
27
28 @SuppressWarnings("unchecked")
29 public List<WatchedFolder> getAllWatchedFolders() {
30 List<WatchedFolder> watchedFolders = hibernateTemplate.find("from WatchedFolder");
31 return watchedFolders;
32 }
33
34
35
36
37 public void saveWatchedFolder(WatchedFolder watchedFolder) {
38 hibernateTemplate.save(watchedFolder);
39 }
40
41
42
43
44 @Override
45 public WatchedFolder getWatchedFolderById(Long folderId) {
46 WatchedFolder watchedFolder = (WatchedFolder) hibernateTemplate.get(WatchedFolder.class, folderId);
47 return watchedFolder;
48 }
49
50
51
52
53 @Override
54 public void addWatchedFileToFolder(WatchedFolder watchedFolder,
55 String filePath) {
56 List<WatchedFile> watchedFiles = watchedFolder.getWatchedFiles();
57
58 boolean filePathAlreadyExists = false;
59 for(WatchedFile watchedFile : watchedFiles) {
60 if (watchedFile.getFilename().equals(filePath)) {
61 filePathAlreadyExists = true;
62 }
63 }
64
65 if (!filePathAlreadyExists) {
66 WatchedFile watchedFile = new WatchedFile();
67 watchedFile.setFilename(filePath);
68 watchedFile.setWatchedFileState(WatchedFileState.NEW);
69 watchedFile.setWatchedFolder(watchedFolder);
70
71 if (logger.isDebugEnabled()) {
72 logger.debug("Saving new watched file: " + filePath);
73 }
74
75 hibernateTemplate.save(watchedFile);
76 }
77 }
78
79
80
81
82 @Override
83 public List<WatchedFile> addWatchedFilesToFolder(WatchedFolder watchedFolder,
84 List<String> filePaths) {
85 List<WatchedFile> watchedFiles = watchedFolder.getWatchedFiles();
86 List<WatchedFile> newWatchedFiles = new LinkedList<WatchedFile>();
87
88 for (String filePath : filePaths) {
89 boolean filePathAlreadyExists = false;
90 for(WatchedFile watchedFile : watchedFiles) {
91 if (watchedFile.getFilename().equals(filePath)) {
92 filePathAlreadyExists = true;
93 }
94 }
95
96 if (!filePathAlreadyExists) {
97 WatchedFile watchedFile = new WatchedFile();
98 watchedFile.setFilename(filePath);
99 watchedFile.setWatchedFileState(WatchedFileState.NEW);
100 watchedFile.setWatchedFolder(watchedFolder);
101
102 newWatchedFiles.add(watchedFile);
103 }
104 }
105
106 if (logger.isDebugEnabled()) {
107 logger.debug("Batch saving " + filePaths.size() + " watched files");
108 }
109 hibernateTemplate.saveOrUpdateAll(newWatchedFiles);
110
111 return newWatchedFiles;
112 }
113
114
115
116
117 @Override
118 public void deleteWatchedFolder(Long folderId) {
119 WatchedFolder watchedFolder = (WatchedFolder) hibernateTemplate.load(WatchedFolder.class, folderId);
120 hibernateTemplate.delete(watchedFolder);
121 }
122
123 }