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;
            }
        }
        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;
        }
        private void btnSaveSubject_Click(object sender, EventArgs e)
        {
            subject.ClearErrors();
            lblValidation.Text = string.Empty;

            this.subject.Ects = Convert.ToDouble(seEcts.Value);
            this.subject.Institute = cbInstitute.SelectedItem.ToString();
            this.subject.IsExam = ckbxExam.Checked;
            this.subject.Name = tbSubjectName.Text;
            this.subject.IsElective = cbElective.Checked;
            this.subject.IsGeneral = cbGeneral.Checked;

            Semester sem = SemesterController.Instance.GetSemester(cbSemester.SelectedItem.ToString());
            if (sem != null)
                this.subject.SemesterId = sem.SemesterID;

            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);
                    }
                }
            }

            this.subject.SubjectTypes = nstdlist;

            if (subject.Specializations != null && subject.Specializations.Count() > 0)
            {
                if (dgSpecializations.Rows.Count > 0 && dgSpecializations.Enabled)
                {
                    Specialization spec = SpecializationController.Instance.GetSpecialization(dgSpecializations.Rows[0].Cells["specialization"].Value.ToString());

                    if (spec != null)
                    {
                        subject.Specializations.ElementAt(0).SpecializationId = spec.SpecializationID;
                        subject.Specializations.ElementAt(0).IsElective = Convert.ToBoolean(dgSpecializations.Rows[0].Cells["elective"].Value);
                        subject.Specializations.ElementAt(0).IsGenereal = Convert.ToBoolean(dgSpecializations.Rows[0].Cells["general"].Value);
                    }
                }
                else
                    subject.Specializations = null;
            }

            if (SubjectController.Instance.EditSubject(subject))
            {
                changes = true;
                RadMessageBox.Show("Zmiany zosta³y zapisane", "Wiadomoœæ");
                this.Close();
            }
            else
            {
                string errors = string.Empty;
                foreach(string error in subject.Errors)
                    errors = errors + error + "\n";

                lblValidation.Text = errors;
            }
        }
        public void CopyArchivePlan(int sourcePlanId, int targetPlanId)
        {
            Plan source = this.repository.GetPlan(sourcePlanId);
            Plan target = this.repository.GetPlan(targetPlanId);
            
            if (source != null && target != null)
            {
                foreach (SubjectsData sd in source.SubjectsDatas)
                {
                    if (target.SemesterStart <= sd.Semester.Semester1 && target.SemesterEnd >= sd.Semester.Semester1)
                    {
                        NewSubject ns = new NewSubject()
                                            {
                                                PlanId = targetPlanId,
                                                DepartamentId = sd.DepartamentID,
                                                Ects = sd.Ects,
                                                FacultyId = sd.FacultyID,
                                                IsElective = sd.IsElective,
                                                IsExam = sd.IsExam,
                                                IsGeneral = sd.IsGeneral,
                                                Name = sd.Subject.Name,
                                                SemesterId = sd.SemesterID,
                                            };

                        if (sd.SpecializationsData != null)
                        {
                            List<NewSpecializationData> specList = new List<NewSpecializationData>();
                            NewSpecializationData nsd = new NewSpecializationData()
                                                            {
                                                                IsElective = sd.SpecializationsData.IsElective,
                                                                IsGenereal = sd.SpecializationsData.IsGeneral,
                                                                SpecializationId =
                                                                    sd.SpecializationsData.SpecializationID
                                                            };

                            specList.Add(nsd);
                            ns.Specializations = specList.AsEnumerable();
                        }

                        List<NewSubjectTypeData> nstdList = new List<NewSubjectTypeData>();

                        foreach (SubjectTypesData std in sd.SubjectTypesDatas)
                        {
                            NewSubjectTypeData nstd = new NewSubjectTypeData()
                                                          {
                                                              Hours = (float) std.Hours,
                                                              SubjectTypeId = std.SubjectTypeID
                                                          };

                            nstdList.Add(nstd);
                        }

                        ns.SubjectTypes = nstdList.AsEnumerable();

                        if (sd.InstituteID > 0)
                            ns.InstituteId = (int) sd.InstituteID;
                        else
                            sd.InstituteID = null;

                        SubjectController.Instance.AddSubject(ns);
                    }
                }
            }
        }