f9706595d479aa51e60342b4e7ec0c6cffa94d58
[proteocache.git] / datadb / compbio / cassandra / readers / CassandraReader.java
1 package compbio.cassandra.readers;
2
3 import org.apache.log4j.Logger;
4
5 import com.datastax.driver.core.ResultSet;
6 import com.datastax.driver.core.Session;
7 import com.datastax.driver.core.exceptions.QueryExecutionException;
8 import com.datastax.driver.core.exceptions.QueryValidationException;
9
10 import compbio.cassandra.CassandraNativeConnector;
11
12 public class CassandraReader {
13         protected static long earlestDate = 0;
14         protected Session session;
15         protected static Logger log = Logger.getLogger(CassandraNativeConnector.class);
16
17         public CassandraReader() {
18                 Session inis = CassandraNativeConnector.getSession();
19                 setSession(inis);
20         }
21
22         public void setSession(Session s) {
23                 assert s != null;
24                 session = s;
25         }
26
27         protected ResultSet CassandraQuery(String command) {
28                 try {
29                         ResultSet results = session.execute(command);
30                         return results;
31                 } catch (QueryExecutionException e) {
32                         String mess = "ProteoCache Cassandra DB interface: query execution exception...\n   Command: " + command;
33                         System.out.println(mess);
34                         log.error(mess);
35                         log.error(e.getLocalizedMessage(), e.getCause());
36                         return null;
37                 } catch (QueryValidationException e) {
38                         String mess = "CProteoCache Cassandra DB interface: query validation exception...\n   Command: " + command;
39                         System.out.println(mess);
40                         log.error(mess);
41                         log.error(e.getLocalizedMessage(), e.getCause());
42                         return null;
43                 }
44
45         }
46
47         /*
48          * find the earliest date in the database
49          */
50         public static long earliestDate() {
51                 earlestDate = CassandraNativeConnector.getEarliestDateInDB();
52                 return earlestDate;
53         }
54         
55         /**
56          * prepares an example of either job id or IP for the DB
57          * 
58          * @param exampletype
59          *            defines which example you need (an existing job from the DB -
60          *            jobid, an IP - "ip")
61          * @return a string representation of the requested example, if the example
62          *         type is not known empty string is returned
63          */
64         public String getExample(String exampletype) {
65                 if (exampletype.equals("jobid")) {
66                         return "jp_NzBOJKo";
67                 } else if (exampletype.equals("ip")) {
68                         return "127.0.0.1";
69                 }
70                 return "";
71         }
72
73 }