Fix problem with dates
authorSasha Sherstnev <a.sherstnev@dundee.ac.uk>
Tue, 29 Oct 2013 22:31:27 +0000 (22:31 +0000)
committerSasha Sherstnev <a.sherstnev@dundee.ac.uk>
Tue, 29 Oct 2013 22:31:27 +0000 (22:31 +0000)
server/compbio/statistic/StatisticsProt.java [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index df6fec5..4ee2917
@@ -23,7 +23,7 @@ import compbio.cassandra.CassandraCreate;
 import compbio.cassandra.DataBase;
 
 public class StatisticsProt {
-       private final static long MILLISECONDS_PER_DAY = 1000L * 60 * 60 * 24;
+//     private final static long MILLISECONDS_PER_DAY = 1000L * 60 * 60 * 24;
        private CassandraCreate cc = new CassandraCreate();
        private ArrayList<DataBase> query;
        private static long currentDate = 0;
@@ -31,45 +31,47 @@ public class StatisticsProt {
 
        /* query: the period from date1 till date2 */
        public List<DataBase> readDetails(String date1, String date2) {
+       
                if (!isThisDateValid(date1) || !isThisDateValid(date2)) {
                        System.out.println("Wrong date: point 1");
                        return null;
                }
                SetDateRange();
-
                long dateStart = DateParsing(date1);
                long dateEnd = DateParsing(date2);
+               if ((dateStart < earlestDate && dateEnd < earlestDate) || (dateStart > currentDate && dateEnd > currentDate)
+                               || dateStart > dateEnd)
+                       return null;
                if (dateStart < earlestDate)
                        dateStart = earlestDate;
-               if (dateStart > currentDate)
-                       dateStart = currentDate - MILLISECONDS_PER_DAY;
-               if (dateEnd < earlestDate)
-                       dateStart = earlestDate + MILLISECONDS_PER_DAY;
                if (dateEnd > currentDate)
                        dateStart = currentDate;
                System.out.println("StatisticsProt.readDetails: earlestDate = " + earlestDate + ", currentDate = " + currentDate);
                System.out.println("StatisticsProt.readDetails: Start date " + date1 + ": int representation = " + dateStart);
                System.out.println("StatisticsProt.readDetails: End date " + date2 + ": int representation = " + dateEnd);
-
+               Calendar start = Calendar.getInstance();
+               start.setTime(new Date(dateStart));
+               Calendar end = Calendar.getInstance();
+               end.setTime(new Date(dateEnd));
                query = new ArrayList<DataBase>();
                int day = 0;
-               while (dateStart <= dateEnd) {
+               for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
                        SliceQuery<Long, String, String> result = HFactory.createSliceQuery(cc.GetKeyspace(), LongSerializer.get(),
                                        StringSerializer.get(), StringSerializer.get());
                        result.setColumnFamily("ProteinData");
-                       result.setKey(dateStart);
+                       result.setKey(date.getTime());
                        result.setRange(null, null, false, Integer.MAX_VALUE);
                        QueryResult<ColumnSlice<String, String>> columnSlice = result.execute();
                        ++day;
-                       System.out.print("Day " + day + ": dataStart = " + dateStart + ": ");
+                       System.out.print("Day " + day + ": dataStart = " + date + ": ");
                        if (!columnSlice.get().getColumns().isEmpty()) {
-                               DataBase db = new DataBase(DateFormat(dateStart), columnSlice.get().getColumns().size());
+                               DataBase db = new DataBase(DateFormat(date.getTime()), columnSlice.get().getColumns().size());
                                query.add(db);
                                System.out.println("data exist");
                        } else {
                                System.out.println("no data");
                        }
-                       dateStart += MILLISECONDS_PER_DAY;
+               //      dateStart += MILLISECONDS_PER_DAY;
                }
                System.out.println("StatisticsProt.readLength: total number of dates = " + query.size());
                return query;
@@ -84,31 +86,32 @@ public class StatisticsProt {
                        return null;
                }
                SetDateRange();
-
                long dateStart = DateParsing(date1);
                long dateEnd = DateParsing(date2);
+               if ((dateStart < earlestDate && dateEnd < earlestDate) || (dateStart > currentDate && dateEnd > currentDate)
+                               || dateStart > dateEnd)
+                       return null;
                if (dateStart < earlestDate)
                        dateStart = earlestDate;
-               if (dateStart > currentDate)
-                       dateStart = currentDate - MILLISECONDS_PER_DAY;
-               if (dateEnd < earlestDate)
-                       dateStart = earlestDate + MILLISECONDS_PER_DAY;
                if (dateEnd > currentDate)
                        dateStart = currentDate;
                System.out.println("StatisticsProt.readLength: earlestDate = " + earlestDate + ", currentDate = " + currentDate);
                System.out.println("StatisticsProt.readLength: Start date is " + date1 + ": int representation = " + dateStart);
                System.out.println("StatisticsProt.readLength: End date is " + date2 + ": int representation = " + dateEnd);
-
+               Calendar start = Calendar.getInstance();
+               start.setTime(new Date(dateStart));
+               Calendar end = Calendar.getInstance();
+               end.setTime(new Date(dateEnd));
                query = new ArrayList<DataBase>();
                List<Integer> totalTime = new ArrayList<Integer>();
                for (int i = 0; i < 4; i++)
                        totalTime.add(i, 0);
-               while (dateStart <= dateEnd) {
+               for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
                        List<Integer> timeResult = new ArrayList<Integer>();
                        SliceQuery<Long, String, String> result = HFactory.createSliceQuery(cc.GetKeyspace(), LongSerializer.get(),
                                        StringSerializer.get(), StringSerializer.get());
                        result.setColumnFamily("ProteinData");
-                       result.setKey(dateStart);
+                       result.setKey(date.getTime());
                        result.setRange(null, null, false, Integer.MAX_VALUE);
                        QueryResult<ColumnSlice<String, String>> columnSlice = result.execute();
                        List<HColumn<String, String>> col = columnSlice.get().getColumns();
@@ -133,10 +136,10 @@ public class StatisticsProt {
                                        totalTime.set(i, totalTime.get(i) + timeResult.get(i));
                                DataBase db = new DataBase();
                                db.setTimeRez(timeResult);
-                               db.setDate(DateFormat(dateStart));
+                               db.setDate(DateFormat(date.getTime()));
                                query.add(db);
                        }
-                       dateStart += MILLISECONDS_PER_DAY;
+       //              dateStart += MILLISECONDS_PER_DAY;
                }
                DataBase db = new DataBase();
                db.setTimeTotalExec(totalTime);
@@ -195,7 +198,7 @@ public class StatisticsProt {
                                                ++npred;
                                        }
                                }
-                               if (npred >= counter) {
+                               if (npred > counter) {
                                        DataBase db = new DataBase();
                                        db.setProt(last_key);
                                        db.setTotalId(npred);