View Javadoc

1   package com.atlassian.plugins.rest.sample.expansion.resource;
2   
3   import com.atlassian.plugins.rest.sample.expansion.entity.Player;
4   import com.atlassian.plugins.rest.sample.expansion.entity.PlayerRecord;
5   import com.atlassian.plugins.rest.sample.expansion.entity.Game;
6   import com.atlassian.plugins.rest.sample.expansion.entity.Players;
7   import com.atlassian.plugins.rest.sample.expansion.entity.SubRecord;
8   
9   import javax.ws.rs.core.UriBuilder;
10  import java.util.*;
11  
12  /**
13   * Simulates a data storage layer.  It provides an interface to load the raw data and convert it in to the REST
14   * level API objects.
15   *
16   * This datastore knows of two players, one of which has a record and one that doesn't.  Its intended to be used
17   * by the {@link PlayerResource} to show an example on how to optionally show fields for expansion.
18   */
19  public class DataStore {
20      private static final DataStore singleton = new DataStore();
21  
22      public static DataStore getInstance() {
23          return singleton;
24      }
25  
26      private Map<String, Game> games = new HashMap<String, Game>();
27      private Map<Integer, String> playerNames = new TreeMap<Integer, String>();
28      private Map<Integer, Integer> pointsScored = new HashMap<Integer, Integer>();
29  
30      private DataStore() {
31          playerNames.put(0, "Adam Ashley-Cooper");
32          playerNames.put(1, "Matt Giteau");
33          playerNames.put(2, "Stephen Larkham");
34          playerNames.put(3, "George Gregan");
35          playerNames.put(4, "Mark Gerrard");
36          playerNames.put(5, "George Smith");
37          playerNames.put(6, "Stirling Mortlock");
38          playerNames.put(7, "Clyde Rathbone");
39          playerNames.put(8, "Jeremy Paul");
40          playerNames.put(9, "Mark Chisholm");
41          pointsScored.put(2, 513);
42          games.put("rugby", new Game("Rugby"));
43      }
44  
45      public Game getGame(String name, UriBuilder uriBuilder) {
46          Game game = games.get(name);
47          game.setPlayers(new Players(playerNames.size(), 5, uriBuilder));
48          return game;
49      }
50  
51      public List<Player> getPlayers(UriBuilder uriBuilder) {
52          final List<Player> players = new ArrayList<Player>();
53          for (Map.Entry<Integer, String> player : playerNames.entrySet()) {
54              players.add(new Player(player.getKey(), player.getValue(), uriBuilder));
55          }
56          return players;
57      }
58  
59  
60      public Player getPlayer(int id, UriBuilder builder) {
61          Player player = new Player(id, playerNames.get(id), builder);
62          if (playerHasRecord(id)) {
63              player.setRecord(PlayerRecord.emptyRecord(player));
64          }
65          return player;
66      }
67  
68      public String getPlayerName(int id) {
69          return playerNames.get(id);
70      }
71  
72      /**
73       * This simulates the use of a cheap call to a database to check the existence of potentially more expensive
74       * data.  This is being used to detirmine if the client can see the expensive element at all and expand it.
75       *
76       * @param id
77       * @return
78       */
79      private boolean playerHasRecord(int id) {
80          return pointsScored.containsKey(id);
81      }
82  
83      /**
84       * This simulates the lookup of the expensive data element about the given player.  In this case it simulates
85       * the action of performing a query on a series of tables and calculating the total points scored by this player.
86       *
87       * @param player
88       * @return
89       */
90      public PlayerRecord getPlayerRecord(Player player) {
91          PlayerRecord playerRecord = new PlayerRecord(this.pointsScored.get(player.getId()));
92          playerRecord.setSubRecord1(SubRecord.emptySubRecord(playerRecord));
93          playerRecord.setSubRecord2(SubRecord.emptySubRecord(playerRecord));
94          return playerRecord;
95      }
96  
97      public SubRecord getSubRecord(final PlayerRecord playerRecord) {
98          return new SubRecord(playerRecord.getPointsScored() / 2);
99      }
100 }