public bool AddSubject(NewSubject ns) { SubjectsData sd = this.repository.GetSubjectDataForAdding(ns.Name, ns.DepartamentId, ns.Ects, ns.FacultyId, ns.InstituteId, ns.IsExam, ns.PlanId, ns.SemesterId, ns.Specializations); if (sd != null) ns.AddError("Przedmiot o podanych danych już\nistnieje w planie"); if (ns.IsElective && ns.IsGeneral) ns.AddError("Przedmiot nie może być\njednocześnie obowiązkowy i obieralny"); if (!ns.IsElective && !ns.IsGeneral && (ns.Specializations == null || ns.Specializations.Count() == 0)) ns.AddError("Przedmiot musi być obowiązkowy\nlub obieralny dla wszystkich lub\nprzynajmniej na jednej specjalności"); if (ns.SubjectTypes == null || ns.SubjectTypes.Count() == 0) ns.AddError("Przedmiot musi być przypisany\nprzynajmniej do jednego typu"); if(ns.Specializations != null) foreach(NewSpecializationData nsd in ns.Specializations) if (!nsd.IsElective && !nsd.IsGenereal) { ns.AddError("Przedmiot na specjalizacji musi być\nobowiązkowy lub obieralny"); break; } if (ns != null && ns.IsValid) { this.repository.AddSubject(ns); return true; } return false; }
private void btnAddSubject_Click(object sender, EventArgs e) { lblValidation.Text = string.Empty; Institute institute = null; if (!cbInstitute.SelectedItem.ToString().Equals("Brak")) institute = InstituteController.Instance.GetInstitute(cbInstitute.SelectedItem.ToString(), plan.DepartamentID); if (institute == null && !cbInstitute.SelectedItem.ToString().Equals("Brak")) { MessageBox.Show("Wybrany instytut ju¿ nie istnieje"); FillWithInstitutes(); return; } Semester semester = SemesterController.Instance.GetSemester(cbSemester.SelectedItem.ToString()); if(semester == null) { MessageBox.Show("Wybrany semestr ju¿ nie istnieje"); FillWithSemesters(); return; } List<NewSubjectTypeData> nstdlist = new List<NewSubjectTypeData>(); for (int i = 0; i < dgSubjectTypes.Rows.Count; i++) { if(Convert.ToInt32(dgSubjectTypes.Rows[i].Cells["hours"].Value) > 0) { SubjectType st = SubjectTypeController.Instance.GetSubjectType(dgSubjectTypes.Rows[i].Cells["subjectType"].Value.ToString()); if (st != null) { NewSubjectTypeData nstd = new NewSubjectTypeData() { Hours = Convert.ToInt32(dgSubjectTypes.Rows[i].Cells["hours"].Value), SubjectTypeId = st.SubjectTypeID }; nstdlist.Add(nstd); } } } List<NewSpecializationData> nspdlist = new List<NewSpecializationData>(); if (dgSpecializations.Enabled == true) for (int i = 0; i < dgSpecializations.Rows.Count; i++) { if (dgSpecializations.Rows[i].Cells["specialization"].Value != null) { SpecializationEdit s = SpecializationController.Instance.GetSpecializationEdit(dgSpecializations.Rows[i].Cells["specialization"].Value.ToString()); if (s != null) { NewSpecializationData nspd = new NewSpecializationData() { IsElective = Convert.ToBoolean(dgSpecializations.Rows[i].Cells["elective"].Value), IsGenereal = Convert.ToBoolean(dgSpecializations.Rows[i].Cells["general"].Value), SpecializationId = s.SpecializationID }; nspdlist.Add(nspd); } } } NewSubject subject = new NewSubject() { DepartamentId = plan.DepartamentID, Ects = Convert.ToDouble(seEcts.Value), FacultyId = plan.FacultyID, InstituteId = institute == null ? 0 : institute.InstituteID, IsExam = ckbxExam.Checked, Name = tbSubjectName.Text, SemesterId = semester.SemesterID, SubjectTypes = nstdlist, PlanId = plan.PlanID, Specializations = nspdlist.Count > 0 ? nspdlist : null, IsElective = cbElective.Checked, IsGeneral = cbGeneral.Checked }; if ((plan.SemesterStart.HasValue && plan.SemesterStart.Value > semester.Semester1) || (plan.SemesterEnd.HasValue && plan.SemesterEnd.Value < semester.Semester1)) subject.AddError("W planie nie mo¿e\nbyæ przedmiotu na takim semestrze"); if (SubjectController.Instance.AddSubject(subject)) RadMessageBox.Show("Przedmiot zosta³ dodany", "Wiadomoœæ"); else { string errors = string.Empty; foreach (string error in subject.Errors) errors = errors + error + "\n"; lblValidation.Text = errors; } }