Fix bug for inserting job without known predictions
[proteocache.git] / datadb / compbio / cassandra / CassandraWriter.java
1 package compbio.cassandra;
2
3 import java.io.IOException;
4 import java.util.List;
5
6 import org.apache.log4j.Logger;
7
8 import com.datastax.driver.core.Row;
9 import com.datastax.driver.core.Session;
10 import com.datastax.driver.core.ResultSet;
11 import com.datastax.driver.core.PreparedStatement;
12 import com.datastax.driver.core.BoundStatement;
13
14 import compbio.engine.ProteoCachePropertyHelperManager;
15 import compbio.util.PropertyHelper;
16
17 public class CassandraWriter {
18         private Session session;
19         private static final PropertyHelper ph = ProteoCachePropertyHelperManager.getPropertyHelper();
20         private static Logger log = Logger.getLogger(CassandraNativeConnector.class);
21
22         CassandraWriter() {
23                 Session inis = CassandraNativeConnector.getSession();
24                 setSession(inis);
25         }
26
27         public void setSession(Session s) {
28                 assert s != null;
29                 session = s;
30         }
31
32         public boolean JobisNotInsterted(String jobid) {
33                 ResultSet results1 = session.execute("SELECT * FROM ProteinLog WHERE JobID = '" + jobid + "';");
34                 if (results1.isExhausted()) {
35                         return true;
36                 }
37                 return false;
38         }
39
40         public boolean JobisNotArchived(String jobid) {
41                 ResultSet results1 = session.execute("SELECT * FROM JpredArchive WHERE JobID = '" + jobid + "';");
42                 if (results1.isExhausted()) {
43                         return true;
44                 }
45                 return false;
46         }
47
48         /*
49          * inserting data into the tables for queries
50          */
51         public int FormQueryTables(long jobtime, String startdate, String enddate, String ip, String jobid, String statusEx,
52                         String statusFinal, String protein, List<FastaSequence> predictions) {
53                 if (JobisNotInsterted(jobid)) {
54                         String com1 = "INSERT INTO ProteinLog " + "(JobID, IP, DataBegin, DataEnd, FinalStatus, ExecutionStatus, Protein)"
55                                         + " VALUES ('" + jobid + "','" + ip + "','" + startdate + "','" + enddate + "','" + statusFinal + "','" + statusEx
56                                         + "','" + protein + "');";
57                         session.execute(com1);
58
59                         String com2 = "INSERT INTO ProteinData " + "(jobtime, JobID, Protein)" + " VALUES (" + jobtime + ",'" + jobid + "','" + protein
60                                         + "');";
61                         session.execute(com2);
62
63                         String allpredictions = "";
64                         for (FastaSequence pred : predictions) {
65                                 String predictionname = pred.getId();
66                                 String prediction = pred.getSequence().replaceAll("\n", "");
67                                 allpredictions += "'" + predictionname + "':'" + prediction + "',";
68                         }
69                         String final_prediction = "";
70                         if (!allpredictions.equals("")) {
71                                 final_prediction = allpredictions.substring(0, allpredictions.length() - 1);
72                         }
73
74                         String check2 = "SELECT * FROM ProteinRow WHERE JobID = '" + jobid + "';";
75                         ResultSet results2 = session.execute(check2);
76                         if (results2.isExhausted()) {
77                                 String com3 = "INSERT INTO ProteinRow " + "(Protein, JobID, Predictions)" + " VALUES ('" + protein + "','" + jobid + "',{"
78                                                 + final_prediction + "});";
79                                 session.execute(com3);
80                         }
81
82                         // update some internal query tables
83                         String check3 = "SELECT * FROM MainParameters WHERE Name = 'EarliestJobDate';";
84                         ResultSet results3 = session.execute(check3);
85                         boolean updateparameter = true;
86                         if (!results3.isExhausted()) {
87                                 Row r = results3.one();
88                                 if (jobtime >= Long.parseLong(r.getString("Value")))
89                                         updateparameter = false;
90                         }
91                         if (updateparameter) {
92                                 String com = "INSERT INTO MainParameters " + "(Name, Value)" + " VALUES ('EarliestJobDate','" + String.valueOf(jobtime)
93                                                 + "');";
94                                 session.execute(com);
95                         }
96                         String check4 = "SELECT * FROM JobDateInfo WHERE jobday = " + jobtime + ";";
97                         ResultSet results4 = session.execute(check4);
98                         updateparameter = true;
99                         int njobs = 1;
100                         if (!results4.isExhausted()) {
101                                 Row r = results4.one();
102                                 njobs += r.getLong("Total");
103                         }
104                         String com = "INSERT INTO JobDateInfo " + "(jobday, Total)" + " VALUES (" + jobtime + "," + njobs + ");";
105                         session.execute(com);
106
107                         return 1;
108                 }
109                 return 0;
110         }
111
112         /*
113          * insert data from a real Jpred job: timing+IP, Execution Status, Final
114          * status, protein sequence, predictions, alignment, LOG and tar.gz files
115          */
116         public int ArchiveData(long starttime, long exectime, String ip, String jobid, String statusEx, String statusFinal, String protein,
117                         List<FastaSequence> predictions, List<FastaSequence> seqs, String LogFile, String archivepath) {
118                 if (JobisNotArchived(jobid)) {
119                         String log = LogFile.replaceAll("'", "");
120                         session.execute("INSERT INTO JpredArchive (JobID, Protein, IP, StartTime, ExecTime,LOG) VALUES ('" + jobid + "','" + protein
121                                         + "','" + ip + "'," + starttime + "," + exectime + ",'" + log + "');");
122                         if (false) {
123                                 PreparedStatement statement = session.prepare("INSERT INTO JpredArchive (JobID, archive) VALUES (?,?);");
124                                 BoundStatement boundStatement = new BoundStatement(statement);
125                                 session.execute(boundStatement.bind(jobid, archivepath));
126                         }
127
128                         for (FastaSequence p : predictions) {
129                                 session.execute("UPDATE JpredArchive SET predictions = predictions + {'" + p.getId() + "':'"
130                                                 + p.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + jobid + "';");
131                         }
132
133                         for (FastaSequence s : seqs) {
134                                 session.execute("UPDATE JpredArchive SET alignment = alignment + {'" + s.getId() + "':'"
135                                                 + s.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + jobid + "';");
136                         }
137                         return 1;
138                 }
139                 return 0;
140         }
141
142 }