All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javastrava.api.v3.service.impl.ClubServiceImpl Maven / Gradle / Ivy

There is a newer version: 2.0.0-alpha
Show newest version
/**
 *
 */
package javastrava.api.v3.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import javastrava.api.v3.auth.model.Token;
import javastrava.api.v3.model.StravaActivity;
import javastrava.api.v3.model.StravaAthlete;
import javastrava.api.v3.model.StravaClub;
import javastrava.api.v3.model.StravaClubAnnouncement;
import javastrava.api.v3.model.StravaClubEvent;
import javastrava.api.v3.model.StravaClubMembershipResponse;
import javastrava.api.v3.model.reference.StravaResourceState;
import javastrava.api.v3.service.ClubService;
import javastrava.api.v3.service.exception.NotFoundException;
import javastrava.api.v3.service.exception.UnauthorizedException;
import javastrava.cache.StravaCache;
import javastrava.cache.impl.StravaCacheImpl;
import javastrava.util.Paging;
import javastrava.util.PagingHandler;
import javastrava.util.PrivacyUtils;

/**
 * 

* Implementation of {@link ClubService} *

* * @author Dan Shannon * */ public class ClubServiceImpl extends StravaServiceImpl implements ClubService { /** * Generates a response indicating that the club membership activity (either a {@link ClubService#joinClub(Integer)} or a {@link ClubService#leaveClub(Integer)}) failed * * @return The response */ private static StravaClubMembershipResponse failedClubMembershipResponse() { final StravaClubMembershipResponse response = new StravaClubMembershipResponse(); response.setActive(Boolean.FALSE); response.setSuccess(Boolean.FALSE); return response; } /** *

* Returns an instance of {@link ClubService club services} *

* *

* Instances are cached so that if 2 requests are made for the same token, * the same instance is returned *

* * @param token * The Strava access token to be used in requests to the Strava * API * @return An instance of the club services * @throws UnauthorizedException * If the token used to create the service is invalid */ public static ClubService instance(final Token token) { // Get the service from the token's cache ClubService service = token.getService(ClubService.class); // If it's not already there, create a new one and put it in the token if (service == null) { service = new ClubServiceImpl(token); token.addService(ClubService.class, service); } return service; } /** * Cached club instances */ private final StravaCache clubCache; /** * Private constructor requires a valid access token; see {@link #instance(Token)} * @param token A valid token from the Strava OAuth process */ private ClubServiceImpl(final Token token) { super(token); this.clubCache = new StravaCacheImpl(StravaClub.class, token); } /** * @see javastrava.api.v3.service.StravaService#clearCache() */ @Override public void clearCache() { this.clubCache.removeAll(); } /** * @see javastrava.api.v3.service.ClubService#getClub(java.lang.Integer) */ @Override public StravaClub getClub(final Integer id) { // Attempt to get the club from the cache StravaClub club = this.clubCache.get(id); if ((club != null) && (club.getResourceState() != StravaResourceState.META)) { return club; } // If it wasn't in cache, get it from Strava try { club = this.api.getClub(id); } catch (final NotFoundException e) { return null; } catch (final UnauthorizedException e) { club = PrivacyUtils.privateClubRepresentation(id); } // Put it in the cache and return it this.clubCache.put(club); return club; } /** * @see javastrava.api.v3.service.ClubService#getClubAsync(java.lang.Integer) */ @Override public CompletableFuture getClubAsync(final Integer clubId) { return StravaServiceImpl.future(() -> { return getClub(clubId); }); } /** * @see javastrava.api.v3.service.ClubService#joinClub(java.lang.Integer) */ @Override public StravaClubMembershipResponse joinClub(final Integer id) { try { return this.api.joinClub(id); } catch (final NotFoundException e) { return failedClubMembershipResponse(); } catch (final UnauthorizedException e) { if (accessTokenIsValid()) { return failedClubMembershipResponse(); } throw e; } } /** * @see javastrava.api.v3.service.ClubService#joinClubAsync(java.lang.Integer) */ @Override public CompletableFuture joinClubAsync(final Integer clubId) { return StravaServiceImpl.future(() -> { return joinClub(clubId); }); } /** * @see javastrava.api.v3.service.ClubService#leaveClub(java.lang.Integer) */ @Override public StravaClubMembershipResponse leaveClub(final Integer id) { try { return this.api.leaveClub(id); } catch (final UnauthorizedException e) { if (accessTokenIsValid()) { return failedClubMembershipResponse(); } throw e; } catch (final NotFoundException e) { return failedClubMembershipResponse(); } } /** * @see javastrava.api.v3.service.ClubService#leaveClubAsync(java.lang.Integer) */ @Override public CompletableFuture leaveClubAsync(final Integer clubId) { return StravaServiceImpl.future(() -> { return leaveClub(clubId); }); } /** * @see javastrava.api.v3.service.ClubService#listAllClubMembers(java.lang.Integer) */ @Override public List listAllClubMembers(final Integer clubId) { return PagingHandler.handleListAll(thisPage -> listClubMembers(clubId, thisPage)); } /** * @see javastrava.api.v3.service.ClubService#listAllClubMembersAsync(java.lang.Integer) */ @Override public CompletableFuture> listAllClubMembersAsync(final Integer clubId) { return StravaServiceImpl.future(() -> { return listAllClubMembers(clubId); }); } /** * @see javastrava.api.v3.service.ClubService#listAllRecentClubActivities(java.lang.Integer) */ @Override public List listAllRecentClubActivities(final Integer clubId) { return PagingHandler.handleListAll(thisPage -> listRecentClubActivities(clubId, thisPage)); } /** * @see javastrava.api.v3.service.ClubService#listAllRecentClubActivitiesAsync(java.lang.Integer) */ @Override public CompletableFuture> listAllRecentClubActivitiesAsync(final Integer clubId) { return StravaServiceImpl.future(() -> { return listAllRecentClubActivities(clubId); }); } /** * @see javastrava.api.v3.service.ClubService#listAuthenticatedAthleteClubs() */ @Override public List listAuthenticatedAthleteClubs() { return Arrays.asList(this.api.listAuthenticatedAthleteClubs()); } /** * @see javastrava.api.v3.service.ClubService#listAuthenticatedAthleteClubsAsync() */ @Override public CompletableFuture> listAuthenticatedAthleteClubsAsync() { return StravaServiceImpl.future(() -> { return listAuthenticatedAthleteClubs(); }); } /** * @see javastrava.api.v3.service.ClubService#listClubAnnouncements(java.lang.Integer) */ @Override public List listClubAnnouncements(final Integer clubId) { List announcements; try { announcements = Arrays.asList(this.api.listClubAnnouncements(clubId)); } catch (final NotFoundException e) { return null; } catch (final UnauthorizedException e) { return new ArrayList(); } return announcements; } /** * @see javastrava.api.v3.service.ClubService#listClubAnnouncementsAsync(java.lang.Integer) */ @Override public CompletableFuture> listClubAnnouncementsAsync(final Integer clubId) { return StravaServiceImpl.future(() -> { return listClubAnnouncements(clubId); }); } /** * @see javastrava.api.v3.service.ClubService#listClubGroupEvents(java.lang.Integer) */ @Override public List listClubGroupEvents(final Integer clubId) { return Arrays.asList(this.api.listClubGroupEvents(clubId)); } /** * @see javastrava.api.v3.service.ClubService#listClubGroupEventsAsync(java.lang.Integer) */ @Override public CompletableFuture> listClubGroupEventsAsync(final Integer clubId) { return StravaServiceImpl.future(() -> { return Arrays.asList(this.api.listClubGroupEvents(clubId)); }); } /** * @see javastrava.api.v3.service.ClubService#listClubMembers(java.lang.Integer) */ @Override public List listClubMembers(final Integer id) { return listClubMembers(id, null); } /** * @see javastrava.api.v3.service.ClubService#listClubMembers(Integer, * Paging) */ @Override public List listClubMembers(final Integer id, final Paging pagingInstruction) { return PagingHandler.handlePaging( pagingInstruction, thisPage -> Arrays.asList(this.api.listClubMembers(id, thisPage.getPage(), thisPage.getPageSize()))); } /** * @see javastrava.api.v3.service.ClubService#listClubMembersAsync(java.lang.Integer) */ @Override public CompletableFuture> listClubMembersAsync(final Integer clubId) { return StravaServiceImpl.future(() -> { return listClubMembers(clubId); }); } /** * @see javastrava.api.v3.service.ClubService#listClubMembersAsync(java.lang.Integer, javastrava.util.Paging) */ @Override public CompletableFuture> listClubMembersAsync(final Integer clubId, final Paging pagingInstruction) { return StravaServiceImpl.future(() -> { return listClubMembers(clubId, pagingInstruction); }); } /** * @see javastrava.api.v3.service.ClubService#listRecentClubActivities(java.lang.Integer) */ @Override public List listRecentClubActivities(final Integer id) { List activities = listRecentClubActivities(id, null); // Strava API returns NULL instead of an empty array if (activities == null) { activities = new ArrayList(); } return activities; } /** * @see javastrava.api.v3.service.ClubService#listRecentClubActivities(Integer, * Paging) */ @Override public List listRecentClubActivities(final Integer id, final Paging pagingInstruction) { final List activities = PagingHandler.handlePaging(pagingInstruction, thisPage -> Arrays .asList(ClubServiceImpl.this.api.listRecentClubActivities(id, thisPage.getPage(), thisPage.getPageSize()))); return PrivacyUtils.handlePrivateActivities(activities, this.getToken()); } /** * @see javastrava.api.v3.service.ClubService#listRecentClubActivitiesAsync(java.lang.Integer) */ @Override public CompletableFuture> listRecentClubActivitiesAsync(final Integer clubId) { return StravaServiceImpl.future(() -> { return listRecentClubActivities(clubId); }); } /** * @see javastrava.api.v3.service.ClubService#listRecentClubActivitiesAsync(java.lang.Integer, javastrava.util.Paging) */ @Override public CompletableFuture> listRecentClubActivitiesAsync(final Integer clubId, final Paging pagingInstruction) { return StravaServiceImpl.future(() -> { return listRecentClubActivities(clubId, pagingInstruction); }); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy