1
2 package hoplugins.teamAnalyzer.dao;
3
4 import gui.UserParameter;
5
6 import hoplugins.Commons;
7
8 import hoplugins.commons.utils.HTCalendar;
9 import hoplugins.commons.utils.HTCalendarFactory;
10
11 import hoplugins.teamAnalyzer.vo.PlayerInfo;
12
13 import java.sql.ResultSet;
14 import java.sql.SQLException;
15
16 import java.util.Calendar;
17
18
19 /***
20 * DB Access Manager for Player Data
21 *
22 * @author <a href=mailto:draghetto@users.sourceforge.net>Massimiliano Amato</a>
23 */
24 public class PlayerDataDAO {
25
26
27 /***
28 * Creates a new TeamPlayerDAO object.
29 */
30 public PlayerDataDAO() {
31 checkTable();
32 }
33
34
35
36 /***
37 * Returns the specialEvent code for a player
38 *
39 * @param playerId the playerId
40 * @param week TODO Missing Constructuor Parameter Documentation
41 * @param season TODO Missing Constructuor Parameter Documentation
42 *
43 * @return a numeric code
44 */
45 public PlayerInfo getPlayerInfo(int playerId, int week, int season) {
46 int weekNumber = week + (season * 16);
47
48 String query = "select * from TEAMANALYZER_PLAYERDATA where PLAYERID=" + playerId
49 + " and week=" + weekNumber;
50
51 ResultSet rs = Commons.getModel().getAdapter().executeQuery(query);
52
53 try {
54 rs.next();
55
56 PlayerInfo info = new PlayerInfo();
57 info.setPlayerId(playerId);
58 info.setAge(rs.getInt("AGE"));
59 info.setForm(rs.getInt("FORM"));
60 info.setTSI(rs.getInt("TSI"));
61 info.setSpecialEvent(rs.getInt("SPECIALEVENT"));
62 info.setTeamId(rs.getInt("TEAMID"));
63 info.setExperience(rs.getInt("EXPERIENCE"));
64 info.setStatus(rs.getInt("STATUS"));
65 return info;
66 } catch (SQLException e) {
67 return new PlayerInfo();
68 }
69 }
70
71 /***
72 * Returns the specialEvent code for a player
73 *
74 * @param playerId the playerId
75 *
76 * @return a numeric code
77 */
78 public PlayerInfo getPlayerInfo(int playerId) {
79 HTCalendar cal = getCurrentHTWeek();
80 return getPlayerInfo(playerId, cal.getHTWeek(), cal.getHTSeason());
81 }
82
83 /***
84 * TODO Missing Method Documentation
85 *
86 * @param id TODO Missing Method Parameter Documentation
87 *
88 * @return TODO Missing Return Method Documentation
89 */
90 public PlayerInfo getPreviousPlayeInfo(int id) {
91 ResultSet rs = Commons.getModel().getAdapter().executeQuery("SELECT WEEK FROM TEAMANALYZER_PLAYERDATA WHERE WEEK<"
92 + getCurrentWeekNumber()
93 + " AND PLAYERID=" + id
94 + " ORDER BY WEEK DESC");
95
96 try {
97 if (rs.next()) {
98 int week = rs.getInt("WEEK");
99 int season = week / 16;
100 int wk = week % 16;
101 return getPlayerInfo(id, wk, season);
102 }
103 } catch (SQLException e) {
104 }
105
106 return new PlayerInfo();
107 }
108
109 /***
110 * Add a player to a team
111 *
112 * @param info
113 */
114 public void addPlayer(PlayerInfo info) {
115 Commons.getModel().getAdapter().executeUpdate("insert into TEAMANALYZER_PLAYERDATA values ("
116 + info.getTeamId() + ", "
117 + info.getPlayerId() + ", "
118 + info.getStatus() + " , "
119 + info.getSpecialEvent() + ", "
120 + info.getTSI() + ", " + info.getForm()
121 + ", " + info.getAge() + ", "
122 + info.getExperience() + ", "
123 + getCurrentWeekNumber() + ")");
124 }
125
126 /***
127 * TODO Missing Method Documentation
128 *
129 * @param info TODO Missing Method Parameter Documentation
130 */
131 public void updatePlayer(PlayerInfo info) {
132 Commons.getModel().getAdapter().executeUpdate("update TEAMANALYZER_PLAYERDATA set "
133 + " SPECIALEVENT=" + info.getSpecialEvent()
134 + " , TSI=" + info.getTSI() + " , FORM="
135 + +info.getForm() + " , AGE=" + info.getAge()
136 + " , EXPERIENCE=" + info.getExperience()
137 + " , STATUS=" + info.getStatus()
138 + " where PLAYERID=" + info.getPlayerId()
139 + " and WEEK=" + getCurrentWeekNumber());
140 }
141
142 /***
143 * TODO Missing Method Documentation
144 *
145 * @return TODO Missing Return Method Documentation
146 */
147 private HTCalendar getCurrentHTWeek() {
148 Calendar date = Calendar.getInstance();
149 date.add(Calendar.HOUR, UserParameter.instance().TimeZoneDifference);
150 return HTCalendarFactory.createTrainingCalendar(Commons.getModel(), date.getTime());
151 }
152
153 /***
154 * TODO Missing Method Documentation
155 *
156 * @return TODO Missing Return Method Documentation
157 */
158 private int getCurrentWeekNumber() {
159 HTCalendar ht = getCurrentHTWeek();
160 return ht.getHTWeek() + (ht.getHTSeason() * 16);
161 }
162
163 /***
164 * Check if the table exists, if not create it with default values
165 */
166 private void checkTable() {
167 try {
168 ResultSet rs = Commons.getModel().getAdapter().executeQuery("select * from TEAMANALYZER_PLAYERDATA");
169 rs.next();
170 } catch (Exception e) {
171
172 try {
173 Commons.getModel().getAdapter().executeUpdate("DROP TABLE TEAMANALYZER_SPECIALEVENT");
174 } catch (Exception e1) {
175 }
176
177 try {
178 Commons.getModel().getAdapter().executeUpdate("DROP TABLE TEAMANALYZER_PLAYERS");
179 } catch (Exception e1) {
180 }
181
182 try {
183 Commons.getModel().getAdapter().executeUpdate("DROP TABLE TEAMANALYZER_TEAMPLAYERS");
184 } catch (Exception e1) {
185 }
186
187 Commons.getModel().getAdapter().executeUpdate("CREATE TABLE TEAMANALYZER_PLAYERDATA(TEAMID integer,PLAYERID integer,STATUS integer,SPECIALEVENT integer, TSI integer, FORM integer, AGE integer, EXPERIENCE integer)");
188 }
189
190
191 try {
192 ResultSet rs = Commons.getModel().getAdapter().executeQuery("select WEEK from TEAMANALYZER_PLAYERDATA");
193 rs.next();
194 } catch (Exception e) {
195 Commons.getModel().getAdapter().executeUpdate("ALTER TABLE TEAMANALYZER_PLAYERDATA ADD COLUMN WEEK INTEGER");
196
197 Commons.getModel().getAdapter().executeUpdate("UPDATE TEAMANALYZER_PLAYERDATA SET WEEK = "
198 + getCurrentWeekNumber());
199 }
200
201
202 try {
203 Commons.getModel().getAdapter().executeUpdate("DELETE FROM TEAMANALYZER_PLAYERDATA WHERE WEEK<"
204 + (getCurrentWeekNumber() - 10));
205 } catch (Exception e) {
206 }
207 }
208 }