Change structure of CFMerge branch 'servlets' of https://source.jalview.org/git/prote...
[proteocache.git] / datadb / compbio / cassandra / CassandraWriter.java
1 package compbio.cassandra;
2
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.Date;
6 import java.util.List;
7
8 import org.apache.log4j.Logger;
9
10 import com.datastax.driver.core.Row;
11 import com.datastax.driver.core.Session;
12 import com.datastax.driver.core.ResultSet;
13
14 import compbio.data.sequence.FastaSequence;
15 import compbio.engine.JpredJob;
16 import compbio.engine.ProteoCachePropertyHelperManager;
17 import compbio.util.PropertyHelper;
18
19 public class CassandraWriter {
20         private Session session;
21         private static final PropertyHelper ph = ProteoCachePropertyHelperManager.getPropertyHelper();
22         private static Logger log = Logger.getLogger(CassandraNativeConnector.class);
23
24         CassandraWriter() {
25                 Session inis = CassandraNativeConnector.getSession();
26                 setSession(inis);
27         }
28
29         public void setSession(Session s) {
30                 assert s != null;
31                 session = s;
32         }
33
34         public boolean JobisNotInsterted(String jobid) {
35                 ResultSet results1 = session.execute("SELECT * FROM ProteinLog WHERE JobID = '" + jobid + "';");
36                 if (results1.isExhausted()) {
37                         return true;
38                 }
39                 return false;
40         }
41
42         public boolean JobisNotArchived(String jobid) {
43                 ResultSet results1 = session.execute("SELECT * FROM JpredArchive WHERE JobID = '" + jobid + "';");
44                 if (results1.isExhausted()) {
45                         return true;
46                 }
47                 return false;
48         }
49
50         /*
51          * inserting data into the tables for queries
52          */
53         public int FormQueryTables(JpredJob job) {
54                 if (JobisNotInsterted(job.getJobID())) {
55                         int njobsOk = 0;
56                         int njobsStop = 0;
57                         int njobsError = 0;
58                         int njobsTimeOut = 0;
59                         String id = job.getJobID();
60                         String ip = job.getIP();
61                         String protein = job.getProtein();
62                         String finalstatus = job.getFinalStatus();
63                         String execstatus = job.getExecutionStatus();
64                         String com1 = "INSERT INTO ProteinLog " + "(JobID, IP, DataBegin, DataEnd, FinalStatus, ExecutionStatus, Protein)"
65                                         + " VALUES ('" + id + "','" + ip + "','" + job.getStartingTimeStr() + "','" + job.getEndTimeStr() + "','" + finalstatus
66                                         + "','" + execstatus + "','" + protein + "');";
67                         session.execute(com1);
68                         if (execstatus.equals("OK")) {
69                                 String com2 = "INSERT INTO ProteinData " + "(jobtime, JobID, ExecTime, Protein)" + " VALUES (" + job.getStartingDate() + ",'" + id
70                                                 + "'," + job.getExecutionTime() + ",'" + protein + "');";
71                                 session.execute(com2);
72                                 
73                                 String allpredictions = "";
74                                 List<FastaSequence> pr = job.getPredictions();
75                                 for (FastaSequence pred : pr) {
76                                         String predictionname = pred.getId();
77                                         String prediction = pred.getSequence().replaceAll("\n", "");
78                                         allpredictions += "'" + predictionname + "':'" + prediction + "',";
79                                 }
80                                 String final_prediction = "";
81                                 if (!allpredictions.equals("")) {
82                                         final_prediction = allpredictions.substring(0, allpredictions.length() - 1);
83                                 }
84
85                                 String check2 = "SELECT * FROM ProteinRow WHERE JobID = '" + job.getJobID() + "';";
86                                 ResultSet results2 = session.execute(check2);
87                                 if (results2.isExhausted()) {
88                                         String com3 = "INSERT INTO ProteinRow " + "(Protein, JobID, Predictions)" + " VALUES ('" + protein + "','" + id + "',{"
89                                                         + final_prediction + "});";
90                                         session.execute(com3);
91                                 }
92                                 njobsOk = 1;
93                         } else {
94                                 String com5 = "INSERT INTO FailLog " + "(jobtime, JobID, ExecTime, ip, FinalStatus)" + " VALUES ("
95                                                 + job.getStartingDate() + ",'" + id
96                                                 + "'," + job.getExecutionTime() + ",'" + ip + "', '" +  finalstatus + "');";
97                                 session.execute(com5);
98                                 if (finalstatus.equals("TIMEDOUT"))
99                                         njobsTimeOut = 1;
100                                 else if (finalstatus.equals("JPREDERROR"))
101                                         njobsError = 1;
102                                 else if (finalstatus.equals("STOPPED"))
103                                         njobsStop = 1;
104                         }
105                         // update some internal query tables
106                         String check3 = "SELECT * FROM MainParameters WHERE Name = 'EarliestJobDate';";
107                         ResultSet results3 = session.execute(check3);
108                         boolean updateparameter = true;
109                         if (!results3.isExhausted()) {
110                                 Row r = results3.one();         
111                                 if (job.getStartingDate() >= Long.parseLong(r.getString("Value")))
112                                         updateparameter = false;
113                         }
114                         if (updateparameter) {
115                                 String com = "INSERT INTO MainParameters " + "(Name, Value)" + " VALUES ('EarliestJobDate','" + job.getStartingDate()
116                                                 + "');";
117                                 session.execute(com);
118                         }
119                         String check4 = "SELECT * FROM JobDateInfo WHERE jobday = " + job.getStartingDate() + ";";
120                         ResultSet results4 = session.execute(check4);
121                         updateparameter = true;
122                         int njobs = 1;
123                         if (!results4.isExhausted()) {
124                                 Row r = results4.one();
125                                 njobs += r.getLong("Total");
126                                 njobsOk += r.getLong("TotalOK");
127                                 njobsError += r.getLong("TotalError");
128                                 njobsStop += r.getLong("TotalStopped");
129                                 njobsTimeOut += r.getLong("TotalTimeOut");
130                         }
131                         String com = "INSERT INTO JobDateInfo " + "(jobday, Total, TotalOK, TotalStopped, TotalError, TotalTimeOut)" + " VALUES ("
132                                         + job.getStartingDate() + "," + njobs + "," + njobsOk + "," + njobsStop + "," + njobsError + "," + njobsTimeOut + ");";
133                         session.execute(com);
134                         return 1;
135                 }
136                 return 0;
137         }
138
139         /*
140          * insert data from a real Jpred job: timing+IP, Execution Status, Final
141          * status, protein sequence, predictions, alignment, LOG and tar.gz files
142          */
143         public int ArchiveData(JpredJob job, String archivepath) {
144                 if (JobisNotArchived(job.getJobID())) {
145                         String id = job.getJobID();
146                         String log = job.getLog().replaceAll("'", "");
147                         String com = "INSERT INTO JpredArchive (JobID, Protein, IP, StartTime, ExecTime, FinalStatus, ExecutionStatus, LOG, ArchiveLink) VALUES ('" + id + "','"
148                                         + job.getProtein() + "','" + job.getIP() + "'," + job.getStartingTime() + "," + job.getExecutionTime() + ",'" + job.getFinalStatus() + "','"
149                                         + job.getExecutionStatus() + "','" + log + "','" + archivepath + "');";
150                         session.execute(com);
151                         List<FastaSequence> predictions = job.getPredictions();
152                         for (FastaSequence p : predictions) {
153                                 session.execute("UPDATE JpredArchive SET predictions = predictions + {'" + p.getId() + "':'"
154                                                 + p.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + id + "';");
155                         }
156
157                         List<FastaSequence> seqs = job.getAlignment();
158                         for (FastaSequence s : seqs) {
159                                 session.execute("UPDATE JpredArchive SET alignment = alignment + {'" + s.getId() + "':'"
160                                                 + s.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + id + "';");
161                         }
162                         return 1;
163                 }
164                 return 0;
165         }
166
167 }