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;

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

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

 » Join This Project Source code file content CODE FOR PLAYERSERVICE 001.import java.player = new Player(name). 006. 008.PrintWriter.io.private String dataDirectory.model. 011.public Player getPlayer(String name) { 023.* This method finds the specified Player object from the 020.io.io.File.this.import java.BufferedReader.import java.package sl314. 014.. 002.public class PlayerService { 012.} 017./** 019.if ( player == null ) { 027. 025.*/ 022. 024.FileWriter.* complete set of players. 005.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.import java.FileReader. 016.dataDirectory = dataDirectory.import java. 007. 021.IOException. kenai. 003.io.. 026. and has 23 members. 010.public PlayerService(String dataDirectory) { 015. . 018.io. 004. 013.io.Player player = readPlayer(name).import java. 009. is owned by Harita.Login | Join Now | Help Jump to.

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

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

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

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

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

08.this.this.title = title.this. String season.String season. 11.League(int objectID.} // END of LeagueService class Code for league 01.int year./** 02.year = year. 13. 151. 16. 10. String title) { 14.String title. . int year. 03.* This domain object represents a soccer league.season = season.package sl314. 09.objectID = objectID. 05.model. 17. 12.int objectID.public class League { 07.150. 06. 15.this.*/ 04.

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

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

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

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

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

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