1 | package edu.ucsb.cs156.courses.services; | |
2 | ||
3 | import com.fasterxml.jackson.databind.JsonNode; | |
4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
5 | import com.fasterxml.jackson.databind.node.ArrayNode; | |
6 | import edu.ucsb.cs156.courses.documents.ConvertedSection; | |
7 | import edu.ucsb.cs156.courses.documents.CoursePage; | |
8 | import java.util.Arrays; | |
9 | import java.util.HashMap; | |
10 | import java.util.List; | |
11 | import java.util.Map; | |
12 | import java.util.Set; | |
13 | import java.util.TreeSet; | |
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 | private static final String[] GE_CODES = { | |
59 | "A1", "A2", "AMH", "B", "C", "D", "E", "E1", "E2", "ETH", "EUR", "F", "G", "H", "NWC", "QNT", | |
60 | "SUB", "WRT" | |
61 | }; | |
62 | ||
63 | public static final String GE_ENDPOINT = "https://api.ucsb.edu/students/lookups/v1/requirements"; | |
64 | ||
65 | public String getJSON(String subjectArea, String quarter, String courseLevel) throws Exception { | |
66 | ||
67 | HttpHeaders headers = new HttpHeaders(); | |
68 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
69 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
70 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
71 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
72 | ||
73 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
74 | ||
75 | String params = | |
76 | String.format( | |
77 | "?quarter=%s&subjectCode=%s&objLevelCode=%s&pageNumber=%d&pageSize=%d&includeClassSections=%s", | |
78 | quarter, subjectArea, courseLevel, 1, 100, "true"); | |
79 | String url = CURRICULUM_ENDPOINT + params; | |
80 | ||
81 |
1
1. getJSON : negated conditional → KILLED |
if (courseLevel.equals("A")) { |
82 | params = | |
83 | String.format( | |
84 | "?quarter=%s&subjectCode=%s&pageNumber=%d&pageSize=%d&includeClassSections=%s", | |
85 | quarter, subjectArea, 1, 100, "true"); | |
86 | url = CURRICULUM_ENDPOINT + params; | |
87 | } | |
88 | ||
89 | log.info("url=" + url); | |
90 | ||
91 | String retVal = ""; | |
92 | MediaType contentType = null; | |
93 | HttpStatus statusCode = null; | |
94 | ||
95 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
96 | contentType = re.getHeaders().getContentType(); | |
97 | statusCode = (HttpStatus) re.getStatusCode(); | |
98 | retVal = re.getBody(); | |
99 | ||
100 | log.trace("json: {}", retVal); | |
101 | log.info("contentType: {} statusCode: {}", contentType, statusCode); | |
102 |
1
1. getJSON : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getJSON → KILLED |
return retVal; |
103 | } | |
104 | ||
105 | public List<ConvertedSection> getConvertedSections( | |
106 | String subjectArea, String quarter, String courseLevel) throws Exception { | |
107 | String json = getJSON(subjectArea, quarter, courseLevel); | |
108 | CoursePage coursePage = objectMapper.readValue(json, CoursePage.class); | |
109 | List<ConvertedSection> result = coursePage.convertedSections(); | |
110 |
1
1. getConvertedSections : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getConvertedSections → KILLED |
return result; |
111 | } | |
112 | ||
113 | public String getSectionJSON(String subjectArea, String quarter, String courseLevel) | |
114 | throws Exception { | |
115 | List<ConvertedSection> l = getConvertedSections(subjectArea, quarter, courseLevel); | |
116 | ||
117 | String arrayToJson = objectMapper.writeValueAsString(l); | |
118 | ||
119 |
1
1. getSectionJSON : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getSectionJSON → KILLED |
return arrayToJson; |
120 | } | |
121 | ||
122 | public String getSubjectsJSON() throws Exception { | |
123 | ||
124 | HttpHeaders headers = new HttpHeaders(); | |
125 |
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
126 |
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
127 |
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
128 |
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
129 | ||
130 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
131 | ||
132 | log.info("url=" + SUBJECTS_ENDPOINT); | |
133 | ||
134 | String retVal = ""; | |
135 | MediaType contentType = null; | |
136 | HttpStatus statusCode = null; | |
137 | ||
138 | ResponseEntity<String> re = | |
139 | restTemplate.exchange(SUBJECTS_ENDPOINT, HttpMethod.GET, entity, String.class); | |
140 | contentType = re.getHeaders().getContentType(); | |
141 | statusCode = (HttpStatus) re.getStatusCode(); | |
142 | retVal = re.getBody(); | |
143 | ||
144 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
145 |
1
1. getSubjectsJSON : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getSubjectsJSON → KILLED |
return retVal; |
146 | } | |
147 | ||
148 | /** | |
149 | * This method retrieves exactly one section matching the enrollCode and quarter arguments, if | |
150 | * such a section exists. | |
151 | */ | |
152 | public String getSection(String enrollCode, String quarter) throws Exception { | |
153 | ||
154 | HttpHeaders headers = new HttpHeaders(); | |
155 |
1
1. getSection : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
156 |
1
1. getSection : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
157 |
1
1. getSection : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
158 |
1
1. getSection : 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 | String url = SECTION_ENDPOINT; | |
163 | ||
164 | log.info("url=" + url); | |
165 | ||
166 | Map<String, String> params = new HashMap<>(); | |
167 | params.put("quarter", quarter); | |
168 | params.put("enrollcode", enrollCode); | |
169 | ||
170 | String retVal = ""; | |
171 | MediaType contentType = null; | |
172 | HttpStatus statusCode = null; | |
173 | ||
174 | ResponseEntity<String> re = | |
175 | restTemplate.exchange(url, HttpMethod.GET, entity, String.class, params); | |
176 | contentType = re.getHeaders().getContentType(); | |
177 | statusCode = (HttpStatus) re.getStatusCode(); | |
178 | retVal = re.getBody(); | |
179 | ||
180 |
1
1. getSection : negated conditional → KILLED |
if (retVal.equals("null")) { |
181 | retVal = "{\"error\": \"Enroll code doesn't exist in that quarter.\"}"; | |
182 | } | |
183 | ||
184 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
185 |
1
1. getSection : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getSection → KILLED |
return retVal; |
186 | } | |
187 | ||
188 | /** | |
189 | * This method retrieves all of the sections related to a certain enroll code. For example, if the | |
190 | * enrollCode is for a discussion section, the lecture section and all related discussion sections | |
191 | * will also be returned. | |
192 | */ | |
193 | public String getAllSections(String enrollCode, String quarter) throws Exception { | |
194 | ||
195 | HttpHeaders headers = new HttpHeaders(); | |
196 |
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
197 |
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
198 |
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "3.0"); |
199 |
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
200 | ||
201 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
202 | ||
203 | String url = ALL_SECTIONS_ENDPOINT; | |
204 | ||
205 | log.info("url=" + url); | |
206 | ||
207 | Map<String, String> params = new HashMap<>(); | |
208 | params.put("quarter", quarter); | |
209 | params.put("enrollcode", enrollCode); | |
210 | ||
211 | String retVal = ""; | |
212 | MediaType contentType = null; | |
213 | HttpStatus statusCode = null; | |
214 | ||
215 | ResponseEntity<String> re = | |
216 | restTemplate.exchange(url, HttpMethod.GET, entity, String.class, params); | |
217 | contentType = re.getHeaders().getContentType(); | |
218 | statusCode = (HttpStatus) re.getStatusCode(); | |
219 | retVal = re.getBody(); | |
220 | ||
221 |
1
1. getAllSections : negated conditional → KILLED |
if (retVal.equals("null")) { |
222 | retVal = "{\"error\": \"Enroll code doesn't exist in that quarter.\"}"; | |
223 | } | |
224 | ||
225 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
226 |
1
1. getAllSections : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getAllSections → KILLED |
return retVal; |
227 | } | |
228 | ||
229 | public String getJSONbyQtrEnrollCd(String quarter, String enrollCd) throws Exception { | |
230 | ||
231 | HttpHeaders headers = new HttpHeaders(); | |
232 |
1
1. getJSONbyQtrEnrollCd : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
233 |
1
1. getJSONbyQtrEnrollCd : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
234 |
1
1. getJSONbyQtrEnrollCd : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
235 |
1
1. getJSONbyQtrEnrollCd : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
236 | ||
237 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
238 | ||
239 | String url = | |
240 | "https://api.ucsb.edu/academics/curriculums/v3/classsection/" + quarter + "/" + enrollCd; | |
241 | ||
242 | log.info("url=" + url); | |
243 | ||
244 | String retVal = ""; | |
245 | MediaType contentType = null; | |
246 | HttpStatus statusCode = null; | |
247 | ||
248 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
249 | contentType = re.getHeaders().getContentType(); | |
250 | statusCode = (HttpStatus) re.getStatusCode(); | |
251 | retVal = re.getBody(); | |
252 | ||
253 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
254 |
1
1. getJSONbyQtrEnrollCd : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getJSONbyQtrEnrollCd → KILLED |
return retVal; |
255 | } | |
256 | ||
257 | public String getFinalsInfo(String quarter, String enrollCd) throws Exception { | |
258 | HttpHeaders headers = new HttpHeaders(); | |
259 |
1
1. getFinalsInfo : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
260 |
1
1. getFinalsInfo : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "3.0"); |
261 |
1
1. getFinalsInfo : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
262 | ||
263 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
264 | ||
265 | String params = String.format("?quarter=%s&enrollCode=%s", quarter, enrollCd); | |
266 | String url = FINALS_ENDPOINT + params; | |
267 | ||
268 | log.info("url=" + url); | |
269 | ||
270 | String retVal; | |
271 | MediaType contentType; | |
272 | HttpStatus statusCode; | |
273 | ||
274 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
275 | contentType = re.getHeaders().getContentType(); | |
276 | statusCode = (HttpStatus) re.getStatusCode(); | |
277 | retVal = re.getBody(); | |
278 | ||
279 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
280 |
1
1. getFinalsInfo : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getFinalsInfo → KILLED |
return retVal; |
281 | } | |
282 | ||
283 | public String[] getStaticGeInfo() { | |
284 |
1
1. getStaticGeInfo : replaced return value with null for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getStaticGeInfo → KILLED |
return GE_CODES; |
285 | } | |
286 | ||
287 | public String getGeInfo() throws Exception { | |
288 | HttpHeaders headers = new HttpHeaders(); | |
289 |
1
1. getGeInfo : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
290 |
1
1. getGeInfo : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "3.0"); |
291 |
1
1. getGeInfo : 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 url = GE_ENDPOINT; | |
296 | ||
297 | log.info("url=" + url); | |
298 | ||
299 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
300 | MediaType contentType = re.getHeaders().getContentType(); | |
301 | HttpStatus statusCode = (HttpStatus) re.getStatusCode(); | |
302 | String fullResponse = re.getBody(); | |
303 | ||
304 | log.info("json: {} contentType: {} statusCode: {}", fullResponse, contentType, statusCode); | |
305 | ||
306 | // Parse the full JSON response | |
307 | ObjectMapper objectMapper = new ObjectMapper(); | |
308 | JsonNode rootNode = objectMapper.readTree(fullResponse); | |
309 | ||
310 | // Create a new array with just teh requirement coes as strings | |
311 | Set<String> uniqueCodes = new TreeSet<>(); | |
312 | for (JsonNode item : rootNode) { | |
313 | uniqueCodes.add(item.get("requirementCode").asText()); | |
314 | } | |
315 | ||
316 | // Convert the Set to a JSON array | |
317 | ArrayNode resultArray = objectMapper.createArrayNode(); | |
318 | for (String code : uniqueCodes) { | |
319 | resultArray.add(code); | |
320 | } | |
321 | ||
322 | // Convert the filtered array back to JSON string | |
323 |
1
1. getGeInfo : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getGeInfo → KILLED |
return objectMapper.writeValueAsString(resultArray); |
324 | } | |
325 | } | |
Mutations | ||
68 |
1.1 |
|
69 |
1.1 |
|
70 |
1.1 |
|
71 |
1.1 |
|
81 |
1.1 |
|
102 |
1.1 |
|
110 |
1.1 |
|
119 |
1.1 |
|
125 |
1.1 |
|
126 |
1.1 |
|
127 |
1.1 |
|
128 |
1.1 |
|
145 |
1.1 |
|
155 |
1.1 |
|
156 |
1.1 |
|
157 |
1.1 |
|
158 |
1.1 |
|
180 |
1.1 |
|
185 |
1.1 |
|
196 |
1.1 |
|
197 |
1.1 |
|
198 |
1.1 |
|
199 |
1.1 |
|
221 |
1.1 |
|
226 |
1.1 |
|
232 |
1.1 |
|
233 |
1.1 |
|
234 |
1.1 |
|
235 |
1.1 |
|
254 |
1.1 |
|
259 |
1.1 |
|
260 |
1.1 |
|
261 |
1.1 |
|
280 |
1.1 |
|
284 |
1.1 |
|
289 |
1.1 |
|
290 |
1.1 |
|
291 |
1.1 |
|
323 |
1.1 |