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 org.apache.log4j.Logger;
19  import org.kuali.mobility.library.entity.*;
20  import org.kuali.mobility.library.service.LibraryService;
21  import org.kuali.mobility.security.user.api.User;
22  import org.kuali.mobility.shared.Constants;
23  import org.springframework.beans.factory.annotation.Autowired;
24  import org.springframework.beans.factory.annotation.Qualifier;
25  import org.springframework.stereotype.Controller;
26  import org.springframework.ui.Model;
27  import org.springframework.web.bind.annotation.PathVariable;
28  import org.springframework.web.bind.annotation.RequestMapping;
29  import org.springframework.web.bind.annotation.RequestMethod;
30  import org.springframework.web.bind.annotation.RequestParam;
31  
32  import javax.annotation.Resource;
33  import javax.servlet.http.HttpServletRequest;
34  import java.text.DateFormat;
35  import java.text.SimpleDateFormat;
36  import java.util.*;
37  
38  /**
39   * Controller for Library
40   * @author Kuali Mobility Team (mobility.collab@kuali.org)
41   * @since 2.3.0
42   */
43  @Controller
44  @RequestMapping("/library")
45  public class LibraryControllerImpl {
46  
47  	/** 
48  	 * A reference to a Logger 
49  	 */
50  	private static final Logger LOG = Logger.getLogger( LibraryControllerImpl.class );
51  
52  	/**
53  	 * A reference to the <code>service</code>
54  	 */
55  	@Autowired
56  	@Qualifier("libraryService")
57  	private LibraryService service;
58  
59  	/**
60  	 * A reference to the KME properties
61  	 */
62  	@Resource(name="kmeProperties")
63  	private Properties kmeProperties;
64  
65  	/**
66  	 * Sets the reference to the <code>service</code>
67  	 */
68  	public void setService(LibraryService service) {
69  		this.service = service;
70  	}
71  
72  	
73  	/**
74  	 * The main menu for the library tool
75  	 */
76  	@RequestMapping(method = RequestMethod.GET)
77  	public String index() {
78  		if("3".equalsIgnoreCase(getKmeProperties().getProperty("kme.uiVersion","classic"))) {
79  			return "ui3/library/index";
80  		}
81  		else{
82  			return "library/index";
83  		}
84  	}
85  
86  
87      /**
88       * The main menu for the library tool
89       */
90      @RequestMapping(value="home", method = RequestMethod.GET)
91      public String ui3Home() {
92              return "ui3/library/partials/home";
93      }
94  
95  	/**
96  	 *	Used to retrieve libraries and then displays the menu selection for the Library Contact Details selection screen 
97  	 */
98  	@RequestMapping(value = "/viewContact", method = RequestMethod.GET)
99  	public String viewContact(Model uiModel) {
100 		
101 		Map<String, List<Library>> libraryMap = this.service.getLibrariesByCampus();
102 		List<String> campusCodes = this.service.getCampusWithLibraries();
103 		
104 		uiModel.addAttribute("campusCodes" , campusCodes);
105 		uiModel.addAttribute("libraryMap" , libraryMap);
106 		uiModel.addAttribute("actionPage", "viewContact");
107 
108 		if("3".equalsIgnoreCase(getKmeProperties().getProperty("kme.uiVersion","classic"))) {
109 			return "ui3/library/partials/selectLibrary";
110 		}
111 		else{
112 			return "library/selectLibrary";
113 		}
114 	}
115 
116 	
117 	/**
118 	 *	Display the Library contact details for the selected library 
119 	 */
120 	@RequestMapping(value = "/viewContact/{id}", method = RequestMethod.GET)
121 	public String viewContact(
122 			HttpServletRequest request,
123 			@PathVariable(value="id") int libraryId,
124 			Model uiModel) {
125 		
126 		User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
127 		boolean isAdmin = LibraryPermissions.ADMIN_EXPRESSION.evaluate(user);
128 		
129 		// Get lib id from get request. Retrieve Library record from DB.
130 		Library library = service.getLibrary(libraryId);
131 		LibraryContactDetail lcd = library.getLibraryContactDetail();
132 
133 		uiModel.addAttribute("libraryId", libraryId);
134 		uiModel.addAttribute("library", library);
135 		uiModel.addAttribute("libraryContactDetail", lcd);
136 		uiModel.addAttribute("isAdmin", isAdmin);
137 
138 		if("3".equalsIgnoreCase(getKmeProperties().getProperty("kme.uiVersion","classic"))) {
139 			return "ui3/library/partials/viewContact";
140 		}
141 		else{
142 			return "library/viewContact";
143 		}
144 	}
145 	
146 	/**
147 	 * Admin page to allow users to edit contact details of a specific library
148 	 */
149 	@RequestMapping(value = "/editContact/{id}", method = RequestMethod.GET)
150 	public String editContact(
151 			HttpServletRequest request,
152 			@PathVariable(value = "id") int libraryId,
153 			Model uiModel){
154 		
155 		User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
156 		
157 		// If the user is not admin take him away from here
158 		if (!LibraryPermissions.ADMIN_EXPRESSION.evaluate(user)){
159 			return "redirect:/library/viewContact/"+libraryId;
160 		}
161 		
162 		Library library = service.getLibrary(libraryId);
163 		uiModel.addAttribute("contactDetail", library.getLibraryContactDetail());
164 		uiModel.addAttribute("libraryId", libraryId);
165         uiModel.addAttribute("library", library);
166 
167 		if("3".equalsIgnoreCase(getKmeProperties().getProperty("kme.uiVersion","classic"))) {
168 			return "ui3/library/partials/editContact";
169 		}
170 		else{
171 			return "library/editContact";
172 		}
173 	}
174 	
175 	
176 	/**
177 	 * Invoked when the user submits the form to edit the contact details of a library
178 	 */
179 	@RequestMapping(value = "/editContact/{id}", method = RequestMethod.POST)
180 	public String editContact(
181 			HttpServletRequest request,
182 			@PathVariable(value="id") long libraryId,
183 			@RequestParam(value="telephone") String telephone,
184 			@RequestParam(value="fax") String fax,
185 			@RequestParam(value="generalInfoDesk") String generalInfoDesk,
186 			@RequestParam(value="email") String email,
187 			@RequestParam(value="postalAddress") String postalAddress,
188 			@RequestParam(value="physicalAddress") String physicalAddress,
189 			@RequestParam(value="latitude") String latitude,
190 			@RequestParam(value="longitude") String longitude,
191 			Model uiModel){
192 		
193 		User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
194 
195 		// If the user is not admin take him away from here
196 		if (!LibraryPermissions.ADMIN_EXPRESSION.evaluate(user)){
197 			return "redirect:/library/viewContact/"+libraryId;
198 		}
199 		
200 		Library library = service.getLibrary(libraryId);
201 		LibraryContactDetail lcd = library.getLibraryContactDetail();
202 		if (lcd == null){
203 			lcd = new LibraryContactDetail();
204 		}
205 		
206 		lcd.setEmail(email);
207 		lcd.setFax(fax);
208 		lcd.setGeneralInfoDesk(generalInfoDesk);
209 		lcd.setLatitude(latitude);
210 		lcd.setLongitude(longitude);
211 		lcd.setPhysicalAddress(fixNewLines(physicalAddress));
212 		lcd.setPostalAddress(fixNewLines(postalAddress));
213 		lcd.setTelephone(telephone);
214 		
215 		library = service.saveLibrary(library);
216 		
217 		uiModel.addAttribute("library", library);
218 		return "redirect:/library/viewContact/"+libraryId;
219 	}
220 	
221 	
222 	/**
223 	 *	Used to retrieve libraries and then displays the menu selection for the Library operating hours selection screen 
224 	 */
225 	@RequestMapping(value = "/viewHours", method = RequestMethod.GET)
226 	public String viewHours(
227 			HttpServletRequest request,
228 			Model uiModel) {
229 		
230 		Map<String, List<Library>> libraryMap = this.service.getLibrariesByCampus();
231 		List<String> campusCodes = this.service.getCampusWithLibraries();
232 		// TODO get the campus objects for these campuses to get the campus names
233 		
234 		uiModel.addAttribute("campusCodes" , campusCodes);
235 		uiModel.addAttribute("libraryMap" , libraryMap );
236 		uiModel.addAttribute("actionPage", "viewHours");
237 
238 
239 		if("3".equalsIgnoreCase(getKmeProperties().getProperty("kme.uiVersion","classic"))) {
240 			return "ui3/library/partials/selectLibrary";
241 		}
242 		else{
243 			return "library/selectLibrary";
244 		}
245 
246 	}
247 	
248 	
249 	/**
250 	 * Allows the user to view the hours for a library
251 	 */
252 	@RequestMapping(value = "/viewHours/{id}", method = RequestMethod.GET)
253 	public String viewHours(
254 			HttpServletRequest request,
255 			@PathVariable(value = "id") int libraryId,
256 			Model uiModel) {
257 		
258 		
259 		User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
260 		boolean isAdmin = LibraryPermissions.ADMIN_EXPRESSION.evaluate(user);
261 		
262 		Library library = service.getLibrary(libraryId);
263 		List<LibraryHourSet> hourSets = service.getLibraryHourSets(libraryId);
264 		List<LibraryHourSet> displayHourSets = new ArrayList<LibraryHourSet>(hourSets.size());
265 		for (LibraryHourSet lhs : hourSets){
266 			displayHourSets.add(service.getDisplayableHoursSet(lhs));
267 		}
268 		// Based on Library record get Library Contact Details
269 		LibraryContactDetail lcd = library.getLibraryContactDetail();
270 		
271 		uiModel.addAttribute("hourSets", displayHourSets);
272 		uiModel.addAttribute("library", library);
273 		uiModel.addAttribute("libraryContactDetails", lcd);
274 		uiModel.addAttribute("isAdmin", isAdmin);
275 
276 
277 		if("3".equalsIgnoreCase(getKmeProperties().getProperty("kme.uiVersion","classic"))) {
278 			return "ui3/library/partials/viewHours";
279 		}
280 		else{
281 			return "library/viewHours";
282 		}
283 	}
284 	
285 	
286 	/**
287 	 * Admin page to edit the hours
288 	 */
289 	@RequestMapping(value="/editHours/{id}" , method = RequestMethod.GET)
290 	public String editHours(
291 			HttpServletRequest request,
292 			@PathVariable(value="id") long libraryId,
293 			Model uiModel){
294 		
295 		User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
296 		
297 		// If the user is not admin take him away from here
298 		if (!LibraryPermissions.ADMIN_EXPRESSION.evaluate(user)){
299 			return "redirect:/library/viewHours/"+libraryId;
300 		}
301 		
302 		// Get lib id from get request. Retrieve Library record from DB.
303 		Library library = service.getLibrary(libraryId);
304 		List<LibraryHourSet> hourSets = service.getLibraryHourSets(libraryId);
305 		
306 		uiModel.addAttribute("library", library);
307 		uiModel.addAttribute("libraryId", libraryId);
308 		uiModel.addAttribute("hourSets", hourSets);
309 
310 
311         if("3".equalsIgnoreCase(getKmeProperties().getProperty("kme.uiVersion","classic"))) {
312             return "ui3/library/partials/editHours";
313         }
314         else{
315             return "library/editHours";
316         }
317 
318 		
319 	}
320 	
321 	/**
322 	 * Invoked when the user saves edited hours.
323 	 */
324 	@RequestMapping(value="/editHours/{id}" , method = RequestMethod.POST)
325 	public String editHours(
326 			HttpServletRequest request,
327 			@PathVariable(value="id") int libraryId){
328 		
329 		User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
330 		
331 		// If the user is not admin take him away from here
332 		if (!LibraryPermissions.ADMIN_EXPRESSION.evaluate(user)){
333 			return "redirect:/library/viewHours/"+libraryId;
334 		}
335 		
336 		List<LibraryHourSet> hourSets = service.getLibraryHourSets(libraryId);
337 		LibraryHourSet hourSet;
338 		List<LibraryHour> hours;
339 		LibraryHour hour;
340 
341 		for (int hsIdx = 0 ; hsIdx < hourSets.size() ; hsIdx++){
342 			hourSet = hourSets.get(hsIdx);
343 			hours = hourSet.getHours();
344 			
345 			for(int hIdx = 0 ; hIdx < hours.size() ; hIdx++){
346 				hour = hours.get(hIdx);
347 				int hourSetIndex = hsIdx + 1;
348 				int hourIndex = hIdx + 1;
349 				if( request.getParameter("s"+ hourSetIndex +"h"+ hourIndex + "closed") == null){
350 					hour.setFromTime(this.stringToDate("HH:mm:ss" , request.getParameter("s"+ hourSetIndex +"h"+ hourIndex +"fromTime")));
351 					hour.setToTime(this.stringToDate("HH:mm:ss" , request.getParameter("s"+ hourSetIndex +"h"+ hourIndex +"toTime")));
352 				}else{
353 					hour.setFromTime(null);
354 					hour.setToTime(null);
355 				}
356 				hours.set(hIdx , hour);
357 			}
358 			hourSet.setHours(hours);
359 			service.saveLibraryHourSets(hourSet);
360 		}
361 
362         // Call the view hours controller
363         return "redirect:/library/viewHours/"+libraryId;
364 	}
365 	
366 
367 	/**
368 	 * Replace new lines with HTML friendly versions
369 	 * @param string
370 	 * @return
371 	 */
372 	private static final String fixNewLines(String string){
373 		string = string.replace("\r\n", "\n");
374 		string = string.replace("\r", "\n");
375 		string = string.replace("\n", "<br/>");
376 		return string;
377 	}
378 	
379 	private Date stringToDate(String format, String time ){
380 		Date date = null;
381 		DateFormat formatter = new SimpleDateFormat(format);
382 		try{
383 			date = formatter.parse(time+":00");
384 		}catch(Exception e){
385 			LOG.error(">>>> stringToDate : "+e.getMessage());
386 		}
387 		return date;
388 	}
389 
390 	public Properties getKmeProperties() {
391 		return kmeProperties;
392 	}
393 
394 	public void setKmeProperties(Properties kmeProperties) {
395 		this.kmeProperties = kmeProperties;
396 	}
397 }