View Javadoc
1   package org.kuali.student.ap.coursesearch.util;
2   
3   import java.util.Arrays;
4   import java.util.HashSet;
5   import java.util.List;
6   import java.util.Set;
7   
8   import org.kuali.student.ap.coursesearch.CourseSearchItem;
9   import org.kuali.student.ap.coursesearch.dataobject.CourseSearchItemImpl;
10  import org.kuali.student.ap.coursesearch.dataobject.FacetItem;
11  import org.slf4j.Logger;
12  import org.slf4j.LoggerFactory;
13  
14  /**
15   * Logic for building list of Course Level FacetItems and coding
16   * CourseSearchItems.
17   */
18  public class CourseLevelFacet extends AbstractFacet {
19  
20  	private static final Logger LOG = LoggerFactory.getLogger(CourseLevelFacet.class);
21  
22  	private HashSet<Integer> courseFacetSet = new HashSet<Integer>();
23  
24  	public CourseLevelFacet() {
25  		super();
26  	}
27  
28  	/**
29  	 * Overriding because the course level facet list needs to be in numeric
30  	 * order rather than string order.
31  	 * 
32  	 * @return A list of FacetItems.
33  	 */
34  	@Override
35  	public List<FacetItem> getFacetItems() {
36  		Integer[] list = courseFacetSet.toArray(new Integer[0]);
37  		Arrays.sort(list);
38  
39  		for (Integer credit : list) {
40  			String display = credit.toString();
41  			facetItems.add(new FacetItem(display, display));
42  		}
43  
44  		return facetItems;
45  	}
46  
47  	/**
48  	 * {@inheritDoc}
49  	 */
50  	@Override
51  	public void process(CourseSearchItem course) {
52  		String key = course.getLevel();
53  
54  		int level;
55  		try {
56  			level = key == null ? 0 : Integer.valueOf(key);
57  		} catch (NumberFormatException e) {
58  			LOG.warn(String.format("Invalid number in course level %s", key), e);
59  			level = 0;
60  		}
61  		courseFacetSet.add(level);
62  
63  		key = String.valueOf(level);
64  
65  		// Code the item with the facet key.
66  		Set<String> keys = new HashSet<String>();
67  		keys.add(key);
68  		((CourseSearchItemImpl) course).setCourseLevelFacetKeys(keys);
69  	}
70  }