View Javadoc

1   // %1126721329604:hoplugins.transfers.dao%
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      //~ Static fields/initializers -----------------------------------------------------------------
22  
23      /*** Name of the table in the HO database */
24      private static final String TABLE_NAME = "transfers_bookmarks"; //$NON-NLS-1$
25  
26      static {
27          checkTable();
28      }
29  
30      //~ Constructors -------------------------------------------------------------------------------
31  
32      /***
33       * Private default constuctor to prevent class instantiation. 
34       */
35      private BookmarkDAO() {
36      }
37  
38      //~ Methods ------------------------------------------------------------------------------------
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); //$NON-NLS-1$
49          sqlStmt.append(" WHERE type=" + type); //$NON-NLS-1$
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); //$NON-NLS-1$
79          sqlStmt.append("(type, id, name) VALUES ("); //$NON-NLS-1$
80          sqlStmt.append(type + ","); //$NON-NLS-1$
81          sqlStmt.append(id + ","); //$NON-NLS-1$
82          sqlStmt.append("'" + Commons.getModel().getHelper().encodeString4Database(name) + "',"); //$NON-NLS-1$ //$NON-NLS-2$
83          sqlStmt.append(")"); //$NON-NLS-1$
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); //$NON-NLS-1$
96          sqlStmt.append(" WHERE type=" + type + " AND id=" + id); //$NON-NLS-1$
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); //$NON-NLS-1$
109             rs.next();
110         } catch (Exception e) {
111             final StringBuffer sqlStmt = new StringBuffer("CREATE TABLE " + TABLE_NAME); //$NON-NLS-1$
112             sqlStmt.append("("); //$NON-NLS-1$
113             sqlStmt.append("type INTEGER NOT NULL,"); //$NON-NLS-1$
114             sqlStmt.append("id INTEGER NOT NULL,"); //$NON-NLS-1$
115             sqlStmt.append("name VARCHAR(256),"); //$NON-NLS-1$
116             sqlStmt.append("PRIMARY KEY (type, id)"); //$NON-NLS-1$
117             sqlStmt.append(")"); //$NON-NLS-1$
118 
119             Commons.getModel().getAdapter().executeUpdate(sqlStmt.toString());
120         }
121     }
122 }