[chronojump] ForceSensor exercise delete takes care of existing forceSensor records



commit b8ba31e171188123f6174c969b2a32aecc8b09f8
Author: Xavier de Blas <xaviblas gmail com>
Date:   Thu Sep 12 12:31:39 2019 +0200

    ForceSensor exercise delete takes care of existing forceSensor records

 src/gui/forceSensor.cs    | 52 ++++++++++++++++++++++++++++++++++++++---------
 src/gui/genericWindow.cs  |  5 +++--
 src/sqlite/forceSensor.cs | 40 ++++++++++++++++++++++++++++++++++++
 3 files changed, 85 insertions(+), 12 deletions(-)
---
diff --git a/src/gui/forceSensor.cs b/src/gui/forceSensor.cs
index e74d7817..a6df74a5 100644
--- a/src/gui/forceSensor.cs
+++ b/src/gui/forceSensor.cs
@@ -2143,23 +2143,55 @@ LogB.Information(" re R ");
                return false;
        }
 
+       //based on: on_button_encoder_exercise_delete
+       //maybe unify them on the future
        void on_button_force_sensor_exercise_delete (object o, EventArgs args)
        {
-               ForceSensorExercise ex = (ForceSensorExercise) SqliteForceSensorExercise.Select(
-                               false, genericWin.uniqueID, false)[0];
+               int exerciseID = genericWin.uniqueID;
 
-               //TODO: when forceSensor records get in database, ensure to delete them if exercise is deleted
-               //see: on_button_encoder_exercise_delete
+               //1st find if there are sets with this exercise
+               ArrayList array = SqliteForceSensor.SelectRowsOfAnExercise(false, exerciseID);
 
-               //delete exercise
-               Sqlite.Delete(false, Constants.ForceSensorExerciseTable, genericWin.uniqueID);
+               if(array.Count > 0) {
+                       //there are some records of this exercise on encoder table, do not delete
+                       genericWin.SetTextview(
+                                       Catalog.GetString("Sorry, this exercise cannot be deleted until these 
tests are deleted:"));
 
-               genericWin.HideAndNull();
+                       ArrayList nonSensitiveRows = new ArrayList();
+                       for(int i=0; i < array.Count; i ++)
+                               nonSensitiveRows.Add(i);
 
-               fillForceSensorExerciseCombo("");
-               combo_force_sensor_exercise.Active = 0;
+                       genericWin.SetTreeview(
+                                       new string [] {
+                                       "count",        //not shown, unused
+                                       Catalog.GetString("Sets"), Catalog.GetString("Person"),
+                                       Catalog.GetString("Session"), Catalog.GetString("Date") },
+                                       false, array, nonSensitiveRows, Constants.ContextMenu.NONE, false);
+
+                       genericWin.ShowTextview();
+                       genericWin.ShowTreeview();
+                       genericWin.ShowButtonDelete(false);
+                       genericWin.DeletingExerciseHideSomeWidgets();
+
+                       genericWin.Button_accept.Clicked -= new 
EventHandler(on_button_force_sensor_exercise_edit_accepted);
+                       genericWin.Button_accept.Clicked += new 
EventHandler(on_button_force_sensor_exercise_do_not_delete);
+               } else {
+                       //forceSensor table has not records of this exercise. Delete exercise
+                       SqliteForceSensorExercise.Delete(false, exerciseID);
+
+                       genericWin.HideAndNull();
 
-               new DialogMessage(Constants.MessageTypes.INFO, Catalog.GetString("Exercise deleted."));
+                       fillForceSensorExerciseCombo("");
+                       combo_force_sensor_exercise.Active = 0;
+
+                       new DialogMessage(Constants.MessageTypes.INFO, Catalog.GetString("Exercise 
deleted."));
+               }
+       }
+
+       //accept does not save changes, just closes window
+       void on_button_force_sensor_exercise_do_not_delete (object o, EventArgs args) {
+               genericWin.Button_accept.Clicked -= new 
EventHandler(on_button_force_sensor_exercise_do_not_delete);
+               genericWin.HideAndNull();
        }
 
        // -------------------------------- end of exercise stuff --------------------
diff --git a/src/gui/genericWindow.cs b/src/gui/genericWindow.cs
index f1dcb537..fb96e38d 100644
--- a/src/gui/genericWindow.cs
+++ b/src/gui/genericWindow.cs
@@ -839,14 +839,15 @@ public class GenericWindow
                store.SetValue (iter, commentColumn, entry_edit_row.Text);
        }
 
-       //this method is only used when try to delete an encoder exercise,
-       //and cannot because there are encoder rows done with this exercise.
+       //this method is only used when try to delete an encoder/forceSensor exercise,
+       //and cannot because there are rows done with this exercise.
        //Just unsensitive some stuff now in order to not be able to change them
        public void DeletingExerciseHideSomeWidgets() {
                hbox_spin_int.Hide();
                hbox_entry2.Hide();
                hbox_entry3.Hide();
                hbox_spin_double2.Hide();
+               check1.Hide();
 
                SetButtonAcceptLabel(Catalog.GetString("Close"));
        }       
diff --git a/src/sqlite/forceSensor.cs b/src/sqlite/forceSensor.cs
index 64bf91f3..19c5cc39 100644
--- a/src/sqlite/forceSensor.cs
+++ b/src/sqlite/forceSensor.cs
@@ -160,6 +160,46 @@ class SqliteForceSensor : Sqlite
                return array;
        }
 
+       public static ArrayList SelectRowsOfAnExercise(bool dbconOpened, int exerciseID)
+       {
+               if(! dbconOpened)
+                       Sqlite.Open();
+
+               dbcmd.CommandText = "select count(*), " +
+                       Constants.PersonTable + ".name, " +
+                       Constants.SessionTable + ".name, " +
+                       Constants.SessionTable + ".date " +
+                       " FROM " + table + ", " + Constants.PersonTable + ", " + Constants.SessionTable +
+                       " WHERE exerciseID == " + exerciseID +
+                       " AND " + Constants.PersonTable + ".uniqueID == " + table + ".personID " +
+                       " AND " + Constants.SessionTable + ".uniqueID == " + table + ".sessionID " +
+                       " GROUP BY sessionID, personID";
+
+               LogB.SQL(dbcmd.CommandText.ToString());
+               dbcmd.ExecuteNonQuery();
+               SqliteDataReader reader;
+               reader = dbcmd.ExecuteReader();
+
+               ArrayList array = new ArrayList();
+               int count = 0;
+               while(reader.Read()) {
+                       array.Add(new string [] {
+                                       count.ToString(),
+                                       reader[0].ToString(), //count
+                                       reader[1].ToString(), //person name
+                                       reader[2].ToString(), //session name
+                                       reader[3].ToString()  //session date
+                       });
+                       count ++;
+               }
+
+               reader.Close();
+               if(! dbconOpened)
+                       Sqlite.Close();
+
+               return array;
+       }
+
        protected internal static void import_from_1_68_to_1_69() //database is opened
        {
                LogB.PrintAllThreads = true; //TODO: remove this


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