Class TourGuideService

java.lang.Object
com.openclassrooms.tourguide.service.TourGuideService

@Service public class TourGuideService extends Object
Main service that manages the TourGuide app's features: - Tracks user locations - Assigns reward points - Provides recommendations for nearby attractions - Generates personalized travel offers
  • Field Details

    • executor

      private final ExecutorService executor
    • allUsers

      private List<User> allUsers
    • internalUserMap

      private final Map<String,User> internalUserMap
    • gpsUtil

      private final gpsUtil.GpsUtil gpsUtil
    • rewardsService

      private final RewardsService rewardsService
    • tripPricer

      private final tripPricer.TripPricer tripPricer
    • tracker

      public final Tracker tracker
    • startTracker

      private final boolean startTracker
    • locationCache

      private final com.github.benmanes.caffeine.cache.Cache<UUID,gpsUtil.location.VisitedLocation> locationCache
    • TRIP_PRICER_API_KEY

      private static final String TRIP_PRICER_API_KEY
      See Also:
    • testMode

      private final boolean testMode
      See Also:
  • Constructor Details

    • TourGuideService

      public TourGuideService(gpsUtil.GpsUtil gpsUtil, RewardsService rewardsService, ExecutorService executorService, @Value("${tourguide.startTracker:true}") boolean startTracker)
      Builder of the main TourGuide service. Initializes the necessary components
      Parameters:
      gpsUtil - User geolocation service
      rewardsService - Rewards Management Service
      executorService - Thread pool for asynchronous processing
      startTracker - Indicates whether to enable automatic user tracking
  • Method Details

    • setAllUsers

      public void setAllUsers(List<User> allUsers)
      Defines a new user list for the service. This method replaces the current users with those provided in parameters The list must not be null or empty, otherwise an error is thrown.
      Parameters:
      allUsers - Liste des utilisateurs à enregistrer
      Throws:
      IllegalArgumentException - if the list is null
      IllegalStateException - if the list is empty
    • getUserRewards

      public List<UserReward> getUserRewards(User user)
      Returns the list of rewards associated with a given user.
      Parameters:
      user - User for whom you want to get the rewards
      Returns:
      List of reward points earned by this user
    • getUserLocation

      public gpsUtil.location.VisitedLocation getUserLocation(User user)
      Returns the user's current location. If the user already has a saved location, it is returned. Otherwise, a new location is obtained in real time.
      Parameters:
      user - Concerned user
      Returns:
      Last known position or updated position
    • trackUserLocation

      public CompletableFuture<gpsUtil.location.VisitedLocation> trackUserLocation(User user)
      Starts location tracking for a given user. If a recent position is already available in the cache, it is used directly. Otherwise, a new position is retrieved from the GPS service, added to the user's history, and rewards are calculated in the background.
      Parameters:
      user - The user to locate
      Returns:
      A CompletableFuture containing the user's new (or old) position
    • trackAllUsersLocations

      public void trackAllUsersLocations(List<User> users)
      Starts location tracking for all provided users. Each user is located in parallel via asynchronous calls, and execution waits for all operations to complete before continuing.
      Parameters:
      users - List of users to follow
    • getNearByAttractions

      public List<gpsUtil.location.Attraction> getNearByAttractions(gpsUtil.location.VisitedLocation visitedLocation)
      Returns the 5 closest attractions to the given position. This method sorts all known attractions by distance from the user's current location, then returns the top 5.
      Parameters:
      visitedLocation - Current position of the user
      Returns:
      List of the 5 nearest attractions
    • getUser

      public User getUser(String userName)
      Search for a user by username. If the user exists in the internal list, it is returned. Otherwise, an error is thrown.
      Parameters:
      userName - Username to search for
      Returns:
      The user corresponding to the given name
      Throws:
      IllegalArgumentException - if no user is found
    • getAllUsers

      public List<User> getAllUsers()
      Returns the full list of registered users. This method returns a new list containing all users currently stored in internal memory.
      Returns:
      List of all known users
    • addUser

      public void addUser(User user)
      Adds a user to the internal list if it does not already exist. If a user with the same name is already registered, they will not be replaced.
      Parameters:
      user - The user to add
    • getTripDeals

      public List<tripPricer.Provider> getTripDeals(User user)
      Generate a list of personalized travel offers for a user. The number of reward points is taken into account to calculate offers tailored to the user's profile (adults, children, duration). The list is then saved to the user's account.
      Parameters:
      user - The user for whom we want to get offers
      Returns:
      List of suppliers with their travel offers
    • initializeInternalUsers

      private void initializeInternalUsers()
      Initializes a list of internal test users. The number of users to be created is determined by a configuration value. Each user receives a username, a unique name, and a history of simulated locations. These users are added to the service's internal map.
    • generateUserLocationHistory

      private void generateUserLocationHistory(User user)
      Generates a random location history for a user. This method adds three simulated locations with randomly generated coordinates and dates to create a movement history.
      Parameters:
      user - The user to add the positions to
    • generateRandomLongitude

      private double generateRandomLongitude()
      Generates a random longitude between -180 and 180 degrees. This method is used to simulate geographic coordinates.
      Returns:
      A random longitude value
    • generateRandomLatitude

      private double generateRandomLatitude()
      Generates a random latitude between -85.05 and 85.05 degrees.
      Returns:
      A random latitude value
    • getRandomTime

      private Date getRandomTime()
      Creates a random date within the last 30 days.
      Returns:
      A recent random date
    • shutdown

      public void shutdown()
      Stops user tracking if it is active.