1
2 package hoplugins.transfers.dao;
3
4 import hoplugins.Commons;
5
6 import hoplugins.transfers.vo.Bookmark;
7
8 import java.sql.ResultSet;
9 import java.sql.SQLException;
10
11 import java.util.List;
12 import java.util.Vector;
13
14
15 /***
16 * DAO to store and retrieve bookmarks in the HO database.
17 *
18 * @author <a href=mailto:nethyperon@users.sourceforge.net>Boy van der Werf</a>
19 */
20 public final class BookmarkDAO {
21
22
23 /*** Name of the table in the HO database */
24 private static final String TABLE_NAME = "transfers_bookmarks";
25
26 static {
27 checkTable();
28 }
29
30
31
32 /***
33 * Private default constuctor to prevent class instantiation.
34 */
35 private BookmarkDAO() {
36 }
37
38
39
40 /***
41 * Gets all bookmarks of a specified type.
42 *
43 * @param type Type of bookmark
44 *
45 * @return List of bookmarks
46 */
47 public static List getBookmarks(int type) {
48 final StringBuffer sqlStmt = new StringBuffer("SELECT * FROM " + TABLE_NAME);
49 sqlStmt.append(" WHERE type=" + type);
50
51 final List results = new Vector();
52 final ResultSet rs = Commons.getModel().getAdapter().executeQuery(sqlStmt.toString());
53
54 if (rs == null) {
55 return results;
56 }
57
58 try {
59 while (rs.next()) {
60 results.add(new Bookmark(type, rs.getInt("id"),
61 Commons.getModel().getHelper().decodeStringFromDatabase(rs
62 .getString("name"))));
63 }
64 } catch (SQLException e) {
65 }
66
67 return results;
68 }
69
70 /***
71 * Adds a bookmark to the HO database.
72 *
73 * @param type Type of bookmark
74 * @param id Bookmark id
75 * @param name Bookmark name
76 */
77 public static void addBookmark(int type, int id, String name) {
78 final StringBuffer sqlStmt = new StringBuffer("INSERT INTO " + TABLE_NAME);
79 sqlStmt.append("(type, id, name) VALUES (");
80 sqlStmt.append(type + ",");
81 sqlStmt.append(id + ",");
82 sqlStmt.append("'" + Commons.getModel().getHelper().encodeString4Database(name) + "',");
83 sqlStmt.append(")");
84
85 Commons.getModel().getAdapter().executeUpdate(sqlStmt.toString());
86 }
87
88 /***
89 * Remove a bookmark from the HO database.
90 *
91 * @param type Type of bookmark
92 * @param id Bookmark id
93 */
94 public static void removeBookmark(int type, int id) {
95 final StringBuffer sqlStmt = new StringBuffer("DELETE FROM " + TABLE_NAME);
96 sqlStmt.append(" WHERE type=" + type + " AND id=" + id);
97
98 Commons.getModel().getAdapter().executeUpdate(sqlStmt.toString());
99 }
100
101 /***
102 * Method that check if the table exists, if not creates it and sets the values to default
103 */
104 private static void checkTable() {
105 ResultSet rs = null;
106
107 try {
108 rs = Commons.getModel().getAdapter().executeQuery("SELECT * FROM " + TABLE_NAME);
109 rs.next();
110 } catch (Exception e) {
111 final StringBuffer sqlStmt = new StringBuffer("CREATE TABLE " + TABLE_NAME);
112 sqlStmt.append("(");
113 sqlStmt.append("type INTEGER NOT NULL,");
114 sqlStmt.append("id INTEGER NOT NULL,");
115 sqlStmt.append("name VARCHAR(256),");
116 sqlStmt.append("PRIMARY KEY (type, id)");
117 sqlStmt.append(")");
118
119 Commons.getModel().getAdapter().executeUpdate(sqlStmt.toString());
120 }
121 }
122 }