LocationRepository.java

  1. package no.nav.data.team.location;

  2. import no.nav.data.team.location.domain.Location;
  3. import no.nav.data.team.location.domain.LocationType;
  4. import org.springframework.stereotype.Component;

  5. import java.util.LinkedList;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Optional;
  9. import java.util.concurrent.ConcurrentHashMap;
  10. import java.util.stream.Collectors;

  11. @Component
  12. public class LocationRepository {

  13.     private static final List<Location> locationHierarchy = new LinkedList<>();
  14.     private static final Map<String, Location> locationByCode = new ConcurrentHashMap<>(10);

  15.     public LocationRepository(){
  16.         locationHierarchy.add(buildFAEN());

  17.         locationHierarchy.forEach(h -> {
  18.             locationByCode.putAll(h.flatMap());
  19.         });
  20.     }

  21.     public static Location getLocationFor(String locationCode){
  22.         return locationByCode.get(locationCode);
  23.     }

  24.     public Optional<Location> getLocationByCode(String code){
  25.         return Optional.ofNullable(code != null ? locationByCode.get(code) : null);
  26.     }

  27.     public Map<String, Location> getLocationsByType(LocationType locationType){
  28.         return locationByCode.entrySet().stream()
  29.                 .filter(e -> e.getValue().getType().equals(locationType) || locationType == null)
  30.                 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
  31.     }

  32.     public List<Location> getLocationHierarchy(){
  33.         return locationHierarchy;
  34.     }

  35.     public Map<String, Location> getLocation(){
  36.         return locationByCode;
  37.     }

  38.     private Location buildFAEN(){
  39.         return new Location("FA1", "Fyrstikkalléen 1", LocationType.BUILDING)
  40.                 .newSubLocation("BA", "Bygg A", LocationType.SECTION)
  41.                     .newSubLocation("E1", "Etasje 1", LocationType.FLOOR).build()
  42.                     .newSubLocation("E2", "Etasje 2", LocationType.FLOOR).build()
  43.                     .newSubLocation("E3", "Etasje 3", LocationType.FLOOR).build()
  44.                     .newSubLocation("E4", "Etasje 4", LocationType.FLOOR).build()
  45.                     .newSubLocation("E5", "Etasje 5", LocationType.FLOOR).build()
  46.                     .newSubLocation("E6", "Etasje 6", LocationType.FLOOR).build()
  47.                     .newSubLocation("E7", "Etasje 7", LocationType.FLOOR).build()
  48.                     .newSubLocation("E8", "Etasje 8", LocationType.FLOOR).build()
  49.                 .build()
  50.                 .newSubLocation("BB", "Bygg B", LocationType.SECTION)
  51.                     .newSubLocation("E1", "Etasje 1", LocationType.FLOOR).build()
  52.                     .newSubLocation("E2", "Etasje 2", LocationType.FLOOR).build()
  53.                     .newSubLocation("E3", "Etasje 3", LocationType.FLOOR).build()
  54.                     .newSubLocation("E4", "Etasje 4", LocationType.FLOOR).build()
  55.                     .newSubLocation("E5", "Etasje 5", LocationType.FLOOR).build()
  56.                     .newSubLocation("E6", "Etasje 6", LocationType.FLOOR).build()
  57.                     .newSubLocation("E7", "Etasje 7", LocationType.FLOOR).build()
  58.                     .newSubLocation("E8", "Etasje 8", LocationType.FLOOR).build()
  59.                 .build()
  60.                 .newSubLocation("BC", "Bygg C", LocationType.SECTION)
  61.                     .newSubLocation("E1", "Etasje 1", LocationType.FLOOR).build()
  62.                     .newSubLocation("E2", "Etasje 2", LocationType.FLOOR).build()
  63.                     .newSubLocation("E3", "Etasje 3", LocationType.FLOOR).build()
  64.                     .newSubLocation("E4", "Etasje 4", LocationType.FLOOR).build()
  65.                     .newSubLocation("E5", "Etasje 5", LocationType.FLOOR).build()
  66.                     .newSubLocation("E6", "Etasje 6", LocationType.FLOOR).build()
  67.                     .newSubLocation("E7", "Etasje 7", LocationType.FLOOR).build()
  68.                     .newSubLocation("E8", "Etasje 8", LocationType.FLOOR).build()
  69.                 .build();
  70.     }

  71.     public List<Location> getAll() {
  72.         return locationByCode.values().stream().toList();
  73.     }
  74. }