CoursePage.java

  1. package edu.ucsb.cs156.courses.documents;

  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import com.fasterxml.jackson.databind.DeserializationFeature;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import lombok.Data;
  8. import lombok.NoArgsConstructor;
  9. import lombok.extern.slf4j.Slf4j;

  10. @Data
  11. @NoArgsConstructor
  12. @Slf4j
  13. public class CoursePage {
  14.   private int pageNumber;
  15.   private int pageSize;
  16.   private int total;
  17.   private List<Course> classes;

  18.   /**
  19.    * Create a CoursePage object from json representation
  20.    *
  21.    * @param json String of json returned by API endpoint {@code /classes/search}
  22.    * @return a new CoursePage object
  23.    * @see <a href=
  24.    *     "https://developer.ucsb.edu/content/academic-curriculums">https://developer.ucsb.edu/content/academic-curriculums</a>
  25.    */
  26.   public static CoursePage fromJSON(String json) {
  27.     try {
  28.       ObjectMapper objectMapper = new ObjectMapper();
  29.       objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

  30.       CoursePage coursePage = objectMapper.readValue(json, CoursePage.class);
  31.       return coursePage;
  32.     } catch (JsonProcessingException jpe) {
  33.       log.error("JsonProcessingException:" + jpe);
  34.       return null;
  35.     }
  36.   }

  37.   /**
  38.    * Create a List of ConvertedSections from json representation
  39.    *
  40.    * @return a list of converted sections
  41.    */
  42.   public List<ConvertedSection> convertedSections() {

  43.     List<ConvertedSection> result = new ArrayList<ConvertedSection>();

  44.     for (Course c : this.getClasses()) {
  45.       for (Section section : c.getClassSections()) {
  46.         int lectureNum = Integer.parseInt(section.getSection()) / 100;

  47.         CourseInfo courseInfo =
  48.             CourseInfo.builder()
  49.                 .quarter(c.getQuarter())
  50.                 .courseId(c.getCourseId() + "-" + Integer.toString(lectureNum))
  51.                 .title(c.getTitle())
  52.                 .description(c.getDescription())
  53.                 .generalEducation(c.getGeneralEducation())
  54.                 .build();
  55.         ConvertedSection cs =
  56.             ConvertedSection.builder().courseInfo(courseInfo).section(section).build();
  57.         result.add(cs);
  58.       }
  59.     }
  60.     return result;
  61.   }
  62. }