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 Transfer uploading status
12 *
13 * @author <a href=mailto:draghetto@users.sourceforge.net>Massimiliano Amato</a>
14 */
15 public final class TransferStatusDAO {
16
17
18 /*** Name of the table in the HO database */
19 private static final String TABLE_NAME = "TRANSFERS_UPLOAD";
20
21 static {
22 checkTable();
23 }
24
25
26
27 /***
28 * Private default constuctor to prevent class instantiation.
29 */
30 private TransferStatusDAO() {
31 }
32
33
34
35 /***
36 * Set the uploaded status in the databse
37 *
38 * @param transferId the uploaded transfer id
39 */
40 public static void setUploaded(int transferId) {
41 final String query = "update " + TABLE_NAME + " set STATUS = TRUE where TRANSFER_ID ="
42 + transferId;
43 final int count = Commons.getModel().getAdapter().executeUpdate(query);
44
45 if (count == 0) {
46 Commons.getModel().getAdapter().executeUpdate("insert into " + TABLE_NAME
47 + " (TRANSFER_ID, STATUS) values ("
48 + transferId + ",TRUE)");
49 }
50 }
51
52 /***
53 * Returns if the tranfer has been already uploaded to server or not
54 *
55 * @param transferId the uploaded transfer id
56 *
57 * @return true if transferId has been already uploaded
58 */
59 public static boolean isUploaded(int transferId) {
60 final String query = "select STATUS from " + TABLE_NAME + " where TRANSFER_ID="
61 + transferId;
62 final ResultSet rs = Commons.getModel().getAdapter().executeQuery(query);
63
64 try {
65 rs.next();
66 return rs.getBoolean("STATUS");
67 } catch (SQLException e) {
68 return false;
69 }
70 }
71
72 /***
73 * Returns if the tranfer has been already uploaded to server or not
74 */
75 public static void resetTransfers() {
76 final String query = "DELETE from " + TABLE_NAME;
77 Commons.getModel().getAdapter().executeUpdate(query);
78 }
79
80 /***
81 * Check if the table exists, if not create it with default values
82 */
83 private static void checkTable() {
84 try {
85 final ResultSet rs = Commons.getModel().getAdapter().executeQuery("select * from "
86 + TABLE_NAME);
87 rs.next();
88 } catch (Exception e) {
89 Commons.getModel().getAdapter().executeUpdate("CREATE TABLE " + TABLE_NAME
90 + " (TRANSFER_ID INTEGER,STATUS BIT)");
91 Commons.getModel().getAdapter().executeUpdate("CREATE INDEX plstatus_id ON "
92 + TABLE_NAME + " (TRANSFER_ID)");
93 }
94
95 try {
96 final ResultSet rs = Commons.getModel().getAdapter().executeQuery("select STATUS from "
97 + TABLE_NAME);
98 rs.next();
99 } catch (Exception e) {
100 Commons.getModel().getAdapter().executeUpdate("ALTER TABLE " + TABLE_NAME
101 + " ADD COLUMN STATUS Bit");
102 Commons.getModel().getAdapter().executeUpdate("UPDATE " + TABLE_NAME
103 + " SET STATUS=UPLOADED");
104 Commons.getModel().getAdapter().executeUpdate("ALTER TABLE " + TABLE_NAME
105 + " DROP COLUMN UPLOADED");
106 }
107 }
108 }