1
2 package hoplugins.trainingExperience.dao;
3
4 import hoplugins.Commons;
5
6 import java.sql.ResultSet;
7 import java.sql.SQLException;
8
9
10 /***
11 * DAO that store the Divider position into the HO Database
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 * Store the new divider position value into the DB
34 *
35 * @param key Divider indentificaton
36 * @param position New divider position value
37 */
38 public static void setDividerPosition(String key, int position) {
39 String query = "update TRAININGEXPERIENCE_DIVIDER set POSITIONE = "
40 + position
41 + " where NAME = '" + key + "'";
42 int count = Commons.getModel().getAdapter().executeUpdate(query);
43
44 if (count == 0) {
45 Commons.getModel().getAdapter().executeUpdate("insert into TRAININGEXPERIENCE_DIVIDER (NAME, POSITIONE) values ('"
46 + key + "', " + position + ")");
47 }
48 }
49
50 /***
51 * Return the divider position value from the DB
52 *
53 * @param key Divider indentificaton
54 *
55 * @return the divider position value
56 */
57 public static int getDividerPosition(String key) {
58 String query = "select POSITIONE from TRAININGEXPERIENCE_DIVIDER where NAME='" + key + "'";
59 ResultSet rs = Commons.getModel().getAdapter().executeQuery(query);
60
61 try {
62 rs.next();
63
64 return rs.getInt("POSITIONE");
65 } catch (SQLException e) {
66 return 0;
67 } finally {
68 try {
69 rs.close();
70 } catch (SQLException e1) {
71 }
72 }
73 }
74
75 /***
76 * Method that check if the table exists, if not creates it and sets the values to default
77 */
78 private static void checkTable() {
79 try {
80 ResultSet rs = Commons.getModel().getAdapter().executeQuery("select * from TRAININGEXPERIENCE_DIVIDER");
81 rs.next();
82 } catch (Exception e) {
83 Commons.getModel().getAdapter().executeUpdate("CREATE TABLE TRAININGEXPERIENCE_DIVIDER(NAME varchar(20),POSITIONE integer)");
84 setDividerPosition("LowerLeftDivider", 115);
85 setDividerPosition("RightDivider", 778);
86 setDividerPosition("MainDivider", 429);
87 setDividerPosition("BottomDivider", 205);
88 setDividerPosition("TrainingDivider", 334);
89 }
90
91 try {
92 ResultSet rs = Commons.getModel().getAdapter().executeQuery("select NAME from TRAININGEXPERIENCE_DIVIDER");
93 rs.next();
94 } catch (Exception e) {
95 Commons.getModel().getAdapter().executeUpdate("ALTER TABLE TRAININGEXPERIENCE_DIVIDER ADD COLUMN NAME varchar(20)");
96 Commons.getModel().getAdapter().executeUpdate("UPDATE TRAININGEXPERIENCE_DIVIDER SET NAME=KEY");
97 Commons.getModel().getAdapter().executeUpdate("ALTER TABLE TRAININGEXPERIENCE_DIVIDER DROP COLUMN KEY");
98 }
99 }
100 }