View Javadoc

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   * This class represents the controller of the folder page
24   * 
25   * @author julien@busyboxes.ch
26   */
27  @Controller
28  public class FolderController {
29  	
30  	/** Logger for this class */
31  	private Logger logger = Logger.getLogger(FolderController.class);
32  	
33  	/** The watched folder service */
34  	private WatchedFolderService watchedFolderService;
35  	
36  	/** The watched file service */
37  	private WatchedFileService watchedFileService;
38  	
39  	/** Number of files per page */
40  	private static final int FILES_PER_PAGE = 20;
41  	
42  	/**
43  	 * This method responds to a GET request on the folders index page
44  	 * 
45  	 * @param model the model for the page
46  	 */
47  	@RequestMapping(value = "/folder", method = RequestMethod.GET)
48  	public void folderHomepage(ModelMap model) {
49  		
50  		// Fetches all watched folders
51  		List<WebWatchedFolder> watchedFolders = watchedFolderService.getAllWatchedFolders();
52  
53  		model.addAttribute("watchedFolders", watchedFolders);
54  		model.addAttribute("watchedFoldersCount", watchedFolders.size());
55  	}
56  	
57  	/**
58  	 * This method responds to a GET request on the folder addition page
59  	 * 
60  	 * @param model the model for the page
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  	 * This method responds to a GET request on the folder addition page
70  	 * 
71  	 * @param folderAdditionForm the form for adding a folder
72  	 * @param result binding result
73  	 * @param model the model for the page
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  				// Providing an empty form to the model
96  				model.addAttribute("folderAdditionForm", new FolderAdditionForm());
97  				model.addAttribute("creationSuccess", true);
98  				model.addAttribute("folderCreated", folderAdditionForm.getFolderPath());
99  			}
100 		}
101 	}
102 
103 	/**
104 	 * This method responds to a request on the folder details page
105 	 * 
106 	 * @param folderId the id of the folder
107 	 * @return model the model for the page
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 	 * This method refresh the watched files in the given folder
135 	 * 
136 	 * @param folderId the id of the folder
137 	 * @return the new view
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 	 * This method deletes the watched folder
151 	 * 
152 	 * @param folderId the id of the folder
153 	 * @return the new view
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 	 * Sets the watched folder service
168 	 * 
169 	 * @param watchedFolderService the watched file service
170 	 */
171 	@Autowired
172 	public void setWatchedFolderService(WatchedFolderService watchedFolderService) {
173 		this.watchedFolderService = watchedFolderService;
174 	}
175 	
176 	/**
177 	 * Sets the watched file service
178 	 * 
179 	 * @param watchedFileService the watched file service
180 	 */
181 	@Autowired
182 	public void setWatchedFileService(WatchedFileService watchedFileService) {
183 		this.watchedFileService = watchedFileService;
184 	}
185 }