You are on page 1of 17

Login | Join Now | Help

Jump to...

kenai.com> projects>dukessoccerleague>source code>DukesSoccerLeague


Mailing List Source Code Repository DukesSoccerLeague Project chat room DukesSoccerLeague chat room

Dukes Soccer League was started in April 2011, is owned by Harita, and has 23 members.

Join This Project

Source code file conten

001.package sl314.model; 002. 003.import java.io.File; 004.import java.io.FileReader; 005.import java.io.BufferedReader; 006.import java.io.FileWriter; 007.import java.io.PrintWriter; 008.import java.io.IOException; 009. 010. 011.public class PlayerService { 012.private String dataDirectory; 013. 014.public PlayerService(String dataDirectory) { 015.this.dataDirectory = dataDirectory; 016.} 017. 018./** 019.* This method finds the specified Player object from the 020.* complete set of players. 021.*/ 022.public Player getPlayer(String name) { 023. 024.Player player = readPlayer(name); 025. 026.if ( player == null ) { 027.player = new Player(name); 028.} 029. 030.return player;

031.} 032. 033./** 034.* This method adds a new Player object. 035.*/ 036.public void save(Player player) { 037.// Store the player. 038.storePlayer(player); 039.// This is a poor design because it will allow duplicate player 040.// records in the data file. 041.} 042. 043./** 044.* This private method retrieves a single player from the data file. 045.*/ 046.private Player readPlayer(String search_name) { 047.File playerFile = new File(dataDirectory, "players.txt"); 048.BufferedReader playerReader = null; 049.Player player = null; 050. 051.// Return null if the players data file does not yet exist. 052.if ( ! playerFile.exists() ) { 053.return null; 054.} 055. 056.// Otherwise, loop through each record in the data file 057.try { 058.playerReader = new BufferedReader(new FileReader(playerFile)); 059.String record; 060. 061.// Read every record (one per line) 062.while ( (record = playerReader.readLine()) != null ) { 063.String[] fields = record.split("\t"); 064. 065.// Extract the name field from the record 066.String name = fields[0]; 067. 068.// If this record does not match the SEARCH NAME, 069.// then continue searching the data file 070.if ( ! search_name.equals(name) ) { 071.continue; 072.} 073. 074.// Otherwise, we have a match... 075.// Extract the rest of the data fields 076.String address = fields[1]; 077.String city = fields[2]; 078.String province = fields[3]; 079.String postalCode = fields[4]; 080. 081.// Create the Player object from the data fields 082.player = new Player(name, address, city, province, postalCode); 083. 084.// and break from the loop

085.break; 086. 087.} // END while loop 088. 089.} catch (Exception e) { 090.System.err.println(e); 091. 092.// Clean up IO resources 093.} finally { 094.if ( playerReader != null ) { 095.try { playerReader.close(); } catch (IOException e) { System.err.println(e); } 096.} 097.} 098. 099.return player; 100.} 101. 102./** 103.* This private method stores a single player to the data file. 104.*/ 105.private void storePlayer(Player player) { 106.String playerFile = dataDirectory + "players.txt"; 107.PrintWriter playerWriter = null; 108. 109.try { 110.// Open a writer stream and mark it to append the new data 111.playerWriter = new PrintWriter(new FileWriter(playerFile, true)); 112. 113.playerWriter.print(player.name); 114.playerWriter.print('\t'); 115.playerWriter.print(player.address); 116.playerWriter.print('\t'); 117.playerWriter.print(player.city); 118.playerWriter.print('\t'); 119.playerWriter.print(player.province); 120.playerWriter.print('\t'); 121.playerWriter.print(player.postalCode); 122.playerWriter.println(); 123. 124.} catch (Exception e) { 125.System.err.println(e); 126. 127.// Clean up IO resources 128.} finally { 129.if ( playerWriter != null ) { 130.try { playerWriter.close(); } catch (Exception e) { System.err.println(e); } 131.} 132.} 133.} 134.}

Login | Join Now | Help


Jump to...

kenai.com> projects>dukessoccerleague>source code>DukesSoccerLeague


Mailing List Source Code Repository DukesSoccerLeague Project chat room DukesSoccerLeague chat room

Dukes Soccer League was started in April 2011, is owned by Harita, and has 23 members.

Join This Project

Source code file content


CODE FOR PLAYERSERVICE
001.package sl314.model; 002. 003.import java.io.File; 004.import java.io.FileReader; 005.import java.io.BufferedReader; 006.import java.io.FileWriter; 007.import java.io.PrintWriter; 008.import java.io.IOException; 009. 010. 011.public class PlayerService { 012.private String dataDirectory; 013. 014.public PlayerService(String dataDirectory) { 015.this.dataDirectory = dataDirectory; 016.} 017. 018./** 019.* This method finds the specified Player object from the 020.* complete set of players. 021.*/ 022.public Player getPlayer(String name) { 023. 024.Player player = readPlayer(name); 025. 026.if ( player == null ) { 027.player = new Player(name);

028.} 029. 030.return player; 031.} 032. 033./** 034.* This method adds a new Player object. 035.*/ 036.public void save(Player player) { 037.// Store the player. 038.storePlayer(player); 039.// This is a poor design because it will allow duplicate player 040.// records in the data file. 041.} 042. 043./** 044.* This private method retrieves a single player from the data file. 045.*/ 046.private Player readPlayer(String search_name) { 047.File playerFile = new File(dataDirectory, "players.txt"); 048.BufferedReader playerReader = null; 049.Player player = null; 050. 051.// Return null if the players data file does not yet exist. 052.if ( ! playerFile.exists() ) { 053.return null; 054.} 055. 056.// Otherwise, loop through each record in the data file 057.try { 058.playerReader = new BufferedReader(new FileReader(playerFile)); 059.String record; 060. 061.// Read every record (one per line) 062.while ( (record = playerReader.readLine()) != null ) { 063.String[] fields = record.split("\t"); 064. 065.// Extract the name field from the record 066.String name = fields[0]; 067. 068.// If this record does not match the SEARCH NAME, 069.// then continue searching the data file 070.if ( ! search_name.equals(name) ) { 071.continue; 072.} 073. 074.// Otherwise, we have a match... 075.// Extract the rest of the data fields 076.String address = fields[1]; 077.String city = fields[2]; 078.String province = fields[3]; 079.String postalCode = fields[4]; 080. 081.// Create the Player object from the data fields

082.player = new Player(name, address, city, province, postalCode); 083. 084.// and break from the loop 085.break; 086. 087.} // END while loop 088. 089.} catch (Exception e) { 090.System.err.println(e); 091. 092.// Clean up IO resources 093.} finally { 094.if ( playerReader != null ) { 095.try { playerReader.close(); } catch (IOException e) { System.err.println(e); } 096.} 097.} 098. 099.return player; 100.} 101. 102./** 103.* This private method stores a single player to the data file. 104.*/ 105.private void storePlayer(Player player) { 106.String playerFile = dataDirectory + "players.txt"; 107.PrintWriter playerWriter = null; 108. 109.try { 110.// Open a writer stream and mark it to append the new data 111.playerWriter = new PrintWriter(new FileWriter(playerFile, true)); 112. 113.playerWriter.print(player.name); 114.playerWriter.print('\t'); 115.playerWriter.print(player.address); 116.playerWriter.print('\t'); 117.playerWriter.print(player.city); 118.playerWriter.print('\t'); 119.playerWriter.print(player.province); 120.playerWriter.print('\t'); 121.playerWriter.print(player.postalCode); 122.playerWriter.println(); 123. 124.} catch (Exception e) { 125.System.err.println(e); 126. 127.// Clean up IO resources 128.} finally { 129.if ( playerWriter != null ) { 130.try { playerWriter.close(); } catch (Exception e) { System.err.println(e); } 131.} 132.} 133.}134.

LEagueSERvice code

001.package sl314.model; 002. 003.import java.util.List; 004.import java.util.LinkedList; 005.import java.util.Iterator; 006.import java.util.Collections; 007.import java.io.FileReader; 008.import java.io.BufferedReader; 009.import java.io.FileWriter; 010.import java.io.PrintWriter; 011.import java.io.IOException; 012. 013./** 014.* This object performs a variety of league services, such as looking 015.* up league objects and creating new ones. 016.*/ 017.public class LeagueService { 018. 019./** The cache of League objects. */ 020.private static final List LEAGUES_CACHE = new LinkedList(); 021.private String dataDirectory; 022. 023.public LeagueService(String dataDirectory) { 024.this.dataDirectory = dataDirectory; 025. 026.// Make sure that the leagues cache has been initialized 027.synchronized ( LEAGUES_CACHE ) { 028.if ( LEAGUES_CACHE.isEmpty() ) { 029.cacheLeagues(); 030.} 031.} 032.} 033. 034./** 035.* This method returns a complete set of leagues. 036.*/ 037.public List getAllLeagues() { 038.// Return an immutable List; which makes this read-only 039.return Collections.unmodifiableList(LEAGUES_CACHE); 040.} 041. 042./** 043.* This method finds the specified League object from the

044.* complete set of leagues. 045.*/ 046.public League getLeague(int year, String season) 047.throws ObjectNotFoundException { 048. 049.// Search in the cache. 050.Iterator set = LEAGUES_CACHE.iterator(); 051.while ( set.hasNext() ) { 052.League l = (League) set.next(); 053.if ( season.equals(l.getSeason()) && (year == l.getYear()) ) { 054.return l; 055.} 056.} 057. 058.// Throw an exception if the league was not found. 059.throw new ObjectNotFoundException(); 060.} 061. 062./** 063.* This method adds a new League object. 064.*/ 065.public League createLeague(int year, String season, String title) { 066. 067.// Determine the next league objectID 068.int nextID = LEAGUES_CACHE.size() + 1; 069. 070.// Create new league object 071.League league = new League(nextID, year, season, title); 072. 073.// Store the league object 074.storeLeague(league); 075. 076.// Record the league in the cache for easy retrieval 077.LEAGUES_CACHE.add(league); 078. 079.return league; 080.} 081. 082./** 083.* This method populates the cache of leagues from the data file. 084.*/ 085.private void cacheLeagues() { 086.String leagueFile = dataDirectory + "leagues.txt"; 087.BufferedReader leagueReader = null; 088. 089.try { 090.leagueReader = new BufferedReader(new FileReader(leagueFile)); 091.String record; 092. 093.// Read every record (one per line) 094.while ( (record = leagueReader.readLine()) != null ) { 095.String[] fields = record.split("\t"); 096. 097.// Extract the data fields for the record

098.int objectID = Integer.parseInt(fields[0]); 099.int year = Integer.parseInt(fields[1]); 100.String season = fields[2]; 101.String title = fields[3]; 102. 103.// Create the League object and save it in the cache 104.League l = new League(objectID, year, season, title); 105.LEAGUES_CACHE.add(l); 106. 107.} // END while loop 108. 109.} catch (Exception e) { 110.System.err.println(e); 111. 112.// Clean up IO resources 113.} finally { 114.if ( leagueReader != null ) { 115.try { leagueReader.close(); } catch (IOException e) { System.err.println(e); } 116.} 117.} 118.} // END of cacheLeagues method 119. 120./** 121.* This private method stores a single league to the data file. 122.*/ 123.private void storeLeague(League league) { 124.String leagueFile = dataDirectory + "leagues.txt"; 125.PrintWriter leagueWriter = null; 126. 127.try { 128.// Open a writer stream and mark it to append the new data 129.leagueWriter = new PrintWriter(new FileWriter(leagueFile, true)); 130. 131.leagueWriter.print(league.objectID); 132.leagueWriter.print('\t'); 133.leagueWriter.print(league.year); 134.leagueWriter.print('\t'); 135.leagueWriter.print(league.season); 136.leagueWriter.print('\t'); 137.leagueWriter.print(league.title); 138.leagueWriter.println(); 139. 140.} catch (Exception e) { 141.System.err.println(e); 142. 143.// Clean up IO resources 144.} finally { 145.if ( leagueWriter != null ) { 146.try { leagueWriter.close(); } catch (Exception e) { System.err.println(e); } 147.} 148.} 149.} // END of storeLeague method

150. 151.} // END of LeagueService class

Code for league

01./** 02.* This domain object represents a soccer league. 03.*/ 04.package sl314.model; 05. 06.public class League { 07. 08.int objectID; 09.int year; 10.String season; 11.String title; 12. 13.League(int objectID, int year, String season, String title) { 14.this.objectID = objectID; 15.this.year = year; 16.this.season = season; 17.this.title = title;

18.} 19. 20.public int getYear() { 21.return year; 22.} 23.public String getSeason() { 24.return season; 25.} 26.public String getTitle() { 27.return title; 28.} 29. 30.// League entities are compared by their object IDs 31.@Override 32.public boolean equals(Object o) { 33.boolean result = false; 34.if ( o instanceof League ) { 35.League l = (League) o; 36.result = (this.objectID == l.objectID); 37.} 38.return result; 39.} 40. 41.// You need to override hashCode if you override equals 42.@Override 43.public int hashCode() { 44.Integer OID = new Integer(objectID); 45.return OID.hashCode(); 46.} 47. 48.@Override 49.public String toString() { 50.return title; 51.} 52.}

Code for registerservlet

001.package sl314.controller; 002. 003.import javax.servlet.http.HttpServlet; 004.import javax.servlet.http.HttpServletRequest; 005.import javax.servlet.http.HttpServletResponse; 006.import javax.servlet.RequestDispatcher; 007.import javax.servlet.ServletException; 008.// Support classes 009.import java.io.IOException; 010.// Model classes 011.import sl314.model.RegisterService; 012.import sl314.model.League; 013.import sl314.model.Player; 014.import sl314.model.ObjectNotFoundException; 015.import java.util.List; 016.import java.util.LinkedList; 017. 018. 019.public class RegisterServlet extends HttpServlet { 020. 021.@Override 022.public void doPost(HttpServletRequest request,HttpServletResponse response) 023.throws IOException, ServletException { 024. 025.// Keep a set of strings to record form processing errors. 026.List errorMsgs = new LinkedList(); 027.// Store this set in the request scope, in case we need to 028.// send the ErrorPage view. 029.request.setAttribute("errorMsgs", errorMsgs); 030. 031.try { 032. 033.// Retrieve form parameters. 034.String season = request.getParameter("season").trim(); 035.String yearStr = request.getParameter("year").trim(); 036.String name = request.getParameter("name").trim(); 037.String address = request.getParameter("address").trim(); 038.String city = request.getParameter("city").trim(); 039.String province = request.getParameter("province").trim(); 040.String postalCode = request.getParameter("postalCode").trim(); 041.String division = request.getParameter("division").trim(); 042. 043.// Perform data conversions. 044.int year = -1; 045.try { 046.year = Integer.parseInt(yearStr); 047.} catch (NumberFormatException nfe) {

048.errorMsgs.add("The 'year' field must be a positive integer."); 049.} 050. 051.// Verify 'Select League' form fields 052.if ( (year != -1) && ((year < 2000) || (year > 2010)) ) { 053.errorMsgs.add("The 'year' field must within 2000 to 2010."); 054.} 055.if ( season.equals("UNKNOWN") ) { 056.errorMsgs.add("Please select a league season."); 057.} 058. 059.// Verify 'Enter Player Information' form fields 060.if ( name.length() == 0 ) { 061.errorMsgs.add("You must enter your full name."); 062.} 063.if ( (address.length() == 0) || (city.length() == 0) 064.|| (province.length() == 0) || (postalCode.length() == 0) ) { 065.errorMsgs.add("You must enter your full address."); 066.} 067. 068.// Verify 'Select Division' form fields 069.if ( division.equals("UNKNOWN") ) { 070.errorMsgs.add("You must select a division."); 071.} 072. 073.// Send the ErrorPage view if there were errors 074.if ( ! errorMsgs.isEmpty() ) { 075.RequestDispatcher view 076.= request.getRequestDispatcher("form.view"); 077.view.forward(request, response); 078.return; 079.} 080. 081.// Perform business logic 082.String dataDirectory = (String)getServletContext().getAttribute("sl314.model.dataDirectory"); 083.RegisterService registerSvc = new RegisterService(dataDirectory); 084.// Retrieve the league 085.League league = registerSvc.getLeague(year, season); 086.// Update the player info 087.Player player = registerSvc.getPlayer(name); 088.player.setAddress(address); 089.player.setCity(city); 090.player.setProvince(province); 091.player.setPostalCode(postalCode); 092.// Perform the registration 093.registerSvc.register(league, player, division); 094. 095.// Store the league and player objects in the request-scope 096.request.setAttribute("league", league); 097.request.setAttribute("player", player); 098. 099.// Send the Success view 100.RequestDispatcher view 101.= request.getRequestDispatcher("thank_you.view");

102.view.forward(request, response); 103.return; 104. 105.// Handle business exceptions 106.} catch (ObjectNotFoundException onfe) { 107.errorMsgs.add("The league you selected does not yet exist." 108.+ " Please select another."); 109.RequestDispatcher view 110.= request.getRequestDispatcher("form.view"); 111.view.forward(request, response); 112. 113.// Handle any unusual exceptions 114.} catch (RuntimeException e) { 115.errorMsgs.add(e.getMessage()); 116.RequestDispatcher view 117.= request.getRequestDispatcher("form.view"); 118.view.forward(request, response); 119. 120.// Log stack trace 121.e.printStackTrace(System.err); 122. 123.} // END of try-catch block 124. 125.} // END of doPost method 126. 127.} // END of RegisterServlet class

Code for Initializemodelproperties


01.package sl314.web; 02. 03.import javax.servlet.ServletContextListener; 04.import javax.servlet.ServletContextEvent; 05.import javax.servlet.ServletContext; 06. 07.public class InitializeModelProperties implements ServletContextListener { 08.@Override 09.public void contextInitialized(ServletContextEvent event) { 10.ServletContext context = event.getServletContext(); 11.String dataDirectory = context.getInitParameter("data-directory"); 12. 13.if ( dataDirectory != null ) { 14.// Store this directory as a System property 15.context.setAttribute("sl314.model.dataDirectory", dataDirectory); 16.context.log("The dataDirectory attribute has been set."); 17.} else { 18.context.log("The 'data-directory' context parameter was not set."); 19.} 20. 21.} // END of contextInitialized 22. 23.@Override 24.public void contextDestroyed(ServletContextEvent event) { 25.// do nothing 26.} 27.}

Register form servlet


01.package sl314.web; 02. 03.import javax.servlet.ServletContextListener; 04.import javax.servlet.ServletContextEvent; 05.import javax.servlet.ServletContext; 06. 07.public class InitializeModelProperties implements ServletContextListener { 08.@Override 09.public void contextInitialized(ServletContextEvent event) { 10.ServletContext context = event.getServletContext(); 11.String dataDirectory = context.getInitParameter("data-directory"); 12. 13.if ( dataDirectory != null ) { 14.// Store this directory as a System property 15.context.setAttribute("sl314.model.dataDirectory", dataDirectory); 16.context.log("The dataDirectory attribute has been set."); 17.} else { 18.context.log("The 'data-directory' context parameter was not set."); 19.} 20. 21.} // END of contextInitialized 22. 23.@Override 24.public void contextDestroyed(ServletContextEvent event) { 25.// do nothing 26.} 27.} You need three txt files i.e leagues.txt,registrations.txt,players.txt

You might also like