//[Action(ToolTip = "Import Students Semester Result to Database", TargetObjectsCriteria="IsImported=false")] public void ImportStudentResult() { Session session = this.Session; string tempStudentFolderPath; string tempStudentFile; string tempStudentLogFile; string filename = CsvFile.FileName + DateTime.Now.ToString("dd-MM-yyyy-HHmmss") + "-log.html"; if (HttpContext.Current != null) { tempStudentFolderPath = HttpContext.Current.Request.MapPath("~/tempFolder"); tempStudentFile = HttpContext.Current.Request.MapPath("~/tempFolder/" + CsvFile.FileName); tempStudentLogFile = HttpContext.Current.Request.MapPath("~/tempFolder/" + filename); } else { tempStudentFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tempFolder"); tempStudentFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tempFolder/", CsvFile.FileName); tempStudentLogFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tempFolder/", filename); } if (!Directory.Exists(tempStudentFolderPath)) { Directory.CreateDirectory(tempStudentFolderPath); } Dictionary <string, int> columnIndexs = new Dictionary <string, int>(); Dictionary <string, string> valueIndexs = new Dictionary <string, string>(); valueIndexs.Add("NHHK", ""); valueIndexs.Add("MSSV", ""); valueIndexs.Add("HO", ""); valueIndexs.Add("TEN", ""); //valueIndexs.Add("NGAYSINH", ""); valueIndexs.Add("NHOMLOPMH", ""); valueIndexs.Add("MAMH", ""); valueIndexs.Add("TENMH", ""); valueIndexs.Add("SOTC", ""); valueIndexs.Add("DTB10", ""); valueIndexs.Add("DTB4", ""); valueIndexs.Add("DIEMCHU", ""); columnIndexs.Add("NHHK", -1); columnIndexs.Add("MSSV", -1); columnIndexs.Add("HO", -1); columnIndexs.Add("TEN", -1); //columnIndexs.Add("NGAYSINH", -1); columnIndexs.Add("NHOMLOPMH", -1); columnIndexs.Add("MAMH", -1); columnIndexs.Add("TENMH", -1); columnIndexs.Add("SOTC", -1); columnIndexs.Add("DTB10", -1); columnIndexs.Add("DTB4", -1); columnIndexs.Add("DIEMCHU", -1); using (System.IO.FileStream fileStream = new FileStream(tempStudentFile, FileMode.OpenOrCreate)) { using (System.IO.StreamWriter fileStreamlog = new StreamWriter(tempStudentLogFile, true)) { fileStreamlog.WriteLine("<html><header><title>" + CsvFile.FileName + DateTime.Now.ToString("dd-MM-yyyy-HHmmss") + "-log </title> "+ "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" />" + "</head><body>"); CsvFile.SaveToStream(fileStream); fileStream.Position = 0; StreamReader r = new StreamReader(fileStream); string newLine; bool foundHeader = false; int iLine = 1; try { //Tìm dòng chứa TEN cột while ((newLine = r.ReadLine()) != null) { string[] row = newLine.Split(new string[] { "~" }, StringSplitOptions.None); if (!foundHeader) { for (int i = 0; i < row.Length; i++) { if (columnIndexs.ContainsKey(row[i].ToUpper().Trim())) { columnIndexs[row[i].ToUpper().Trim()] = i; //Đã tìm thấy dòng chứa TEN cột. Xác định vị trí của cột } } if (!columnIndexs.Values.Contains(-1)) { foundHeader = true; break; } else { for (int i = 0; i < row.Length; i++) { if (columnIndexs.ContainsKey(row[i].ToUpper().Trim())) { columnIndexs[row[i].ToUpper().Trim()] = -1; //không tìm thấy dòng chứa TEN cột. Xác định vị trí của cột } } } } } ResultLink = "./tempFolder/" + CsvFile.FileName + DateTime.Now.ToString("dd-MM-yyyy-HHmmss") + "-log.html"; if (!foundHeader) { throw new UserFriendlyException("Lỗi cấu trúc file"); } //Các dòng sau đó đều là dòng dữ liệu IsImported = true; List <Student> listStudent = new List <Student>(); List <Lesson> listLessons = new List <Lesson>(); List <Subject> listSubject = new List <Subject>(); List <Semester> listSemester = new List <Semester>(); while ((newLine = r.ReadLine()) != null) { iLine++; using (NestedUnitOfWork uow = Session.BeginNestedUnitOfWork()) { uow.BeginTransaction(); try { string[] row = newLine.Split(new string[] { "~" }, StringSplitOptions.None); foreach (var column in columnIndexs) { valueIndexs[column.Key] = row[column.Value]; } //tìm sinh viên Student student = listStudent.Find(s => s.StudentCode == valueIndexs["MSSV"]); if (student == null) { student = session.FindObject <Student>(CriteriaOperator.Parse("StudentCode = ?", valueIndexs["MSSV"])); } if (student == null) { fileStreamlog.WriteLine(string.Format("ERROR:Cannot find student: \"{0} - {1} {2} \" on line \"{3}\" on {4:dd-mm-yy HH:MM:ss}<br/>", valueIndexs["MSSV"], valueIndexs["HO"], valueIndexs["TEN"], iLine, DateTime.Now)); continue; } else { if (!(student.FirstName.Contains(valueIndexs["HO"]) && student.LastName.Contains(valueIndexs["TEN"]))) { fileStreamlog.WriteLine(string.Format("WARNING: Found student: \"{0}\" but Name:\"{1} {2}\" is not Like \"{3} {4}\" on line \"{5}\" on {6:dd-mm-yy HH:MM:ss}<br/>", valueIndexs["MSSV"], student.FirstName, student.LastName, valueIndexs["HO"], valueIndexs["TEN"], iLine, DateTime.Now)); } listStudent.Add(student); } //found student //tìm nhóm lớp int nhomlop; if (!int.TryParse(valueIndexs["NHOMLOPMH"], out nhomlop)) { fileStreamlog.WriteLine(string.Format("CANNNOT CONVERT TO NUMBER for LessonCode: \"{0}\" on line \"{3}\" on {4:dd-mm-yy HH:MM:ss}<br/>", valueIndexs["NHOMLOPMH"], iLine, DateTime.Now)); continue; } Lesson lesson = listLessons.Find(l => l.LessonCode == nhomlop); if (lesson == null) { lesson = session.FindObject <Lesson>(CriteriaOperator.Parse("LessonCode = ?", nhomlop)); } if (lesson != null) { if (lesson.Semester.SemesterName != valueIndexs["NHHK"]) { fileStreamlog.WriteLine(string.Format("Found Lesson \"{0}\" but Semester {1} not same {2} on line \"{3}\" on {4:dd-mm-yy HH:MM:ss} - CANNOT IMPORT THIS LINE<br/>", valueIndexs["NHOMLOPMH"], lesson.Semester.SemesterName, valueIndexs["NHHK"], iLine, DateTime.Now)); continue; } if (lesson.Subject.SubjectCode != valueIndexs["MAMH"]) { fileStreamlog.WriteLine(string.Format("Found Lesson \"{0}\" but Subject Code {1} not same {2} on line \"{3}\" on {4:dd-mm-yy HH:MM:ss} - CANNOT IMPORT THIS LINE<br/>", valueIndexs["NHOMLOPMH"], lesson.Subject.SubjectCode, valueIndexs["MAMH"], iLine, DateTime.Now)); continue; } if (!lesson.ClassIDs.Contains(student.StudentClass.ClassCode)) { lesson.ClassIDs += "," + student.StudentClass.ClassCode; } if (!listLessons.Contains(lesson)) { listLessons.Add(lesson); } } else //create new lesson { Semester semester = listSemester.Find(s => s.SemesterName == valueIndexs["NHHK"]); if (semester == null) { semester = session.FindObject <Semester>(CriteriaOperator.Parse("SemesterName = ?", valueIndexs["NHHK"])); } if (semester == null) //create new semester { semester = new Semester(session) { SemesterName = valueIndexs["NHHK"] }; semester.Save(); fileStreamlog.WriteLine(string.Format("Create Semester:{0} - on line \"{1}\" on {2:dd-mm-yy HH:MM:ss} <br/>", valueIndexs["NHHK"], iLine, DateTime.Now)); listSemester.Add(semester); } else if (!listSemester.Contains(semester)) { listSemester.Add(semester); } Subject subject = listSubject.Find(s => s.SubjectCode == valueIndexs["MAMH"]); if (subject == null) { subject = session.FindObject <Subject>(CriteriaOperator.Parse("SubjectCode = ?", valueIndexs["MAMH"])); } if (subject != null) { if (subject.SubjectName != valueIndexs["TENMH"]) { fileStreamlog.WriteLine(string.Format("WARNING: Found Subject \"{0}\" for lesson {1} but Name {2} not same {3} on line \"{4}\" on {5:dd-mm-yy HH:MM:ss} <br/>", valueIndexs["MAMH"], valueIndexs["NHOMLOPMH"], subject.SubjectName, valueIndexs["TENMH"], iLine, DateTime.Now)); } if (!listSubject.Contains(subject)) { listSubject.Add(subject); } } else//create new subject { subject = new Subject(session) { SubjectCode = valueIndexs["MAMH"], SubjectName = valueIndexs["TENMH"], Credit = Convert.ToDouble(valueIndexs["SOTC"]) }; subject.Save(); fileStreamlog.WriteLine(string.Format("Create Subject:{0} - {1} ({2}TC) on line \"{3}\" on {4:dd-mm-yy HH:MM:ss} <br/>", valueIndexs["MAMH"], valueIndexs["TENMH"], valueIndexs["SOTC"], iLine, DateTime.Now)); listSubject.Add(subject); } lesson = new Lesson(session) { Semester = semester, Subject = subject, LessonCode = nhomlop, CanRegister = false, LessonNote = "Tạo mới cho phân hệ điểm", ClassIDs = student.StudentClass.ClassCode }; lesson.Save(); fileStreamlog.WriteLine(string.Format("Create Lesson :{0} - {1} ({2}TC) on line \"{3}\" on {4:dd-mm-yy HH:MM:ss} <br/>", nhomlop, valueIndexs["MAMH"], valueIndexs["NHHK"], iLine, DateTime.Now)); listLessons.Add(lesson); } try { StudentResult studResult = session.FindObject <StudentResult>(CriteriaOperator.Parse("Student = ? and Lesson=?", student, lesson)); if (studResult == null) { studResult = new StudentResult(session) { Student = student, Lesson = lesson, AvgMark10 = Convert.ToDouble(valueIndexs["DTB10"]), AvgMark4 = Convert.ToDouble(valueIndexs["DTB4"]), AvgChar = valueIndexs["DIEMCHU"] }; studResult.Save(); fileStreamlog.WriteLine(string.Format("Create StudentResult Lesson {0} with Subject Code {1} for student: \"{2}\"-\"{3}\" on {4:dd-mm-yy HH:MM:ss} - line {5} <br/>", lesson.LessonCode, lesson.Subject.SubjectCode, student.StudentCode, student.FullName, DateTime.Now, iLine)); } else { studResult.AvgMark10 = Convert.ToDouble(valueIndexs["DTB10"]); studResult.AvgMark4 = Convert.ToDouble(valueIndexs["DTB4"]); studResult.AvgChar = valueIndexs["DIEMCHU"]; studResult.Save(); fileStreamlog.WriteLine(string.Format("Update StudentResult Lesson {0} with Subject Code {1} for student: \"{2}\"-\"{3}\" on {4:dd-mm-yy HH:MM:ss} - line {5} <br/>", lesson.LessonCode, lesson.Subject.SubjectCode, student.StudentCode, student.FullName, DateTime.Now, iLine)); } } catch (Exception ex) { fileStreamlog.WriteLine(string.Format("Cannot create StudentResult for student \"{0}\"-{1} on {2:dd-mm-yy HH:MM:ss} - Line {3} <br/>", student.StudentCode, student.FullName, DateTime.Now, iLine)); fileStreamlog.WriteLine(ex.Message + ex.StackTrace); } uow.CommitTransaction(); } catch (Exception ex) { fileStreamlog.WriteLine(string.Format("Error Line \"{0}\": \r\n {1}\r\n {1} on {1:dd-mm-yy HH:MM:ss} <br/>", iLine, ex.Message, ex.StackTrace, DateTime.Now)); } } } fileStreamlog.WriteLine(string.Format("Create \"{0}\" all StudentResult on {1:dd-mm-yy HH:MM:ss} <br/>", iLine, DateTime.Now)); } catch (UserFriendlyException ex) { fileStreamlog.WriteLine(string.Format("Error Line \"{0}\": \"{1}\" on {1:dd-mm-yy HH:MM:ss} <br/>", iLine, ex.StackTrace, DateTime.Now)); fileStreamlog.WriteLine("</body></html>"); fileStream.Close(); fileStreamlog.Close(); throw ex; } finally { fileStreamlog.WriteLine("</body></html>"); fileStream.Close(); fileStreamlog.Close(); } } } }
//[Action(ToolTip = "Import Students Semester Result to Database", TargetObjectsCriteria="IsImported=false")] public void ImportStudentResult() { Session session = this.Session; string tempStudentFolderPath; string tempStudentFile; string tempStudentLogFile; string filename = CsvFile.FileName + DateTime.Now.ToString("dd-MM-yyyy-HHmmss") + "-log.html"; if (HttpContext.Current != null) { tempStudentFolderPath = HttpContext.Current.Request.MapPath("~/tempFolder"); tempStudentFile = HttpContext.Current.Request.MapPath("~/tempFolder/" + CsvFile.FileName); tempStudentLogFile = HttpContext.Current.Request.MapPath("~/tempFolder/" + filename); } else { tempStudentFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tempFolder"); tempStudentFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tempFolder/", CsvFile.FileName); tempStudentLogFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tempFolder/", filename); } if (!Directory.Exists(tempStudentFolderPath)) Directory.CreateDirectory(tempStudentFolderPath); Dictionary<string, int> columnIndexs = new Dictionary<string, int>(); Dictionary<string, string> valueIndexs = new Dictionary<string, string>(); valueIndexs.Add("NHHK", ""); valueIndexs.Add("MSSV", ""); valueIndexs.Add("HO", ""); valueIndexs.Add("TEN", ""); //valueIndexs.Add("NGAYSINH", ""); valueIndexs.Add("NHOMLOPMH", ""); valueIndexs.Add("MAMH", ""); valueIndexs.Add("TENMH", ""); valueIndexs.Add("SOTC", ""); valueIndexs.Add("DTB10", ""); valueIndexs.Add("DTB4", ""); valueIndexs.Add("DIEMCHU", ""); columnIndexs.Add("NHHK",-1); columnIndexs.Add("MSSV", -1); columnIndexs.Add("HO", -1); columnIndexs.Add("TEN", -1); //columnIndexs.Add("NGAYSINH", -1); columnIndexs.Add("NHOMLOPMH", -1); columnIndexs.Add("MAMH", -1); columnIndexs.Add("TENMH", -1); columnIndexs.Add("SOTC", -1); columnIndexs.Add("DTB10", -1); columnIndexs.Add("DTB4", -1); columnIndexs.Add("DIEMCHU", -1); using (System.IO.FileStream fileStream = new FileStream(tempStudentFile, FileMode.OpenOrCreate)) { using (System.IO.StreamWriter fileStreamlog = new StreamWriter(tempStudentLogFile, true)) { fileStreamlog.WriteLine("<html><header><title>"+ CsvFile.FileName + DateTime.Now.ToString("dd-MM-yyyy-HHmmss") + "-log </title> " + "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" />" + "</head><body>"); CsvFile.SaveToStream(fileStream); fileStream.Position = 0; StreamReader r = new StreamReader(fileStream); string newLine; bool foundHeader = false; int iLine = 1; try { //Tìm dòng chứa TEN cột while ((newLine = r.ReadLine()) != null) { string[] row = newLine.Split(new string[] { "~" }, StringSplitOptions.None); if (!foundHeader) { for (int i = 0; i < row.Length; i++) if (columnIndexs.ContainsKey(row[i].ToUpper().Trim())) { columnIndexs[row[i].ToUpper().Trim()] = i; //Đã tìm thấy dòng chứa TEN cột. Xác định vị trí của cột } if (!columnIndexs.Values.Contains(-1)) { foundHeader = true; break; } else { for (int i = 0; i < row.Length; i++) if (columnIndexs.ContainsKey(row[i].ToUpper().Trim())) { columnIndexs[row[i].ToUpper().Trim()] = -1; //không tìm thấy dòng chứa TEN cột. Xác định vị trí của cột } } } } ResultLink = "./tempFolder/" + CsvFile.FileName + DateTime.Now.ToString("dd-MM-yyyy-HHmmss") + "-log.html"; if (!foundHeader) throw new UserFriendlyException("Lỗi cấu trúc file"); //Các dòng sau đó đều là dòng dữ liệu IsImported = true; List<Student> listStudent = new List<Student>(); List<Lesson> listLessons = new List<Lesson>(); List<Subject> listSubject = new List<Subject>(); List<Semester> listSemester = new List<Semester>(); while ((newLine = r.ReadLine()) != null) { iLine++; using (NestedUnitOfWork uow = Session.BeginNestedUnitOfWork()) { uow.BeginTransaction(); try { string[] row = newLine.Split(new string[] { "~" }, StringSplitOptions.None); foreach (var column in columnIndexs) { valueIndexs[column.Key] = row[column.Value]; } //tìm sinh viên Student student = listStudent.Find(s=> s.StudentCode ==valueIndexs["MSSV"]); if (student ==null) student = session.FindObject<Student>(CriteriaOperator.Parse("StudentCode = ?", valueIndexs["MSSV"])); if (student == null) { fileStreamlog.WriteLine(string.Format("ERROR:Cannot find student: \"{0} - {1} {2} \" on line \"{3}\" on {4:dd-mm-yy HH:MM:ss}<br/>", valueIndexs["MSSV"], valueIndexs["HO"], valueIndexs["TEN"], iLine, DateTime.Now)); continue; } else{ if (!(student.FirstName.Contains(valueIndexs["HO"]) && student.LastName.Contains(valueIndexs["TEN"]))) { fileStreamlog.WriteLine(string.Format("WARNING: Found student: \"{0}\" but Name:\"{1} {2}\" is not Like \"{3} {4}\" on line \"{5}\" on {6:dd-mm-yy HH:MM:ss}<br/>", valueIndexs["MSSV"], student.FirstName,student.LastName,valueIndexs["HO"], valueIndexs["TEN"], iLine, DateTime.Now)); } listStudent.Add(student); } //found student //tìm nhóm lớp int nhomlop; if (!int.TryParse(valueIndexs["NHOMLOPMH"], out nhomlop)) { fileStreamlog.WriteLine(string.Format("CANNNOT CONVERT TO NUMBER for LessonCode: \"{0}\" on line \"{3}\" on {4:dd-mm-yy HH:MM:ss}<br/>", valueIndexs["NHOMLOPMH"], iLine, DateTime.Now)); continue; } Lesson lesson = listLessons.Find(l=>l.LessonCode == nhomlop); if (lesson==null) lesson = session.FindObject<Lesson>(CriteriaOperator.Parse("LessonCode = ?", nhomlop)); if (lesson != null) { if (lesson.Semester.SemesterName != valueIndexs["NHHK"]) { fileStreamlog.WriteLine(string.Format("Found Lesson \"{0}\" but Semester {1} not same {2} on line \"{3}\" on {4:dd-mm-yy HH:MM:ss} - CANNOT IMPORT THIS LINE<br/>", valueIndexs["NHOMLOPMH"], lesson.Semester.SemesterName, valueIndexs["NHHK"], iLine, DateTime.Now)); continue; } if (lesson.Subject.SubjectCode != valueIndexs["MAMH"]) { fileStreamlog.WriteLine(string.Format("Found Lesson \"{0}\" but Subject Code {1} not same {2} on line \"{3}\" on {4:dd-mm-yy HH:MM:ss} - CANNOT IMPORT THIS LINE<br/>", valueIndexs["NHOMLOPMH"], lesson.Subject.SubjectCode, valueIndexs["MAMH"], iLine, DateTime.Now)); continue; } if (!lesson.ClassIDs.Contains(student.StudentClass.ClassCode)) lesson.ClassIDs += "," + student.StudentClass.ClassCode; if (!listLessons.Contains(lesson)) listLessons.Add(lesson); } else //create new lesson { Semester semester = listSemester.Find(s => s.SemesterName == valueIndexs["NHHK"]); if (semester ==null) semester = session.FindObject<Semester>(CriteriaOperator.Parse("SemesterName = ?" ,valueIndexs["NHHK"])); if (semester == null) //create new semester { semester = new Semester(session) { SemesterName = valueIndexs["NHHK"] }; semester.Save(); fileStreamlog.WriteLine(string.Format("Create Semester:{0} - on line \"{1}\" on {2:dd-mm-yy HH:MM:ss} <br/>", valueIndexs["NHHK"], iLine, DateTime.Now)); listSemester.Add(semester); } else if (!listSemester.Contains(semester)) listSemester.Add(semester); Subject subject = listSubject.Find(s => s.SubjectCode == valueIndexs["MAMH"]); if (subject ==null) subject=session.FindObject<Subject>(CriteriaOperator.Parse("SubjectCode = ?",valueIndexs["MAMH"])); if (subject != null) { if (subject.SubjectName != valueIndexs["TENMH"]) { fileStreamlog.WriteLine(string.Format("WARNING: Found Subject \"{0}\" for lesson {1} but Name {2} not same {3} on line \"{4}\" on {5:dd-mm-yy HH:MM:ss} <br/>", valueIndexs["MAMH"], valueIndexs["NHOMLOPMH"], subject.SubjectName, valueIndexs["TENMH"], iLine, DateTime.Now)); } if (!listSubject.Contains(subject)) listSubject.Add(subject); } else//create new subject { subject = new Subject(session) { SubjectCode = valueIndexs["MAMH"], SubjectName = valueIndexs["TENMH"], Credit = Convert.ToDouble(valueIndexs["SOTC"]) }; subject.Save(); fileStreamlog.WriteLine(string.Format("Create Subject:{0} - {1} ({2}TC) on line \"{3}\" on {4:dd-mm-yy HH:MM:ss} <br/>", valueIndexs["MAMH"], valueIndexs["TENMH"], valueIndexs["SOTC"], iLine, DateTime.Now)); listSubject.Add(subject); } lesson = new Lesson(session) { Semester = semester, Subject = subject, LessonCode = nhomlop, CanRegister = false, LessonNote = "Tạo mới cho phân hệ điểm", ClassIDs = student.StudentClass.ClassCode }; lesson.Save(); fileStreamlog.WriteLine(string.Format("Create Lesson :{0} - {1} ({2}TC) on line \"{3}\" on {4:dd-mm-yy HH:MM:ss} <br/>", nhomlop, valueIndexs["MAMH"], valueIndexs["NHHK"], iLine, DateTime.Now)); listLessons.Add(lesson); } try { StudentResult studResult = session.FindObject<StudentResult>(CriteriaOperator.Parse("Student = ? and Lesson=?",student,lesson)); if (studResult == null) { studResult = new StudentResult(session) { Student = student, Lesson = lesson, AvgMark10 = Convert.ToDouble(valueIndexs["DTB10"]), AvgMark4 = Convert.ToDouble(valueIndexs["DTB4"]), AvgChar = valueIndexs["DIEMCHU"] }; studResult.Save(); fileStreamlog.WriteLine(string.Format("Create StudentResult Lesson {0} with Subject Code {1} for student: \"{2}\"-\"{3}\" on {4:dd-mm-yy HH:MM:ss} - line {5} <br/>", lesson.LessonCode, lesson.Subject.SubjectCode, student.StudentCode, student.FullName, DateTime.Now, iLine)); } else { studResult.AvgMark10 = Convert.ToDouble(valueIndexs["DTB10"]); studResult.AvgMark4 = Convert.ToDouble(valueIndexs["DTB4"]); studResult.AvgChar = valueIndexs["DIEMCHU"]; studResult.Save(); fileStreamlog.WriteLine(string.Format("Update StudentResult Lesson {0} with Subject Code {1} for student: \"{2}\"-\"{3}\" on {4:dd-mm-yy HH:MM:ss} - line {5} <br/>", lesson.LessonCode, lesson.Subject.SubjectCode, student.StudentCode, student.FullName, DateTime.Now, iLine)); } } catch (Exception ex) { fileStreamlog.WriteLine(string.Format("Cannot create StudentResult for student \"{0}\"-{1} on {2:dd-mm-yy HH:MM:ss} - Line {3} <br/>", student.StudentCode, student.FullName, DateTime.Now, iLine)); fileStreamlog.WriteLine(ex.Message + ex.StackTrace); } uow.CommitTransaction(); } catch (Exception ex) { fileStreamlog.WriteLine(string.Format("Error Line \"{0}\": \r\n {1}\r\n {1} on {1:dd-mm-yy HH:MM:ss} <br/>", iLine, ex.Message, ex.StackTrace, DateTime.Now)); } } } fileStreamlog.WriteLine(string.Format("Create \"{0}\" all StudentResult on {1:dd-mm-yy HH:MM:ss} <br/>", iLine, DateTime.Now)); } catch (UserFriendlyException ex) { fileStreamlog.WriteLine(string.Format("Error Line \"{0}\": \"{1}\" on {1:dd-mm-yy HH:MM:ss} <br/>", iLine, ex.StackTrace, DateTime.Now)); fileStreamlog.WriteLine("</body></html>"); fileStream.Close(); fileStreamlog.Close(); throw ex; } finally { fileStreamlog.WriteLine("</body></html>"); fileStream.Close(); fileStreamlog.Close(); } } } }
private void ImportLessons(Session uow, XElement xElement, Dictionary <string,Lesson> lessonDic, Dictionary<string,Subject> subjectDic, Dictionary<string,TkbClass> classDic, Dictionary<string, TkbGroup> groupDic, Dictionary<string,TkbTeacher> teacherDic, StreamWriter fileStreamlog) { SortProperty sort = new SortProperty("LessonCode", SortingDirection.Descending); int nextcode = 0; using (XPCollection collection = new XPCollection(uow, typeof(Lesson), new BinaryOperator( "Semester.SemesterName", Semester.SemesterName), sort) { TopReturnedObjects = 1 }) { if (collection.Count > 0) { Lesson firstLesson = collection[0] as Lesson; nextcode = firstLesson.LessonCode; } } int newcode=Convert.ToInt32(Semester.SemesterName+"0000"); //first lesson of semester if (newcode > nextcode) nextcode = newcode; foreach (XElement row in xElement.Elements()) { string sid = "", classids = "", subjectid = "", teacherids = "", classroomids = "", groupids = ""; string weeks = "", studentcoustudentidsnt = "", periodsperweek = ""; int periodspercard = 0; foreach (XAttribute column in row.Attributes()) { switch (column.Name.LocalName) { case "id": sid = column.Value; break; case "classids": classids = column.Value; break; case "subjectid": subjectid = column.Value; break; case "periodspercard": periodspercard = int.Parse(column.Value); break; case "periodsperweek": periodsperweek = column.Value; break; case "teacherids": teacherids = column.Value; break; case "classroomids": classroomids = column.Value; break; case "groupids": groupids = column.Value; break; case "studentcoustudentidsnt": studentcoustudentidsnt = column.Value; break; case "weeks": weeks = column.Value; break; default: break; } } TkbLesson tkblesson = uow.FindObject<TkbLesson>(new BinaryOperator("ID", sid)); if (tkblesson == null) { tkblesson = new TkbLesson(uow) { Semester = uow.FindObject<Semester>(new BinaryOperator("Oid", this.Semester.Oid)), ID = sid, PeriodsPerCard = periodspercard, PeriodsPerWeek = periodsperweek, ClassroomIDs = classroomids, GroupIDs = groupids, StudentIDs = studentcoustudentidsnt, Week = weeks, Note = this.Note, SubjectID = (subjectDic.ContainsKey(subjectid)?subjectDic[subjectid].SubjectCode:null) }; tkblesson.NumExpectation = 0; tkblesson.GroupIDs = ""; if (groupids != "") { foreach (string groupid in groupids.Split(',')) { string groupname =""; if (groupDic.ContainsKey(groupid)) { if (groupDic[groupid].EntireClass) { groupname = classDic.ContainsKey(groupDic[groupid].Classid) ? classDic[groupDic[groupid].Classid].Name : groupDic[groupid].Name; } else { groupname = groupDic[groupid].Name; } } else { groupname = groupid; } tkblesson.GroupIDs += (groupname!="" ? groupname + "," : ""); tkblesson.NumExpectation += (groupDic.ContainsKey(groupid) ? groupDic[groupid].StudentCount : 0); } tkblesson.GroupIDs = tkblesson.GroupIDs.TrimEnd(','); } tkblesson.ClassIDs = ""; if (classids != "") { foreach (string classid in classids.Split(',')) { tkblesson.ClassIDs += (classDic.ContainsKey(classid) ? classDic[classid].Short + "," :""); } tkblesson.ClassIDs= tkblesson.ClassIDs.TrimEnd(','); } tkblesson.TeacherIDs = ""; if (teacherids != "") { foreach (string teacherid in teacherids.Split(',')) { tkblesson.TeacherIDs += (teacherDic.ContainsKey(teacherid)? teacherDic[teacherid].Short + "," : ""); } tkblesson.TeacherIDs = tkblesson.TeacherIDs.TrimEnd(','); } tkblesson.Save(); //fileStreamlog.WriteLine(String.Format("Create tkblesson: \"{0}\" successful on {1: dd-mm-yyyy HH:MM:ss}", tkblesson.ID + "-" + tkblesson.SubjectID, DateTime.Now)); } else { tkblesson.Semester = uow.FindObject<Semester>(new BinaryOperator("Oid", this.Semester.Oid)); tkblesson.ID = sid; tkblesson.PeriodsPerCard = periodspercard; tkblesson.PeriodsPerWeek = periodsperweek; tkblesson.ClassroomIDs = classroomids; tkblesson.StudentIDs = studentcoustudentidsnt; tkblesson.Week = weeks; tkblesson.Note = this.Note; tkblesson.SubjectID = (subjectDic.ContainsKey(subjectid) ? subjectDic[subjectid].SubjectCode : ""); tkblesson.NumExpectation = 0; tkblesson.GroupIDs = ""; if (groupids != "") { foreach (string groupid in groupids.Split(',')) { string groupname = ""; if (groupDic.ContainsKey(groupid)) { if (groupDic[groupid].EntireClass) { groupname = classDic.ContainsKey(groupDic[groupid].Classid) ? classDic[groupDic[groupid].Classid].Name : groupDic[groupid].Name; } else { groupname = groupDic[groupid].Name; } } else { groupname = groupid; } tkblesson.GroupIDs += (groupname != "" ? groupname + "," : ""); tkblesson.NumExpectation += (groupDic.ContainsKey(groupid) ? groupDic[groupid].StudentCount : 0); } tkblesson.GroupIDs = tkblesson.GroupIDs.TrimEnd(','); } tkblesson.ClassIDs = ""; if (classids != "") { foreach (string classid in classids.Split(',')) { tkblesson.ClassIDs += (classDic.ContainsKey(classid) ? classDic[classid].Short + "," : ""); } tkblesson.ClassIDs = tkblesson.ClassIDs.TrimEnd(','); } tkblesson.TeacherIDs = ""; if (teacherids != "") { foreach (string teacherid in teacherids.Split(',')) { tkblesson.TeacherIDs += (teacherDic.ContainsKey(teacherid) ? teacherDic[teacherid].Short + "," : ""); } tkblesson.TeacherIDs = tkblesson.TeacherIDs.TrimEnd(','); } tkblesson.Save(); //fileStreamlog.WriteLine(String.Format("Update tkblesson: \"{0}\" successful on {1: dd-mm-yyyy HH:MM:ss}", tkblesson.ID, DateTime.Now)); } Lesson lesson = new Lesson(uow) { CanRegister = Active, TkbLesson = tkblesson, Semester = uow.FindObject<Semester>(new BinaryOperator("Oid", this.Semester.Oid)), Subject = (subjectDic.ContainsKey(subjectid) ? subjectDic[subjectid] : null), LessonNote = Note, ClassIDs = tkblesson.GroupIDs, NumExpectation = tkblesson.NumExpectation, LessonCode = ++nextcode //tăng dần }; foreach (string teacherCode in tkblesson.TeacherIDs.Split(',')) { Teacher teacher = uow.FindObject<Teacher>(new BinaryOperator("TeacherCode", teacherCode)); if (teacher !=null) lesson.Teachers.Add(teacher); } lesson.Save(); if (lesson.Subject != null) { //fileStreamlog.WriteLine(String.Format("Create Lesson: \"{0}\" successfully on {1: dd-mm-yyyy HH:MM:ss}", lesson.Subject.SubjectCode + lesson.TkbLesson.Name, DateTime.Now)); fileStreamlog.WriteLine(String.Format("<TR><TD>LESSON</TD><TD>CREATE NEW</TD><TD>\"{0}\"</TD><TD> {1:dd-mm-yy HH:MM:ss}</TD><TD>OK</TD></TR>", lesson.LessonCode.ToString() + "-" + lesson.Subject.SubjectCode +"-"+ lesson.ClassIDs, DateTime.Now)); } else { //fileStreamlog.WriteLine(String.Format("WARNING: Create Lesson: \"{0}\" without Lesson \"{1}\" successfully on {2: dd-mm-yyyy HH:MM:ss}", lesson.TkbLesson.Name, tkblesson.SubjectID, DateTime.Now)); fileStreamlog.WriteLine(String.Format("<TR><TD>LESSON</TD><TD>CREATE NEW</TD><TD>\"{0}\"</TD><TD> {1:dd-mm-yy HH:MM:ss}</TD><TD>WARNING</TD></TR>", lesson.LessonCode.ToString() + "-" + lesson.TkbLesson.Name + "-"+lesson.TkbLesson.SubjectID + "-" + lesson.ClassIDs, DateTime.Now)); } lessonDic.Add(sid, lesson); } //fileStreamlog.WriteLine(String.Format("Create {0} lesson successfully on {1: dd-mm-yyyy HH:MM:ss}", lessonDic.Count, DateTime.Now)); }