| 1 | package edu.ucsb.cs156.dining.startup; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.dining.entities.Admin; | |
| 4 | import edu.ucsb.cs156.dining.jobs.LoadMonthMenuItemsJob; | |
| 5 | import edu.ucsb.cs156.dining.repositories.AdminRepository; | |
| 6 | import edu.ucsb.cs156.dining.services.DiningCommonsService; | |
| 7 | import edu.ucsb.cs156.dining.services.UCSBDiningMenuItemsService; | |
| 8 | import edu.ucsb.cs156.dining.services.UCSBDiningMenuService; | |
| 9 | import edu.ucsb.cs156.jobs.services.JobService; | |
| 10 | import java.time.YearMonth; | |
| 11 | import java.util.List; | |
| 12 | import lombok.extern.slf4j.Slf4j; | |
| 13 | import org.springframework.beans.factory.annotation.Autowired; | |
| 14 | import org.springframework.beans.factory.annotation.Value; | |
| 15 | import org.springframework.boot.context.event.ApplicationReadyEvent; | |
| 16 | import org.springframework.context.event.EventListener; | |
| 17 | import org.springframework.stereotype.Component; | |
| 18 | ||
| 19 | /** This class contains a method that is called once at application startup time. */ | |
| 20 | @Slf4j | |
| 21 | @Component | |
| 22 | public class DiningStartup { | |
| 23 | ||
| 24 | @Value("#{'${app.admin.emails}'.split(',')}") | |
| 25 | List<String> adminEmails; | |
| 26 | ||
| 27 | @Value("${app.ucsb.api.consumer_key}") | |
| 28 | String apiKey; | |
| 29 | ||
| 30 | @Value("${app.startup.loadMonthMenuItems:true}") | |
| 31 | boolean loadMonthMenuItems; | |
| 32 | ||
| 33 | @Autowired AdminRepository adminRepository; | |
| 34 | @Autowired JobService jobService; | |
| 35 | @Autowired DiningCommonsService diningCommonsService; | |
| 36 | @Autowired UCSBDiningMenuService menuService; | |
| 37 | @Autowired UCSBDiningMenuItemsService menuItemsService; | |
| 38 | ||
| 39 | /** | |
| 40 | * Called once at application startup time. Loads all emails in ADMIN_EMAILS into the Admin table, | |
| 41 | * then starts a background job that downloads the current month's menu items. | |
| 42 | */ | |
| 43 | @EventListener(ApplicationReadyEvent.class) | |
| 44 | public void alwaysRunOnStartup() { | |
| 45 | log.info("alwaysRunOnStartup called"); | |
| 46 | ||
| 47 |
1
1. alwaysRunOnStartup : removed call to java/util/List::forEach → KILLED |
adminEmails.forEach( |
| 48 | (email) -> { | |
| 49 | try { | |
| 50 |
1
1. lambda$alwaysRunOnStartup$0 : negated conditional → KILLED |
if (!adminRepository.existsByEmail(email)) { |
| 51 | Admin admin = new Admin(email); | |
| 52 | adminRepository.save(admin); | |
| 53 | } | |
| 54 | } catch (Exception e) { | |
| 55 | log.error("Error saving admin email {}: {}", email, e.getMessage()); | |
| 56 | } | |
| 57 | }); | |
| 58 |
1
1. alwaysRunOnStartup : negated conditional → KILLED |
if (loadMonthMenuItems) { |
| 59 | try { | |
| 60 | jobService.runAsJob( | |
| 61 | LoadMonthMenuItemsJob.builder() | |
| 62 | .diningCommonsService(diningCommonsService) | |
| 63 | .menuService(menuService) | |
| 64 | .menuItemsService(menuItemsService) | |
| 65 | .apiKey(apiKey) | |
| 66 | .month(YearMonth.now()) | |
| 67 | .sleeper(Thread::sleep) | |
| 68 | .build()); | |
| 69 | } catch (Exception e) { | |
| 70 | log.error("Failed to start menu item download job: {}", e.getMessage()); | |
| 71 | } | |
| 72 | } else { | |
| 73 | log.info("Skipping menu item download job (app.startup.loadMonthMenuItems=false)"); | |
| 74 | } | |
| 75 | } | |
| 76 | } | |
Mutations | ||
| 47 |
1.1 |
|
| 50 |
1.1 |
|
| 58 |
1.1 |