| 1 | package edu.ucsb.cs156.dining.startup; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 4 | import edu.ucsb.cs156.dining.entities.Admin; | |
| 5 | import edu.ucsb.cs156.dining.jobs.LoadMenuItemsJob; | |
| 6 | import edu.ucsb.cs156.dining.repositories.AdminRepository; | |
| 7 | import edu.ucsb.cs156.dining.services.DiningCommonsService; | |
| 8 | import edu.ucsb.cs156.dining.services.UCSBDiningMenuItemsService; | |
| 9 | import edu.ucsb.cs156.dining.services.UCSBDiningMenuService; | |
| 10 | import edu.ucsb.cs156.jobs.services.JobService; | |
| 11 | import java.time.Clock; | |
| 12 | import java.time.LocalDate; | |
| 13 | import java.time.ZoneId; | |
| 14 | import java.util.List; | |
| 15 | import lombok.extern.slf4j.Slf4j; | |
| 16 | import org.springframework.beans.factory.annotation.Autowired; | |
| 17 | import org.springframework.beans.factory.annotation.Value; | |
| 18 | import org.springframework.boot.context.event.ApplicationReadyEvent; | |
| 19 | import org.springframework.context.event.EventListener; | |
| 20 | import org.springframework.stereotype.Component; | |
| 21 | ||
| 22 | /** This class contains a method that is called once at application startup time. */ | |
| 23 | @Slf4j | |
| 24 | @Component | |
| 25 | public class DiningStartup { | |
| 26 | ||
| 27 | /** Default value of app.ucsb.api.consumer_key when UCSB_API_KEY is not set. */ | |
| 28 | static final String API_KEY_PLACEHOLDER = "see-instructions-in-readme"; | |
| 29 | ||
| 30 | @Value("#{'${app.admin.emails}'.split(',')}") | |
| 31 | List<String> adminEmails; | |
| 32 | ||
| 33 | @Value("${app.ucsb.api.consumer_key}") | |
| 34 | String apiKey; | |
| 35 | ||
| 36 | @Autowired AdminRepository adminRepository; | |
| 37 | @Autowired JobService jobService; | |
| 38 | @Autowired DiningCommonsService diningCommonsService; | |
| 39 | @Autowired UCSBDiningMenuService menuService; | |
| 40 | @Autowired UCSBDiningMenuItemsService menuItemsService; | |
| 41 | @Autowired ObjectMapper objectMapper; | |
| 42 | ||
| 43 | Clock clock = Clock.system(ZoneId.of("America/Los_Angeles")); | |
| 44 | ||
| 45 | /** | |
| 46 | * Called once at application startup time. Loads all emails in ADMIN_EMAILS into the Admin table, | |
| 47 | * then launches a background job to load the current month's MenuItems. | |
| 48 | */ | |
| 49 | @EventListener(ApplicationReadyEvent.class) | |
| 50 | public void alwaysRunOnStartup() { | |
| 51 | log.info("alwaysRunOnStartup called"); | |
| 52 | ||
| 53 |
1
1. alwaysRunOnStartup : removed call to java/util/List::forEach → KILLED |
adminEmails.forEach( |
| 54 | (email) -> { | |
| 55 | try { | |
| 56 |
1
1. lambda$alwaysRunOnStartup$0 : negated conditional → KILLED |
if (!adminRepository.existsByEmail(email)) { |
| 57 | Admin admin = new Admin(email); | |
| 58 | adminRepository.save(admin); | |
| 59 | } | |
| 60 | } catch (Exception e) { | |
| 61 | log.error("Error saving admin email {}: {}", email, e.getMessage()); | |
| 62 | } | |
| 63 | }); | |
| 64 | ||
| 65 |
1
1. alwaysRunOnStartup : removed call to edu/ucsb/cs156/dining/startup/DiningStartup::loadCurrentMonthMenuItems → KILLED |
loadCurrentMonthMenuItems(); |
| 66 | } | |
| 67 | ||
| 68 | /** | |
| 69 | * Launches a LoadMenuItemsJob for every day of the current month, unless the UCSB API key is not | |
| 70 | * configured (in which case every request would fail, so we don't send any). | |
| 71 | */ | |
| 72 | void loadCurrentMonthMenuItems() { | |
| 73 |
3
1. loadCurrentMonthMenuItems : negated conditional → KILLED 2. loadCurrentMonthMenuItems : negated conditional → KILLED 3. loadCurrentMonthMenuItems : negated conditional → KILLED |
if (apiKey == null || apiKey.isBlank() || apiKey.equals(API_KEY_PLACEHOLDER)) { |
| 74 | log.warn("UCSB_API_KEY is not set; skipping loading of current month's menu items"); | |
| 75 | return; | |
| 76 | } | |
| 77 | ||
| 78 | LocalDate today = LocalDate.now(clock); | |
| 79 | LocalDate start = today.withDayOfMonth(1); | |
| 80 | LocalDate end = today.withDayOfMonth(today.lengthOfMonth()); | |
| 81 | ||
| 82 | LoadMenuItemsJob job = | |
| 83 | LoadMenuItemsJob.builder() | |
| 84 | .diningCommonsService(diningCommonsService) | |
| 85 | .menuService(menuService) | |
| 86 | .menuItemsService(menuItemsService) | |
| 87 | .objectMapper(objectMapper) | |
| 88 | .startDate(start) | |
| 89 | .endDate(end) | |
| 90 | .build(); | |
| 91 | ||
| 92 | try { | |
| 93 | jobService.runAsJob(job); | |
| 94 | log.info("Launched job to load menu items from {} to {}", start, end); | |
| 95 | } catch (Exception e) { | |
| 96 | log.error("Error launching job to load menu items: {}", e.getMessage()); | |
| 97 | } | |
| 98 | } | |
| 99 | } | |
Mutations | ||
| 53 |
1.1 |
|
| 56 |
1.1 |
|
| 65 |
1.1 |
|
| 73 |
1.1 2.2 3.3 |