1 package ch.busyboxes.agoo.controller;
2
3 import java.util.List;
4
5 import org.apache.log4j.Logger;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.stereotype.Controller;
8 import org.springframework.ui.ModelMap;
9 import org.springframework.validation.BindingResult;
10 import org.springframework.web.bind.annotation.ModelAttribute;
11 import org.springframework.web.bind.annotation.RequestMapping;
12 import org.springframework.web.bind.annotation.RequestMethod;
13 import org.springframework.web.bind.annotation.RequestParam;
14
15 import ch.busyboxes.agoo.controller.form.FolderAdditionForm;
16 import ch.busyboxes.agoo.controller.model.WebWatchedFile;
17 import ch.busyboxes.agoo.controller.model.WebWatchedFolder;
18 import ch.busyboxes.agoo.service.WatchedFileService;
19 import ch.busyboxes.agoo.service.WatchedFolderService;
20 import ch.busyboxes.agoo.validator.FolderAdditionValidator;
21
22
23
24
25
26
27 @Controller
28 public class FolderController {
29
30
31 private Logger logger = Logger.getLogger(FolderController.class);
32
33
34 private WatchedFolderService watchedFolderService;
35
36
37 private WatchedFileService watchedFileService;
38
39
40 private static final int FILES_PER_PAGE = 20;
41
42
43
44
45
46
47 @RequestMapping(value = "/folder", method = RequestMethod.GET)
48 public void folderHomepage(ModelMap model) {
49
50
51 List<WebWatchedFolder> watchedFolders = watchedFolderService.getAllWatchedFolders();
52
53 model.addAttribute("watchedFolders", watchedFolders);
54 model.addAttribute("watchedFoldersCount", watchedFolders.size());
55 }
56
57
58
59
60
61
62 @RequestMapping(value = "/folder/add", method = RequestMethod.GET)
63 public void addFolder(ModelMap model) {
64 FolderAdditionForm folderAdditionForm = new FolderAdditionForm();
65 model.addAttribute(folderAdditionForm);
66 }
67
68
69
70
71
72
73
74
75 @RequestMapping(value = "/folder/add", method = RequestMethod.POST)
76 public void addFolder(@ModelAttribute(value = "folderAdditionForm") FolderAdditionForm folderAdditionForm, BindingResult result, ModelMap model) {
77 if (folderAdditionForm == null) {
78 folderAdditionForm = new FolderAdditionForm();
79 if (logger.isDebugEnabled()) {
80 logger.debug("folderAdditionForm is null, create a new form");
81 }
82 } else {
83 if (logger.isDebugEnabled()) {
84 logger.debug("folderAdditionFom is not null, path is: " + folderAdditionForm.getFolderPath());
85 }
86
87 new FolderAdditionValidator().validate(folderAdditionForm, result);
88
89 if (result.hasErrors()) {
90 model.addAttribute("folderAdditionForm", folderAdditionForm);
91 } else {
92
93 watchedFolderService.createWatchedFolderByPath(folderAdditionForm.getFolderPath());
94
95
96 model.addAttribute("folderAdditionForm", new FolderAdditionForm());
97 model.addAttribute("creationSuccess", true);
98 model.addAttribute("folderCreated", folderAdditionForm.getFolderPath());
99 }
100 }
101 }
102
103
104
105
106
107
108
109 @RequestMapping(value = "/folder/details", method = RequestMethod.GET)
110 public ModelMap folderDetails(@RequestParam(value = "folderId") Long folderId, @RequestParam(value = "page", required = false) Integer page) {
111 if (logger.isDebugEnabled()) {
112 logger.debug("Getting /folder/details, with folderId: " + Long.toString(folderId));
113 }
114
115 ModelMap model = new ModelMap();
116 WebWatchedFolder watchedFolder = watchedFolderService.getWatchedFolderById(folderId);
117
118 int pageNumber = 1;
119 if (page != null && page > 0) {
120 pageNumber = page;
121 }
122
123 List<WebWatchedFile> watchedFiles = watchedFileService.getWatchedFilesByFolderPaged(folderId, FILES_PER_PAGE, pageNumber);
124
125 model.addAttribute("watchedFolder", watchedFolder);
126 model.addAttribute("watchedFiles", watchedFiles);
127 model.addAttribute("pageNumber", pageNumber);
128 model.addAttribute("pageTotal", Math.round(Math.ceil((double)watchedFolder.getWatchedFilesCount() / (double)FILES_PER_PAGE)));
129
130 return model;
131 }
132
133
134
135
136
137
138
139 @RequestMapping(value = "/folder/refresh", method = RequestMethod.POST)
140 public String refreshFolder(@RequestParam(value = "folderId") Long folderId) {
141 if (logger.isDebugEnabled()) {
142 logger.debug("Posting /folder/refresh, with folderId: " + Long.toString(folderId));
143 }
144
145 watchedFolderService.refreshWatchedFilesInWatchedFolder(folderId);
146 return "redirect:/folder/details.html?folderId=" + Long.toString(folderId);
147 }
148
149
150
151
152
153
154
155 @RequestMapping(value = "/folder/delete", method = RequestMethod.POST)
156 public String deleteFolder(@RequestParam(value = "folderId") Long folderId) {
157 if (logger.isDebugEnabled()) {
158 logger.debug("Posting /folder/delete, with folderId: " + Long.toString(folderId));
159 }
160
161 watchedFolderService.deleteWatchedFolder(folderId);
162 return "redirect:/folder.html";
163 }
164
165
166
167
168
169
170
171 @Autowired
172 public void setWatchedFolderService(WatchedFolderService watchedFolderService) {
173 this.watchedFolderService = watchedFolderService;
174 }
175
176
177
178
179
180
181 @Autowired
182 public void setWatchedFileService(WatchedFileService watchedFileService) {
183 this.watchedFileService = watchedFileService;
184 }
185 }