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