1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
package com.buckosoft.fibs.BuckoFIBS.db; |
82 | |
|
83 | |
import java.util.Iterator; |
84 | |
import java.util.LinkedList; |
85 | |
import java.util.List; |
86 | |
|
87 | |
import org.hibernate.Session; |
88 | |
import org.slf4j.Logger; |
89 | |
import org.slf4j.LoggerFactory; |
90 | |
|
91 | |
import com.buckosoft.fibs.domain.FinishedMatch; |
92 | |
import com.buckosoft.fibs.domain.GroupOfPlayers; |
93 | |
import com.buckosoft.fibs.domain.Player; |
94 | |
import com.buckosoft.fibs.domain.PlayerGroup; |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public class DatabaseImpl implements Database { |
103 | |
private final static boolean DEBUG = false; |
104 | 11 | private Logger logger = LoggerFactory.getLogger(getClass()); |
105 | |
|
106 | 11 | private boolean virgin = true; |
107 | |
|
108 | |
private List<FinishedMatch> finishedMatches; |
109 | 11 | private String url = null; |
110 | 11 | private int activeProfile = 0; |
111 | |
|
112 | |
HibernateUtil hibernateUtil; |
113 | |
|
114 | 11 | public DatabaseImpl() { |
115 | 11 | hibernateUtil = new HibernateUtil(); |
116 | 11 | } |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public void setURL(String url) { |
123 | 11 | this.url = url; |
124 | 11 | } |
125 | |
|
126 | |
public void setActiveProfile(int activeProfile) { |
127 | 0 | this.activeProfile = activeProfile; |
128 | 0 | this.finishedMatches = null; |
129 | 0 | } |
130 | |
|
131 | |
private void initDB() { |
132 | 31 | if (virgin) |
133 | 4 | hibernateUtil.maybeCreateNewDatabase(url); |
134 | 31 | virgin = false; |
135 | 31 | } |
136 | |
public void shutdown() { |
137 | 1 | Session session = hibernateUtil.getSessionFactory().getCurrentSession(); |
138 | 1 | session.close(); |
139 | 1 | } |
140 | |
|
141 | |
|
142 | |
|
143 | |
@Override |
144 | |
public void store(Player player) { |
145 | 6 | initDB(); |
146 | 6 | Session session = hibernateUtil.getSessionFactory().getCurrentSession(); |
147 | 6 | session.beginTransaction(); |
148 | 6 | session.save(player); |
149 | 6 | session.getTransaction().commit(); |
150 | |
|
151 | 6 | } |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
@Override |
157 | |
public Player getPlayer(int id) { |
158 | 5 | initDB(); |
159 | 5 | Session session = hibernateUtil.getSessionFactory().getCurrentSession(); |
160 | 5 | session.beginTransaction(); |
161 | 5 | Player p = null; |
162 | |
try { |
163 | 5 | p = (Player)session.createQuery("from Player where id = ?") |
164 | 5 | .setInteger(0, id).uniqueResult(); |
165 | 0 | } catch (Exception e) { |
166 | 0 | e.printStackTrace(); |
167 | 0 | return(null); |
168 | 5 | } |
169 | 5 | session.getTransaction().commit(); |
170 | 5 | return p; |
171 | |
} |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
@Override |
177 | |
public Player getPlayer(String name) { |
178 | 5 | initDB(); |
179 | 5 | Session session = hibernateUtil.getSessionFactory().getCurrentSession(); |
180 | 5 | session.beginTransaction(); |
181 | 5 | Player p = null; |
182 | |
try { |
183 | 5 | p = (Player)session.createQuery("from Player where name = ?") |
184 | 5 | .setString(0, name).uniqueResult(); |
185 | 0 | } catch (Exception e) { |
186 | 0 | e.printStackTrace(); |
187 | 0 | return(null); |
188 | 5 | } |
189 | 5 | session.getTransaction().commit(); |
190 | 5 | return p; |
191 | |
} |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
@Override |
197 | |
public void store(FinishedMatch finishedMatch) { |
198 | 5 | finishedMatch.setProfileId(this.activeProfile); |
199 | 5 | initDB(); |
200 | 5 | Session session = hibernateUtil.getSessionFactory().getCurrentSession(); |
201 | 5 | session.beginTransaction(); |
202 | 5 | session.save(finishedMatch); |
203 | 5 | session.getTransaction().commit(); |
204 | 5 | this.finishedMatches = null; |
205 | 5 | } |
206 | |
|
207 | |
@SuppressWarnings("unchecked") |
208 | |
private void getFinishedMatches() { |
209 | 7 | initDB(); |
210 | 7 | Session session = hibernateUtil.getSessionFactory().getCurrentSession(); |
211 | 7 | session.beginTransaction(); |
212 | 7 | List<?> list = session.createQuery("from FinishedMatch where profileId = ? ORDER BY MatchDate") |
213 | 7 | .setInteger(0, this.activeProfile).list(); |
214 | 7 | finishedMatches = (List<FinishedMatch>) list; |
215 | 7 | session.getTransaction().commit(); |
216 | 7 | } |
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
@Override |
222 | |
public double[] getAllRatingsByDate() { |
223 | 1 | if (this.finishedMatches == null) |
224 | 1 | getFinishedMatches(); |
225 | 1 | double[] dd = new double[this.finishedMatches.size()]; |
226 | 1 | int i = 0; |
227 | 1 | for (FinishedMatch fm : this.finishedMatches) { |
228 | 3 | dd[i++] = fm.getRatingAfterMatch(); |
229 | 3 | } |
230 | 1 | return dd; |
231 | |
} |
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
@Override |
237 | |
public void updateOpponent(Player p) { |
238 | 4 | if (this.finishedMatches == null) |
239 | 2 | getFinishedMatches(); |
240 | 4 | if (p.getId() == 0) { |
241 | 3 | Player p1 = this.getPlayer(p.getName()); |
242 | 3 | if (p1 != null) |
243 | 1 | p.setId(p1.getId()); |
244 | |
else |
245 | 2 | return; |
246 | |
} |
247 | 2 | int w = 0; |
248 | 2 | int l = 0; |
249 | 2 | for (FinishedMatch fm : this.finishedMatches) { |
250 | 4 | if (fm.getOpponentId() == p.getId()) { |
251 | 4 | if (fm.getYourScore() > fm.getOpponentScore()) |
252 | 2 | w++; |
253 | |
else |
254 | 2 | l++; |
255 | |
} |
256 | 4 | } |
257 | 2 | if (w != 0 || l != 0) |
258 | 2 | p.setWinLoss(w, l); |
259 | 2 | } |
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
@Override |
265 | |
public List<FinishedMatch> getFinishedMatches(int pid) { |
266 | 4 | List<FinishedMatch> matches = new LinkedList<FinishedMatch>(); |
267 | 4 | if (this.finishedMatches == null) |
268 | 4 | getFinishedMatches(); |
269 | 4 | if (this.finishedMatches == null) |
270 | 0 | return(matches); |
271 | 4 | for (FinishedMatch fm : this.finishedMatches) { |
272 | 4 | if (fm.getOpponentId() == pid) |
273 | 4 | matches.add(fm); |
274 | 4 | } |
275 | 4 | return(matches); |
276 | |
} |
277 | |
|
278 | |
@SuppressWarnings("unchecked") |
279 | |
public List<GroupOfPlayers> getGroupsOfPlayers() { |
280 | 3 | initDB(); |
281 | 3 | Session session = hibernateUtil.getSessionFactory().getCurrentSession(); |
282 | 3 | session.beginTransaction(); |
283 | 3 | List<?> list = session.createQuery("from GroupOfPlayers").list(); |
284 | 3 | List<GroupOfPlayers> ll = (List<GroupOfPlayers>) list; |
285 | 3 | for (GroupOfPlayers gop : ll) { |
286 | 3 | loadPlayerGroups(gop); |
287 | 3 | gop.setDirty(false); |
288 | 3 | } |
289 | 3 | session.getTransaction().commit(); |
290 | 3 | return(ll); |
291 | |
} |
292 | |
|
293 | |
@SuppressWarnings("unchecked") |
294 | |
private void loadPlayerGroups(GroupOfPlayers groupOfPlayers) { |
295 | 3 | Session session = hibernateUtil.getSessionFactory().getCurrentSession(); |
296 | 3 | List<?> list = session.createQuery("from PlayerGroup where groupId = ?").setInteger(0, groupOfPlayers.getId()).list(); |
297 | 3 | List<PlayerGroup> lpg = (List<PlayerGroup>)list; |
298 | 3 | for (PlayerGroup pg : lpg) { |
299 | |
if (DEBUG) |
300 | |
logger.info("load PlayerGroup: id: " + pg.getId() + " gid: " + pg.getGroupId() + " pid: " + pg.getPlayerId()); |
301 | 3 | pg.setDirty(false); |
302 | 3 | } |
303 | 3 | groupOfPlayers.setPlayerGroups(lpg); |
304 | 3 | } |
305 | |
|
306 | |
@SuppressWarnings("unchecked") |
307 | |
public void store(List<GroupOfPlayers> groupList) { |
308 | 4 | Session session = hibernateUtil.getSessionFactory().getCurrentSession(); |
309 | 4 | session.beginTransaction(); |
310 | 4 | for (GroupOfPlayers gop : groupList) { |
311 | 6 | if (gop.isDirty()) { |
312 | 4 | store(gop); |
313 | 4 | gop.setDirty(false); |
314 | |
} |
315 | 6 | } |
316 | 4 | session.flush(); |
317 | |
if (DEBUG) { |
318 | |
List<?> list = session.createQuery("from PlayerGroup ").list(); |
319 | |
List<PlayerGroup> lpg = (List<PlayerGroup>)list; |
320 | |
for (PlayerGroup pg : lpg) { |
321 | |
logger.info("store PlayerGroup: id: " + pg.getId() + " gid: " + pg.getGroupId() + " pid: " + pg.getPlayerId()); |
322 | |
} |
323 | |
} |
324 | 4 | session.getTransaction().commit(); |
325 | 4 | } |
326 | |
|
327 | |
private void store(GroupOfPlayers groupOfPlayers) { |
328 | 4 | Session session = hibernateUtil.getSessionFactory().getCurrentSession(); |
329 | |
if (DEBUG) |
330 | |
logger.info("db: Store=" + groupOfPlayers.getGroupName()); |
331 | 4 | session.saveOrUpdate(groupOfPlayers); |
332 | 4 | Iterator<PlayerGroup> iter = groupOfPlayers.getPlayerGroups().iterator(); |
333 | 9 | while (iter.hasNext()) { |
334 | 5 | PlayerGroup pg = iter.next(); |
335 | 5 | if (pg.isTagForDelete()) { |
336 | 1 | session.delete(pg); |
337 | 1 | iter.remove(); |
338 | 4 | } else if (pg.isDirty()) { |
339 | 3 | pg.setGroupId(groupOfPlayers.getId()); |
340 | 3 | session.save(pg); |
341 | 3 | pg.setDirty(false); |
342 | |
} |
343 | 5 | } |
344 | 4 | } |
345 | |
} |