Add mode comments to the classes
[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 /**
13  * basic reader of the ProteoCache database. It depends on open
14  * CassandraNativeConnector, which should provide an open session to the
15  * ProteoCache DB (in the default constructor)
16  * 
17  * @author as373024
18  *
19  */
20 public class CassandraReader {
21         protected static long earlestDate = 0;
22         protected Session session;
23         protected static Logger log = Logger.getLogger(CassandraNativeConnector.class);
24
25         /**
26          * default constructor.
27          */
28         public CassandraReader() {
29                 Session inis = CassandraNativeConnector.getSession();
30                 setSession(inis);
31         }
32
33         public void setSession(Session s) {
34                 assert s != null;
35                 session = s;
36         }
37
38         /**
39          * Basic query to the DB. The method throws exception if the input command
40          * is not valid or if there is a problem with execution of the command
41          * 
42          * @param command - reading CQL command
43          * @return resulting outout of the command
44          */
45         protected ResultSet CassandraQuery(String command) {
46                 try {
47                         ResultSet results = session.execute(command);
48                         return results;
49                 } catch (QueryExecutionException e) {
50                         String mess = "ProteoCache Cassandra DB interface: query execution exception...\n   Command: " + command;
51                         System.out.println(mess);
52                         log.error(mess);
53                         log.error(e.getLocalizedMessage(), e.getCause());
54                         return null;
55                 } catch (QueryValidationException e) {
56                         String mess = "CProteoCache Cassandra DB interface: query validation exception...\n   Command: " + command;
57                         System.out.println(mess);
58                         log.error(mess);
59                         log.error(e.getLocalizedMessage(), e.getCause());
60                         return null;
61                 }
62
63         }
64
65         /**
66          * finds the earliest date in the database
67          * 
68          * @return the earliest date (long)
69          */
70         public static long earliestDate() {
71                 earlestDate = CassandraNativeConnector.getEarliestDateInDB();
72                 return earlestDate;
73         }
74
75         /**
76          * prepares an example of either job id or IP for the DB
77          * 
78          * @param exampletype
79          *            defines which example you need (an existing job from the DB -
80          *            jobid, an IP - "ip")
81          * @return a string representation of the requested example, if the example
82          *         type is not known empty string is returned
83          */
84         public String getExample(String exampletype) {
85                 if (exampletype.equals("jobid")) {
86                         return "jp_NzBOJKo";
87                 } else if (exampletype.equals("ip")) {
88                         return "127.0.0.1";
89                 }
90                 return "";
91         }
92
93 }