View Javadoc

1   // %2702752272:hoplugins.transfers.dao%
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   * 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 final class DividerDAO {
16      //~ Static fields/initializers -----------------------------------------------------------------
17  
18      static {
19          checkTable();
20      }
21  
22      //~ Constructors -------------------------------------------------------------------------------
23  
24      /***
25       * Creates a new DividerDAO object.
26       */
27      private DividerDAO() {
28      }
29  
30      //~ Methods ------------------------------------------------------------------------------------
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          final String query = "update TRANSFERS_DIVIDER set POSITIONE = " + position //$NON-NLS-1$
40                               + " where NAME = '" + key + "'"; //$NON-NLS-1$ //$NON-NLS-2$
41          final int count = Commons.getModel().getAdapter().executeUpdate(query);
42  
43          if (count == 0) {
44              Commons.getModel().getAdapter().executeUpdate("insert into TRANSFERS_DIVIDER (NAME, POSITIONE) values ('" //$NON-NLS-1$
45                                                            + key + "', " + position + ")"); //$NON-NLS-1$ //$NON-NLS-2$
46          }
47      }
48  
49      /***
50       * Return the divider position value from the DB
51       *
52       * @param key Divider indentificaton
53       *
54       * @return the divider position value
55       */
56      public static int getDividerPosition(String key) {
57          final String query = "select POSITIONE from TRANSFERS_DIVIDER where NAME='" + key + "'"; //$NON-NLS-1$ //$NON-NLS-2$
58          final ResultSet rs = Commons.getModel().getAdapter().executeQuery(query);
59  
60          try {
61              rs.next();
62              return rs.getInt("POSITIONE"); //$NON-NLS-1$
63          } catch (SQLException e) {
64              return 0;
65          } finally {
66              try {
67                  rs.close();
68              } catch (SQLException e1) {
69              }
70          }
71      }
72  
73      /***
74       * Method that check if the table exists, if not creates it and sets the values to default
75       */
76      private static void checkTable() {
77          try {
78              final ResultSet rs = Commons.getModel().getAdapter().executeQuery("select * from TRANSFERS_DIVIDER"); //$NON-NLS-1$
79              rs.next();
80          } catch (Exception e) {
81              Commons.getModel().getAdapter().executeUpdate("CREATE TABLE TRANSFERS_DIVIDER(NAME varchar(20),POSITIONE integer)"); //$NON-NLS-1$
82              setDividerPosition("HistoryTabDivider", 400); //$NON-NLS-1$
83              setDividerPosition("TypeTabDivider", 400); //$NON-NLS-1$
84          }
85  
86          try {
87              final ResultSet rs = Commons.getModel().getAdapter().executeQuery("select NAME from TRANSFERS_DIVIDER"); //$NON-NLS-1$
88              rs.next();
89          } catch (Exception e) {
90              Commons.getModel().getAdapter().executeUpdate("ALTER TABLE TRANSFERS_DIVIDER ADD COLUMN NAME varchar(20)");
91              Commons.getModel().getAdapter().executeUpdate("UPDATE TRANSFERS_DIVIDER SET NAME=KEY");
92              Commons.getModel().getAdapter().executeUpdate("ALTER TABLE TRANSFERS_DIVIDER DROP COLUMN KEY");
93          }
94      }
95  }