1 package ch.busyboxes.agoo.service.impl;
2
3 import java.util.LinkedList;
4 import java.util.List;
5
6 import ch.busyboxes.agoo.controller.model.WebWatchedFolder;
7 import ch.busyboxes.agoo.dao.FileSystemDAO;
8 import ch.busyboxes.agoo.dao.WatchedFolderDAO;
9 import ch.busyboxes.agoo.model.WatchedFile;
10 import ch.busyboxes.agoo.model.WatchedFolder;
11 import ch.busyboxes.agoo.service.WatchedFolderService;
12 import ch.busyboxes.agoo.service.WatcherTaskService;
13 import ch.busyboxes.agoo.service.impl.adapter.WatchedFolderAdapter;
14
15
16
17
18
19
20 public class WatchedFolderServiceImpl implements WatchedFolderService {
21
22
23 private WatcherTaskService watcherTaskService;
24
25
26 private WatchedFolderDAO watchedFolderDAO;
27
28
29 private FileSystemDAO fileSystemDAO;
30
31
32
33
34 public List<WebWatchedFolder> getAllWatchedFolders() {
35 List<WatchedFolder> watchedFolders = watchedFolderDAO.getAllWatchedFolders();
36 List<WebWatchedFolder> webWatchedFolders = new LinkedList<WebWatchedFolder>();
37
38 for(WatchedFolder watchedFolder : watchedFolders) {
39 webWatchedFolders.add(WatchedFolderAdapter.adaptWatchedFolderShallow(watchedFolder));
40 }
41
42 return webWatchedFolders;
43 }
44
45
46
47
48 public WebWatchedFolder createWatchedFolderByPath(String folderPath) {
49
50
51 WatchedFolder watchedFolder = new WatchedFolder();
52 watchedFolder.setFolderPath(folderPath);
53
54
55 watchedFolderDAO.saveWatchedFolder(watchedFolder);
56
57
58 watcherTaskService.addScanFolderTask(watchedFolder.getId());
59
60 return WatchedFolderAdapter.adaptWatchedFolderShallow(watchedFolder);
61 }
62
63
64
65
66
67
68 public void setWatchedFolderDAO(WatchedFolderDAO watchedFolderDAO) {
69 this.watchedFolderDAO = watchedFolderDAO;
70 }
71
72
73
74
75 @Override
76 public WebWatchedFolder getWatchedFolderById(Long folderId) {
77 return WatchedFolderAdapter.adaptWatchedFolderShallow(watchedFolderDAO.getWatchedFolderById(folderId));
78 }
79
80
81
82
83 @Override
84 public void refreshWatchedFilesInWatchedFolder(Long folderId) {
85 WatchedFolder watchedFolder = watchedFolderDAO.getWatchedFolderById(folderId);
86
87 List<String> filePathList = fileSystemDAO.getFilesInPath(watchedFolder.getFolderPath());
88 List<WatchedFile> watchedFiles = watchedFolderDAO.addWatchedFilesToFolder(watchedFolder, filePathList);
89
90 for (WatchedFile file : watchedFiles) {
91 watcherTaskService.addCheckFileTask(file.getId());
92 }
93 }
94
95
96
97
98
99
100 public void setFileSystemDAO(FileSystemDAO fileSystemDAO) {
101 this.fileSystemDAO = fileSystemDAO;
102 }
103
104
105
106
107 @Override
108 public void deleteWatchedFolder(Long folderId) {
109 watchedFolderDAO.deleteWatchedFolder(folderId);
110 }
111
112
113
114
115
116
117 public void setWatcherTaskService(WatcherTaskService watcherTaskService) {
118 this.watcherTaskService = watcherTaskService;
119 }
120 }