public async void Validate(object input = null)
        {
            // Path to Logs folder (root directory + Logs)
            string current_directory = Program.GetSectionLogsPath();

            // All pdf files in Files folder
            //string[] existingFiles = Directory.GetFiles(User.GetFilesFolder(), " *.pdf").Select(System.IO.Path.GetFileName).ToArray();
            string[] existingFiles = Directory.GetFiles(Program.GetSectionFilesPath(), "*.pdf").Select(System.IO.Path.GetFileName).ToArray();
            // All file names in tblArticle database table
            string[] databaseFiles = new GlobalRepo().GetFileNames();

            // Mismatch between physical pdf files and database names
            if (databaseFiles != null)
            {
                string[] mismatch = databaseFiles.Except(existingFiles).ToArray();
                // If any mismatch was found create log file and write mismatched files in there
                if (mismatch.Length > 0)
                {
                    _workStatus(true);

                    await Task.Run(() =>
                    {
                        // Current date will be set as file name
                        DateTime current_date = DateTime.Today;
                        // Full file path including its name
                        string file_path = current_directory + current_date.ToLongDateString() + ".txt";

                        // Open or Create file
                        using (StreamWriter sw = File.AppendText(file_path))
                        {
                            // Start writing log startin with current hour as timestamp
                            sw.WriteLine(current_date.ToLongTimeString());
                            sw.WriteLine("Following files are missing:");
                            // Write each file name from mismatch folder into text file
                            foreach (string file in mismatch)
                            {
                                sw.WriteLine(file);
                            }
                            sw.WriteLine("");
                        }
                    });

                    _workStatus(false);

                    _dialogService.OpenDialog(new DialogOkViewModel("Some files are missing see Logs for more info...", "Validation", DialogType.Error));
                }
                else
                {
                    _dialogService.OpenDialog(new DialogOkViewModel("No missing files were found!", "Validation", DialogType.Success));
                }
            }
            else
            {
                _dialogService.OpenDialog(new DialogOkViewModel("There are no records in database yet", "Validation", DialogType.Information));
            }
        }
Exemplo n.º 2
0
 public GlobalService()
 {
     globalRepo         = new GlobalRepo();
     ventaRepo          = new VentaRepo();
     ingresoPorLoteRepo = new IngresoPorLoteRepo();
 }
        public void Import(object input = null)
        {
            try
            {
                string destination = null;

                // 1. Using winforms dialog box select a folder
                destination = _browserService.OpenFolderDialog();

                // 2. If nothing was selected return
                if (destination == null)
                {
                    return;
                }

                // 3. If the wrong folder was selected return with an error messagebox
                if ((!File.Exists(destination + "\\" + "NikasDB.sqlite3")) || (!Directory.Exists(destination + @"\Files")) || (!File.Exists(destination + "\\" + "user.sqlite3")))
                {
                    _dialogService.OpenDialog(new DialogOkViewModel("Please select a correct folder.", "Error", DialogType.Error));
                    return;
                }

                string root_directory = Environment.CurrentDirectory;

                // 4. If an old temp query file exists delete it
                if (File.Exists(Environment.CurrentDirectory + "\\" + "temp_query.txt"))
                {
                    File.Delete(Environment.CurrentDirectory + "\\" + "temp_query.txt");
                }

                // 5. Set up queries for database import
                string base_query    = System.IO.File.ReadAllText(root_directory + "\\" + "Query.txt");
                string file_path     = root_directory + "\\" + "temp_query.txt";
                string attach_string =
                    "ATTACH DATABASE \'" + root_directory + "\\" + "user.sqlite3\'" + "AS user;\n" +
                    "ATTACH DATABASE \'" + destination + "\\" + "NikasDB.sqlite3\'" + "AS db2main;\n" +
                    "ATTACH DATABASE \'" + destination + "\\" + "user.sqlite3\'" + "AS db2user;\n";
                string files_query      = @"SELECT tf.File AS OldFile, tnf.File AS NewFile FROM temp_Files AS tf, temp_NewFiles AS tnf WHERE tf.Title = tnf.Title;";
                string duplicates_query = @"SELECT Title, File FROM temp_tblDuplicates;";

                // 1. Get Section name
                string        info     = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "sections.json"));
                List <string> sections = JsonConvert.DeserializeObject <List <string> >(info);

                string section_name;

                if (sections == null || sections.Count == 0)
                {
                    section_name = "Section 1";
                }
                else
                {
                    int number = int.Parse(sections.Last().Split(' ')[1]) + 1;
                    section_name = "Section " + number.ToString();
                }

                // 2. Add section to json file
                sections.Add(section_name);
                info = JsonConvert.SerializeObject(sections);
                File.WriteAllText(Path.Combine(Environment.CurrentDirectory, "sections.json"), info);

                // 3. Query for pending articles
                string pending_query = $"INSERT INTO tblPending (Article_ID, Section) SELECT ID, \"{section_name}\" AS Section FROM tblArticle WHERE Title IN (SELECT Title FROM temp_tblArticle);";

                // Open or Create file
                using (StreamWriter sw = File.AppendText(file_path))
                {
                    sw.WriteLine(attach_string);
                    sw.WriteLine(base_query);
                }

                // Path to temp file where full query is written (Including attaches)
                string full_query = System.IO.File.ReadAllText(root_directory + "\\" + "temp_query.txt");

                // 6. Import recrods to database, return list of files to copy and duplicates to log
                List <ExistCheck> duplicates;
                List <CompFile>   files = new GlobalRepo().ImportSection(full_query, pending_query, files_query, duplicates_query, out duplicates);

                // 7. Create dictionary of files to copy
                Dictionary <string, string> files_to_copy = new Dictionary <string, string>();
                foreach (CompFile file in files)
                {
                    files_to_copy.Add(file.OldFile + ".pdf", file.NewFile + ".pdf");
                }

                // 8. Log duplicate files
                DirectoryInfo current_directory_logs = new DirectoryInfo("." + @"\Logs\");

                // Current date will be set as file name
                DateTime current_date = DateTime.Today;
                // Current time
                DateTime current_time = DateTime.Now;
                // Full file path including its name
                string merge_log_path = current_directory_logs.FullName + "Merge --- " + current_date.ToLongDateString() + ".txt";

                // Open or Create file
                using (StreamWriter sw = File.AppendText(merge_log_path))
                {
                    // Start writing log starting with current hour as timestamp
                    sw.WriteLine(current_time.ToLongTimeString());
                    sw.WriteLine("Following titles are duplicate:");
                    // Write each file name from mismatch folder into text file
                    foreach (ExistCheck duplicate in duplicates)
                    {
                        sw.WriteLine(duplicate.Title + " ||| " + duplicate.File);
                    }
                    sw.WriteLine("");
                }

                // 9. Create progress bar and copy physical .pdf files
                _windowService.OpenWindow(
                    new ImportViewModel(files_to_copy, destination, _dialogService),
                    WindowType.Generic,
                    true,
                    true,
                    true
                    );
            }
            catch (Exception e)
            {
                new BugTracker().Track("App", "Import", e.Message, e.StackTrace);
                _dialogService.OpenDialog(new DialogOkViewModel("Something went wrong.", "Error", DialogType.Error));
            }
            finally
            {
                _workStatus(false);
            }
        }