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