1
2 package hoplugins.transfers.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 Plugin Settings
12 *
13 * @author <a href=mailto:draghetto@users.sourceforge.net>Massimiliano Amato</a>
14 */
15 public final class TransferSettingDAO {
16
17
18 /*** Name of the table in the HO database */
19 private static final String TABLE_NAME = "TRANSFERS_SETTINGS";
20
21 static {
22 checkTable();
23 }
24
25
26
27 /***
28 * Private default constuctor to prevent class instantiation.
29 */
30 private TransferSettingDAO() {
31 }
32
33
34
35 /***
36 * Set the toggle to enable/disable transfer upload
37 *
38 * @param auto Boolean value
39 */
40 public static void setAutomatic(boolean auto) {
41 setValue("AUTOMATIC", auto);
42 }
43
44 /***
45 * Gets the settings for automatic upload
46 *
47 * @return true if enabled
48 */
49 public static boolean isAutomatic() {
50 return getValue("AUTOMATIC", true);
51 }
52
53 /***
54 * Set the Plugin Status to started
55 */
56 public static void setCalendarFix() {
57 setValue("TRANSFERS-CALENDARFIX", true);
58 }
59
60 /***
61 * Gets the plugin status
62 *
63 * @return true if already started
64 */
65 public static boolean isCalendarFix() {
66 return getValue("TRANSFERS-CALENDARFIX", false);
67 }
68
69 /***
70 * Set the Plugin Status to started
71 */
72 public static void setFixed() {
73 setValue("FIXED", true);
74 }
75
76 /***
77 * Gets the plugin status
78 *
79 * @return true if already started
80 */
81 public static boolean isFixed() {
82 return getValue("FIXED", false);
83 }
84
85 /***
86 * Set the Plugin Status to started
87 */
88 public static void setStarted() {
89 setValue("STARTED", true);
90 }
91
92 /***
93 * Gets the plugin status
94 *
95 * @return true if already started
96 */
97 public static boolean isStarted() {
98 return getValue("STARTED", false);
99 }
100
101 /***
102 * Set the value in the databse
103 *
104 * @param key The key
105 * @param value the value
106 */
107 private static void setValue(String key, boolean value) {
108 String val = (value) ? "1" : "0";
109 String query = "update " + TABLE_NAME + " set VALUE = " + val + " where NAME = '" + key
110 + "'";
111 int count = Commons.getModel().getAdapter().executeUpdate(query);
112
113 if (count == 0) {
114 Commons.getModel().getAdapter().executeUpdate("insert into " + TABLE_NAME
115 + " (NAME, VALUE) values ('" + key
116 + "', " + val + ")");
117 }
118 }
119
120 /***
121 * Returns a value
122 *
123 * @param key the key to be returned
124 * @param defaultValue to be used if not existing
125 *
126 * @return the value
127 */
128 private static boolean getValue(String key, boolean defaultValue) {
129 String query = "select VALUE from " + TABLE_NAME + " where NAME='" + key + "'";
130 ResultSet rs = Commons.getModel().getAdapter().executeQuery(query);
131
132 try {
133 rs.next();
134 return rs.getBoolean("VALUE");
135 } catch (SQLException e) {
136 return defaultValue;
137 }
138 }
139
140 /***
141 * Check if the table exists, if not create it with default values
142 */
143 private static void checkTable() {
144 try {
145 final ResultSet rs = Commons.getModel().getAdapter().executeQuery("select * from "
146 + TABLE_NAME);
147 rs.next();
148 } catch (Exception e) {
149 Commons.getModel().getAdapter().executeUpdate("CREATE TABLE " + TABLE_NAME
150 + " (NAME VARCHAR(25),VALUE Bit)");
151 Commons.getModel().getAdapter().executeUpdate("CREATE INDEX plstatus_id ON "
152 + TABLE_NAME + " (NAME)");
153 }
154
155 try {
156 final ResultSet rs = Commons.getModel().getAdapter().executeQuery("select NAME from "
157 + TABLE_NAME);
158 rs.next();
159 } catch (Exception e) {
160 Commons.getModel().getAdapter().executeUpdate("ALTER TABLE " + TABLE_NAME
161 + " ADD COLUMN NAME varchar(20)");
162 Commons.getModel().getAdapter().executeUpdate("UPDATE " + TABLE_NAME + " SET NAME=KEY");
163 Commons.getModel().getAdapter().executeUpdate("DROP INDEX plstatus_id ON " + TABLE_NAME);
164 Commons.getModel().getAdapter().executeUpdate("ALTER TABLE " + TABLE_NAME
165 + " DROP COLUMN KEY");
166 Commons.getModel().getAdapter().executeUpdate("CREATE INDEX plstatus_id ON "
167 + TABLE_NAME + " (NAME)");
168
169 Commons.getModel().getAdapter().executeUpdate("ALTER TABLE " + TABLE_NAME
170 + " ADD COLUMN VALUE Bit");
171 Commons.getModel().getAdapter().executeUpdate("UPDATE " + TABLE_NAME
172 + " SET VALUE=STATUS");
173 Commons.getModel().getAdapter().executeUpdate("ALTER TABLE " + TABLE_NAME
174 + " DROP COLUMN STATUS");
175 }
176 }
177 }