View Javadoc

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   * Implementation of the watched folder service
17   * 
18   * @author julien@busyboxes.ch
19   */
20  public class WatchedFolderServiceImpl implements WatchedFolderService {
21  	
22  	/** The service to create new tasks */
23  	private WatcherTaskService watcherTaskService;
24  	
25  	/** The DAO used to access data */
26  	private WatchedFolderDAO watchedFolderDAO;
27  	
28  	/** The DAO to access filesystem */
29  	private FileSystemDAO fileSystemDAO;
30  
31  	/**
32  	 * @see WatchedFolderService#getAllWatchedFolders()
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  	 * @see WatchedFolderService#createWatchedFolderByPath(String)
47  	 */
48  	public WebWatchedFolder createWatchedFolderByPath(String folderPath) {
49  
50  		// Create and initializes the Watched Folder
51  		WatchedFolder watchedFolder = new WatchedFolder();
52  		watchedFolder.setFolderPath(folderPath);
53  
54  		// Save the new watched folder
55  		watchedFolderDAO.saveWatchedFolder(watchedFolder);
56  		
57  		// Creating a new task
58  		watcherTaskService.addScanFolderTask(watchedFolder.getId());
59  		
60  		return WatchedFolderAdapter.adaptWatchedFolderShallow(watchedFolder);
61  	}
62  	
63  	/**
64  	 * The watched folders DAO used in this service
65  	 * 
66  	 * @param watchedFolderDAO the watched files DAO
67  	 */
68  	public void setWatchedFolderDAO(WatchedFolderDAO watchedFolderDAO) {
69  		this.watchedFolderDAO = watchedFolderDAO;
70  	}
71  
72  	/**
73  	 * @see WatchedFolderService#getWatchedFolderById(Long)
74  	 */
75  	@Override
76  	public WebWatchedFolder getWatchedFolderById(Long folderId) {
77  		return WatchedFolderAdapter.adaptWatchedFolderShallow(watchedFolderDAO.getWatchedFolderById(folderId));
78  	}
79  
80  	/**
81  	 * @see WatchedFolderService#refreshWatchedFilesInWatchedFolder(Long)
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  	 * The file system DAO used in this service
97  	 * 
98  	 * @param fileSystemDAO the fileSystemDAO to set
99  	 */
100 	public void setFileSystemDAO(FileSystemDAO fileSystemDAO) {
101 		this.fileSystemDAO = fileSystemDAO;
102 	}
103 
104 	/**
105 	 * @see WatchedFolderService#deleteWatchedFolder(Long)
106 	 */
107 	@Override
108 	public void deleteWatchedFolder(Long folderId) {
109 		watchedFolderDAO.deleteWatchedFolder(folderId);
110 	}
111 
112 	/**
113 	 * The service used to create new task
114 	 * 
115 	 * @param taskTaskService the taskTaskService to set
116 	 */
117 	public void setWatcherTaskService(WatcherTaskService watcherTaskService) {
118 		this.watcherTaskService = watcherTaskService;
119 	}
120 }