1
2 package hoplugins.teamAnalyzer.dao;
3
4 import hoplugins.Commons;
5
6 import java.sql.ResultSet;
7 import java.sql.SQLException;
8
9
10 /***
11 * DB Access Manager for Divider locations
12 *
13 * @author <a href=mailto:draghetto@users.sourceforge.net>Massimiliano Amato</a>
14 */
15 public class DividerDAO {
16
17
18 static {
19 checkTable();
20 }
21
22
23
24 /***
25 * Creates a new DividerDAO object.
26 */
27 public DividerDAO() {
28 }
29
30
31
32 /***
33 * Set the value in the databse
34 *
35 * @param key The key
36 * @param position the value
37 */
38 public static void setDividerPosition(String key, int position) {
39 String query = "update TEAMANALYZER_DIVIDER set POSITIONE = " + position
40 + " where NAME = '" + key + "'";
41 int count = Commons.getModel().getAdapter().executeUpdate(query);
42
43 if (count == 0) {
44 Commons.getModel().getAdapter().executeUpdate("insert into TEAMANALYZER_DIVIDER (NAME, POSITIONE) values ('"
45 + key + "', " + position + ")");
46 }
47 }
48
49 /***
50 * Returns a value
51 *
52 * @param key the key to be returned
53 *
54 * @return the value
55 */
56 public static int getDividerPosition(String key) {
57 String query = "select POSITIONE from TEAMANALYZER_DIVIDER where NAME='" + key + "'";
58 ResultSet rs = Commons.getModel().getAdapter().executeQuery(query);
59
60 try {
61 rs.next();
62
63 return rs.getInt("POSITIONE");
64 } catch (SQLException e) {
65 return 0;
66 }
67 }
68
69 /***
70 * Check if the table exists, if not create it with default values
71 */
72 private static void checkTable() {
73 try {
74 ResultSet rs = Commons.getModel().getAdapter().executeQuery("select * from TEAMANALYZER_DIVIDER");
75 rs.next();
76 } catch (Exception e) {
77 Commons.getModel().getAdapter().executeUpdate("CREATE TABLE TEAMANALYZER_DIVIDER(NAME varchar(20),POSITIONE integer)");
78 setDividerPosition("LowerLeftDivider", 115);
79 setDividerPosition("UpperLeftDivider", 380);
80 setDividerPosition("MainDivider", 230);
81 setDividerPosition("BottomDivider", 550);
82 }
83
84 try {
85 ResultSet rs = Commons.getModel().getAdapter().executeQuery("select NAME from TEAMANALYZER_DIVIDER");
86 rs.next();
87 } catch (Exception e) {
88 Commons.getModel().getAdapter().executeUpdate("ALTER TABLE TEAMANALYZER_DIVIDER ADD COLUMN NAME varchar(20)");
89 Commons.getModel().getAdapter().executeUpdate("UPDATE TEAMANALYZER_DIVIDER SET NAME=KEY");
90 Commons.getModel().getAdapter().executeUpdate("ALTER TABLE TEAMANALYZER_DIVIDER DROP COLUMN KEY");
91 }
92 }
93 }