[chronojump/chronojump-importer-warn-database-version] Moves chronojump importer code out from chronojump.cs into ChronojumpImporter new class.



commit c23d08a9e36f39c1a89a8deb0a336887093ba2ae
Author: Carles Pina i Estany <carles pina cat>
Date:   Thu Sep 29 20:03:30 2016 +0200

    Moves chronojump importer code out from chronojump.cs into ChronojumpImporter new class.

 chronojump.csproj         |    1 +
 src/Makefile.am           |    1 +
 src/chronojumpImporter.cs |  129 +++++++++++++++++++++++++++++++++++++++++++++
 src/commandLineEncoder.cs |    2 +-
 src/gui/chronojump.cs     |   64 ++--------------------
 5 files changed, 138 insertions(+), 59 deletions(-)
---
diff --git a/chronojump.csproj b/chronojump.csproj
index 9620ecd..d04d7a8 100644
--- a/chronojump.csproj
+++ b/chronojump.csproj
@@ -878,6 +878,7 @@
     <Compile Include="src\commandLineEncoder.cs" />
     <Compile Include="src\chronopicRegister.cs" />
     <Compile Include="src\sqlite\chronopicRegister.cs" />
+    <Compile Include="src\chronojumpImporter.cs" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="src\" />
diff --git a/src/Makefile.am b/src/Makefile.am
index 8ebbd08..0054c01 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -179,6 +179,7 @@ SOURCES = \
        chronopic.cs\
        chronopicDetect.cs\
        chronopicRegister.cs\
+       chronojumpImporter.cs\
        oldCodeNeedToDBConvert/person.cs\
        oldCodeNeedToDBConvert/personSession.cs\
        oldCodeNeedToDBConvert/sqlite/person.cs\
