Advertisement
Guest User

Untitled

a guest
Apr 16th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.20 KB | None | 0 0
  1. package es.cbgp.upm.logic;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.Statement;
  8. import java.util.Iterator;
  9. import java.util.Properties;
  10. import java.util.Set;
  11. import java.util.TreeSet;
  12.  
  13. import es.cbgp.upm.objects.Input;
  14. import es.cbgp.upm.objects.Output;
  15. import es.cbgp.upm.objects.Result;
  16.  
  17. public class DerbyDBManager {
  18.  
  19.     private Connection conn;
  20.  
  21.     private int api;
  22.     private int reasoner;
  23.     private int mode;
  24.     private MyLogger logger;
  25.    
  26.     public DerbyDBManager(int a, int r, int m, MyLogger l) throws Exception {
  27.         this.api = a;
  28.         this.reasoner = r;
  29.         this.mode = m;
  30.         this.logger = l;
  31.         load();
  32.     }
  33.  
  34.     public void load() throws Exception {
  35.         String driver = ConfigManager.getConfig(Constants.DERBY_DB_DRIVER);
  36.         String protocol = ConfigManager.getConfig(Constants.DERBY_DB_PROTOCOL);
  37.         String dbName = ConfigManager.getConfig(Constants.DERBY_DB_NAME);
  38.         Class.forName(driver).newInstance();
  39.         Properties props = new Properties();
  40.         props.put(Constants.USER,
  41.                 ConfigManager.getConfig(Constants.DERBY_DB_USER));
  42.         props.put(Constants.PASSWORD,
  43.                 ConfigManager.getConfig(Constants.DERBY_DB_PASSWORD));
  44.         conn = DriverManager.getConnection(protocol + "db/" + dbName
  45.                 + ";create=true", props);
  46.         conn.setAutoCommit(false);
  47.     }
  48.  
  49.     public boolean existsSchema() throws Exception {
  50.         Statement s = conn.createStatement();
  51.         try {
  52.             return s.execute("SELECT * from inputs");
  53.         } catch (Exception e) {
  54.             return false;
  55.         }
  56.     }
  57.  
  58.     public Result createSchema() {
  59.         try {
  60.             Statement s = conn.createStatement();
  61.             s.execute("create table inputs(id int NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), input_class varchar(512), error varchar(1000), api varchar(128))");
  62.             s.execute("create table outputs(id int NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), output_class varchar(512), error varchar(1000), api varchar(128))");
  63.             s.execute("create table pairs(id int, input_class varchar(512), output_class varchar(512), reasoned smallint, error varchar(1000), results varchar(1000), api varchar(128), reasoner varchar(128), mode varchar(128))");
  64.         } catch (Exception e) {
  65.             return new Result(false, e.getMessage());
  66.         }
  67.         return new Result(true);
  68.     }
  69.  
  70.     public Set<Input> getInputsFromDB() {
  71.         Set<Input> inputs = new TreeSet<Input>();
  72.         try {
  73.             ResultSet rs = conn.createStatement(
  74.                     ResultSet.TYPE_SCROLL_INSENSITIVE,
  75.                     ResultSet.CONCUR_UPDATABLE,
  76.                     ResultSet.HOLD_CURSORS_OVER_COMMIT).executeQuery(
  77.                     "SELECT * from inputs");
  78.             int rowcount = 0;
  79.             if (rs.last()) {
  80.                 rowcount = rs.getRow();
  81.                 rs.beforeFirst();
  82.             }
  83.             if (rowcount <= 0) {
  84.                 return inputs;
  85.             } else {
  86.                 while (!rs.next()) {
  87.                     System.out.println(rs.getString("input_class"));
  88.                     // String inClass = rs.getString(1);
  89.                     // inputs.add(new Input(inClass));
  90.                 }
  91.             }
  92.         } catch (Exception e) {
  93.             e.printStackTrace();
  94.         }
  95.         return inputs;
  96.     }
  97.  
  98.     public Set<Output> getOutputsFromDB() throws Exception {
  99.         Set<Output> outputs = new TreeSet<Output>();
  100.         ResultSet rs = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
  101.                 ResultSet.CONCUR_UPDATABLE, ResultSet.HOLD_CURSORS_OVER_COMMIT)
  102.                 .executeQuery("SELECT output_class from outputs");
  103.         while (!rs.next()) {
  104.             String ouClass = rs.getString(1);
  105.             outputs.add(new Output(ouClass));
  106.         }
  107.         return outputs;
  108.     }
  109.  
  110.     /**
  111.      * Method to insert the inputs.
  112.      * @param ri Receives the set with the inputs.
  113.      * @throws Exception It can throw an exception.
  114.      */
  115.     public void insertInputs(Set<Input> ri) throws Exception {
  116.         PreparedStatement psInsert = null;
  117.         Iterator<Input> it = ri.iterator();
  118.         while (it.hasNext()) {
  119.             Input in = it.next();
  120.             psInsert = conn
  121.                     .prepareStatement("insert into inputs(input_class,api) values (?, ?)");
  122.             psInsert.setString(1, in.getObject());
  123.             psInsert.setString(2, Constants.APIS[this.api]);
  124.             this.logger.log("Inserting input in db: " + in.getObject() + " - API[" + Constants.APIS[this.api] + "]");
  125.             psInsert.executeUpdate();
  126.             psInsert.close();
  127.         }
  128.  
  129.     }
  130.  
  131.     public void close() throws Exception {
  132.         this.conn.commit();
  133.         this.conn.close();
  134.     }
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement