Exemple #1
0
        /// <summary>
        /// Write the Star Colleagues report
        /// </summary>
        private void CreateStarColleaguesReport()
        {
            // Note: Errors are handled by calling function

            // If we're continuing the previous report, rename it to a backup filename,
            // then copy every person in it to the file to append -- except for the
            // last person, which we'll restart.
            string    Filename     = Folder.Text + "\\" + StarColleagues.Text;
            ArrayList SetnbsToSkip = new ArrayList();

            if (ContinueStarColleagues.Checked && File.Exists(Filename))
            {
                SetnbsToSkip = Reports.BackupReportAndGetSetnbs(Filename);
            }

            StreamWriter writer = new StreamWriter(Filename, ContinueStarColleagues.Checked);

            StarColleaguesReport.Generate(writer, DB, JournalWeights.Text, SetnbsToSkip, this, OverwriteStarColleagues.Checked);
            writer.Close();
        }
Exemple #2
0
        public void TestEntireReport()
        {
            string Setnb = "";

            // Set up the database with test publications (and don't forget to add the
            // publication types!)
            DB = new Database("Publication Harvester Unit Test");
            Harvester harvester = new Harvester(DB);

            harvester.CreateTables();
            PublicationTypes PubTypes = new PublicationTypes(
                AppDomain.CurrentDomain.BaseDirectory + "\\Unit Tests\\TestPublicationTypes",
                "PublicationTypes.csv"
                );

            PubTypes.WriteToDB(DB);
            reports = new Reports(DB, AppDomain.CurrentDomain.BaseDirectory + "\\Unit Tests\\TestReports\\pubmed_jifs.xls");
            Assert.IsTrue(reports.Weights.Count == 10);
            TestHarvester.GetPublicationsFromInput1XLS_Using_MockNCBI(false, new string[] { "eng" }, 22);
            people = new People(DB);


            // Write the report
            StreamWriter writer = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\TestEntireReport.csv");

            Reports.ReportStatus StatusCallback   = delegate(int number, int total, Person person, bool ProgressBarOnly) {
                //
            };
            Reports.ReportMessage MessageCallback = delegate(string Message)
            {
                //
            };
            reports.PeopleReport(null, writer, StatusCallback, MessageCallback);
            writer.Close();

            // Read the report into an array
            var lines = File.ReadAllLines($"{AppDomain.CurrentDomain.BaseDirectory}\\TestEntireReport.csv")
                        .Select(line => line.Split(new char[] { ',' }));
            var header = lines.First();
            var data   = lines.Skip(1).ToList();

            Assert.AreEqual(85, data.Count);

            string ReportData(string setnb, string year, string column)
            {
                var index = Array.IndexOf(header, column);
                var row   = data.Where(line => line[0] == setnb && line[1] == year);

                Assert.AreEqual(1, row.Count(), $"Unable to find setnb={setnb} year={year} in TestEntireReport.csv");
                return(row.First()[index]);
            }

            var q = ReportData("A5401532", "1997", "wghtd_pubcount_pos1");

            // Read the report file that was generated by hand (TestEntireReport_Data.xls)
            // and check each value against the report that was generated by Reports()
            string[] Columns =
            {
                "setnb",                    "year",                   "pubcount",               "wghtd_pubcount",       "pubcount_pos1",
                "wghtd_pubcount_pos1",      "pubcount_posN",          "wghtd_pubcount_posN",
                "pubcount_posM",            "wghtd_pubcount_posM",    "pubcount_posNTL",
                "wghtd_pubcount_posNTL",    "pubcount_pos2",          "wghtd_pubcount_pos2",
                "123pubcount",              "wghtd_123pubcount",      "123pubcount_pos1",
                "wghtd_123pubcount_pos1",   "123pubcount_posN",       "wghtd_123pubcount_posN",
                "123pubcount_posM",         "wghtd_123pubcount_posM", "123pubcount_posNTL",
                "wghtd_123pubcount_posNTL", "123pubcount_pos2",       "wghtd_123pubcount_pos2",
                "1pubcount",                "wghtd_1pubcount",        "1pubcount_pos1",         "wghtd_1pubcount_pos1",
                "1pubcount_posN",           "wghtd_1pubcount_posN",   "1pubcount_posM",
                "wghtd_1pubcount_posM",     "1pubcount_posNTL",       "wghtd_1pubcount_posNTL",
                "1pubcount_pos2",           "wghtd_1pubcount_pos2",   "2pubcount",              "wghtd_2pubcount",
                "2pubcount_pos1",           "wghtd_2pubcount_pos1",   "2pubcount_posN",
                "wghtd_2pubcount_posN",     "2pubcount_posM",         "wghtd_2pubcount_posM",
                "2pubcount_posNTL",         "wghtd_2pubcount_posNTL", "2pubcount_pos2",
                "wghtd_2pubcount_pos2",     "3pubcount",              "wghtd_3pubcount",        "3pubcount_pos1",
                "wghtd_3pubcount_pos1",     "3pubcount_posN",         //"wghtd_3pubcount_posN",
                "3pubcount_posM",           "wghtd_3pubcount_posM",   "3pubcount_posNTL",
                "wghtd_3pubcount_posNTL",   "3pubcount_pos2",         "wghtd_3pubcount_pos2",
                "4pubcount",                "wghtd_4pubcount",        "4pubcount_pos1",         "wghtd_4pubcount_pos1",
                "4pubcount_posN",           "wghtd_4pubcount_posN",   "4pubcount_posM",
                "wghtd_4pubcount_posM",     "4pubcount_posNTL",       "wghtd_4pubcount_posNTL",
                "4pubcount_pos2",           "wghtd_4pubcount_pos2"
            };

            DataTable HandGeneratedData = NpoiHelper.ReadExcelFileToDataTable(AppDomain.CurrentDomain.BaseDirectory +
                                                                              "\\Unit Tests\\TestReports", "TestEntireReport_Data.xls");

            Assert.AreEqual(HandGeneratedData.Rows.Count, 85);

            var valuesChecked = 0;

            for (int RowNum = 0; RowNum < HandGeneratedData.Rows.Count; RowNum++)
            {
                // Find the rows in the hand-generated data and the report
                DataRow HandGeneratedRow = HandGeneratedData.Rows[RowNum];
                Setnb = HandGeneratedRow[0].ToString();
                int Year = Convert.ToInt32(HandGeneratedRow[1]);

                for (int i = 2; i < Columns.Length; i++)
                {
                    valuesChecked++;
                    String columnName    = Columns[i];
                    var    actualValue   = ReportData(Setnb, Year.ToString(), columnName);
                    string expectedValue = HandGeneratedRow[columnName].ToString();
                    Assert.AreEqual(expectedValue, actualValue, Setnb + "/" + Year.ToString() + "/" + columnName + " -- hand generated has " + expectedValue + ", report has" + actualValue);
                }
            }

            Assert.AreEqual((HandGeneratedData.Rows.Count) * (Columns.Length - 2), valuesChecked);

            // Use BackupReportAndGetSetnbs to back up the report -- check that it
            // returns the correct list of Setnbs and removes the last one from
            // the file. The last Setnb should still be in Setnb.
            ArrayList Setnbs = Reports.BackupReportAndGetSetnbs(AppDomain.CurrentDomain.BaseDirectory + "\\TestEntireReport.csv");
            // Read the backup file that was created, make sure that the last setnb in the
            // file isn't contained and the others are
            StreamReader reader = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "\\TestEntireReport.csv.bak");
            string       Line   = reader.ReadLine();   //skip the header row

            while ((Line = reader.ReadLine()) != null) // Find the last setnb in the original file
            {
                Setnb = Line.Substring(0, 8);
            }
            string RemovedSetnb = Setnb;

            Assert.IsFalse(Setnbs.Contains(RemovedSetnb));
            reader.Close();

            // Verify that the new file contains only the other setnbs
            Assert.IsTrue(Setnbs.Count == 3);
            Assert.IsFalse(Setnbs.Contains(RemovedSetnb));
            reader = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "\\TestEntireReport.csv");
            Line   = reader.ReadLine(); //skip the header row
            while ((Line = reader.ReadLine()) != null)
            {
                Setnb = Line.Substring(0, 8);
                Assert.IsTrue(Setnbs.Contains(Setnb));
                Assert.IsFalse(Setnb == RemovedSetnb);
            }
            reader.Close();

            // Delete the temporary files
            File.Delete(AppDomain.CurrentDomain.BaseDirectory + "\\TestEntireReport.csv");
            File.Delete(AppDomain.CurrentDomain.BaseDirectory + "\\TestEntireReport.csv.bak");
        }
        /// <summary>
        /// Write the People or Publications report
        /// </summary>
        private void PeopleOrPublicationsReport(WhichReport whichReport, bool ContinuePreviousReport, Reports reports)
        {
            ArrayList SetnbsToSkip = null;

            string Filename = Folder.Text + "\\";

            if (whichReport == WhichReport.People)
            {
                Filename += People.Text;
            }
            else
            {
                Filename += Publications.Text;
            }

            // If we're continuing the previous report, rename it to a backup filename,
            // then copy every person in it to the file to append -- except for the
            // last person, which we'll restart.
            if (ContinuePreviousReport && File.Exists(Filename))
            {
                try
                {
                    SetnbsToSkip = Reports.BackupReportAndGetSetnbs(Filename);
                }
                catch (Exception ex)
                {
                    AddLogEntry("An error occurred while creating the report '" + Filename + "': " + ex.Message);
                    return;
                }
            }

            // Make an anonymous callback function that keeps track of the callback data
            Reports.ReportStatus StatusCallback = delegate(int number, int total, Person person, bool StatusBarOnly)
            {
                toolStripProgressBar1.Minimum = 0;
                toolStripProgressBar1.Maximum = total;
                toolStripProgressBar1.Value   = number;
                if (!StatusBarOnly)
                {
                    AddLogEntry("Writing " + person.Last + " " + person.Setnb + " (" + number.ToString() + " of " + total.ToString() + ")");
                }
                Application.DoEvents();
            };

            // Make an anonymous callback function to receive message
            Reports.ReportMessage MessageCallback = delegate(string Message)
            {
                AddLogEntry(Message);
                Application.DoEvents();
            };

            // Write the Stars report
            StreamWriter writer;

            try
            {
                // Open the writer to append the file only if continuing from a previous report
                writer = new StreamWriter(Filename, ContinuePreviousReport);
            }
            catch (Exception ex)
            {
                AddLogEntry("An error occurred while creating the report '" + Filename + "': " + ex.Message);
                return;
            }
            try
            {
                if (whichReport == WhichReport.People)
                {
                    reports.PeopleReport(SetnbsToSkip, writer, StatusCallback, MessageCallback);
                }
                else
                {
                    reports.PubsReport(SetnbsToSkip, writer, StatusCallback, MessageCallback);
                }
            }
            catch (Exception ex)
            {
                AddLogEntry("An error occurred while creating the report '" + Filename + "': " + ex.Message);
            }
            finally
            {
                writer.Close();
            }
        }