public SubjectsData GetSubject(SubjectEdit subject)
        {
            Departament dep = DepartamentController.Instance.GetDepartament(subject.Departament);
            int departamentId = dep == null ? 0 : dep.DepartamentID;

            Faculty fac = FacultyController.Instance.GetFaculty(subject.Faculty);
            int facultyId = fac == null ? 0 : fac.FacultyID;

            Institute inst = InstituteController.Instance.GetInstitute(subject.Institute, departamentId);
            int instituteId = inst == null ? 0 : inst.InstituteID;

            return this.repository.GetSubjectDataForEditing(subject.Name, departamentId, subject.Ects, 
                facultyId, instituteId, subject.IsExam, subject.PlanId, subject.SemesterId, subject.Specializations);
        }
        public EditSubject(Plan plan, SubjectEdit subject)
        {
            InitializeComponent();
            this.plan = plan;
            this.subject = subject;
            lblDepartament.Text = plan.Departament.Name;
            lblFaculty.Text = plan.Faculty.Name;
            FillWithInstitutes();
            FillWithSemesters();
            FillWithSubjectTypes();
            FillWithSpecializations();

            tbSubjectName.Text = subject.Name;
            cbInstitute.SelectedItem.Value = subject.Institute;

            Semester sem = SemesterController.Instance.GetSemester(subject.SemesterId);
            cbSemester.SelectedItem.Value = sem == null ? "B³¹d" : sem.Name;

            seEcts.Value = Convert.ToDecimal(subject.Ects);
            ckbxExam.Checked = subject.IsExam;

            foreach (NewSubjectTypeData nst in subject.SubjectTypes)
            {
                SubjectType st = SubjectTypeController.Instance.GetSubjectType(nst.SubjectTypeId);
                for (int i = 0; i < dgSubjectTypes.Rows.Count; i++)
                {
                    if (dgSubjectTypes.Rows[i].Cells["subjectType"].Value.ToString().Equals(st.Name))
                        dgSubjectTypes.Rows[i].Cells["hours"].Value = nst.Hours.ToString();
                }
            }

            if (subject.Specializations != null)
            {
                foreach (SpecializationDataEdit sde in subject.Specializations)
                {
                    Specialization s = SpecializationController.Instance.GetSpecialization(sde.SpecializationId);
                    dgSpecializations.Rows.Add(s.Name, sde.IsGenereal, sde.IsElective);
                    if(sde.IsGenereal)
                        dgSpecializations.Rows[dgSpecializations.Rows.Count - 1].Cells["elective"].ReadOnly = true;
                }
            }
            else
                dgSpecializations.Enabled = false;

            cbElective.Checked = subject.IsElective;
            cbGeneral.Checked = subject.IsGeneral;

            ShowButtonsToolTips();
        }
 public bool DeleteSubject(SubjectEdit se)
 {
     if (se != null)
     {
         SubjectsData sd = GetSubject(se);
         if (sd != null)
         {
             this.repository.DeleteSubject(sd);
             return true;
         }
     }
     return false;
 }
        public bool EditSubject(SubjectEdit subject)
        {
            if (subject != null)
            {
                SubjectsData st = GetSubject(subject);
                if (st != null && st.SubjectDataID != subject.SubjectId && st.Subject.Name.ToLower().Equals(subject.Name.ToLower())
                    && subject.SemesterId == st.SemesterID)
                    subject.AddError("Przedmiot o podanych danych już\nistnieje w planie");

                if (subject.IsElective && subject.IsGeneral)
                    subject.AddError("Przedmiot nie może być\njednocześnie obowiązkowy i obieralny");

                if (!subject.IsElective && !subject.IsGeneral && (subject.Specializations == null || subject.Specializations.Count() == 0))
                    subject.AddError("Przedmiot musi być obowiązkowy\nlub obieralny dla wszystkich lub\nprzynajmniej na jednej specjalności");

                if (subject.SubjectTypes == null || subject.SubjectTypes.Count() == 0)
                    subject.AddError("Przedmiot musi być przypisany\nprzynajmniej do jednego typu");

                if(st != null && st.Ects != subject.Ects && st.SubjectDataID != subject.SubjectId && 
                    st.Subject.Name.ToLower().Equals(subject.Name.ToLower()))
                    subject.AddError("Istnieje przedmiot nadrzędny\n o innej liczbie ECTS");

                if (subject.IsValid)
                {
                    st = this.repository.GetSubjectData(subject.SubjectId);

                    Subject s = this.repository.GetSubject(subject.Name);
                    if (s == null)
                    {
                        Subject newSubject = new Subject()
                        {
                            Name = subject.Name
                        };
                        this.repository.AddSubjectName(newSubject);
                    }

                    s = this.repository.GetSubject(subject.Name);

                    st.Subject = s;

                    Departament dep = DepartamentController.Instance.GetDepartament(subject.Departament);
                    int departamentId = dep == null ? 0 : dep.DepartamentID;

                    Institute inst = InstituteController.Instance.GetInstitute(subject.Institute, departamentId);
                    
                    if (inst != null)
                        st.InstituteID = inst.InstituteID;
                    else
                        st.InstituteID = null;

                    st.Ects = subject.Ects;
                    st.IsExam = subject.IsExam;
                    st.SemesterID = subject.SemesterId;
                    st.IsElective = subject.IsElective;
                    st.IsGeneral = subject.IsGeneral;

                    st.SubjectTypesDatas.Clear();

                    foreach (NewSubjectTypeData d in subject.SubjectTypes)
                    {
                        SubjectTypesData std = new SubjectTypesData()
                        {
                            Hours = d.Hours,
                            SubjectTypeID = d.SubjectTypeId
                        };

                        st.SubjectTypesDatas.Add(std);
                    }

                    if (subject.Specializations == null || subject.Specializations.Count() <= 0)
                    {
                        st.SpecializationsData = null;
                    }
                    else 
                    {
                        st.SpecializationsData = new SpecializationsData()
                        {
                            SpecializationID = subject.Specializations.ElementAt(0).SpecializationId,
                            IsElective = subject.Specializations.ElementAt(0).IsElective,
                            IsGeneral = subject.Specializations.ElementAt(0).IsGenereal
                        };
                    }

                    this.repository.EditSubject(st);
                    return true;
                }
            }
            return false;
        }
        private SubjectEdit CreateSubjectEditFromGrid()
        {
            if (LoadedPlan != null && btnVerify.Rows.Count > 0)
            {
                int row = btnVerify.SelectedRows.ElementAt(0).Index;

                Semester sem = SemesterController.Instance.GetSemester(btnVerify.Rows[row].Cells["semester"].Value.ToString());
                int semId = sem == null ? 0 : sem.SemesterID;

                SubjectEdit ns = new SubjectEdit()
                {
                    Departament = LoadedPlan.Departament.Name,
                    Faculty = LoadedPlan.Faculty.Name,
                    Institute = btnVerify.Rows[row].Cells["institute"].Value.ToString().Equals("Brak") ? null : btnVerify.Rows[row].Cells["institute"].Value.ToString(),
                    Ects = Convert.ToDouble(btnVerify.Rows[row].Cells["ects"].Value),
                    IsExam = Convert.ToBoolean(btnVerify.Rows[row].Cells["isExam"].Value),
                    Name = btnVerify.Rows[row].Cells["subjectName"].Value.ToString(),
                    PlanId = LoadedPlan.PlanID,
                    SemesterId = semId,
                    IsGeneral = Convert.ToBoolean(btnVerify.Rows[row].Cells["isGeneral"].Value),
                    IsElective = Convert.ToBoolean(btnVerify.Rows[row].Cells["isElective"].Value)
                };

                if (btnVerify.Rows[row].Cells["specialization"].Value != null
                    && !btnVerify.Rows[row].Cells["specialization"].Value.ToString().Equals(string.Empty))
                {
                    string name = GetSpecializationFromGrid(btnVerify.Rows[row].Cells["specialization"].Value.ToString());
                    
                    SpecializationEdit se = SpecializationController.Instance.GetSpecializationEdit(name, LoadedPlan.Departament.Name, LoadedPlan.Faculty.Name);

                    if (se!= null)
                    {
                        bool general = GetBoolFromGrid(btnVerify.Rows[row].Cells["specialization"].Value.ToString(), true, false);
                        bool elective = GetBoolFromGrid(btnVerify.Rows[row].Cells["specialization"].Value.ToString(), false, true);
                        
                        SpecializationDataEdit nsd = SpecializationController.Instance.GetSpecializationDataEdit(name, LoadedPlan.PlanID, general, elective, ns.Name, sem.Semester1);

                        List<SpecializationDataEdit> listnsd = new List<SpecializationDataEdit>();
                        listnsd.Add(nsd);
                        ns.Specializations = listnsd.AsEnumerable();
                    }
                }

                List<NewSubjectTypeData> nstdlist = new List<NewSubjectTypeData>();
                List<SubjectType> list = SubjectTypeController.Instance.ListSubjectTypes();
                for (int i = 0; i < list.Count; i++)
                {
                    if (btnVerify.Rows[row].Cells[list.ElementAt(i).Name].Value != null
                        && !btnVerify.Rows[row].Cells[list.ElementAt(i).Name].Value.ToString().Equals(string.Empty))
                    {
                        NewSubjectTypeData nstd = new NewSubjectTypeData()
                        {
                            Hours = Convert.ToInt32(btnVerify.Rows[row].Cells[list.ElementAt(i).Name].Value),
                            SubjectTypeId = list.ElementAt(i).SubjectTypeID
                        };
                        nstdlist.Add(nstd);
                    }
                }

                ns.SubjectTypes = nstdlist;

                SubjectsData sd = SubjectController.Instance.GetSubject(ns);
                ns.SubjectId = sd.SubjectDataID;
                return ns;
            }

            return null;
        }