[chronojump] On MONTHLY automatically add persons of last month



commit d4bbab7c2ed6d270ba6eb8f59fded60ecfb518fa
Author: Xavier de Blas <xaviblas gmail com>
Date:   Fri Jan 27 20:45:50 2017 +0100

    On MONTHLY automatically add persons of last month

 src/gui/encoder.cs          |    1 -
 src/gui/networks.cs         |   26 +++++++++++++++++++++++++-
 src/personSession.cs        |    8 +++++++-
 src/sqlite/personSession.cs |   37 ++++++++++++++++++++++++++++++++++++-
 src/utilDate.cs             |    9 +++++++++
 5 files changed, 77 insertions(+), 4 deletions(-)
---
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index 8bfdfe1..0a7e0ec 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -62,7 +62,6 @@ public partial class ChronoJumpWindow
        [Widget] Gtk.Box vbox_encoder_capture_doing;
        [Widget] Gtk.HScale hscale_encoder_capture_inertial_angle_now;
        
-       [Widget] Gtk.Box hbox_encoder_capture_1_or_cont;
        [Widget] Gtk.Box hbox_encoder_inertial_calibrate;
        [Widget] Gtk.RadioButton radio_encoder_capture_1set;
        [Widget] Gtk.RadioButton radio_encoder_capture_cont;
diff --git a/src/gui/networks.cs b/src/gui/networks.cs
index 8f99fc4..095f0be 100644
--- a/src/gui/networks.cs
+++ b/src/gui/networks.cs
@@ -161,12 +161,36 @@ public partial class ChronoJumpWindow
                                //configChronojump.SessionMode == Config.SessionModeEnum.MONTHLY
 
                                string yearMonthStr = UtilDate.GetCurrentYearMonthStr();
-                               if(! Sqlite.Exists(false, Constants.SessionTable, yearMonthStr)) {
+                               LogB.Information("yearMonthStr: " + yearMonthStr);
+                               if(! Sqlite.Exists(false, Constants.SessionTable, yearMonthStr))
+                               {
                                        //this creates the session and inserts at DB
                                        currentSession = new Session(
                                                        yearMonthStr, "", DateTime.Today,       //name, 
place, dateTime
                                                        Constants.SportUndefinedID, 
Constants.SpeciallityUndefinedID, Constants.LevelUndefinedID,
                                                        "", Constants.ServerUndefinedID); //comments, serverID
+
+                                       //insert personSessions from last month
+                                       string yearLastMonthStr = UtilDate.GetCurrentYearLastMonthStr();
+                                       if(Sqlite.Exists(false, Constants.SessionTable, yearLastMonthStr))
+                                       {
+                                               Session s = SqliteSession.SelectByName(yearLastMonthStr);
+
+                                               //import all persons from last session
+                                               List<PersonSession> personSessions = 
SqlitePersonSession.SelectPersonSessionList(s.UniqueID);
+
+                                               //convert all personSessions to currentSession
+                                               //and nullify UniqueID in order to be inserted incrementally 
by SQL
+                                               foreach(PersonSession ps in personSessions)
+                                               {
+                                                       ps.UniqueID = -1;
+                                                       ps.SessionID = currentSession.UniqueID;
+                                               }
+
+
+                                               //insert personSessions using a transaction
+                                               new SqlitePersonSessionTransaction(personSessions);
+                                       }
                                } else
                                        currentSession = SqliteSession.SelectByName(yearMonthStr);
                        }
diff --git a/src/personSession.cs b/src/personSession.cs
index b40c98a..d112b3d 100644
--- a/src/personSession.cs
+++ b/src/personSession.cs
@@ -102,7 +102,13 @@ public class PersonSession {
        
        public string ToSQLInsertString()
        {
-               return uniqueID.ToString() + ", " + personID + ", " + sessionID + ", " + 
+               string uniqueIDStr;
+               if(uniqueID == -1)
+                       uniqueIDStr = "null";
+               else
+                       uniqueIDStr = uniqueID.ToString();
+
+               return uniqueIDStr + ", " + personID + ", " + sessionID + ", " +
                        Util.ConvertToPoint(height) + ", " + Util.ConvertToPoint(weight) + ", " +
                        sportID + ", " + speciallityID + ", " + practice + ", '" + 
                        comments + "', '', ''"; 
diff --git a/src/sqlite/personSession.cs b/src/sqlite/personSession.cs
index d859a40..34fb3ff 100644
--- a/src/sqlite/personSession.cs
+++ b/src/sqlite/personSession.cs
@@ -305,7 +305,42 @@ class SqlitePersonSession : Sqlite
                return myArray;
        }
        
-       
+       //use this in the future:
+       public static List<PersonSession> SelectPersonSessionList(int sessionID)
+       {
+               string tps = Constants.PersonSessionTable;
+
+               Sqlite.Open();
+               dbcmd.CommandText = "SELECT " + tps + ".*" +
+                       " FROM " + tps +
+                       " WHERE " + tps + ".sessionID == " + sessionID;
+               LogB.SQL(dbcmd.CommandText.ToString());
+               dbcmd.ExecuteNonQuery();
+               SqliteDataReader reader;
+               reader = dbcmd.ExecuteReader();
+
+               List<PersonSession> list = new List<PersonSession>();
+               while(reader.Read())
+               {
+                       PersonSession ps = new PersonSession(
+                                       Convert.ToInt32(reader[0].ToString()),  //uniqueID
+                                       Convert.ToInt32(reader[1].ToString()),  //personID
+                                       Convert.ToInt32(reader[2].ToString()),  //sessionID
+                                       Convert.ToDouble(Util.ChangeDecimalSeparator(reader[3].ToString())), 
//height
+                                       Convert.ToDouble(Util.ChangeDecimalSeparator(reader[4].ToString())), 
//weight
+                                       Convert.ToInt32(reader[5].ToString()),  //sportID
+                                       Convert.ToInt32(reader[6].ToString()),  //speciallityID
+                                       Convert.ToInt32(reader[7].ToString()),  //practice
+                                       reader[8].ToString()                    //comments
+                                       );
+                       list.Add(ps);
+               }
+               reader.Close();
+               Sqlite.Close();
+               return list;
+       }
+
+
        public static void DeletePersonFromSessionAndTests(string sessionID, string personID)
        {
                Sqlite.Open();
diff --git a/src/utilDate.cs b/src/utilDate.cs
index 2731bd4..939156f 100644
--- a/src/utilDate.cs
+++ b/src/utilDate.cs
@@ -94,4 +94,13 @@ public class UtilDate
                return UtilAll.DigitsCreate(dt.Year,4) + "-" + Catalog.GetString(dt.ToString("MMMM"));
        }
 
+       //get a string of last month
+       public static string GetCurrentYearLastMonthStr()
+       {
+               DateTime dt = DateTime.Now;
+               dt = dt.AddMonths(-1);
+
+               return UtilAll.DigitsCreate(dt.Year,4) + "-" + Catalog.GetString(dt.ToString("MMMM"));
+       }
+
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]