1 | package edu.ucsb.cs156.courses.services; | |
2 | ||
3 | import com.fasterxml.jackson.core.type.TypeReference; | |
4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
5 | import edu.ucsb.cs156.courses.documents.ConvertedSection; | |
6 | import edu.ucsb.cs156.courses.documents.CoursePage; | |
7 | import edu.ucsb.cs156.courses.documents.GERequirement; | |
8 | import edu.ucsb.cs156.courses.documents.Primary; | |
9 | import java.util.Arrays; | |
10 | import java.util.HashMap; | |
11 | import java.util.List; | |
12 | import java.util.Map; | |
13 | import java.util.stream.Collectors; | |
14 | import lombok.extern.slf4j.Slf4j; | |
15 | import org.springframework.beans.factory.annotation.Autowired; | |
16 | import org.springframework.beans.factory.annotation.Value; | |
17 | import org.springframework.boot.web.client.RestTemplateBuilder; | |
18 | import org.springframework.http.HttpEntity; | |
19 | import org.springframework.http.HttpHeaders; | |
20 | import org.springframework.http.HttpMethod; | |
21 | import org.springframework.http.HttpStatus; | |
22 | import org.springframework.http.MediaType; | |
23 | import org.springframework.http.ResponseEntity; | |
24 | import org.springframework.stereotype.Service; | |
25 | import org.springframework.web.client.RestTemplate; | |
26 | ||
27 | /** Service object that wraps the UCSB Academic Curriculum API */ | |
28 | @Service | |
29 | @Slf4j | |
30 | public class UCSBCurriculumService { | |
31 | ||
32 | @Autowired private ObjectMapper objectMapper; | |
33 | ||
34 | @Value("${app.ucsb.api.consumer_key}") | |
35 | private String apiKey; | |
36 | ||
37 | private RestTemplate restTemplate = new RestTemplate(); | |
38 | ||
39 | public UCSBCurriculumService(RestTemplateBuilder restTemplateBuilder) throws Exception { | |
40 | restTemplate = restTemplateBuilder.build(); | |
41 | } | |
42 | ||
43 | public static final String CURRICULUM_ENDPOINT = | |
44 | "https://api.ucsb.edu/academics/curriculums/v1/classes/search"; | |
45 | ||
46 | public static final String SUBJECTS_ENDPOINT = | |
47 | "https://api.ucsb.edu/students/lookups/v1/subjects"; | |
48 | ||
49 | public static final String SECTION_ENDPOINT = | |
50 | "https://api.ucsb.edu/academics/curriculums/v1/classsection/{quarter}/{enrollcode}"; | |
51 | ||
52 | public static final String ALL_SECTIONS_ENDPOINT = | |
53 | "https://api.ucsb.edu/academics/curriculums/v3/classes/{quarter}/{enrollcode}"; | |
54 | ||
55 | public static final String FINALS_ENDPOINT = | |
56 | "https://api.ucsb.edu/academics/curriculums/v3/finals"; | |
57 | ||
58 | public static final String GE_ENDPOINT = "https://api.ucsb.edu/students/lookups/v1/requirements"; | |
59 | ||
60 | public String getJSON(String subjectArea, String quarter, String courseLevel) throws Exception { | |
61 | ||
62 | HttpHeaders headers = new HttpHeaders(); | |
63 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
64 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
65 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
66 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
67 | ||
68 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
69 | ||
70 | String params = | |
71 | String.format( | |
72 | "?quarter=%s&subjectCode=%s&objLevelCode=%s&pageNumber=%d&pageSize=%d&includeClassSections=%s", | |
73 | quarter, subjectArea, courseLevel, 1, 100, "true"); | |
74 | String url = CURRICULUM_ENDPOINT + params; | |
75 | ||
76 |
1
1. getJSON : negated conditional → KILLED |
if (courseLevel.equals("A")) { |
77 | params = | |
78 | String.format( | |
79 | "?quarter=%s&subjectCode=%s&pageNumber=%d&pageSize=%d&includeClassSections=%s", | |
80 | quarter, subjectArea, 1, 100, "true"); | |
81 | url = CURRICULUM_ENDPOINT + params; | |
82 | } | |
83 | ||
84 | log.info("url=" + url); | |
85 | ||
86 | String retVal = ""; | |
87 | MediaType contentType = null; | |
88 | HttpStatus statusCode = null; | |
89 | ||
90 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
91 | contentType = re.getHeaders().getContentType(); | |
92 | statusCode = (HttpStatus) re.getStatusCode(); | |
93 | retVal = re.getBody(); | |
94 | ||
95 | log.trace("json: {}", retVal); | |
96 | log.info("contentType: {} statusCode: {}", contentType, statusCode); | |
97 |
1
1. getJSON : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getJSON → KILLED |
return retVal; |
98 | } | |
99 | ||
100 | public List<ConvertedSection> getConvertedSections( | |
101 | String subjectArea, String quarter, String courseLevel) throws Exception { | |
102 | String json = getJSON(subjectArea, quarter, courseLevel); | |
103 | CoursePage coursePage = objectMapper.readValue(json, CoursePage.class); | |
104 | List<ConvertedSection> result = coursePage.convertedSections(); | |
105 |
1
1. getConvertedSections : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getConvertedSections → KILLED |
return result; |
106 | } | |
107 | ||
108 | /** | |
109 | * This method retrieves course information and reformats it so that each primary is a single | |
110 | * object, that has a list of all of it's secondaries. | |
111 | * | |
112 | * <p>As a reminder, the UCSB course registration systems (GOLD, Blue, Star, etc.) treat all | |
113 | * courses as "sections", even what we typically call "lectures". In the database, some courses | |
114 | * have both primary and secondary sections. Primary sections have section numbers ending in 00 | |
115 | * (e.g. 100, 200, 300, up to 9900), while secondaries have numbers ending with 01, 02, 03, etc. | |
116 | * and "belong" with a primary. | |
117 | * | |
118 | * <p>What we typically refer to as "lectures" are primary sections ending in 00 (e.g. 100, 200, | |
119 | * 300). What we typically refer to as discussion sections are secondaries and end with 01, 02, | |
120 | * 03, etc. | |
121 | * | |
122 | * <p> | |
123 | * | |
124 | * <p>The UCSB API <i>almost</i> does this, but not quite. It returns a list of all sections, but | |
125 | * does not group them into primaries and secondaries. This method relies on the <code> | |
126 | * getPrimaries</code> method of the <code>Page</code> class to reformat the data so that each | |
127 | * primary section is a single object, with a list of all of its secondaries. | |
128 | * | |
129 | * @param subjectArea | |
130 | * @param quarter | |
131 | * @param courseLevel | |
132 | * @return a list of Primaries | |
133 | * @throws Exception | |
134 | */ | |
135 | public List<Primary> getPrimaries(String subjectArea, String quarter, String courseLevel) | |
136 | throws Exception { | |
137 | String json = getJSON(subjectArea, quarter, courseLevel); | |
138 | CoursePage coursePage = objectMapper.readValue(json, CoursePage.class); | |
139 | List<Primary> result = coursePage.getPrimaries(); | |
140 |
1
1. getPrimaries : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getPrimaries → KILLED |
return result; |
141 | } | |
142 | ||
143 | public String getSectionJSON(String subjectArea, String quarter, String courseLevel) | |
144 | throws Exception { | |
145 | List<ConvertedSection> l = getConvertedSections(subjectArea, quarter, courseLevel); | |
146 | ||
147 | String arrayToJson = objectMapper.writeValueAsString(l); | |
148 | ||
149 |
1
1. getSectionJSON : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getSectionJSON → KILLED |
return arrayToJson; |
150 | } | |
151 | ||
152 | public String getSubjectsJSON() throws Exception { | |
153 | ||
154 | HttpHeaders headers = new HttpHeaders(); | |
155 |
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
156 |
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
157 |
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
158 |
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
159 | ||
160 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
161 | ||
162 | log.info("url=" + SUBJECTS_ENDPOINT); | |
163 | ||
164 | String retVal = ""; | |
165 | MediaType contentType = null; | |
166 | HttpStatus statusCode = null; | |
167 | ||
168 | ResponseEntity<String> re = | |
169 | restTemplate.exchange(SUBJECTS_ENDPOINT, HttpMethod.GET, entity, String.class); | |
170 | contentType = re.getHeaders().getContentType(); | |
171 | statusCode = (HttpStatus) re.getStatusCode(); | |
172 | retVal = re.getBody(); | |
173 | ||
174 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
175 |
1
1. getSubjectsJSON : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getSubjectsJSON → KILLED |
return retVal; |
176 | } | |
177 | ||
178 | /** | |
179 | * This method retrieves exactly one section matching the enrollCode and quarter arguments, if | |
180 | * such a section exists. | |
181 | */ | |
182 | public String getSection(String enrollCode, String quarter) throws Exception { | |
183 | ||
184 | HttpHeaders headers = new HttpHeaders(); | |
185 |
1
1. getSection : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
186 |
1
1. getSection : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
187 |
1
1. getSection : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
188 |
1
1. getSection : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
189 | ||
190 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
191 | ||
192 | String url = SECTION_ENDPOINT; | |
193 | ||
194 | log.info("url=" + url); | |
195 | ||
196 | Map<String, String> params = new HashMap<>(); | |
197 | params.put("quarter", quarter); | |
198 | params.put("enrollcode", enrollCode); | |
199 | ||
200 | String retVal = ""; | |
201 | MediaType contentType = null; | |
202 | HttpStatus statusCode = null; | |
203 | ||
204 | ResponseEntity<String> re = | |
205 | restTemplate.exchange(url, HttpMethod.GET, entity, String.class, params); | |
206 | contentType = re.getHeaders().getContentType(); | |
207 | statusCode = (HttpStatus) re.getStatusCode(); | |
208 | retVal = re.getBody(); | |
209 | ||
210 |
1
1. getSection : negated conditional → KILLED |
if (retVal.equals("null")) { |
211 | retVal = "{\"error\": \"Enroll code doesn't exist in that quarter.\"}"; | |
212 | } | |
213 | ||
214 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
215 |
1
1. getSection : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getSection → KILLED |
return retVal; |
216 | } | |
217 | ||
218 | /** | |
219 | * This method retrieves all of the sections related to a certain enroll code. For example, if the | |
220 | * enrollCode is for a discussion section, the lecture section and all related discussion sections | |
221 | * will also be returned. | |
222 | */ | |
223 | public String getAllSections(String enrollCode, String quarter) throws Exception { | |
224 | ||
225 | HttpHeaders headers = new HttpHeaders(); | |
226 |
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
227 |
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
228 |
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "3.0"); |
229 |
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
230 | ||
231 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
232 | ||
233 | String url = ALL_SECTIONS_ENDPOINT; | |
234 | ||
235 | log.info("url=" + url); | |
236 | ||
237 | Map<String, String> params = new HashMap<>(); | |
238 | params.put("quarter", quarter); | |
239 | params.put("enrollcode", enrollCode); | |
240 | ||
241 | String retVal = ""; | |
242 | MediaType contentType = null; | |
243 | HttpStatus statusCode = null; | |
244 | ||
245 | ResponseEntity<String> re = | |
246 | restTemplate.exchange(url, HttpMethod.GET, entity, String.class, params); | |
247 | contentType = re.getHeaders().getContentType(); | |
248 | statusCode = (HttpStatus) re.getStatusCode(); | |
249 | retVal = re.getBody(); | |
250 | ||
251 |
1
1. getAllSections : negated conditional → KILLED |
if (retVal.equals("null")) { |
252 | retVal = "{\"error\": \"Enroll code doesn't exist in that quarter.\"}"; | |
253 | } | |
254 | ||
255 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
256 |
1
1. getAllSections : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getAllSections → KILLED |
return retVal; |
257 | } | |
258 | ||
259 | public String getJSONbyQtrEnrollCd(String quarter, String enrollCd) throws Exception { | |
260 | ||
261 | HttpHeaders headers = new HttpHeaders(); | |
262 |
1
1. getJSONbyQtrEnrollCd : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
263 |
1
1. getJSONbyQtrEnrollCd : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
264 |
1
1. getJSONbyQtrEnrollCd : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
265 |
1
1. getJSONbyQtrEnrollCd : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
266 | ||
267 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
268 | ||
269 | String url = | |
270 | "https://api.ucsb.edu/academics/curriculums/v3/classsection/" + quarter + "/" + enrollCd; | |
271 | ||
272 | log.info("url=" + url); | |
273 | ||
274 | String retVal = ""; | |
275 | MediaType contentType = null; | |
276 | HttpStatus statusCode = null; | |
277 | ||
278 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
279 | contentType = re.getHeaders().getContentType(); | |
280 | statusCode = (HttpStatus) re.getStatusCode(); | |
281 | retVal = re.getBody(); | |
282 | ||
283 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
284 |
1
1. getJSONbyQtrEnrollCd : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getJSONbyQtrEnrollCd → KILLED |
return retVal; |
285 | } | |
286 | ||
287 | public String getFinalsInfo(String quarter, String enrollCd) throws Exception { | |
288 | HttpHeaders headers = new HttpHeaders(); | |
289 |
1
1. getFinalsInfo : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
290 |
1
1. getFinalsInfo : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "3.0"); |
291 |
1
1. getFinalsInfo : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
292 | ||
293 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
294 | ||
295 | String params = String.format("?quarter=%s&enrollCode=%s", quarter, enrollCd); | |
296 | String url = FINALS_ENDPOINT + params; | |
297 | ||
298 | log.info("url=" + url); | |
299 | ||
300 | String retVal; | |
301 | MediaType contentType; | |
302 | HttpStatus statusCode; | |
303 | ||
304 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
305 | contentType = re.getHeaders().getContentType(); | |
306 | statusCode = (HttpStatus) re.getStatusCode(); | |
307 | retVal = re.getBody(); | |
308 | ||
309 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
310 |
1
1. getFinalsInfo : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getFinalsInfo → KILLED |
return retVal; |
311 | } | |
312 | ||
313 | public String getGeneralEducationInfo() throws Exception { | |
314 | HttpHeaders headers = new HttpHeaders(); | |
315 |
1
1. getGeneralEducationInfo : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
316 |
1
1. getGeneralEducationInfo : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
317 |
1
1. getGeneralEducationInfo : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
318 |
1
1. getGeneralEducationInfo : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
319 | ||
320 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
321 | ||
322 | log.info("Fetching GE areas from {}", GE_ENDPOINT); | |
323 | ResponseEntity<String> response = | |
324 | restTemplate.exchange(GE_ENDPOINT, HttpMethod.GET, entity, String.class); | |
325 | ||
326 | log.debug("GE areas JSON: {}", response.getBody()); | |
327 |
1
1. getGeneralEducationInfo : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getGeneralEducationInfo → KILLED |
return response.getBody(); |
328 | } | |
329 | ||
330 | public List<String> getRequirementCodesByCollege(String collegeCode) throws Exception { | |
331 | String json = getGeneralEducationInfo(); | |
332 | ||
333 | List<GERequirement> allAreas = | |
334 | objectMapper.readValue(json, new TypeReference<List<GERequirement>>() {}); | |
335 | ||
336 |
1
1. getRequirementCodesByCollege : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getRequirementCodesByCollege → KILLED |
return allAreas.stream() |
337 |
2
1. lambda$getRequirementCodesByCollege$0 : replaced boolean return with false for edu/ucsb/cs156/courses/services/UCSBCurriculumService::lambda$getRequirementCodesByCollege$0 → KILLED 2. lambda$getRequirementCodesByCollege$0 : replaced boolean return with true for edu/ucsb/cs156/courses/services/UCSBCurriculumService::lambda$getRequirementCodesByCollege$0 → KILLED |
.filter(area -> area.getCollegeCode().equalsIgnoreCase(collegeCode)) |
338 | .map(GERequirement::getRequirementCode) | |
339 | .distinct() | |
340 | .collect(Collectors.toList()); | |
341 | } | |
342 | } | |
Mutations | ||
63 |
1.1 |
|
64 |
1.1 |
|
65 |
1.1 |
|
66 |
1.1 |
|
76 |
1.1 |
|
97 |
1.1 |
|
105 |
1.1 |
|
140 |
1.1 |
|
149 |
1.1 |
|
155 |
1.1 |
|
156 |
1.1 |
|
157 |
1.1 |
|
158 |
1.1 |
|
175 |
1.1 |
|
185 |
1.1 |
|
186 |
1.1 |
|
187 |
1.1 |
|
188 |
1.1 |
|
210 |
1.1 |
|
215 |
1.1 |
|
226 |
1.1 |
|
227 |
1.1 |
|
228 |
1.1 |
|
229 |
1.1 |
|
251 |
1.1 |
|
256 |
1.1 |
|
262 |
1.1 |
|
263 |
1.1 |
|
264 |
1.1 |
|
265 |
1.1 |
|
284 |
1.1 |
|
289 |
1.1 |
|
290 |
1.1 |
|
291 |
1.1 |
|
310 |
1.1 |
|
315 |
1.1 |
|
316 |
1.1 |
|
317 |
1.1 |
|
318 |
1.1 |
|
327 |
1.1 |
|
336 |
1.1 |
|
337 |
1.1 2.2 |