Esempio n. 1
0
        public void FillGapsInBD()
        {
            List <string> linksToFill = new List <string>();

            using (var con = new NpgsqlConnection(connStr))
            {
                con.Open();
                NpgsqlCommand comd = con.CreateCommand();
                //comd.CommandText = "SELECT to_regclass('dbo.\"Professors\"');";
                //bool exist = comd.ExecuteScalar() == null ? true : false;
                //if (exist)
                //{
                NpgsqlCommand cmd = con.CreateCommand();

                cmd.CommandText = "Select link from dbo.\"Professors\" where pib =''";
                using (NpgsqlDataReader rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        linksToFill.Add(rdr.GetString(0));
                    }
                }
            }

            using (var db = new IndividualContext())
            {
                foreach (string lecturerUrl in linksToFill)
                {
                    Professor newProfessor = new Professor();
                    newProfessor.FillProfessorInfo(RequestGetter.GetRequestByUrl(lecturerUrl));
                    newProfessor.link = lecturerUrl;
                    db.Professors.Add(newProfessor);
                    db.Entry(newProfessor).State = EntityState.Modified;
                    db.SaveChanges();
                }
            }
        }
Esempio n. 2
0
        public void FillStaffBD()
        {
            string LastFaculty       = "";
            string LastDepartment    = "";
            int    startFacultyIndex = 0;

            if (!startNewDB)
            {
                using (var con = new NpgsqlConnection(connStr))
                {
                    con.Open();
                    NpgsqlCommand comd = con.CreateCommand();
                    //comd.CommandText = "SELECT to_regclass('dbo.\"Professors\"');";
                    //bool exist = comd.ExecuteScalar() == null ? true : false;
                    //if (exist)
                    //{
                    NpgsqlCommand cmd = con.CreateCommand();
                    cmd.CommandText = "Select department,faculty from dbo.\"Professors\" order by id desc limit 1";
                    using (NpgsqlDataReader rdr = cmd.ExecuteReader())
                    {
                        while (rdr.Read())
                        {
                            LastDepartment = rdr.GetString(0).ToLower();
                            LastFaculty    = rdr.GetString(1).ToLower();
                        }
                    }
                    //}
                }
                startFacultyIndex = facultyCollegeUrlDict.Keys.ToList().IndexOf(LastFaculty);
            }
            //else{
            //    using (var con = new NpgsqlConnection(connStr))
            //    {
            //        con.Open();
            //        NpgsqlCommand comd = con.CreateCommand();

            //        NpgsqlCommand cmd = con.CreateCommand();
            //        cmd.CommandText = "delete from dbo.\"Professors\"";
            //        cmd.ExecuteNonQuery();
            //    }
            //}


            bool restart;

            do
            {
                restart = false;
                try
                {
                    foreach (string facultyCollegeUrl in facultyCollegeUrlDict.Values.Skip(startFacultyIndex))
                    {
                        string requestUrl = (facultyCollegeUrl + "/about/staff");
                        Dictionary <string, List <string> > facultyStaffUrl = GetFacultyStaffUrlDict(RequestGetter.GetRequestByUrl(requestUrl));
                        int startStaffIndex = -1;
                        if (!startNewDB)
                        {
                            startStaffIndex = facultyStaffUrl.Keys.ToList().IndexOf("кафедра " + LastDepartment);
                            if (startStaffIndex == -1)
                            {
                                startStaffIndex = facultyStaffUrl.Keys.ToList().IndexOf(LastDepartment.Replace("лабораторії ", ""));
                            }
                            if (startStaffIndex == -1)
                            {
                                startStaffIndex = facultyStaffUrl.Keys.ToList().IndexOf(LastDepartment.Replace("лабораторії", "лабораторія"));
                            }
                        }


                        using (var db = new IndividualContext())
                        {
                            foreach (KeyValuePair <string, List <string> > department in facultyStaffUrl.Skip(startStaffIndex + 1))
                            {
                                List <Professor> departmentStaff = new List <Professor>();
                                foreach (string lecturerUrl in department.Value)
                                {
                                    Professor newProfessor = new Professor();
                                    newProfessor.FillProfessorInfo(RequestGetter.GetRequestByUrl(lecturerUrl));
                                    newProfessor.link = lecturerUrl;
                                    //departmentStaff.Add(newProfessor);
                                    db.Professors.Add(newProfessor);
                                }

                                //facultyStaffDict.Add(department.Key, departmentStaff);//title-get department title
                                db.SaveChanges();
                            }
                        }
                    }
                }
                catch (System.Net.WebException)
                {
                    restart = true;
                }
            } while (restart);
        }