View Javadoc

1   /**
2    * Copyright 2011-2012 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.mobility.library.controllers;
17  
18  import java.text.DateFormat;
19  import java.text.SimpleDateFormat;
20  import java.util.ArrayList;
21  import java.util.Date;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.servlet.http.HttpServletRequest;
26  
27  import org.apache.log4j.Logger;
28  import org.kuali.mobility.library.entity.Library;
29  import org.kuali.mobility.library.entity.LibraryContactDetail;
30  import org.kuali.mobility.library.entity.LibraryHour;
31  import org.kuali.mobility.library.entity.LibraryHourSet;
32  import org.kuali.mobility.library.entity.LibraryPermissions;
33  import org.kuali.mobility.library.service.LibraryService;
34  import org.kuali.mobility.security.user.api.User;
35  import org.kuali.mobility.shared.Constants;
36  import org.springframework.beans.factory.annotation.Autowired;
37  import org.springframework.beans.factory.annotation.Qualifier;
38  import org.springframework.stereotype.Controller;
39  import org.springframework.ui.Model;
40  import org.springframework.web.bind.annotation.PathVariable;
41  import org.springframework.web.bind.annotation.RequestMapping;
42  import org.springframework.web.bind.annotation.RequestMethod;
43  import org.springframework.web.bind.annotation.RequestParam;
44  
45  /**
46   * Controller for Library
47   * @author Kuali Mobility Team (mobility.collab@kuali.org)
48   * @since 2.3.0
49   */
50  @Controller
51  @RequestMapping("/library")
52  public class LibraryControllerImpl {
53  
54  	/** 
55  	 * A reference to a Logger 
56  	 */
57  	private static final Logger LOG = Logger.getLogger( LibraryControllerImpl.class );
58  
59  	/**
60  	 * A reference to the <code>service</code>
61  	 */
62  	@Autowired
63  	@Qualifier("libraryService")
64  	private LibraryService service;
65  
66  
67  	/**
68  	 * Sets the reference to the <code>service</code>
69  	 */
70  	public void setService(LibraryService service) {
71  		this.service = service;
72  	}
73  
74  	
75  	/**
76  	 * The main menu for the library tool
77  	 */
78  	@RequestMapping(method = RequestMethod.GET)
79  	public String index() {
80  		return "library/index";
81  	}
82  
83  	/**
84  	 *	Used to retrieve libraries and then displays the menu selection for the Library Contact Details selection screen 
85  	 */
86  	@RequestMapping(value = "/viewContact", method = RequestMethod.GET)
87  	public String viewContact(
88  			HttpServletRequest request,
89  			Model uiModel) {
90  		
91  		Map<String, List<Library>> libraryMap = this.service.getLibrariesByCampus();
92  		List<String> campusCodes = this.service.getCampusWithLibraries();
93  		
94  		uiModel.addAttribute("campusCodes" , campusCodes);
95  		uiModel.addAttribute("libraryMap" , libraryMap);
96  		uiModel.addAttribute("actionPage", "viewContact");
97  		return "library/selectLibrary";
98  	}
99  
100 	
101 	/**
102 	 *	Display the Library contact details for the selected library 
103 	 */
104 	@RequestMapping(value = "/viewContact/{id}", method = RequestMethod.GET)
105 	public String viewContact(
106 			HttpServletRequest request,
107 			@PathVariable(value="id") int libraryId,
108 			Model uiModel) {
109 		
110 		User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
111 		boolean isAdmin = LibraryPermissions.ADMIN_EXPRESSION.evaluate(user);
112 		
113 		// Get lib id from get request. Retrieve Library record from DB.
114 		Library library = service.getLibrary(libraryId);
115 		LibraryContactDetail lcd = library.getLibraryContactDetail();
116 
117 		uiModel.addAttribute("libraryId", libraryId);
118 		uiModel.addAttribute("library", library);
119 		uiModel.addAttribute("libraryContactDetail", lcd);
120 		uiModel.addAttribute("isAdmin", isAdmin);
121 		return "library/viewContact";
122 	}
123 	
124 	/**
125 	 * Admin page to allow users to edit contact details of a specific library
126 	 */
127 	@RequestMapping(value = "/editContact/{id}", method = RequestMethod.GET)
128 	public String editContact(
129 			HttpServletRequest request,
130 			@PathVariable(value = "id") int libraryId,
131 			Model uiModel){
132 		
133 		User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
134 		
135 		// If the user is not admin take him away from here
136 		if (!LibraryPermissions.ADMIN_EXPRESSION.evaluate(user)){
137 			return "redirect:/library/viewContact/"+libraryId;
138 		}
139 		
140 		Library library = service.getLibrary(libraryId);
141 		uiModel.addAttribute("contactDetail", library.getLibraryContactDetail());
142 		uiModel.addAttribute("libraryId", libraryId);
143 		return "library/editContact";
144 	}
145 	
146 	
147 	/**
148 	 * Invoked when the user submits the form to edit the contact details of a library
149 	 */
150 	@RequestMapping(value = "/editContact/{id}", method = RequestMethod.POST)
151 	public String editContact(
152 			HttpServletRequest request,
153 			@PathVariable(value="id") long libraryId,
154 			@RequestParam(value="telephone") String telephone,
155 			@RequestParam(value="fax") String fax,
156 			@RequestParam(value="generalInfoDesk") String generalInfoDesk,
157 			@RequestParam(value="email") String email,
158 			@RequestParam(value="postalAddress") String postalAddress,
159 			@RequestParam(value="physicalAddress") String physicalAddress,
160 			@RequestParam(value="latitude") String latitude,
161 			@RequestParam(value="longitude") String longitude,
162 			Model uiModel){
163 		
164 		User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
165 
166 		// If the user is not admin take him away from here
167 		if (!LibraryPermissions.ADMIN_EXPRESSION.evaluate(user)){
168 			return "redirect:/library/viewContact/"+libraryId;
169 		}
170 		
171 		Library library = service.getLibrary(libraryId);
172 		LibraryContactDetail lcd = library.getLibraryContactDetail();
173 		if (lcd == null){
174 			lcd = new LibraryContactDetail();
175 		}
176 		
177 		lcd.setEmail(email);
178 		lcd.setFax(fax);
179 		lcd.setGeneralInfoDesk(generalInfoDesk);
180 		lcd.setLatitude(latitude);
181 		lcd.setLongitude(longitude);
182 		lcd.setPhysicalAddress(fixNewLines(physicalAddress));
183 		lcd.setPostalAddress(fixNewLines(postalAddress));
184 		lcd.setTelephone(telephone);
185 		
186 		library = service.saveLibrary(library);
187 		
188 		uiModel.addAttribute("library", library);
189 		return "redirect:/library/viewContact/"+libraryId;
190 	}
191 	
192 	
193 	/**
194 	 *	Used to retrieve libraries and then displays the menu selection for the Library operating hours selection screen 
195 	 */
196 	@RequestMapping(value = "/viewHours", method = RequestMethod.GET)
197 	public String viewHours(
198 			HttpServletRequest request,
199 			Model uiModel) {
200 		
201 		Map<String, List<Library>> libraryMap = this.service.getLibrariesByCampus();
202 		List<String> campusCodes = this.service.getCampusWithLibraries();
203 		// TODO get the campus objects for these campuses to get the campus names
204 		
205 		uiModel.addAttribute("campusCodes" , campusCodes);
206 		uiModel.addAttribute("libraryMap" , libraryMap );
207 		uiModel.addAttribute("actionPage", "viewHours");
208 		return "library/selectLibrary";
209 	}
210 	
211 	
212 	/**
213 	 * Allows the user to view the hours for a library
214 	 */
215 	@RequestMapping(value = "/viewHours/{id}", method = RequestMethod.GET)
216 	public String viewHours(
217 			HttpServletRequest request,
218 			@PathVariable(value = "id") int libraryId,
219 			Model uiModel) {
220 		
221 		
222 		User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
223 		boolean isAdmin = LibraryPermissions.ADMIN_EXPRESSION.evaluate(user);
224 		
225 		Library library = service.getLibrary(libraryId);
226 		List<LibraryHourSet> hourSets = service.getLibraryHourSets(libraryId);
227 		List<LibraryHourSet> displayHourSets = new ArrayList<LibraryHourSet>(hourSets.size());
228 		for (LibraryHourSet lhs : hourSets){
229 			displayHourSets.add(service.getDisplayableHoursSet(lhs));
230 		}
231 		// Based on Library record get Library Contact Details
232 		LibraryContactDetail lcd = library.getLibraryContactDetail();
233 		
234 		uiModel.addAttribute("hourSets", displayHourSets);
235 		uiModel.addAttribute("library", library);
236 		uiModel.addAttribute("libraryContactDetails", lcd);
237 		uiModel.addAttribute("isAdmin", isAdmin);
238 		return "library/viewHours";
239 	}
240 	
241 	
242 	/**
243 	 * Admin page to edit the hours
244 	 */
245 	@RequestMapping(value="/editHours/{id}" , method = RequestMethod.GET)
246 	public String editHours(
247 			HttpServletRequest request,
248 			@PathVariable(value="id") long libraryId,
249 			Model uiModel){
250 		
251 		User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
252 		
253 		// If the user is not admin take him away from here
254 		if (!LibraryPermissions.ADMIN_EXPRESSION.evaluate(user)){
255 			return "redirect:/library/viewHours/"+libraryId;
256 		}
257 		
258 		// Get lib id from get request. Retrieve Library record from DB.
259 		Library library = service.getLibrary(libraryId);
260 		List<LibraryHourSet> hourSets = service.getLibraryHourSets(libraryId);
261 		
262 		uiModel.addAttribute("library", library);
263 		uiModel.addAttribute("libraryId", libraryId);
264 		uiModel.addAttribute("hourSets", hourSets);
265 		
266 		return "library/editHours";
267 		
268 	}
269 	
270 	/**
271 	 * Invoked when the user saves edited hours.
272 	 */
273 	@RequestMapping(value="/editHours/{id}" , method = RequestMethod.POST)
274 	public String editHours(
275 			HttpServletRequest request,
276 			@PathVariable(value="id") int libraryId){
277 		
278 		User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
279 		
280 		// If the user is not admin take him away from here
281 		if (!LibraryPermissions.ADMIN_EXPRESSION.evaluate(user)){
282 			return "redirect:/library/viewHours/"+libraryId;
283 		}
284 		
285 		List<LibraryHourSet> hourSets = service.getLibraryHourSets(libraryId);
286 		LibraryHourSet hourSet;
287 		List<LibraryHour> hours;
288 		LibraryHour hour;
289 
290 		for (int hsIdx = 0 ; hsIdx < hourSets.size() ; hsIdx++){
291 			hourSet = hourSets.get(hsIdx);
292 			hours = hourSet.getHours();
293 			
294 			for(int hIdx = 0 ; hIdx < hours.size() ; hIdx++){
295 				hour = hours.get(hIdx);
296 				int hourSetIndex = hsIdx + 1;
297 				int hourIndex = hIdx + 1;
298 				if( request.getParameter("s"+ hourSetIndex +"h"+ hourIndex + "-closed") == null){
299 					hour.setFromTime(this.stringToDate("HH:mm:ss" , request.getParameter("s"+ hourSetIndex +"h"+ hourIndex +"-fromTime")));
300 					hour.setToTime(this.stringToDate("HH:mm:ss" , request.getParameter("s"+ hourSetIndex +"h"+ hourIndex +"-toTime")));
301 				}else{
302 					hour.setFromTime(null);
303 					hour.setToTime(null);
304 				}
305 				hours.set(hIdx , hour);
306 			}
307 			hourSet.setHours(hours);
308 			service.saveLibraryHourSets(hourSet);
309 		}
310 		
311 		return "redirect:/library/viewHours/"+libraryId;
312 	}
313 	
314 
315 	/**
316 	 * Replace new lines with HTML friendly versions
317 	 * @param string
318 	 * @return
319 	 */
320 	private static final String fixNewLines(String string){
321 		string = string.replace("\r\n", "\n");
322 		string = string.replace("\r", "\n");
323 		string = string.replace("\n", "<br/>");
324 		return string;
325 	}
326 	
327 	private Date stringToDate(String format, String time ){
328 		Date date = null;
329 		DateFormat formatter = new SimpleDateFormat(format);
330 		try{
331 			date = formatter.parse(time+":00");
332 		}catch(Exception e){
333 			LOG.error(">>>> stringToDate : "+e.getMessage());
334 		}
335 		return date;
336 	}
337 }