diff --git a/src/chronojumpImporter.cs b/src/chronojumpImporter.cs
new file mode 100644
index 0000000..f3a04e9
--- /dev/null
+++ b/src/chronojumpImporter.cs
@@ -0,0 +1,129 @@
+using System.Collections; //ArrayList
+using System.Collections.Generic; //List
+using System.Json;
+using System.Diagnostics;
+using System;
+using Mono.Unix;
+
+class ChronojumpImporter
+{
+       private string sourceFile;
+       private string destinationFile;
+       private string session;
+
+       public ChronojumpImporter(string sourceFile, string destinationFile, string session)
+       {
+               this.sourceFile = sourceFile;
+               this.destinationFile = destinationFile;
+               this.session = session;
+       }
+
+       public bool import()
+       {
+               string sourceDatabaseVersion = getSourceDatabaseVersion ();
+               string destinationDatabaseVersion = getDestinationDatabaseVersion ();
+
+               if (float.Parse(destinationDatabaseVersion) < float.Parse(sourceDatabaseVersion)) {
+                       new DialogMessage (Constants.MessageTypes.WARNING, Catalog.GetString ("Trying to 
import a newer database version than this Chronojump\nPlease, update the running Chronojump."));
+                       return false;
+               }
+
+               List<string> parameters = new List<string> ();
+               parameters.Add ("--source");
+               parameters.Add (CommandLineEncoder.EncodeArgText (sourceFile));
+               parameters.Add ("--destination");
+               parameters.Add (CommandLineEncoder.EncodeArgText (destinationFile));
+               parameters.Add ("--source_session");
+               parameters.Add (CommandLineEncoder.EncodeArgText (session));
+
+               executeChronojumpImporter (parameters);
+
+               return true;
+       }
+
+       public string getSourceDatabaseVersion()
+       {
+               return getDatabaseVersionFromFile (sourceFile);
+       }
+
+       public string getDestinationDatabaseVersion()
+       {
+               return getDatabaseVersionFromFile (destinationFile);
+       }
+
+       private string getDatabaseVersionFromFile(string filePath)
+       {
+               List<string> parameters = new List<string> ();
+
+               parameters.Add ("--source");
+               parameters.Add (filePath);
+               parameters.Add ("--json_information");
+
+               string jsonText = executeChronojumpImporter (parameters);
+
+               JsonValue result = JsonValue.Parse(jsonText);
+
+               string databaseVersion = result ["databaseVersion"];
+
+               return databaseVersion;
+       }
+
+       private string executeChronojumpImporter(List<string> parameters)
+       {
+               string importer_executable;
+               string importer_script_path = "";
+
+               if (UtilAll.IsWindows()) {
+                       importer_executable = System.IO.Path.Combine (Util.GetPrefixDir (), 
"bin\\chronojump-importer\\chronojump_importer.exe");
+               } else {
+                       importer_executable = "python";         // chronojump_importer works on Python 2 and 
Python 3
+                       importer_script_path = CommandLineEncoder.EncodeArgText (System.IO.Path.Combine 
(Util.GetPrefixDir (), "bin/chronojump_importer.py"));
+               }
+
+               Process process = new Process();
+               ProcessStartInfo processStartInfo;
+
+               processStartInfo = new ProcessStartInfo();
+
+               // processStartInfo.Arguments = importer_script_path + " --source \"" + 
CommandLineEncoder.EncodeArgText (sourceFile) + "\" --destination \"" + CommandLineEncoder.EncodeArgText 
(destinationFile) + "\" --source_session \"" + CommandLineEncoder.EncodeArgText (session) + "\"";
+               processStartInfo.Arguments = importer_script_path + " " + string.Join (" ", parameters);
+               processStartInfo.FileName = importer_executable;
+
+               LogB.Debug ("chronojump-importer fileName: " + processStartInfo.FileName);
+               LogB.Debug ("chronojump-importer Arguments: " + processStartInfo.Arguments);
+
+               processStartInfo.CreateNoWindow = true;
+               processStartInfo.UseShellExecute = false;
+               processStartInfo.RedirectStandardInput = false;
+               processStartInfo.RedirectStandardError = true;
+               processStartInfo.RedirectStandardOutput = true;
+
+               process.StartInfo = processStartInfo;
+
+               bool started = false;
+               try {
+                       process.Start();
+                       started = true;
+               }
+               catch(Exception e) {
+                       string errorMessage;
+                       errorMessage = String.Format ("Cannot execute:\n   {0}\nwith the parameters:\n   
{1}\n\nThe exception is: {2}", processStartInfo.FileName, processStartInfo.Arguments, e.Message);
+                       ErrorWindow.Show (errorMessage);
+                       return "";
+               }
+
+/*             if (started) {
+                       process.BeginOutputReadLine ();
+                       process.BeginErrorReadLine ();
+               }
+*/
+               string allOutput = "";
+               allOutput += process.StandardOutput.ReadToEnd();
+               allOutput += process.StandardError.ReadToEnd();
+               Console.WriteLine(allOutput);
+
+               process.WaitForExit ();
+
+               return allOutput;
+       }
+}
\ No newline at end of file
diff --git a/src/commandLineEncoder.cs b/src/commandLineEncoder.cs
index 851c200..291fc0a 100644
--- a/src/commandLineEncoder.cs
+++ b/src/commandLineEncoder.cs
@@ -14,7 +14,7 @@ public class CommandLineEncoder
                        result = TryEncodeSlashesFollowedByQuotes(result);
                        result = TryEncodeQuotes(result);
                        result = TryEncodeLastSlash(result);
-                       return result;
+                       return "\"" + result + "\"";
                }
 
                private static string TryEncodeNewLine(string original)
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index 6645358..6906d20 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -2410,69 +2410,17 @@ public partial class ChronoJumpWindow
                string source_filename = databasePath;
                string destination_filename = Sqlite.DatabaseFilePath;
                string session = Convert.ToString (sessionNumber);
-               string importer_executable;
 
-               string importer_script_path = "";
+               ChronojumpImporter chronojumpImporter = new ChronojumpImporter (source_filename, 
destination_filename, session);
 
-               if (UtilAll.IsWindows()) {
-                       importer_executable = System.IO.Path.Combine (Util.GetPrefixDir (), 
"bin\\chronojump-importer\\chronojump_importer.exe");
-               } else {
-                       importer_executable = "python";         // chronojump_importer works on Python 2 and 
Python 3
-                       importer_script_path = "\"" + CommandLineEncoder.EncodeArgText(System.IO.Path.Combine 
(Util.GetPrefixDir (), "bin" + Path.DirectorySeparatorChar + "chronojump_importer.py")) + "\"";
-               }
-
-               Process process = new Process();
-               ProcessStartInfo processStartInfo;
-
-               processStartInfo = new ProcessStartInfo();
-
-               processStartInfo.Arguments = importer_script_path + " --source \"" + 
CommandLineEncoder.EncodeArgText (source_filename) + "\" --destination \"" + CommandLineEncoder.EncodeArgText 
(destination_filename) + "\" --source_session \"" + CommandLineEncoder.EncodeArgText (session) + "\"";
-               processStartInfo.FileName = importer_executable;
-
-               LogB.Debug ("chronojump-importer fileName:" + processStartInfo.FileName);
-               LogB.Debug ("chronojump-importer Arguments:" + processStartInfo.Arguments);
-
-               processStartInfo.CreateNoWindow = true;
-               processStartInfo.UseShellExecute = false;
-               processStartInfo.RedirectStandardInput = false;
-               processStartInfo.RedirectStandardError = true;
-               processStartInfo.RedirectStandardOutput = true;
-               process.EnableRaisingEvents = true; // So the callback for Exited is called
-
-               process.OutputDataReceived += new DataReceivedEventHandler(
-                       (s, e) =>
-                       { 
-                               LogB.Debug(e.Data); 
-                       }
-               );
-               process.ErrorDataReceived += new DataReceivedEventHandler (
-                       (s, e) =>
-                       {
-                               LogB.Debug(e.Data);
-                       }
-               );
-
-               process.StartInfo = processStartInfo;
-
-               bool started = false;
-               try {
-                       process.Start();
-                       started = true;
-               }
-               catch(Exception e) {
-                       string errorMessage;
-                       errorMessage = String.Format ("Cannot execute:\n   {0}\nwith the parameters:\n   
{1}\n\nThe exception is: {2}", processStartInfo.FileName, processStartInfo.Arguments, e.Message);
-                       ErrorWindow.Show (errorMessage);
-               }
+               bool imported = chronojumpImporter.import ();
 
-               if (started) {
-                       process.BeginOutputReadLine ();
-                       process.BeginErrorReadLine ();
+               if (imported) {
+                       updateComboStats ();
+                       new DialogMessage (Constants.MessageTypes.INFO, Catalog.GetString ("Session 
imported"));
                }
+               // If not imported ChronojumpImporter object has already shown errors to the user.
 
-               process.WaitForExit ();
-               updateComboStats ();
-               new DialogMessage (Constants.MessageTypes.INFO, Catalog.GetString ("Session imported"));
        }
 
        private void on_open_activate (object o, EventArgs args) 


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