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 java.util.Arrays;
18  import java.util.Iterator;
19  import java.util.List;
20  import java.util.Map;
21  
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpSession;
24  import org.apache.commons.collections.CollectionUtils;
25  
26  import org.apache.log4j.Logger;
27  import org.kuali.mobility.academics.entity.Career;
28  import org.kuali.mobility.academics.entity.CatalogNumber;
29  import org.kuali.mobility.academics.entity.Section;
30  import org.kuali.mobility.academics.entity.Subject;
31  import org.kuali.mobility.academics.entity.Term;
32  import org.kuali.mobility.academics.service.AcademicsService;
33  import org.kuali.mobility.academics.util.AcademicsConstants;
34  import org.kuali.mobility.academics.util.CareerPredicate;
35  import org.kuali.mobility.academics.util.CatalogNumberPredicate;
36  import org.kuali.mobility.academics.util.SubjectPredicate;
37  import org.springframework.beans.factory.annotation.Autowired;
38  import org.springframework.beans.factory.annotation.Qualifier;
39  import org.springframework.stereotype.Controller;
40  import org.springframework.ui.Model;
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  @Controller
46  @RequestMapping("/academics")
47  public class AcademicsControllerImpl {
48  
49  	private static final Logger LOG = Logger.getLogger(AcademicsControllerImpl.class);
50  	@Autowired
51  	@Qualifier("academicsService")
52  	private AcademicsService service;
53  
54  	public void setService(AcademicsService service) {
55  		this.service = service;
56  	}
57  
58  	@RequestMapping(method = RequestMethod.GET)
59  	public String index(HttpServletRequest request, Model uiModel) {
60  		HttpSession session = request.getSession();
61  		session.removeAttribute("currentTerm");
62  		session.removeAttribute("currentCareer");
63  		session.removeAttribute("currentSection");
64  		session.removeAttribute("currentCatalogNumber");
65  		session.removeAttribute("sectionsAll");
66  		session.removeAttribute(AcademicsConstants.TERM_ID);
67  		session.removeAttribute(AcademicsConstants.TERM);
68  		session.removeAttribute(AcademicsConstants.CAREER_ID);
69  		session.removeAttribute(AcademicsConstants.CAREER);
70  		session.removeAttribute(AcademicsConstants.SUBJECT_ID);
71  		session.removeAttribute(AcademicsConstants.SUBJECT);
72  		session.removeAttribute(AcademicsConstants.CATALOG_NUMBER);
73  		session.removeAttribute(AcademicsConstants.CATALOG_DESCRIPTION);
74  		session.removeAttribute(AcademicsConstants.CLASS_NUMBER);
75  		session.removeAttribute(AcademicsConstants.CLASS_SECTION);
76  		session.removeAttribute(AcademicsConstants.COURSE_ID);
77  		session.removeAttribute(AcademicsConstants.COURSE_OFFER_NBR);
78  		session.removeAttribute(AcademicsConstants.SECTIONS);
79  		session.removeAttribute(AcademicsConstants.TERM_DESCRIPTION);
80  		return "academics/index";
81  	}
82  
83  	@RequestMapping(value = "/terms")
84  	public String getTerms(HttpServletRequest request, Model uiModel) {
85  		HttpSession session = request.getSession();
86  
87  		session.setAttribute(AcademicsConstants.TERM, service.getTerms());
88  
89  		uiModel.addAttribute(AcademicsConstants.BACKBUTTON_URL, request.getContextPath() + "/academics/");
90  		return "academics/terms";
91  	}
92  
93  	@RequestMapping(value = "/careers")
94  	public String getCareers(HttpServletRequest request, Model uiModel,
95  			@RequestParam(required = true) final String termId) {
96  		HttpSession session = request.getSession();
97  		if (termId.equalsIgnoreCase((String) session.getAttribute(AcademicsConstants.TERM_ID))) {
98  			// use cached data
99  			uiModel.addAttribute(AcademicsConstants.TITLE, ((Term)session.getAttribute("currentTerm")).getDescription());
100 		} else {
101 			Term term = service.getTerm(termId);
102 			session.setAttribute(AcademicsConstants.TERM_ID, termId);
103 			session.setAttribute("currentTerm", term);
104 			List<? extends Career> lCareers = service.getCareers(termId);
105 			session.setAttribute(AcademicsConstants.CAREER, lCareers);
106 			session.setAttribute("watermark", "Search " + term.getDescription() + " Classes");
107 			uiModel.addAttribute(AcademicsConstants.TITLE, ((Term)session.getAttribute("currentTerm")).getDescription());
108 		}
109 
110 		uiModel.addAttribute(AcademicsConstants.BACKBUTTON_URL, request.getContextPath() + "/academics/terms");
111 		return "academics/careers";
112 	}
113 
114 	@RequestMapping(value = "/subjects")
115 	public String getCareers(HttpServletRequest request, Model uiModel,
116 			@RequestParam(required = true) final String termId,
117 			@RequestParam(required = true) final String careerId) {
118 		HttpSession session = request.getSession();
119 		if (termId.equalsIgnoreCase((String) session.getAttribute(AcademicsConstants.TERM_ID))
120 				&& careerId.equalsIgnoreCase((String) session.getAttribute(AcademicsConstants.CAREER_ID))) {
121 			// Use cached data.
122 //			uiModel.addAttribute(AcademicsConstants.TITLE, ((Career)session.getAttribute("currentCareer")).getShortDescription());
123 			uiModel.addAttribute(AcademicsConstants.TITLE, "Subjects" );
124 		} else {
125 			Term term = service.getTerm(termId);
126 			session.setAttribute(AcademicsConstants.TERM_ID, term.getId());
127 			session.setAttribute("currentTerm", term);
128 
129 			session.setAttribute(AcademicsConstants.CAREER_ID, careerId);
130 			Career career = (Career) CollectionUtils.find((List<Career>) session.getAttribute(AcademicsConstants.CAREER), new CareerPredicate(careerId));
131 			session.setAttribute("currentCareer", career);
132 
133 			List<? extends Subject> lSubjects = service.getSubjects(termId, careerId);
134 			session.setAttribute(AcademicsConstants.SUBJECT, lSubjects);
135 //			uiModel.addAttribute(AcademicsConstants.TITLE, ((Career)session.getAttribute("currentCareer")).getShortDescription());
136 			uiModel.addAttribute(AcademicsConstants.TITLE, "Subjects" );
137 		}
138 		uiModel.addAttribute(AcademicsConstants.BACKBUTTON_URL, request.getContextPath() + "/academics/careers?termId=" + session.getAttribute(AcademicsConstants.TERM_ID));
139 		return "academics/subjects";
140 	}
141 
142 	@RequestMapping(value = "/catalogNumbers")
143 	public String getCareers(HttpServletRequest request, Model uiModel,
144 			@RequestParam(required = true)
145 			final String termId,
146 			@RequestParam(required = true)
147 			final String careerId,
148 			@RequestParam(required = true)
149 			final String subjectId) {
150 		HttpSession session = request.getSession();
151 		if (termId.equalsIgnoreCase((String) session.getAttribute(AcademicsConstants.TERM_ID))
152 				&& careerId.equalsIgnoreCase((String) session.getAttribute(AcademicsConstants.CAREER_ID))
153 				&& subjectId.equalsIgnoreCase((String) session.getAttribute(AcademicsConstants.SUBJECT_ID))) {
154 			// Use cached data.
155 			uiModel.addAttribute(AcademicsConstants.TITLE, ((Subject)session.getAttribute("currentSubject")).getDescription());
156 		} else {
157 			Term term = service.getTerm(termId);
158 			session.setAttribute(AcademicsConstants.TERM_ID, term.getId());
159 			session.setAttribute("currentTerm", term);
160 			session.setAttribute(AcademicsConstants.CAREER_ID, careerId);
161 			Career career = (Career) CollectionUtils.find((List<Career>) session.getAttribute(AcademicsConstants.CAREER), new CareerPredicate(careerId));
162 			session.setAttribute("currentCareer", career);
163 			session.setAttribute(AcademicsConstants.SUBJECT_ID, subjectId);
164 			Subject subject = (Subject)CollectionUtils.find((List<Subject>) session.getAttribute(AcademicsConstants.SUBJECT), new SubjectPredicate(subjectId));
165 			session.setAttribute("currentSubject", subject);
166 			List<? extends CatalogNumber> lCatalogNumbers = service.getCatalogNumbers(termId, subjectId);
167 			session.setAttribute("catalogNumbers", lCatalogNumbers);
168 
169 			uiModel.addAttribute(AcademicsConstants.TITLE, ((Subject)session.getAttribute("currentSubject")).getDescription());
170 		}
171 		uiModel.addAttribute(AcademicsConstants.BACKBUTTON_URL, request.getContextPath() + "/academics/subjects?termId=" + session.getAttribute(AcademicsConstants.TERM_ID) + "&careerId=" + session.getAttribute(AcademicsConstants.CAREER_ID));
172 		return "academics/catalogNumbers";
173 	}
174 
175 	@RequestMapping(value = "/browseClasses")
176 	public String browseClasses(HttpServletRequest request, Model uiModel,
177 			@RequestParam(required = false) String stage,
178 			@RequestParam(required = false)
179 			final String termId,
180 			@RequestParam(required = false)
181 			final String subjectId,
182 			@RequestParam(required = false)
183 			final String subject,
184 			@RequestParam(required = false)
185 			final String careerId,
186 			@RequestParam(required = false)
187 			final String catalogNumber,
188 			@RequestParam(required = false)
189 			final String catalogDescription) {
190 		HttpSession session = request.getSession();
191 		if (stage != null) {
192 			session.setAttribute(AcademicsConstants.STAGE, stage);
193 		} else {
194 			stage = AcademicsConstants.TERM;
195 		}
196 		if (termId != null && termId.length() > 0) {
197 			session.setAttribute(AcademicsConstants.TERM_ID, termId);
198 		}
199 		if (subjectId != null && subjectId.length() > 0) {
200 			session.setAttribute(AcademicsConstants.SUBJECT_ID, subjectId);
201 		}
202 		if (subject != null && subject.length() > 0) {
203 			session.setAttribute(AcademicsConstants.SUBJECT, subject);
204 		}
205 		if (careerId != null && careerId.length() > 0) {
206 			session.setAttribute(AcademicsConstants.CAREER_ID, careerId);
207 		}
208 		if (catalogNumber != null && catalogNumber.length() > 0) {
209 			session.setAttribute(AcademicsConstants.CATALOG_NUMBER, catalogNumber);
210 		}
211 		uiModel.addAttribute(AcademicsConstants.STAGE, stage);
212 		uiModel.addAttribute(AcademicsConstants.TERM_ID, session.getAttribute(AcademicsConstants.TERM_ID));
213 		uiModel.addAttribute(AcademicsConstants.SUBJECT_ID, session.getAttribute(AcademicsConstants.SUBJECT_ID));
214 		uiModel.addAttribute(AcademicsConstants.CAREER_ID, session.getAttribute(AcademicsConstants.CAREER_ID));
215 		uiModel.addAttribute(AcademicsConstants.CATALOG_NUMBER, session.getAttribute(AcademicsConstants.CATALOG_NUMBER));
216 		uiModel.addAttribute(AcademicsConstants.SUBJECT, session.getAttribute(AcademicsConstants.SUBJECT));
217 		uiModel.addAttribute("toolContext", "academics");
218 		if (stage.equalsIgnoreCase(AcademicsConstants.CATALOG_NUMBER)) {
219 			uiModel.addAttribute(AcademicsConstants.BACKBUTTON_URL, "browseClasses?stage=" + AcademicsConstants.SUBJECT);
220 		} else if (stage.equalsIgnoreCase(AcademicsConstants.SUBJECT)) {
221 			uiModel.addAttribute(AcademicsConstants.BACKBUTTON_URL, "browseClasses?stage=" + AcademicsConstants.CAREER);
222 		} else if (stage.equalsIgnoreCase(AcademicsConstants.CAREER)) {
223 			uiModel.addAttribute(AcademicsConstants.BACKBUTTON_URL, "browseClasses?stage=" + AcademicsConstants.TERM);
224 		}
225 
226 //		Enumeration<String> e = session.getAttributeNames();
227 //		while (e.hasMoreElements()) {
228 //			String key = e.nextElement();
229 //			LOG.debug("Session contains: [" + key + "] [" + session.getAttribute(key) + "]");
230 //		}
231 
232 		return "academics/browseClasses";
233 	}
234 
235 	@RequestMapping(value = "/sections")
236 	public String getSections(HttpServletRequest request, Model uiModel,
237 			@RequestParam(required = false)
238 			final String termId,
239 			@RequestParam(required = false)
240 			final String subjectId,
241 			@RequestParam(required = false)
242 			final String catalogNumber,
243 			@RequestParam(required = false)
244 			final String careerId) {
245 		HttpSession session = request.getSession();
246 		if (termId.equalsIgnoreCase((String) session.getAttribute(AcademicsConstants.TERM_ID))
247 				&& careerId.equalsIgnoreCase((String) session.getAttribute(AcademicsConstants.CAREER_ID))
248 				&& subjectId.equalsIgnoreCase((String) session.getAttribute(AcademicsConstants.CAREER_ID))
249 				&& catalogNumber.equalsIgnoreCase((String) session.getAttribute(AcademicsConstants.CATALOG_NUMBER))) {
250 			uiModel.addAttribute(AcademicsConstants.TITLE, session.getAttribute(AcademicsConstants.SUBJECT_ID) + " " + session.getAttribute(AcademicsConstants.CATALOG_NUMBER));
251 		} else {
252 			List<Section> lSections = (List<Section>) service.getSections(termId, subjectId, catalogNumber, careerId);
253 			session.setAttribute("sectionsAll", lSections);
254 			session.setAttribute(AcademicsConstants.CATALOG_NUMBER, catalogNumber);
255 			CatalogNumber number = (CatalogNumber) CollectionUtils.find((List<CatalogNumber>) session.getAttribute("catalogNumbers"), new CatalogNumberPredicate(catalogNumber));
256 			session.setAttribute("currentCatalogNumber", number);
257 			session.setAttribute(AcademicsConstants.SUBJECT_ID, subjectId);
258 			session.setAttribute("sectionsAll", lSections);
259 			uiModel.addAttribute(AcademicsConstants.TITLE, session.getAttribute(AcademicsConstants.SUBJECT_ID) + " " + session.getAttribute(AcademicsConstants.CATALOG_NUMBER));
260 		}
261 		uiModel.addAttribute("toolContext", "academics");
262 		uiModel.addAttribute(AcademicsConstants.BACKBUTTON_URL, request.getContextPath() + "/academics/catalogNumbers?termId=" + termId + "&careerId=" + careerId + "&subjectId=" + subjectId);
263 		return "academics/sections";
264 	}
265 
266 	@RequestMapping(value = "/sectionsDetail")
267 	public String getSectionsDetail(HttpServletRequest request, Model uiModel,
268 			@RequestParam(required = false)
269 			final String catalogDescription,
270 			@RequestParam(required = true)
271 			final String sectionUID) {
272 		HttpSession session = request.getSession();
273 		Section sectionDetail = null;
274 		LOG.debug("Entering getSectionsDetail looking for sectionUID [" + sectionUID + "]");
275 		if (null != session.getAttribute(AcademicsConstants.SECTIONS)) {
276 			LOG.debug("Sections are stored in the session.");
277 			List<Section> allSections = (List<Section>) session.getAttribute(AcademicsConstants.SECTIONS);
278 			for (Section thisSection : allSections) {
279 				LOG.debug("Testing section [" + thisSection.getSectionUID() + "]");
280 				if (sectionUID.equalsIgnoreCase(thisSection.getSectionUID())) {
281 					LOG.debug("Matching section found, loading remaining details.");
282 					sectionDetail = service.getSectionDetail(thisSection);
283 					break;
284 				}
285 			}
286 		}
287 //		uiModel.addAttribute(AcademicsConstants.CATALOG_DESCRIPTION, session.getAttribute(AcademicsConstants.CATALOG_DESCRIPTION));
288 		uiModel.addAttribute(AcademicsConstants.TITLE, sectionDetail.getSubjectId() + " " + sectionDetail.getCatalogNumber());
289 		uiModel.addAttribute("thisSection", sectionDetail);
290 		uiModel.addAttribute("toolContext", "academics");
291 		//LOG.debug("BACKBUTTON_URL from request for sectionDetail: -" + session.getAttribute("backButtonURL"));
292 //        String backurl = (String) session.getAttribute("backButtonURL");
293 //        if(backurl.equalsIgnoreCase("search"))
294 //        {
295 //           uiModel.addAttribute("backButtonURL","academics/classSearch");
296 //        }
297 //        else
298 //        {
299 //           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));
300 //        }
301 		uiModel.addAttribute("backButtonURL", request.getHeader("referer"));
302 		return "academics/sectionsDetail";
303 	}
304 
305 	@RequestMapping(value = "/search")
306 	public String searchForm(Model uiModel) {
307 		uiModel.addAttribute(AcademicsConstants.TERM_ID, service.getTerms());
308 		uiModel.addAttribute(AcademicsConstants.CAREER_ID, service.getCareers("ALL"));
309 		uiModel.addAttribute(AcademicsConstants.SUBJECT_ID, service.getSubjects("ALL", "ALL"));
310 		uiModel.addAttribute("toolContext", "academics");
311                 uiModel.addAttribute(AcademicsConstants.TITLE, "Search Classes");
312 		return "academics/search";
313 	}
314 
315 	//@RequestMapping(value = "/classSearch", method = RequestMethod.POST)
316 	public String searchResults(HttpServletRequest request, Model uiModel,
317 			@RequestParam(required = false)
318 			final String termId,
319 			@RequestParam(required = false)
320 			final String termDescription,
321 			@RequestParam(required = false)
322 			final String careerId,
323 			@RequestParam(required = false)
324 			final String subjectId,
325 			@RequestParam(required = false)
326 			final String instructor,
327 			@RequestParam(required = false)
328 			final String searchCriteria,
329 			@RequestParam(required = false)
330 			final String filterCriteria,
331 			@RequestParam(required = false)
332 			final String showOpen,
333 			@RequestParam(required = false)
334 			final String distributionReq,
335 			@RequestParam(required = false)
336 			final String otherReq,
337 			@RequestParam(required = false)
338 			final String keyword) {
339 		List<Section> lSections = (List<Section>) service.getSearchResults(termId, careerId, subjectId, instructor, searchCriteria, filterCriteria, showOpen, distributionReq, otherReq, keyword);
340 		LOG.debug("Sections classSearch POST  " + lSections.size() + " items long.");
341 		HttpSession session = request.getSession();
342 		session.setAttribute("sectionsAll", lSections);
343 //            session.setAttribute(AcademicsConstants.TITLE,subjectId);
344 		session.setAttribute("toolContext", "academics");
345 		session.setAttribute(AcademicsConstants.SUBJECT_ID, subjectId);
346 //			uiModel.addAttribute(AcademicsConstants.CATALOG_DESCRIPTION, session.getAttribute(AcademicsConstants.CATALOG_DESCRIPTION));
347 //	    	uiModel.addAttribute(AcademicsConstants.TITLE,subjectId);
348 		uiModel.addAttribute("sectionsAll", lSections);
349 		uiModel.addAttribute("toolContext", "academics");
350 
351 		uiModel.addAttribute(AcademicsConstants.TITLE, "Search Results");
352 
353 		if (lSections.size() > 100)
354            {uiModel.addAttribute("rowsFnd", "100");}
355         else
356            {uiModel.addAttribute("rowsFnd", "99");}
357 
358 		if (request.getHeader("referer") != null && !request.getHeader("referer").isEmpty()) {  
359 			session.setAttribute(AcademicsConstants.BACKBUTTON_URL, request.getHeader("referer"));
360 			uiModel.addAttribute(AcademicsConstants.BACKBUTTON_URL, request.getHeader("referer"));
361 		} else {
362 			session.setAttribute(AcademicsConstants.BACKBUTTON_URL, request.getContextPath() + "/academics/search");
363 			uiModel.addAttribute(AcademicsConstants.BACKBUTTON_URL, request.getContextPath() + "/academics/search");
364 		}
365 		return "academics/sections";
366 	}
367 
368         @RequestMapping(value = "/classSearch", method = RequestMethod.POST)
369         public String getSearchResults(HttpServletRequest request, Model uiModel) {
370             Map<String, String[]> inputMap = request.getParameterMap();
371             LOG.debug("search request parameter map:");
372             Iterator<Map.Entry<String, String[]>> mapIterator = inputMap.entrySet().iterator();
373             while (mapIterator.hasNext()) {
374                 Map.Entry<String, String[]> entry = mapIterator.next();
375                 LOG.debug(entry.getKey() + ": " + Arrays.toString(entry.getValue()));
376             }   
377             
378             List<Section> lSections = (List<Section>) service.getSearchResults(inputMap);
379             
380             LOG.debug("Sections classSearch POST  " + lSections.size() + " items long.");
381             HttpSession session = request.getSession();
382             session.setAttribute("sectionsAll", lSections);
383 //            session.setAttribute(AcademicsConstants.TITLE,subjectId);
384             session.setAttribute("toolContext", "academics");
385 //		session.setAttribute(AcademicsConstants.SUBJECT_ID, subjectId);
386 //		uiModel.addAttribute(AcademicsConstants.CATALOG_DESCRIPTION, session.getAttribute(AcademicsConstants.CATALOG_DESCRIPTION));
387 //	    	uiModel.addAttribute(AcademicsConstants.TITLE,subjectId);
388             uiModel.addAttribute("sectionsAll", lSections);
389             uiModel.addAttribute("toolContext", "academics");
390 
391             uiModel.addAttribute(AcademicsConstants.TITLE, "Search Results");
392 
393             if (lSections.size() > 100) {
394                 uiModel.addAttribute("rowsFnd", "100");
395             }
396             else {
397                 uiModel.addAttribute("rowsFnd", "99");
398             }
399 
400             if (request.getHeader("referer") != null && !request.getHeader("referer").isEmpty()) {  
401                     session.setAttribute(AcademicsConstants.BACKBUTTON_URL, request.getHeader("referer"));
402                     uiModel.addAttribute(AcademicsConstants.BACKBUTTON_URL, request.getHeader("referer"));
403             } else {
404                     session.setAttribute(AcademicsConstants.BACKBUTTON_URL, request.getContextPath() + "/academics/search");
405                     uiModel.addAttribute(AcademicsConstants.BACKBUTTON_URL, request.getContextPath() + "/academics/search");
406             }
407             return "academics/sections";
408         }
409         
410 	@RequestMapping(value = "/classSearch", method = RequestMethod.GET)
411 	public String searchResults(HttpServletRequest request, Model uiModel) {
412 		HttpSession session = request.getSession();
413 		uiModel.addAttribute(AcademicsConstants.TITLE, "Search Results");
414 		uiModel.addAttribute("sectionsAll", session.getAttribute("sectionsAll"));
415 		uiModel.addAttribute("toolContext", session.getAttribute("toolContext"));
416 		uiModel.addAttribute("backButtonURL", session.getAttribute("backButtonURL"));
417 		return "academics/sections";
418 	}
419 }