View Javadoc

1   // %1126721329979: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   * DB Access Manager for Transfer type manual override
12   *
13   * @author <a href=mailto:draghetto@users.sourceforge.net>Massimiliano Amato</a>
14   */
15  public final class TransferTypeDAO {
16      //~ Static fields/initializers -----------------------------------------------------------------
17  
18      /*** Name of the table in the HO database */
19      private static final String TABLE_NAME = "TRANSFERS_TYPE"; //$NON-NLS-1$
20  
21      static {
22          checkTable();
23      }
24  
25      //~ Constructors -------------------------------------------------------------------------------
26  
27      /***
28       * Private default constuctor to prevent class instantiation. 
29       */
30      private TransferTypeDAO() {
31      }
32  
33      //~ Methods ------------------------------------------------------------------------------------
34  
35      /***
36       * Set the transfer type
37       *
38       * @param playerId the transfer id
39       * @param type TODO Missing Constructuor Parameter Documentation
40       */
41      public static void setType(int playerId, int type) {
42          final String query = "update " + TABLE_NAME + " set TYPE = " + type + " where PLAYER_ID ="
43                         + playerId; //$NON-NLS-1$ //$NON-NLS-2$
44          final int count = Commons.getModel().getAdapter().executeUpdate(query);
45  
46          if (count == 0) {
47              Commons.getModel().getAdapter().executeUpdate("insert into " + TABLE_NAME
48                                                            + " (PLAYER_ID, TYPE) values ("
49                                                            + playerId + "," + type + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
50          }
51      }
52  
53      /***
54       * Returns the transfer type
55       *
56       * @param playerId the playerId to know the transfer type
57       *
58       * @return true if transferId has been already uploaded
59       */
60      public static int getType(int playerId) {
61          final String query = "select TYPE from " + TABLE_NAME + " where PLAYER_ID=" + playerId; //$NON-NLS-1$ //$NON-NLS-2$
62          final ResultSet rs = Commons.getModel().getAdapter().executeQuery(query);
63  
64          try {
65              rs.next();
66              return rs.getInt("TYPE"); //$NON-NLS-1$
67          } catch (SQLException e) {
68              return -2;
69          }
70      }
71  
72      /***
73       * Check if the table exists, if not create it  with default values
74       */
75      private static void checkTable() {
76          try {
77              final ResultSet rs = Commons.getModel().getAdapter().executeQuery("select * from "
78                                                                          + TABLE_NAME); //$NON-NLS-1$
79              rs.next();
80          } catch (Exception e) {
81              Commons.getModel().getAdapter().executeUpdate("CREATE TABLE " + TABLE_NAME
82                                                            + " (PLAYER_ID INTEGER,TYPE INTEGER)"); //$NON-NLS-1$ //$NON-NLS-2$
83              Commons.getModel().getAdapter().executeUpdate("CREATE INDEX pltype_id ON " + TABLE_NAME
84                                                            + " (PLAYER_ID)"); //$NON-NLS-1$ //$NON-NLS-2$
85          }
86      }
87  }