FootballResultsMapStore.java

  1. package com.mojosoft.demo.cache;

  2. import com.hazelcast.map.MapStore;
  3. import com.mojosoft.demo.dto.FootballResultDTO;
  4. import com.mojosoft.demo.entity.FootballResult;
  5. import com.mojosoft.demo.mapper.MapperHelper;
  6. import com.mojosoft.demo.repository.FootballResultRepository;
  7. import com.mojosoft.demo.repository.FootballResultSaveOrUpdateHelper;
  8. import com.mojosoft.demo.service.FootballResultDbService;
  9. import jakarta.transaction.Transactional;
  10. import java.util.ArrayList;
  11. import java.util.Collection;
  12. import java.util.HashMap;
  13. import java.util.List;
  14. import java.util.Map;
  15. import java.util.Optional;
  16. import org.springframework.context.annotation.Lazy;
  17. import org.springframework.stereotype.Component;

  18. /**
  19.  * This class implements the {@link MapStore} interface for the {@link FootballResult} entity.
  20.  */
  21. @Component
  22. public class FootballResultsMapStore
  23.     implements MapStore<String, FootballResultDTO> {

  24.   private final FootballResultRepository footballResultRepository;
  25.   private final FootballResultDbService footballResultDbService;
  26.   private final MapperHelper mapperHelper;

  27.   private final FootballResultSaveOrUpdateHelper footballResultSaveOrUpdateHelper;

  28.   /**
  29.    * Constructor, used by Lazy inject the dependencies. so it occurs only when the bean
  30.    * is used and after hazelcast is started.
  31.    *
  32.    * @param footballResultSaveOrUpdateHelper The save or update helper
  33.    * @param footballResultRepository The football result repository
  34.    * @param mapperHelper The mapper helper
  35.    */
  36.   @SuppressWarnings("all")
  37.   public FootballResultsMapStore(
  38.       @Lazy FootballResultRepository footballResultRepository,
  39.       @Lazy FootballResultDbService footballResultDbService,
  40.       @Lazy FootballResultSaveOrUpdateHelper footballResultSaveOrUpdateHelper,
  41.       MapperHelper mapperHelper) {
  42.     this.footballResultRepository = footballResultRepository;
  43.     this.footballResultDbService = footballResultDbService;
  44.     this.footballResultSaveOrUpdateHelper = footballResultSaveOrUpdateHelper;
  45.     this.mapperHelper = mapperHelper;
  46.   }

  47.   @Override
  48.   public void store(String key, FootballResultDTO value) {
  49.     footballResultSaveOrUpdateHelper.save(MatchKey.extractKeyComponents(key), value);
  50.   }

  51.   @Override
  52.   public void storeAll(Map<String, FootballResultDTO> map) {
  53.     if (map == null || map.isEmpty()) {
  54.       return;
  55.     }
  56.     footballResultSaveOrUpdateHelper.saveAll(new ArrayList<>(map.values()));
  57.   }

  58.   @Override
  59.   @Transactional
  60.   public void delete(String key) {
  61.     MatchKey matchKey = MatchKey.extractKeyComponents(key);
  62.     footballResultRepository.deleteBySeasonAndHomeTeamAndAwayTeam(
  63.         matchKey.season(), matchKey.homeTeam(), matchKey.awayTeam());
  64.   }

  65.   @Override
  66.   public void deleteAll(Collection<String> keys) {
  67.     List<MatchKey> matchKeys = convertToMatchKey(keys);
  68.     footballResultDbService.deleteAllByMatchKeys(matchKeys);
  69.   }

  70.   @Override
  71.   public FootballResultDTO load(String key) {
  72.     MatchKey matchKey = MatchKey.extractKeyComponents(key);
  73.     Optional<FootballResult> footballResult = footballResultRepository
  74.         .findBySeasonAndHomeTeamAndAwayTeam(
  75.             matchKey.season(), matchKey.homeTeam(), matchKey.awayTeam());
  76.     return mapperHelper.footballResultToDto(footballResult.orElse(null));
  77.   }

  78.   @Override
  79.   public Map<String, FootballResultDTO> loadAll(Collection<String> keys) {
  80.     Map<String, FootballResultDTO> results = new HashMap<>();
  81.     keys.forEach(key -> {
  82.       MatchKey matchKey = MatchKey.extractKeyComponents(key);
  83.       Optional<FootballResult> footballResult =
  84.           footballResultRepository.findBySeasonAndHomeTeamAndAwayTeam(
  85.               matchKey.season(), matchKey.homeTeam(), matchKey.awayTeam());

  86.       if (footballResult.isPresent()) {
  87.         results.put(key, mapperHelper.footballResultToDto(footballResult.get()));
  88.       } else {
  89.         results.put(key, null);
  90.       }
  91.     });
  92.     return results;
  93.   }

  94.   @Override
  95.   public Iterable<String> loadAllKeys() {
  96.     return footballResultRepository.findAllMatchKeys();
  97.   }

  98.   /**
  99.    * Converts a collection of keys to a list of {@link MatchKey} objects.
  100.    *
  101.    * @param keys the collection of keys
  102.    * @return a list of {@link MatchKey} objects
  103.    */
  104.   private List<MatchKey> convertToMatchKey(Collection<String> keys) {
  105.     return keys.stream().map(MatchKey::extractKeyComponents).toList();
  106.   }
  107. }