/** - * This object performs a variety of league services, such as looking - * up league objects and creating new ones. + * This object performs a variety of league services, like retrieving + * a league object from the database, or creating a new league object. */ public class LeagueService {
- /** The cache of League objects. */ - private static final List LEAGUES_CACHE = new LinkedList(); - private String dataDirectory; + /** + * The internal Data Access Object used for database CRUD operations. + */ + private LeagueDAO leagueDataAccess;
- public LeagueService(String dataDirectory) { - this.dataDirectory = dataDirectory; + /** + * This constructor creates a League Service object. + */ + public LeagueService() { + leagueDataAccess = new LeagueDAO(); + }
6/21/2014 Dukes Soccer League: commits@dukessoccerleague.kenai.com: Archive Project Kenai https://kenai.com/projects/dukessoccerleague/lists/commits/archive/2011-12/message/0 7/23 - // Make sure that the leagues cache has been initialized - synchronized ( LEAGUES_CACHE ) { - if ( LEAGUES_CACHE.isEmpty() ) { - cacheLeagues(); - } - } - } + /** + * This method returns a complete set of leagues. + */ + public List getAllLeagues() { + return leagueDataAccess.retrieveAll(); + }
- /** - * This method returns a complete set of leagues. - */ - public List getAllLeagues() { - // Return an immutable List; which makes this read-only - return Collections.unmodifiableList(LEAGUES_CACHE); - } + /** + * This method finds the specified League object in the database. + */ + public League getLeague(int year, String season) + throws ObjectNotFoundException {
- /** - * This method finds the specified League object from the - * complete set of leagues. - */ - public League getLeague(int year, String season) - throws ObjectNotFoundException { + return leagueDataAccess.retrieve(year, season); + }
- // Search in the cache. - Iterator set = LEAGUES_CACHE.iterator(); - while ( set.hasNext() ) { - League l = (League) set.next(); - if ( season.equals(l.getSeason()) && (year == l.getYear()) ) { - return l; - } - } + /** + * This class adds a new League object to the database. + */ + public League createLeague(int year, String season, String title) {
- // Throw an exception if the league was not found. - throw new ObjectNotFoundException(); - } + // Create the league object + League league = new League(-1, year, season, title);
- /** - * This method adds a new League object. 6/21/2014 Dukes Soccer League: commits@dukessoccerleague.kenai.com: Archive Project Kenai https://kenai.com/projects/dukessoccerleague/lists/commits/archive/2011-12/message/0 8/23 - */ - public League createLeague(int year, String season, String title) { + // Perform the DB transaction + leagueDataAccess.insert(league);
- // Determine the next league objectID - int nextID = LEAGUES_CACHE.size() + 1; - - // Create new league object - League league = new League(nextID, year, season, title); - - // Store the league object - storeLeague(league); - - // Record the league in the cache for easy retrieval - LEAGUES_CACHE.add(league); - - return league; - } - - /** - * This method populates the cache of leagues from the data file. - */ - private void cacheLeagues() { - String leagueFile = dataDirectory + "leagues.txt"; - BufferedReader leagueReader = null; - - try { - leagueReader = new BufferedReader(new FileReader(leagueFile)); - String record; - - // Read every record (one per line) - while ( (record = leagueReader.readLine()) != null ) { - String[] fields = record.split("\t"); - - // Extract the data fields for the record - int objectID = Integer.parseInt(fields[0]); - int year = Integer.parseInt(fields[1]); - String season = fields[2]; - String title = fields[3]; - - // Create the League object and save it in the cache - League l = new League(objectID, year, season, title); - LEAGUES_CACHE.add(l); - - } // END while loop - - } catch (Exception e) { - System.err.println(e); - - // Clean up IO resources - } finally { - if ( leagueReader != null ) { - try { leagueReader.close(); } catch (IOException e) { System.err.println(e); } - } - } 6/21/2014 Dukes Soccer League: commits@dukessoccerleague.kenai.com: Archive Project Kenai https://kenai.com/projects/dukessoccerleague/lists/commits/archive/2011-12/message/0 9/23 - } // END of cacheLeagues method - - /** - * This private method stores a single league to the data file. - */ - private void storeLeague(League league) { - String leagueFile = dataDirectory + "leagues.txt"; - PrintWriter leagueWriter = null; - - try { - // Open a writer stream and mark it to append the new data - leagueWriter = new PrintWriter(new FileWriter(leagueFile, true)); - - leagueWriter.print(league.objectID); - leagueWriter.print('\t'); - leagueWriter.print(league.year); - leagueWriter.print('\t'); - leagueWriter.print(league.season); - leagueWriter.print('\t'); - leagueWriter.print(league.title); - leagueWriter.println(); - - } catch (Exception e) { - System.err.println(e); - - // Clean up IO resources - } finally { - if ( leagueWriter != null ) { - try { leagueWriter.close(); } catch (Exception e) { System.err.println(e); } - } - } - } // END of storeLeague method - -} // END of LeagueService class + return league; + } +} Index: DukesSoccerLeague/src/java/sl314/model/RegisterService.java =================================================================== --- DukesSoccerLeague/src/java/sl314/model/RegisterService.java (revision 8) +++ DukesSoccerLeague/src/java/sl314/model/RegisterService.java (revision 9) @@ -1,79 +1,108 @@ package sl314.model;
6/21/2014 Dukes Soccer League: commits@dukessoccerleague.kenai.com: Archive Project Kenai https://kenai.com/projects/dukessoccerleague/lists/commits/archive/2011-12/message/0 10/23 + /** * This object performs a variety of league registration services. * It acts a Facade into the business logic of registering a Player for * a League. */ public class RegisterService { - private String dataDirectory;
- public RegisterService(String dataDirectory) { - this.dataDirectory = dataDirectory; - // do nothing - } + /** + * This constructor creates a Registration Service object. + */ + public RegisterService() { + }
- /** - * This method finds the specified league, by delegating to the - * LeagueService object. - */ - public League getLeague(int year, String season) - throws ObjectNotFoundException { - LeagueService leagueSvc = new LeagueService(dataDirectory); - return leagueSvc.getLeague(year, season); - } + /** + * This method finds the specified League object. + */ + public League getLeague(int year, String season) + throws ObjectNotFoundException {
- /** - * This method return a Player object for the named person, by - * delegating to the PlayerService object. - */ - public Player getPlayer(String name) { - PlayerService playerSvc = new PlayerService(dataDirectory); - return playerSvc.getPlayer(name); - } + // Delegate to the league service + LeagueService leagueSvc = new LeagueService(); + return leagueSvc.getLeague(year, season); + }
- /** - * This method stores the registration information for the player, - * based on the league and division information. - */ - public void register(League league, Player player, String division) { + public Player getPlayer(String name) { + return new Player(name); + }
- // Use the player service to save the player object 6/21/2014 Dukes Soccer League: commits@dukessoccerleague.kenai.com: Archive Project Kenai https://kenai.com/projects/dukessoccerleague/lists/commits/archive/2011-12/message/0 11/23 - PlayerService playerSvc = new PlayerService(dataDirectory); - playerSvc.save(player); + public void register(League league, Player player, String division) {
- // Record the registration - insertRegistration(league, player, division); - } + // Store the player object into the database + PlayerDAO playerDataAccess = new PlayerDAO(); + playerDataAccess.insert(player);
- public void insertRegistration(League league, Player player,String division) { + // Record the registration + insertRegistration(league, player, division); + }
- // Store the registration info to a data file - String regFile = dataDirectory + "registrations.txt"; - PrintWriter regWriter = null; - try { - // Open a writer stream and mark it to append the new data - regWriter = new PrintWriter(new FileWriter(regFile, true)); + private void insertRegistration(League league, Player player, String division) {