Exemple #1
0
        public string CopyDatabaseForImport(string databasePath)
        {
            string location = this.GetType().Name + "." + MethodBase.GetCurrentMethod().Name;

            _logger.OpenSection(location);

            OriginalDatabasePath = databasePath;

            string copyDatabasePath = null;

            if (!File.Exists(databasePath))
            {
                // TODO UNTESTED
                _logger.Error("File '" + databasePath + "' does not exist!");
                _logger.CloseSectionWithReturnInfo(copyDatabasePath, location);
                return(copyDatabasePath);
            }

            try
            {
                copyDatabasePath = GetCopyFileName(databasePath);

                if (File.Exists(copyDatabasePath))
                {
                    // TODO UNTESTED
                    _logger.Info("Copy file '" + copyDatabasePath + "' already exists. Deleting...");
                    File.Delete(copyDatabasePath);
                    _logger.Info("Deleted.");
                }

                _logger.Info("Copying '" + databasePath + "' to '" + copyDatabasePath + "'...");
                File.Copy(databasePath, copyDatabasePath);
                _logger.Info("Complete.");
            }
            catch (Exception ex)
            {
                // TODO UNTESTED (error during delete)
                // TODO UNTESTED (error during backup)
                _logger.Error(ex);
                copyDatabasePath = null;
            }

            CopyDatabasePath = copyDatabasePath;

            _logger.CloseSectionWithReturnInfo(copyDatabasePath, location);
            return(copyDatabasePath);
        }
Exemple #2
0
        public string CreateDatabase(string folderPath, string filename)
        {
            string location = this.GetType().Name + "." + MethodBase.GetCurrentMethod().Name;

            _logger.OpenSection(location);

            string databasePath = folderPath + @"\" + filename;

            _logger.Info("Creating database in path '" + databasePath + "'...");
            SQLiteConnection.CreateFile(databasePath);

            if (File.Exists(databasePath))
            {
                _logger.Info("File created.");
                PopulateDatabase(databasePath);
            }
            else
            {
                _logger.Error("File '" + databasePath + "' does not exist!");
            }

            _logger.CloseSectionWithReturnInfo(databasePath, location);
            return(databasePath);
        }
 private static FeatherLogger CreateFeatherLogger()
 {
     try
     {
         var returnLogger = new FeatherLogger(
             logMode: FeatherLoggerMode,
             traceLevel: FeatherLoggerTraceLevel,
             folderName: FolderName,
             filename: "Nightingale",
             hasTimestampInFilename: true,
             extension: "xml");
         return(returnLogger);
     }
     catch (Exception ex)
     {
         _logger.Error(ex); // not Logger! You wouldn't want recursion
         throw;
     }
 }
Exemple #4
0
        private void btnImport_Click(object sender, EventArgs e)
        {
            string location = this.GetType().Name + "." + MethodBase.GetCurrentMethod().Name;

            _logger.OpenSection(location);

            var databasePath = GetFilePathFromOpenFileDialog(
                message: "Choose an sqlite database/dictionary",
                filter: "SQLite database|*.sqlite");

            if (String.IsNullOrEmpty(databasePath))
            {
                MessageBox.Show(_logger.Info("Cancelled by user."));
                _logger.CloseSection(location);
                return;
            }

            var importedFilePath = GetFilePathFromOpenFileDialog(
                message: "Choose a file to import",
                filter: "Text files|*.txt");

            if (String.IsNullOrEmpty(importedFilePath))
            {
                MessageBox.Show(_logger.Info("Cancelled by user."));
                _logger.CloseSection(location);
                return;
            }

            _logger.Info("Will copy database first");

            var dbCopier         = GlobalObjects.DatabaseCopier;
            var copyDatabasePath = dbCopier.CopyDatabaseForImport(databasePath);

            if (String.IsNullOrEmpty(copyDatabasePath))
            {
                // TODO UNTESTED
                _logger.Error("Database copy failed.");
            }
            else
            {
                AbstractParser parser;
                using (StreamReader sReader = new StreamReader(importedFilePath))
                {
                    string wordStyleString = ":WordStyle=";
                    var    firstLine       = sReader.ReadLine();
                    if (firstLine.Substring(0, 11) != wordStyleString)
                    {
                        var errorMessage = "First line of file must start with ':WordStyle='.";
                        throw new Exception(_logger.Error(errorMessage));
                    }
                    var wordStyle = firstLine.Substring(wordStyleString.Length);
                    if (wordStyle == "Takoboto")
                    {
                        parser = new TakobotoParser();
                    }
                    else if (wordStyle == "JWPCE")
                    {
                        parser = new JwpceParser();
                    }
                    else
                    {
                        var errorMessage = "Word style '" + wordStyle + "' is invalid.";
                        throw new Exception(_logger.Error(errorMessage));
                    }
                }

                var importSuccess = parser.ImportFile(copyDatabasePath, importedFilePath);
                if (importSuccess)
                {
                    dbCopier.RestoreFile();
                    MessageBox.Show(_logger.Info("Success!"));
                }
                else
                {
                    var errorMessage = "Parsing failed. Some error happened, apparently";
                    MessageBox.Show(_logger.Error(errorMessage));
                }
            }

            _logger.CloseSection(location);
        }