UpdateCourseDataJob.java

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

  2. import edu.ucsb.cs156.courses.collections.ConvertedSectionCollection;
  3. import edu.ucsb.cs156.courses.collections.UpdateCollection;
  4. import edu.ucsb.cs156.courses.documents.ConvertedSection;
  5. import edu.ucsb.cs156.courses.documents.Update;
  6. import edu.ucsb.cs156.courses.models.Quarter;
  7. import edu.ucsb.cs156.courses.services.IsStaleService;
  8. import edu.ucsb.cs156.courses.services.UCSBCurriculumService;
  9. import edu.ucsb.cs156.courses.services.jobs.JobContext;
  10. import edu.ucsb.cs156.courses.services.jobs.JobContextConsumer;
  11. import java.util.List;
  12. import java.util.Optional;
  13. import lombok.AllArgsConstructor;
  14. import lombok.Builder;
  15. import lombok.Getter;
  16. import lombok.extern.slf4j.Slf4j;

  17. @Builder
  18. @Getter
  19. @AllArgsConstructor
  20. @Slf4j
  21. public class UpdateCourseDataJob implements JobContextConsumer {
  22.   private String start_quarterYYYYQ;
  23.   private String end_quarterYYYYQ;
  24.   private List<String> subjects;
  25.   private UCSBCurriculumService ucsbCurriculumService;
  26.   private ConvertedSectionCollection convertedSectionCollection;
  27.   private UpdateCollection updateCollection;
  28.   private IsStaleService isStaleService;
  29.   private boolean ifStale;

  30.   @Override
  31.   public void accept(JobContext ctx) throws Exception {
  32.     List<Quarter> quarters = Quarter.quarterList(start_quarterYYYYQ, end_quarterYYYYQ);
  33.     for (Quarter quarter : quarters) {
  34.       String quarterYYYYQ = quarter.getYYYYQ();
  35.       for (String subjectArea : subjects) {
  36.         boolean isStale = isStaleService.isStale(subjectArea, quarterYYYYQ);
  37.         if (ifStale) {
  38.           if (!isStale) {
  39.             continue;
  40.           }
  41.         }
  42.         updateCourses(ctx, quarterYYYYQ, subjectArea);
  43.       }
  44.     }
  45.   }

  46.   public Update updateUpdatesCollection(
  47.       String quarterYYYYQ, String subjectArea, int saved, int updated, int errors) {
  48.     Update update = new Update(null, subjectArea, quarterYYYYQ, saved, updated, errors, null);
  49.     Update savedUpdate = updateCollection.save(update);
  50.     return savedUpdate;
  51.   }

  52.   public void updateCourses(JobContext ctx, String quarterYYYYQ, String subjectArea)
  53.       throws Exception {
  54.     ctx.log("Updating courses for [" + subjectArea + " " + quarterYYYYQ + "]");

  55.     List<ConvertedSection> convertedSections =
  56.         ucsbCurriculumService.getConvertedSections(subjectArea, quarterYYYYQ, "A");

  57.     int newSections = 0;
  58.     int updatedSections = 0;
  59.     int errors = 0;

  60.     for (ConvertedSection section : convertedSections) {
  61.       try {
  62.         String quarter = section.getCourseInfo().getQuarter();
  63.         String enrollCode = section.getSection().getEnrollCode();
  64.         Optional<ConvertedSection> optionalSection =
  65.             convertedSectionCollection.findOneByQuarterAndEnrollCode(quarter, enrollCode);
  66.         if (optionalSection.isPresent()) {
  67.           ConvertedSection existingSection = optionalSection.get();
  68.           existingSection.setCourseInfo(section.getCourseInfo());
  69.           existingSection.setSection(section.getSection());
  70.           convertedSectionCollection.save(existingSection);
  71.           updatedSections++;
  72.         } else {
  73.           convertedSectionCollection.save(section);
  74.           newSections++;
  75.         }
  76.       } catch (Exception e) {
  77.         errors++;
  78.       }
  79.     }

  80.     Update savedUpdate =
  81.         updateUpdatesCollection(quarterYYYYQ, subjectArea, newSections, updatedSections, errors);

  82.     ctx.log(
  83.         String.format(
  84.             "%d new sections saved, %d sections updated, %d errors, last update: %s",
  85.             newSections, updatedSections, errors, savedUpdate.getLastUpdate()));

  86.     ctx.log("Saved update: " + savedUpdate);
  87.   }
  88. }