View Javadoc

1   /**
2    * Copyright 2011-2013 The Kuali Foundation Licensed under the Educational
3    * Community License, Version 2.0 (the "License"); you may not use this file
4    * except in compliance with the License. You may obtain a copy of the License
5    * at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12   * License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  package org.kuali.mobility.academics.controllers;
16  
17  import org.apache.commons.collections.CollectionUtils;
18  import org.apache.log4j.Logger;
19  import org.kuali.mobility.academics.entity.*;
20  import org.kuali.mobility.academics.service.AcademicsService;
21  import org.kuali.mobility.academics.util.AcademicsConstants;
22  import org.kuali.mobility.academics.util.CareerPredicate;
23  import org.kuali.mobility.academics.util.CatalogNumberPredicate;
24  import org.kuali.mobility.academics.util.SubjectPredicate;
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.Cookie;
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpSession;
36  import java.util.*;
37  
38  @Controller
39  @RequestMapping("/academics")
40  public class AcademicsControllerImpl {
41  
42  	private static final Logger LOG = Logger.getLogger(AcademicsControllerImpl.class);
43  	@Resource(name="academicsService")
44  	private AcademicsService service;
45  
46  	@Resource(name="academicsProperties")
47  	private Properties academicsProperties;
48  
49      @Resource(name="kmeProperties")
50      private Properties kmeProperties;
51  
52  	@RequestMapping(method = RequestMethod.GET)
53  	public String index(HttpServletRequest request, Model uiModel) {
54  		HttpSession session = request.getSession();
55  		session.removeAttribute("currentTerm");
56  		session.removeAttribute("currentCareer");
57  		session.removeAttribute("currentSection");
58  		session.removeAttribute("currentCatalogNumber");
59  		session.removeAttribute("sectionsAll");
60  		session.removeAttribute(AcademicsConstants.TERM_ID);
61  		session.removeAttribute(AcademicsConstants.TERM);
62  		session.removeAttribute(AcademicsConstants.CAREER_ID);
63  		session.removeAttribute(AcademicsConstants.CAREER);
64  		session.removeAttribute(AcademicsConstants.SUBJECT_ID);
65  		session.removeAttribute(AcademicsConstants.SUBJECT);
66  		session.removeAttribute(AcademicsConstants.CATALOG_NUMBER);
67  		session.removeAttribute(AcademicsConstants.CATALOG_DESCRIPTION);
68  		session.removeAttribute(AcademicsConstants.CLASS_NUMBER);
69  		session.removeAttribute(AcademicsConstants.CLASS_SECTION);
70  		session.removeAttribute(AcademicsConstants.COURSE_ID);
71  		session.removeAttribute(AcademicsConstants.COURSE_OFFER_NBR);
72  		session.removeAttribute(AcademicsConstants.SECTIONS);
73  		session.removeAttribute(AcademicsConstants.TERM_DESCRIPTION);
74  
75  		uiModel.addAttribute( AcademicsConstants.HOME_SCREEN_TOOLS, this.getHomeScreenOrder() );
76  		
77          Cookie cks[] = request.getCookies();
78          if (cks != null) {
79              for (Cookie c : cks) {
80                  //LOG.info("---Cookies: " + c.getName() + " " + c.getValue());
81                  if (c.getName().equals("platform")) {
82                      uiModel.addAttribute("platform",c.getValue());
83                  } else if (session.getAttribute("session_platform") != null) {
84                      uiModel.addAttribute("platform",(String)session.getAttribute("session_platform"));
85                  } else {
86                      uiModel.addAttribute("platform","none");
87                  }
88              }
89          }
90          if( "3".equalsIgnoreCase( getKmeProperties().getProperty("kme.uiVersion","classic") ) ) {
91          	return "ui3/academics/index";
92          } else {
93          	return "academics/index";
94          }
95  	}
96  
97  	@RequestMapping(value = "/terms")
98  	public String getTerms(HttpServletRequest request, Model uiModel) {
99          String viewName = null;
100         if( "3".equalsIgnoreCase( getKmeProperties().getProperty("kme.uiVersion","classic") ) ) {
101             viewName = "ui3/academics/scheduleOfClasses";
102         } else {
103             HttpSession session = request.getSession();
104 
105             session.setAttribute(AcademicsConstants.TERM, getService().getTerms());
106 
107             //uiModel.addAttribute(AcademicsConstants.BACKBUTTON_URL, request.getContextPath() + "/academics/");
108             viewName = "academics/terms";
109         }
110         return viewName;
111 	}
112 
113 	@RequestMapping(value = "/careers")
114 	public String getCareers(HttpServletRequest request, Model uiModel,
115 			@RequestParam(required = true) final String termId) {
116 		HttpSession session = request.getSession();
117 		if (termId.equalsIgnoreCase((String) session.getAttribute(AcademicsConstants.TERM_ID))) {
118 			// use cached data
119 			uiModel.addAttribute(AcademicsConstants.TITLE, ((Term)session.getAttribute("currentTerm")).getDescription());
120 		} else {
121 			Term term = getService().getTerm(termId);
122 			session.setAttribute(AcademicsConstants.TERM_ID, termId);
123 			session.setAttribute("currentTerm", term);
124 			List<? extends Career> lCareers = getService().getCareers(termId);
125 			session.setAttribute(AcademicsConstants.CAREER, lCareers);
126 			session.setAttribute("watermark", "Search " + term.getDescription() + " Classes");
127 			uiModel.addAttribute(AcademicsConstants.TITLE, ((Term)session.getAttribute("currentTerm")).getDescription());
128 		}
129 
130 		//uiModel.addAttribute(AcademicsConstants.BACKBUTTON_URL, request.getContextPath() + "/academics/terms");
131 		return "academics/careers";
132 	}
133 
134 	@RequestMapping(value = "/subjects")
135 	public String getCareers(HttpServletRequest request, Model uiModel,
136 			@RequestParam(required = true) final String termId,
137 			@RequestParam(required = true) final String careerId) {
138 		HttpSession session = request.getSession();
139 		if (termId.equalsIgnoreCase((String) session.getAttribute(AcademicsConstants.TERM_ID))
140 				&& careerId.equalsIgnoreCase((String) session.getAttribute(AcademicsConstants.CAREER_ID))) {
141 			// Use cached data.
142 //			uiModel.addAttribute(AcademicsConstants.TITLE, ((Career)session.getAttribute("currentCareer")).getShortDescription());
143 			uiModel.addAttribute(AcademicsConstants.TITLE, "Subjects" );
144 		} else {
145 			Term term = getService().getTerm(termId);
146 			session.setAttribute(AcademicsConstants.TERM_ID, term.getId());
147 			session.setAttribute("currentTerm", term);
148 
149 			session.setAttribute(AcademicsConstants.CAREER_ID, careerId);
150 			Career career = (Career) CollectionUtils.find((List<Career>) session.getAttribute(AcademicsConstants.CAREER), new CareerPredicate(careerId));
151 			session.setAttribute("currentCareer", career);
152 
153 			List<? extends Subject> lSubjects = getService().getSubjects(termId, careerId);
154 			session.setAttribute(AcademicsConstants.SUBJECT, lSubjects);
155 //			uiModel.addAttribute(AcademicsConstants.TITLE, ((Career)session.getAttribute("currentCareer")).getShortDescription());
156 			uiModel.addAttribute(AcademicsConstants.TITLE, "Subjects" );
157 		}
158 		//uiModel.addAttribute(AcademicsConstants.BACKBUTTON_URL, request.getContextPath() + "/academics/careers?termId=" + session.getAttribute(AcademicsConstants.TERM_ID));
159 		return "academics/subjects";
160 	}
161 
162 	@RequestMapping(value = "/catalogNumbers")
163 	public String getCareers(HttpServletRequest request, Model uiModel,
164 			@RequestParam(required = true)
165 			final String termId,
166 			@RequestParam(required = true)
167 			final String careerId,
168 			@RequestParam(required = true)
169 			final String subjectId) {
170 		HttpSession session = request.getSession();
171 		if (termId.equalsIgnoreCase((String) session.getAttribute(AcademicsConstants.TERM_ID))
172 				&& careerId.equalsIgnoreCase((String) session.getAttribute(AcademicsConstants.CAREER_ID))
173 				&& subjectId.equalsIgnoreCase((String) session.getAttribute(AcademicsConstants.SUBJECT_ID))) {
174 			// Use cached data.
175 			uiModel.addAttribute(AcademicsConstants.TITLE, ((Subject)session.getAttribute("currentSubject")).getDescription());
176 		} else {
177 			Term term = getService().getTerm(termId);
178 			session.setAttribute(AcademicsConstants.TERM_ID, term.getId());
179 			session.setAttribute("currentTerm", term);
180 			session.setAttribute(AcademicsConstants.CAREER_ID, careerId);
181 			Career career = (Career) CollectionUtils.find((List<Career>) session.getAttribute(AcademicsConstants.CAREER), new CareerPredicate(careerId));
182 			session.setAttribute("currentCareer", career);
183 			session.setAttribute(AcademicsConstants.SUBJECT_ID, subjectId);
184 			Subject subject = (Subject)CollectionUtils.find((List<Subject>) session.getAttribute(AcademicsConstants.SUBJECT), new SubjectPredicate(subjectId));
185 			session.setAttribute("currentSubject", subject);
186 			List<? extends CatalogNumber> lCatalogNumbers = getService().getCatalogNumbers(termId, subjectId);
187 			session.setAttribute("catalogNumbers", lCatalogNumbers);
188 
189 			uiModel.addAttribute(AcademicsConstants.TITLE, ((Subject)session.getAttribute("currentSubject")).getDescription());
190 		}
191 		//uiModel.addAttribute(AcademicsConstants.BACKBUTTON_URL, request.getContextPath() + "/academics/subjects?termId=" + session.getAttribute(AcademicsConstants.TERM_ID) + "&careerId=" + session.getAttribute(AcademicsConstants.CAREER_ID));
192 		return "academics/catalogNumbers";
193 	}
194 
195 	@RequestMapping(value = "/browseClasses")
196 	public String browseClasses(HttpServletRequest request, Model uiModel,
197 			@RequestParam(required = false) String stage,
198 			@RequestParam(required = false)
199 			final String termId,
200 			@RequestParam(required = false)
201 			final String subjectId,
202 			@RequestParam(required = false)
203 			final String subject,
204 			@RequestParam(required = false)
205 			final String careerId,
206 			@RequestParam(required = false)
207 			final String catalogNumber,
208 			@RequestParam(required = false)
209 			final String catalogDescription) {
210 		HttpSession session = request.getSession();
211 		if (stage != null) {
212 			session.setAttribute(AcademicsConstants.STAGE, stage);
213 		} else {
214 			stage = AcademicsConstants.TERM;
215 		}
216 		if (termId != null && termId.length() > 0) {
217 			session.setAttribute(AcademicsConstants.TERM_ID, termId);
218 		}
219 		if (subjectId != null && subjectId.length() > 0) {
220 			session.setAttribute(AcademicsConstants.SUBJECT_ID, subjectId);
221 		}
222 		if (subject != null && subject.length() > 0) {
223 			session.setAttribute(AcademicsConstants.SUBJECT, subject);
224 		}
225 		if (careerId != null && careerId.length() > 0) {
226 			session.setAttribute(AcademicsConstants.CAREER_ID, careerId);
227 		}
228 		if (catalogNumber != null && catalogNumber.length() > 0) {
229 			session.setAttribute(AcademicsConstants.CATALOG_NUMBER, catalogNumber);
230 		}
231 		uiModel.addAttribute(AcademicsConstants.STAGE, stage);
232 		uiModel.addAttribute(AcademicsConstants.TERM_ID, session.getAttribute(AcademicsConstants.TERM_ID));
233 		uiModel.addAttribute(AcademicsConstants.SUBJECT_ID, session.getAttribute(AcademicsConstants.SUBJECT_ID));
234 		uiModel.addAttribute(AcademicsConstants.CAREER_ID, session.getAttribute(AcademicsConstants.CAREER_ID));
235 		uiModel.addAttribute(AcademicsConstants.CATALOG_NUMBER, session.getAttribute(AcademicsConstants.CATALOG_NUMBER));
236 		uiModel.addAttribute(AcademicsConstants.SUBJECT, session.getAttribute(AcademicsConstants.SUBJECT));
237 		uiModel.addAttribute("toolContext", "academics");
238 		if (stage.equalsIgnoreCase(AcademicsConstants.CATALOG_NUMBER)) {
239 			uiModel.addAttribute(AcademicsConstants.BACKBUTTON_URL, "browseClasses?stage=" + AcademicsConstants.SUBJECT);
240 		} else if (stage.equalsIgnoreCase(AcademicsConstants.SUBJECT)) {
241 			uiModel.addAttribute(AcademicsConstants.BACKBUTTON_URL, "browseClasses?stage=" + AcademicsConstants.CAREER);
242 		} else if (stage.equalsIgnoreCase(AcademicsConstants.CAREER)) {
243 			uiModel.addAttribute(AcademicsConstants.BACKBUTTON_URL, "browseClasses?stage=" + AcademicsConstants.TERM);
244 		}
245 
246 //		Enumeration<String> e = session.getAttributeNames();
247 //		while (e.hasMoreElements()) {
248 //			String key = e.nextElement();
249 //			LOG.debug("Session contains: [" + key + "] [" + session.getAttribute(key) + "]");
250 //		}
251 
252 		return "academics/browseClasses";
253 	}
254 
255 	@RequestMapping(value = "/sections")
256 	public String getSections(HttpServletRequest request, Model uiModel,
257 			@RequestParam(required = false)
258 			final String termId,
259 			@RequestParam(required = false)
260 			final String subjectId,
261 			@RequestParam(required = false)
262 			final String catalogNumber,
263 			@RequestParam(required = false)
264 			final String careerId) {
265 		HttpSession session = request.getSession();
266 		if (termId.equalsIgnoreCase((String) session.getAttribute(AcademicsConstants.TERM_ID))
267 				&& careerId.equalsIgnoreCase((String) session.getAttribute(AcademicsConstants.CAREER_ID))
268 				&& subjectId.equalsIgnoreCase((String) session.getAttribute(AcademicsConstants.CAREER_ID))
269 				&& catalogNumber.equalsIgnoreCase((String) session.getAttribute(AcademicsConstants.CATALOG_NUMBER))) {
270 			uiModel.addAttribute(AcademicsConstants.TITLE, session.getAttribute(AcademicsConstants.SUBJECT_ID) + " " + session.getAttribute(AcademicsConstants.CATALOG_NUMBER));
271 		} else {
272 			List<Section> lSections = (List<Section>) getService().getSections(termId, subjectId, catalogNumber, careerId);
273                         CatalogNumber number = (CatalogNumber) CollectionUtils.find((List<CatalogNumber>) session.getAttribute("catalogNumbers"), new CatalogNumberPredicate(catalogNumber));
274                         for (Section s : lSections) {
275                             if (null==s.getCourseTitle() || s.getCourseTitle().trim().isEmpty()) {
276                                 s.setCourseTitle(number.getDescription());
277                             } 
278                         }
279                         
280 			session.setAttribute("sectionsAll", lSections);
281 			session.setAttribute(AcademicsConstants.CATALOG_NUMBER, catalogNumber);
282 			session.setAttribute(AcademicsConstants.SUBJECT_ID, subjectId);
283 			uiModel.addAttribute("sectionsAll", lSections);
284 			uiModel.addAttribute(AcademicsConstants.TITLE, session.getAttribute(AcademicsConstants.SUBJECT_ID) + " " + session.getAttribute(AcademicsConstants.CATALOG_NUMBER));
285 		}
286 		uiModel.addAttribute("toolContext", "academics");
287 		//uiModel.addAttribute(AcademicsConstants.BACKBUTTON_URL, request.getContextPath() + "/academics/catalogNumbers?termId=" + termId + "&careerId=" + careerId + "&subjectId=" + subjectId);
288 		return "academics/sections";
289 	}
290 
291 	@RequestMapping(value = "/sectionsDetail")
292 	public String getSectionsDetail(HttpServletRequest request, Model uiModel,
293 			@RequestParam(required = false)
294 			final String catalogDescription,
295 			@RequestParam(required = true)
296 			final String sectionUID) {
297 		HttpSession session = request.getSession();
298 		Section sectionDetail = null;
299 		LOG.debug("Entering getSectionsDetail looking for sectionUID [" + sectionUID + "]");
300 		if (null != session.getAttribute(AcademicsConstants.SECTIONS)) {
301 			LOG.debug("Sections are stored in the session.");
302 			List<Section> allSections = (List<Section>) session.getAttribute(AcademicsConstants.SECTIONS);
303 			for (Section thisSection : allSections) {
304 				LOG.debug("Testing section [" + thisSection.getSectionUID() + "]");
305 				if (sectionUID.equalsIgnoreCase(thisSection.getSectionUID())) {
306 					LOG.debug("Matching section found, loading remaining details.");
307 					sectionDetail = getService().getSectionDetail(thisSection);
308 					break;
309 				}
310 			}
311 		}
312 //		uiModel.addAttribute(AcademicsConstants.CATALOG_DESCRIPTION, session.getAttribute(AcademicsConstants.CATALOG_DESCRIPTION));
313 		uiModel.addAttribute(AcademicsConstants.TITLE, sectionDetail.getSubjectId() + " " + sectionDetail.getCatalogNumber());
314 		uiModel.addAttribute("thisSection", sectionDetail);
315 		uiModel.addAttribute("toolContext", "academics");
316 		//LOG.debug("BACKBUTTON_URL from request for sectionDetail: -" + session.getAttribute("backButtonURL"));
317 //        String backurl = (String) session.getAttribute("backButtonURL");
318 //        if(backurl.equalsIgnoreCase("search"))
319 //        {
320 //           uiModel.addAttribute("backButtonURL","academics/classSearch");
321 //        }
322 //        else
323 //        {
324 //           uiModel.addAttribute("backButtonURL","academics/sections?termId="+session.getAttribute(AcademicsConstants.TERM_ID)+"&careerId="+session.getAttribute(AcademicsConstants.CAREER_ID)+"&subjectId="+session.getAttribute(AcademicsConstants.SUBJECT_ID)+"&catalogNumber="+session.getAttribute(AcademicsConstants.CATALOG_NUMBER)+"&catalogDescription="+session.getAttribute(AcademicsConstants.CATALOG_DESCRIPTION));
325 //        }
326 //		uiModel.addAttribute("backButtonURL", request.getHeader("referer"));
327 		return "academics/sectionsDetail";
328 	}
329 
330 	@RequestMapping(value = "/search")
331 	public String searchForm(Model uiModel) {
332 		uiModel.addAttribute(AcademicsConstants.TERM_ID, getService().getTerms());
333 		uiModel.addAttribute(AcademicsConstants.CAREER_ID, getService().getCareers("ALL"));
334 		uiModel.addAttribute(AcademicsConstants.SUBJECT_ID, getService().getSubjects("ALL", "ALL"));
335 		uiModel.addAttribute("toolContext", "academics");
336 		if( "3".equalsIgnoreCase( getKmeProperties().getProperty("kme.uiVersion","classic") ) ) {
337         	return "ui3/academics/search";
338         } else {
339         	return "academics/search";
340         }
341 	}
342 
343         @RequestMapping(value = "/classSearch", method = RequestMethod.POST)
344         public String getSearchResults(HttpServletRequest request, Model uiModel) {
345             Map<String, String[]> inputMap = request.getParameterMap();
346 
347             if ( LOG.isDebugEnabled() ) {
348                 LOG.debug("Search request parameter map:");
349                 Iterator<Map.Entry<String, String[]>> mapIterator = inputMap.entrySet().iterator();
350                 while (mapIterator.hasNext()) {
351                     Map.Entry<String, String[]> entry = mapIterator.next();
352                     LOG.debug(entry.getKey() + ": " + Arrays.toString(entry.getValue()));
353                 }
354             }
355             
356             SearchResult result = getService().getSearchResults(inputMap);
357             List<Section> lSections = (List<Section>)result.getSections();
358             LOG.debug("Sections classSearch POST  " + lSections.size() + " items long.");
359             HttpSession session = request.getSession();
360             session.setAttribute(AcademicsConstants.SECTIONS, lSections);
361             session.setAttribute(AcademicsConstants.SEARCH_RESULT,result);
362             session.setAttribute("toolContext", "academics");
363 
364             uiModel.addAttribute("searchResult", result);
365             uiModel.addAttribute("toolContext", "academics");
366 
367             return "academics/searchResult";
368         }
369 
370 	@RequestMapping(value = "/classSearch", method = RequestMethod.GET)
371 	public String searchResults(HttpServletRequest request, Model uiModel) {
372 		HttpSession session = request.getSession();
373 		//uiModel.addAttribute(AcademicsConstants.TITLE, "Search Results");
374 		//uiModel.addAttribute("sectionsAll", session.getAttribute("sectionsAll"));
375                 uiModel.addAttribute("searchResult", session.getAttribute(AcademicsConstants.SEARCH_RESULT));
376 		uiModel.addAttribute("toolContext", session.getAttribute("toolContext"));
377 		//uiModel.addAttribute("backButtonURL", session.getAttribute("backButtonURL"));
378 		return "academics/searchResult";
379 	}
380 
381     @RequestMapping(value="/templates/{key}")
382     public String getAngularTemplates(
383             @PathVariable("key") String key,
384             HttpServletRequest request,
385             Model uiModel ) {
386         return "ui3/academics/templates/"+key;
387     }
388 
389     @RequestMapping(value = "/js/{key}.js")
390     public String getJavaScript(
391             @PathVariable("key") String key,
392             Model uiModel) {
393         return "ui3/academics/js/"+key;
394     }
395 
396     private List<String> getHomeScreenOrder() {
397 		List<String> toolOrder = new ArrayList<String>();
398 		if( null == getAcademicsProperties() ) {
399 			LOG.error( "Failed to load academics properties" );
400 		}
401 		LOG.debug( "Home screen order is: "+getAcademicsProperties().getProperty("academics.homescreenorder") );
402 		for( String s : getAcademicsProperties().getProperty("academics.homescreenorder").split(",") ) {
403 			if( "true".equalsIgnoreCase( getAcademicsProperties().getProperty("academics."+s+".enabled", "false"))) {
404 				toolOrder.add(s);
405 			}
406 		}
407 		return toolOrder;
408 	}
409 
410 	/**
411 	 * @return the service
412 	 */
413 	public AcademicsService getService() {
414 		return service;
415 	}
416 
417 	/**
418 	 * @param service the service to set
419 	 */
420 	public void setService(AcademicsService service) {
421 		this.service = service;
422 	}
423 
424 	/**
425 	 * @return the academicsProperties
426 	 */
427 	public Properties getAcademicsProperties() {
428 		return academicsProperties;
429 	}
430 
431 	/**
432 	 * @param academicsProperties the academicsProperties to set
433 	 */
434 	public void setAcademicsProperties(Properties academicsProperties) {
435 		this.academicsProperties = academicsProperties;
436 	}
437 
438     public Properties getKmeProperties() {
439         return kmeProperties;
440     }
441 
442     public void setKmeProperties(Properties kmeProperties) {
443         this.kmeProperties = kmeProperties;
444     }
445 }