| 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 java.util.Arrays; | |
| 9 | import java.util.HashMap; | |
| 10 | import java.util.List; | |
| 11 | import java.util.Map; | |
| 12 | import java.util.stream.Collectors; | |
| 13 | import lombok.extern.slf4j.Slf4j; | |
| 14 | import org.springframework.beans.factory.annotation.Autowired; | |
| 15 | import org.springframework.beans.factory.annotation.Value; | |
| 16 | import org.springframework.boot.web.client.RestTemplateBuilder; | |
| 17 | import org.springframework.http.HttpEntity; | |
| 18 | import org.springframework.http.HttpHeaders; | |
| 19 | import org.springframework.http.HttpMethod; | |
| 20 | import org.springframework.http.HttpStatus; | |
| 21 | import org.springframework.http.MediaType; | |
| 22 | import org.springframework.http.ResponseEntity; | |
| 23 | import org.springframework.stereotype.Service; | |
| 24 | import org.springframework.web.client.RestTemplate; | |
| 25 | ||
| 26 | /** Service object that wraps the UCSB Academic Curriculum API */ | |
| 27 | @Service | |
| 28 | @Slf4j | |
| 29 | public class UCSBCurriculumService { | |
| 30 | ||
| 31 | @Autowired private ObjectMapper objectMapper; | |
| 32 | ||
| 33 | @Value("${app.ucsb.api.consumer_key}") | |
| 34 | private String apiKey; | |
| 35 | ||
| 36 | private RestTemplate restTemplate = new RestTemplate(); | |
| 37 | ||
| 38 | public UCSBCurriculumService(RestTemplateBuilder restTemplateBuilder) throws Exception { | |
| 39 | restTemplate = restTemplateBuilder.build(); | |
| 40 | } | |
| 41 | ||
| 42 | public static final String CURRICULUM_ENDPOINT = | |
| 43 | "https://api.ucsb.edu/academics/curriculums/v1/classes/search"; | |
| 44 | ||
| 45 | public static final String SUBJECTS_ENDPOINT = | |
| 46 | "https://api.ucsb.edu/students/lookups/v1/subjects"; | |
| 47 | ||
| 48 | public static final String SECTION_ENDPOINT = | |
| 49 | "https://api.ucsb.edu/academics/curriculums/v1/classsection/{quarter}/{enrollcode}"; | |
| 50 | ||
| 51 | public static final String ALL_SECTIONS_ENDPOINT = | |
| 52 | "https://api.ucsb.edu/academics/curriculums/v3/classes/{quarter}/{enrollcode}"; | |
| 53 | ||
| 54 | public static final String FINALS_ENDPOINT = | |
| 55 | "https://api.ucsb.edu/academics/curriculums/v3/finals"; | |
| 56 | ||
| 57 | public static final String GE_ENDPOINT = "https://api.ucsb.edu/students/lookups/v1/requirements"; | |
| 58 | ||
| 59 | public String getJSON(String subjectArea, String quarter, String courseLevel) throws Exception { | |
| 60 | ||
| 61 | HttpHeaders headers = new HttpHeaders(); | |
| 62 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
| 63 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 64 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
| 65 |
1
1. getJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
| 66 | ||
| 67 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 68 | ||
| 69 | String params = | |
| 70 | String.format( | |
| 71 | "?quarter=%s&subjectCode=%s&objLevelCode=%s&pageNumber=%d&pageSize=%d&includeClassSections=%s", | |
| 72 | quarter, subjectArea, courseLevel, 1, 100, "true"); | |
| 73 | String url = CURRICULUM_ENDPOINT + params; | |
| 74 | ||
| 75 |
1
1. getJSON : negated conditional → KILLED |
if (courseLevel.equals("A")) { |
| 76 | params = | |
| 77 | String.format( | |
| 78 | "?quarter=%s&subjectCode=%s&pageNumber=%d&pageSize=%d&includeClassSections=%s", | |
| 79 | quarter, subjectArea, 1, 100, "true"); | |
| 80 | url = CURRICULUM_ENDPOINT + params; | |
| 81 | } | |
| 82 | ||
| 83 | log.info("url=" + url); | |
| 84 | ||
| 85 | String retVal = ""; | |
| 86 | MediaType contentType = null; | |
| 87 | HttpStatus statusCode = null; | |
| 88 | ||
| 89 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
| 90 | contentType = re.getHeaders().getContentType(); | |
| 91 | statusCode = (HttpStatus) re.getStatusCode(); | |
| 92 | retVal = re.getBody(); | |
| 93 | ||
| 94 | log.trace("json: {}", retVal); | |
| 95 | log.info("contentType: {} statusCode: {}", contentType, statusCode); | |
| 96 |
1
1. getJSON : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getJSON → KILLED |
return retVal; |
| 97 | } | |
| 98 | ||
| 99 | public List<ConvertedSection> getConvertedSections( | |
| 100 | String subjectArea, String quarter, String courseLevel) throws Exception { | |
| 101 | String json = getJSON(subjectArea, quarter, courseLevel); | |
| 102 | CoursePage coursePage = objectMapper.readValue(json, CoursePage.class); | |
| 103 | List<ConvertedSection> result = coursePage.convertedSections(); | |
| 104 |
1
1. getConvertedSections : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getConvertedSections → KILLED |
return result; |
| 105 | } | |
| 106 | ||
| 107 | public String getSectionJSON(String subjectArea, String quarter, String courseLevel) | |
| 108 | throws Exception { | |
| 109 | List<ConvertedSection> l = getConvertedSections(subjectArea, quarter, courseLevel); | |
| 110 | ||
| 111 | String arrayToJson = objectMapper.writeValueAsString(l); | |
| 112 | ||
| 113 |
1
1. getSectionJSON : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getSectionJSON → KILLED |
return arrayToJson; |
| 114 | } | |
| 115 | ||
| 116 | public String getSubjectsJSON() throws Exception { | |
| 117 | ||
| 118 | HttpHeaders headers = new HttpHeaders(); | |
| 119 |
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
| 120 |
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 121 |
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
| 122 |
1
1. getSubjectsJSON : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
| 123 | ||
| 124 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 125 | ||
| 126 | log.info("url=" + SUBJECTS_ENDPOINT); | |
| 127 | ||
| 128 | String retVal = ""; | |
| 129 | MediaType contentType = null; | |
| 130 | HttpStatus statusCode = null; | |
| 131 | ||
| 132 | ResponseEntity<String> re = | |
| 133 | restTemplate.exchange(SUBJECTS_ENDPOINT, HttpMethod.GET, entity, String.class); | |
| 134 | contentType = re.getHeaders().getContentType(); | |
| 135 | statusCode = (HttpStatus) re.getStatusCode(); | |
| 136 | retVal = re.getBody(); | |
| 137 | ||
| 138 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
| 139 |
1
1. getSubjectsJSON : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getSubjectsJSON → KILLED |
return retVal; |
| 140 | } | |
| 141 | ||
| 142 | /** | |
| 143 | * This method retrieves exactly one section matching the enrollCode and quarter arguments, if | |
| 144 | * such a section exists. | |
| 145 | */ | |
| 146 | public String getSection(String enrollCode, String quarter) throws Exception { | |
| 147 | ||
| 148 | HttpHeaders headers = new HttpHeaders(); | |
| 149 |
1
1. getSection : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
| 150 |
1
1. getSection : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 151 |
1
1. getSection : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
| 152 |
1
1. getSection : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
| 153 | ||
| 154 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 155 | ||
| 156 | String url = SECTION_ENDPOINT; | |
| 157 | ||
| 158 | log.info("url=" + url); | |
| 159 | ||
| 160 | Map<String, String> params = new HashMap<>(); | |
| 161 | params.put("quarter", quarter); | |
| 162 | params.put("enrollcode", enrollCode); | |
| 163 | ||
| 164 | String retVal = ""; | |
| 165 | MediaType contentType = null; | |
| 166 | HttpStatus statusCode = null; | |
| 167 | ||
| 168 | ResponseEntity<String> re = | |
| 169 | restTemplate.exchange(url, HttpMethod.GET, entity, String.class, params); | |
| 170 | contentType = re.getHeaders().getContentType(); | |
| 171 | statusCode = (HttpStatus) re.getStatusCode(); | |
| 172 | retVal = re.getBody(); | |
| 173 | ||
| 174 |
1
1. getSection : negated conditional → KILLED |
if (retVal.equals("null")) { |
| 175 | retVal = "{\"error\": \"Enroll code doesn't exist in that quarter.\"}"; | |
| 176 | } | |
| 177 | ||
| 178 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
| 179 |
1
1. getSection : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getSection → KILLED |
return retVal; |
| 180 | } | |
| 181 | ||
| 182 | /** | |
| 183 | * This method retrieves all of the sections related to a certain enroll code. For example, if the | |
| 184 | * enrollCode is for a discussion section, the lecture section and all related discussion sections | |
| 185 | * will also be returned. | |
| 186 | */ | |
| 187 | public String getAllSections(String enrollCode, String quarter) throws Exception { | |
| 188 | ||
| 189 | HttpHeaders headers = new HttpHeaders(); | |
| 190 |
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
| 191 |
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 192 |
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "3.0"); |
| 193 |
1
1. getAllSections : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
| 194 | ||
| 195 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 196 | ||
| 197 | String url = ALL_SECTIONS_ENDPOINT; | |
| 198 | ||
| 199 | log.info("url=" + url); | |
| 200 | ||
| 201 | Map<String, String> params = new HashMap<>(); | |
| 202 | params.put("quarter", quarter); | |
| 203 | params.put("enrollcode", enrollCode); | |
| 204 | ||
| 205 | String retVal = ""; | |
| 206 | MediaType contentType = null; | |
| 207 | HttpStatus statusCode = null; | |
| 208 | ||
| 209 | ResponseEntity<String> re = | |
| 210 | restTemplate.exchange(url, HttpMethod.GET, entity, String.class, params); | |
| 211 | contentType = re.getHeaders().getContentType(); | |
| 212 | statusCode = (HttpStatus) re.getStatusCode(); | |
| 213 | retVal = re.getBody(); | |
| 214 | ||
| 215 |
1
1. getAllSections : negated conditional → KILLED |
if (retVal.equals("null")) { |
| 216 | retVal = "{\"error\": \"Enroll code doesn't exist in that quarter.\"}"; | |
| 217 | } | |
| 218 | ||
| 219 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
| 220 |
1
1. getAllSections : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getAllSections → KILLED |
return retVal; |
| 221 | } | |
| 222 | ||
| 223 | public String getJSONbyQtrEnrollCd(String quarter, String enrollCd) throws Exception { | |
| 224 | ||
| 225 | HttpHeaders headers = new HttpHeaders(); | |
| 226 |
1
1. getJSONbyQtrEnrollCd : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
| 227 |
1
1. getJSONbyQtrEnrollCd : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 228 |
1
1. getJSONbyQtrEnrollCd : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
| 229 |
1
1. getJSONbyQtrEnrollCd : 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 = | |
| 234 | "https://api.ucsb.edu/academics/curriculums/v3/classsection/" + quarter + "/" + enrollCd; | |
| 235 | ||
| 236 | log.info("url=" + url); | |
| 237 | ||
| 238 | String retVal = ""; | |
| 239 | MediaType contentType = null; | |
| 240 | HttpStatus statusCode = null; | |
| 241 | ||
| 242 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
| 243 | contentType = re.getHeaders().getContentType(); | |
| 244 | statusCode = (HttpStatus) re.getStatusCode(); | |
| 245 | retVal = re.getBody(); | |
| 246 | ||
| 247 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
| 248 |
1
1. getJSONbyQtrEnrollCd : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getJSONbyQtrEnrollCd → KILLED |
return retVal; |
| 249 | } | |
| 250 | ||
| 251 | public String getFinalsInfo(String quarter, String enrollCd) throws Exception { | |
| 252 | HttpHeaders headers = new HttpHeaders(); | |
| 253 |
1
1. getFinalsInfo : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
| 254 |
1
1. getFinalsInfo : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "3.0"); |
| 255 |
1
1. getFinalsInfo : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
| 256 | ||
| 257 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 258 | ||
| 259 | String params = String.format("?quarter=%s&enrollCode=%s", quarter, enrollCd); | |
| 260 | String url = FINALS_ENDPOINT + params; | |
| 261 | ||
| 262 | log.info("url=" + url); | |
| 263 | ||
| 264 | String retVal; | |
| 265 | MediaType contentType; | |
| 266 | HttpStatus statusCode; | |
| 267 | ||
| 268 | ResponseEntity<String> re = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); | |
| 269 | contentType = re.getHeaders().getContentType(); | |
| 270 | statusCode = (HttpStatus) re.getStatusCode(); | |
| 271 | retVal = re.getBody(); | |
| 272 | ||
| 273 | log.info("json: {} contentType: {} statusCode: {}", retVal, contentType, statusCode); | |
| 274 |
1
1. getFinalsInfo : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getFinalsInfo → KILLED |
return retVal; |
| 275 | } | |
| 276 | ||
| 277 | public String getGeneralEducationInfo() throws Exception { | |
| 278 | HttpHeaders headers = new HttpHeaders(); | |
| 279 |
1
1. getGeneralEducationInfo : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); |
| 280 |
1
1. getGeneralEducationInfo : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 281 |
1
1. getGeneralEducationInfo : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-version", "1.0"); |
| 282 |
1
1. getGeneralEducationInfo : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
| 283 | ||
| 284 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 285 | ||
| 286 | log.info("Fetching GE areas from {}", GE_ENDPOINT); | |
| 287 | ResponseEntity<String> response = | |
| 288 | restTemplate.exchange(GE_ENDPOINT, HttpMethod.GET, entity, String.class); | |
| 289 | ||
| 290 | log.debug("GE areas JSON: {}", response.getBody()); | |
| 291 |
1
1. getGeneralEducationInfo : replaced return value with "" for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getGeneralEducationInfo → KILLED |
return response.getBody(); |
| 292 | } | |
| 293 | ||
| 294 | public List<String> getRequirementCodesByCollege(String collegeCode) throws Exception { | |
| 295 | String json = getGeneralEducationInfo(); | |
| 296 | ||
| 297 | List<GERequirement> allAreas = | |
| 298 | objectMapper.readValue(json, new TypeReference<List<GERequirement>>() {}); | |
| 299 | ||
| 300 |
1
1. getRequirementCodesByCollege : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBCurriculumService::getRequirementCodesByCollege → KILLED |
return allAreas.stream() |
| 301 |
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)) |
| 302 | .map(GERequirement::getRequirementCode) | |
| 303 | .distinct() | |
| 304 | .collect(Collectors.toList()); | |
| 305 | } | |
| 306 | } | |
Mutations | ||
| 62 |
1.1 |
|
| 63 |
1.1 |
|
| 64 |
1.1 |
|
| 65 |
1.1 |
|
| 75 |
1.1 |
|
| 96 |
1.1 |
|
| 104 |
1.1 |
|
| 113 |
1.1 |
|
| 119 |
1.1 |
|
| 120 |
1.1 |
|
| 121 |
1.1 |
|
| 122 |
1.1 |
|
| 139 |
1.1 |
|
| 149 |
1.1 |
|
| 150 |
1.1 |
|
| 151 |
1.1 |
|
| 152 |
1.1 |
|
| 174 |
1.1 |
|
| 179 |
1.1 |
|
| 190 |
1.1 |
|
| 191 |
1.1 |
|
| 192 |
1.1 |
|
| 193 |
1.1 |
|
| 215 |
1.1 |
|
| 220 |
1.1 |
|
| 226 |
1.1 |
|
| 227 |
1.1 |
|
| 228 |
1.1 |
|
| 229 |
1.1 |
|
| 248 |
1.1 |
|
| 253 |
1.1 |
|
| 254 |
1.1 |
|
| 255 |
1.1 |
|
| 274 |
1.1 |
|
| 279 |
1.1 |
|
| 280 |
1.1 |
|
| 281 |
1.1 |
|
| 282 |
1.1 |
|
| 291 |
1.1 |
|
| 300 |
1.1 |
|
| 301 |
1.1 2.2 |