RIntroduce Bean for Jpred job
[proteocache.git] / datadb / compbio / cassandra / CassandraWriter.java
index c478d68..48bbda7 100644 (file)
@@ -1,6 +1,5 @@
 package compbio.cassandra;
 
-import java.io.IOException;
 import java.util.List;
 
 import org.apache.log4j.Logger;
@@ -8,9 +7,8 @@ import org.apache.log4j.Logger;
 import com.datastax.driver.core.Row;
 import com.datastax.driver.core.Session;
 import com.datastax.driver.core.ResultSet;
-import com.datastax.driver.core.PreparedStatement;
-import com.datastax.driver.core.BoundStatement;
 
+import compbio.engine.JpredJob;
 import compbio.engine.ProteoCachePropertyHelperManager;
 import compbio.util.PropertyHelper;
 
@@ -48,20 +46,25 @@ public class CassandraWriter {
        /*
         * inserting data into the tables for queries
         */
-       public int FormQueryTables(long jobtime, String startdate, String enddate, String ip, String jobid, String statusEx,
-                       String statusFinal, String protein, List<FastaSequence> predictions) {
-               if (JobisNotInsterted(jobid)) {
+       public int FormQueryTables(JpredJob job) {
+               if (JobisNotInsterted(job.getJobID())) {
+                       String id = job.getJobID();
+                       String ip = job.getIP();
+                       String protein = job.getProtein();
+                       String finalstatus = job.getFinalStatus();
+                       String execstatus = job.getExecutionStatus();
                        String com1 = "INSERT INTO ProteinLog " + "(JobID, IP, DataBegin, DataEnd, FinalStatus, ExecutionStatus, Protein)"
-                                       + " VALUES ('" + jobid + "','" + ip + "','" + startdate + "','" + enddate + "','" + statusFinal + "','" + statusEx
-                                       + "','" + protein + "');";
+                                       + " VALUES ('" + id + "','" + ip + "','" + job.getStartingTimeStr() + "','" + job.getEndTimeStr() + "','" + finalstatus
+                                       + "','" + execstatus + "','" + protein + "');";
                        session.execute(com1);
 
-                       String com2 = "INSERT INTO ProteinData " + "(jobtime, JobID, Protein)" + " VALUES (" + jobtime + ",'" + jobid + "','" + protein
-                                       + "');";
+                       String com2 = "INSERT INTO ProteinData " + "(jobtime, JobID, Protein)" + " VALUES (" + job.getStartingDate() + ",'" + id
+                                       + "','" + protein + "');";
                        session.execute(com2);
 
                        String allpredictions = "";
-                       for (FastaSequence pred : predictions) {
+                       List<FastaSequence> pr = job.getPredictions();
+                       for (FastaSequence pred : pr) {
                                String predictionname = pred.getId();
                                String prediction = pred.getSequence().replaceAll("\n", "");
                                allpredictions += "'" + predictionname + "':'" + prediction + "',";
@@ -71,10 +74,10 @@ public class CassandraWriter {
                                final_prediction = allpredictions.substring(0, allpredictions.length() - 1);
                        }
 
-                       String check2 = "SELECT * FROM ProteinRow WHERE JobID = '" + jobid + "';";
+                       String check2 = "SELECT * FROM ProteinRow WHERE JobID = '" + job.getJobID() + "';";
                        ResultSet results2 = session.execute(check2);
                        if (results2.isExhausted()) {
-                               String com3 = "INSERT INTO ProteinRow " + "(Protein, JobID, Predictions)" + " VALUES ('" + protein + "','" + jobid + "',{"
+                               String com3 = "INSERT INTO ProteinRow " + "(Protein, JobID, Predictions)" + " VALUES ('" + protein + "','" + id + "',{"
                                                + final_prediction + "});";
                                session.execute(com3);
                        }
@@ -85,15 +88,15 @@ public class CassandraWriter {
                        boolean updateparameter = true;
                        if (!results3.isExhausted()) {
                                Row r = results3.one();
-                               if (jobtime >= Long.parseLong(r.getString("Value")))
+                               if (job.getStartingDate() >= Long.parseLong(r.getString("Value")))
                                        updateparameter = false;
                        }
                        if (updateparameter) {
-                               String com = "INSERT INTO MainParameters " + "(Name, Value)" + " VALUES ('EarliestJobDate','" + String.valueOf(jobtime)
+                               String com = "INSERT INTO MainParameters " + "(Name, Value)" + " VALUES ('EarliestJobDate','" + job.getStartingDateStr()
                                                + "');";
                                session.execute(com);
                        }
-                       String check4 = "SELECT * FROM JobDateInfo WHERE jobday = " + jobtime + ";";
+                       String check4 = "SELECT * FROM JobDateInfo WHERE jobday = " + job.getStartingDate() + ";";
                        ResultSet results4 = session.execute(check4);
                        updateparameter = true;
                        int njobs = 1;
@@ -101,7 +104,7 @@ public class CassandraWriter {
                                Row r = results4.one();
                                njobs += r.getLong("Total");
                        }
-                       String com = "INSERT INTO JobDateInfo " + "(jobday, Total)" + " VALUES (" + jobtime + "," + njobs + ");";
+                       String com = "INSERT INTO JobDateInfo " + "(jobday, Total)" + " VALUES (" + job.getStartingDate() + "," + njobs + ");";
                        session.execute(com);
 
                        return 1;
@@ -113,26 +116,25 @@ public class CassandraWriter {
         * insert data from a real Jpred job: timing+IP, Execution Status, Final
         * status, protein sequence, predictions, alignment, LOG and tar.gz files
         */
-       public int ArchiveData(long starttime, long exectime, String ip, String jobid, String statusEx, String statusFinal, String protein,
-                       List<FastaSequence> predictions, List<FastaSequence> seqs, String LogFile, String archivepath) {
-               if (JobisNotArchived(jobid)) {
-                       String log = LogFile.replaceAll("'", "");
-                       session.execute("INSERT INTO JpredArchive (JobID, Protein, IP, StartTime, ExecTime,LOG) VALUES ('" + jobid + "','" + protein
-                                       + "','" + ip + "'," + starttime + "," + exectime + ",'" + log + "');");
-                       if (false) {
-                               PreparedStatement statement = session.prepare("INSERT INTO JpredArchive (JobID, archive) VALUES (?,?);");
-                               BoundStatement boundStatement = new BoundStatement(statement);
-                               session.execute(boundStatement.bind(jobid, archivepath));
-                       }
+       public int ArchiveData(JpredJob job, String archivepath) {
+               if (JobisNotArchived(job.getJobID())) {
+                       String id = job.getJobID();
+                       String log = job.getLog().replaceAll("'", "");
+                       String com = "INSERT INTO JpredArchive (JobID, Protein, IP, StartTime, ExecTime,LOG, ArchiveLink) VALUES ('" + id + "','"
+                                       + job.getProtein() + "','" + job.getIP() + "'," + job.getStartingTime() + "," + job.getExecutionTime() + ",'" + log
+                                       + "','" + archivepath + "');";
+                       session.execute(com);
 
+                       List<FastaSequence> predictions = job.getPredictions();
                        for (FastaSequence p : predictions) {
                                session.execute("UPDATE JpredArchive SET predictions = predictions + {'" + p.getId() + "':'"
-                                               + p.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + jobid + "';");
+                                               + p.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + id + "';");
                        }
 
+                       List<FastaSequence> seqs = job.getAlignment();
                        for (FastaSequence s : seqs) {
                                session.execute("UPDATE JpredArchive SET alignment = alignment + {'" + s.getId() + "':'"
-                                               + s.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + jobid + "';");
+                                               + s.getSequence().replaceAll("\n", "") + "'} WHERE JobID = '" + id + "';");
                        }
                        return 1;
                }