1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.academics.dao;
16
17 import org.apache.commons.collections.CollectionUtils;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.kuali.mobility.academics.entity.CatalogNumber;
21 import org.kuali.mobility.academics.entity.SearchResult;
22 import org.kuali.mobility.academics.entity.Section;
23 import org.kuali.mobility.academics.util.AcademicsConstants;
24 import org.kuali.mobility.academics.util.CatalogNumberPredicate;
25 import org.kuali.mobility.shared.entity.ToolMessage;
26 import org.kuali.mobility.shared.entity.ToolMessageType;
27
28 import javax.xml.bind.JAXBContext;
29 import javax.xml.bind.JAXBException;
30 import javax.xml.bind.Unmarshaller;
31 import java.io.InputStream;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36
37
38
39
40 public class AcademicsDaoImplDemo extends AcademicsDaoImpl {
41 private static Logger LOG = LoggerFactory.getLogger(AcademicsDaoImplDemo.class);
42
43 private List<CatalogNumber> catalogNumbers = new ArrayList<CatalogNumber>();
44 private Map<String,List<CatalogNumber>> demoCache = new HashMap<String,List<CatalogNumber>>();
45
46 private void initCatalogNumbers() {
47 if( getCatalogNumbers().isEmpty() ) {
48 for( int i = 0; i < 10; i++) {
49 LOG.debug("Attempting to load file named: CatalogNumber"+i+".xml");
50 CatalogNumber catalogNumber = null;
51 try {
52 JAXBContext jc = JAXBContext.newInstance(CatalogNumber.class);
53 Unmarshaller um = jc.createUnmarshaller();
54 InputStream in = this.getClass().getResourceAsStream("/CatalogNumber"+i+".xml");
55 catalogNumber = (CatalogNumber)um.unmarshal(in);
56 getCatalogNumbers().add(catalogNumber);
57 } catch (JAXBException jbe) {
58 LOG.error(jbe.getLocalizedMessage(), jbe);
59 }
60 }
61 }
62 }
63
64 public List<CatalogNumber> getCatalogNumbers(Map<String, String> query) {
65 List<CatalogNumber> lCatalogNumbers = new ArrayList<CatalogNumber>();
66
67 StringBuilder keyBuilder = new StringBuilder();
68 keyBuilder.append(query.get(AcademicsConstants.TERM_ID));
69 keyBuilder.append("#");
70 keyBuilder.append(query.get(AcademicsConstants.SUBJECT_ID));
71
72 String key = keyBuilder.toString();
73 if( getDemoCache().containsKey(key) ) {
74 lCatalogNumbers = getDemoCache().get(key);
75 } else {
76 initCatalogNumbers();
77 for( CatalogNumber c : getCatalogNumbers() ) {
78 if( (100 - (Math.random() * (100-1))) < 50 ) {
79 lCatalogNumbers.add(c);
80 }
81 }
82 getDemoCache().put(key,lCatalogNumbers);
83 }
84 return lCatalogNumbers;
85 }
86
87 public List<Section> getSections(Map<String, String> query) {
88 List<Section> lSection = new ArrayList<Section>();
89 StringBuilder keyBuilder = new StringBuilder();
90 keyBuilder.append(query.get(AcademicsConstants.TERM_ID));
91 keyBuilder.append("#");
92 keyBuilder.append(query.get(AcademicsConstants.SUBJECT_ID));
93
94 String key = keyBuilder.toString();
95 if( getDemoCache().containsKey(key) ) {
96 List<CatalogNumber> numbers = getDemoCache().get(key);
97 CatalogNumber number = (CatalogNumber)CollectionUtils.find(numbers,new CatalogNumberPredicate(query.get(AcademicsConstants.CATALOG_NUMBER)));
98 for(Section s : number.getSections()) {
99 s.setSubjectId(query.get(AcademicsConstants.SUBJECT_ID));
100 s.setCatalogNumber(query.get(AcademicsConstants.CATALOG_NUMBER));
101 lSection.add(s);
102 }
103 }
104
105 return lSection;
106 }
107
108 public Section getSectionDetail(Section section)
109 {
110 return section;
111 }
112
113 public Section getSectionDetail(String termId, String careerId, String subjectId, String catalogNumber, String sectionNumber)
114 {
115 Section lSection = null;
116 StringBuilder keyBuilder = new StringBuilder();
117 keyBuilder.append(termId);
118 keyBuilder.append("#");
119 keyBuilder.append(subjectId);
120 String key = keyBuilder.toString();
121 if( getDemoCache().containsKey(key) ) {
122 List<CatalogNumber> numbers = getDemoCache().get(key);
123 CatalogNumber number = (CatalogNumber)CollectionUtils.find(numbers,new CatalogNumberPredicate(catalogNumber));
124 for(Section s : number.getSections()) {
125 if( sectionNumber.equalsIgnoreCase(s.getNumber()) ) {
126 lSection = s;
127 s.setSubjectId(subjectId);
128 s.setCatalogNumber(catalogNumber);
129 break;
130 }
131 }
132 }
133 return lSection;
134 }
135
136 public List<CatalogNumber> getCatalogNumbers() {
137 return catalogNumbers;
138 }
139
140 public void setCatalogNumbers(List<CatalogNumber> catalogNumbers) {
141 this.catalogNumbers = catalogNumbers;
142 }
143
144 public SearchResult getSearchResults(Map<String, String[]> query) {
145 SearchResult searchResult = (SearchResult)getApplicationContext().getBean("academicsSearchResult");
146
147 if( null == getCatalogNumbers() || getCatalogNumbers().isEmpty() ) {
148 initCatalogNumbers();
149 }
150
151 int i = (int)(Math.random() * getCatalogNumbers().size());
152
153 CatalogNumber number = getCatalogNumbers().get(i);
154
155 searchResult.setSections(number.getSections());
156
157 if( (int)(Math.random() * 10) < 2 ) {
158 ToolMessage message = new ToolMessage();
159 message.setType(ToolMessageType.ALERT);
160 message.setLabel("Too Many Results");
161 message.setDescription("Your search returned too many results to display, please refine your search criteria.");
162 searchResult.addMessage(message);
163 }
164 if( (int)(Math.random() * 10) < 2 ) {
165 ToolMessage message = new ToolMessage();
166 message.setType(ToolMessageType.ERROR);
167 message.setLabel("Error");
168 message.setDescription("An error occurred while performing your search.");
169 searchResult.addMessage(message);
170 }
171
172 return searchResult;
173 }
174
175 public Map<String, List<CatalogNumber>> getDemoCache() {
176 return demoCache;
177 }
178
179 public void setDemoCache(Map<String, List<CatalogNumber>> demoCache) {
180 this.demoCache = demoCache;
181 }
182 }