1
2 package hoplugins.teamAnalyzer.dao;
3
4 import hoplugins.Commons;
5
6 import hoplugins.teamAnalyzer.vo.Team;
7
8 import java.sql.ResultSet;
9 import java.sql.SQLException;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14
15 /***
16 * DB Access Manager for Favourite Teams
17 *
18 * @author <a href=mailto:draghetto@users.sourceforge.net>Massimiliano Amato</a>
19 */
20 public class FavoritesDAO {
21
22
23 /***
24 * Creates a new FavoritesDAO object.
25 */
26 public FavoritesDAO() {
27 checkTable();
28 }
29
30
31
32 /***
33 * check if a team is a favourite team
34 *
35 * @param teamId
36 *
37 * @return true if team is a favourite
38 */
39 public boolean isFavourite(int teamId) {
40 String query = "select * from TEAMANALYZER_FAVORITES where TEAMID=" + teamId;
41 ResultSet rs = Commons.getModel().getAdapter().executeQuery(query);
42
43 try {
44 if (rs.next()) {
45 return true;
46 }
47 } catch (SQLException e) {
48
49 }
50
51 return false;
52 }
53
54 /***
55 * Returns all favourite teams
56 *
57 * @return List of Teams Object
58 */
59 public List getTeams() {
60 List list = new ArrayList();
61 String query = "select * from TEAMANALYZER_FAVORITES";
62 ResultSet rs = Commons.getModel().getAdapter().executeQuery(query);
63
64 try {
65 while (rs.next()) {
66 Team team = new Team();
67
68 team.setTeamId(rs.getInt(1));
69 team.setName(rs.getString(2));
70 list.add(team);
71 }
72 } catch (SQLException e) {
73 return new ArrayList();
74 }
75
76 return list;
77 }
78
79 /***
80 * Add a new Favourite team
81 *
82 * @param team the new favourite team
83 */
84 public void addTeam(Team team) {
85 Commons.getModel().getAdapter().executeUpdate("insert into TEAMANALYZER_FAVORITES (TEAMID, NAME) values ("
86 + team.getTeamId() + ", '" + team.getName()
87 + "')");
88 }
89
90 /***
91 * Remove a Favourite team
92 *
93 * @param teamId the favourite team to be removed
94 */
95 public void removeTeam(int teamId) {
96 String query = "delete from TEAMANALYZER_FAVORITES where TEAMID=" + teamId;
97
98 Commons.getModel().getAdapter().executeUpdate(query);
99 }
100
101 /***
102 * Check if the table exists, if not create it with default values
103 */
104 private void checkTable() {
105 try {
106 ResultSet rs = Commons.getModel().getAdapter().executeQuery("select * from TEAMANALYZER_FAVORITES");
107
108 rs.next();
109 } catch (Exception e) {
110 Commons.getModel().getAdapter().executeUpdate("CREATE TABLE TEAMANALYZER_FAVORITES(TEAMID integer,NAME varchar(20))");
111 }
112 }
113 }