private bool UpdatePublication(ProjectTrackerContainer context, Publication pub, bool isSub)
        {
            if (ValidateJournalControl())
            {
                var journalExists = context.Journals
                                    .Any(j => j.JournalName == TextBoxJournal.Text);

                if (journalExists)
                {
                    var journal = context.Journals
                                  .First(j => j.JournalName == TextBoxJournal.Text);

                    pub.JounralId = journal.Id;
                }
                else
                {
                    //new journal
                    Journal newJournal = GetJournal();

                    context.Journals.Add(newJournal);

                    context.SaveChanges();

                    pub.JounralId = newJournal.Id;
                }
            }

            if (isSub)
            {
                //sub-publication can change date
                DateTime pubDate = Convert.ToDateTime(TextBoxSubmitDate.Text);
                switch (pub.Status)
                {
                case (int)StatusEnum.Accepted:
                    pub.AcceptDate = pubDate;
                    break;

                case (int)StatusEnum.Published:
                    pub.PubDate = pubDate;
                    break;

                default:
                    pub.SubmitDate = pubDate;
                    break;
                }
            }
            else
            {
                //update PublicationBioStat
                #region Update ProjectBioStat
                ICollection <PublicationBioStat> prevBiostats = pub.PublicationBioStats
                                                                .Where(b => b.EndDate > DateTime.Parse(_currentDate)).ToList();

                List <int> newMemberList = GetSelectedRow_GridView(GridViewBioStat);

                List <int> prevMemberList = new List <int>();

                if (prevBiostats.Count > 0)
                {
                    foreach (PublicationBioStat biostat in prevBiostats)
                    {
                        prevMemberList.Add(biostat.BioStats_Id);
                    }
                }

                var newNotPrevList = newMemberList.Except(prevMemberList).ToList();
                var prevNotNewList = prevMemberList.Except(newMemberList).ToList();

                foreach (var expiredId in prevNotNewList)
                {
                    pub.PublicationBioStats.First(b => b.BioStats_Id == expiredId).EndDate = DateTime.Parse(_currentDate);
                }
                foreach (var newMemberId in newNotPrevList)
                {
                    pub.PublicationBioStats.Add(new PublicationBioStat()
                    {
                        Publications_Id = pub.Id,
                        BioStats_Id     = newMemberId,
                        StartDate       = DateTime.Parse(_currentDate),
                        EndDate         = _endDate
                    });
                }
                #endregion

                // update ProjectGrant
                #region Update Project Grants
                ICollection <PublicationGrant> prevGrants = pub.PublicationGrants
                                                            .Where(b => b.EndDate > DateTime.Parse(_currentDate)).ToList();

                List <int> newGrantList = GetSelectedRow_GridView(GridViewGrant);

                List <int> prevGrantList = new List <int>();
                if (prevGrants.Count > 0)
                {
                    foreach (PublicationGrant grant in prevGrants)
                    {
                        prevGrantList.Add(grant.GrantAffilId);
                    }
                }

                var newNotPrevGrantList = newGrantList.Except(prevGrantList).ToList();
                var prevNotNewGrantList = prevGrantList.Except(newGrantList).ToList();

                foreach (var removeId in prevNotNewGrantList)
                {
                    PublicationGrant pg = pub.PublicationGrants.First(d => d.PublicationId == pub.Id && d.GrantAffilId == removeId);
                    context.PublicationGrants.Remove(pg);
                }
                foreach (var newGrantId in newNotPrevGrantList)
                {
                    pub.PublicationGrants.Add(new PublicationGrant()
                    {
                        PublicationId = pub.Id,
                        GrantAffilId  = newGrantId,
                        StartDate     = DateTime.Parse(_currentDate),
                        EndDate       = _endDate
                    });
                }
                #endregion

                //update conference
                if (IsAbstract)
                {
                    // save conference
                    if (ddlConference.SelectedValue.Equals(string.Empty))
                    {
                        Conference newConf = GetConference();

                        context.Conferences.Add(newConf);
                        context.SaveChanges();
                        pub.ConferenceId = newConf.Id;
                    }
                    else
                    {
                        pub.ConferenceId = Convert.ToInt32(ddlConference.SelectedValue);
                    }
                }
            }

            context.SaveChanges();

            return(true);
        }
        private bool CreateNewPublication(ProjectTrackerContainer context, Publication pub)
        {
            Publication newPub = new Publication();

            //if (IsAbstract)
            //{
            //    newPub = new PubAbstract();
            //}
            //else
            //{
            //    newPub = new PubManuscript();
            //}

            newPub.Status    = PubStatus;
            newPub.StartDate = Convert.ToDateTime(HiddenFieldCurrentDate.Value);
            newPub.EndDate   = Convert.ToDateTime("2099-01-01");


            DateTime pubDate = Convert.ToDateTime(TextBoxSubmitDate.Text);

            newPub.SubmitDate = pubDate;

            //switch (newPub.Status)
            //{
            //    case (int)StatusEnum.Accepted:
            //        newPub.AcceptDate = pubDate;
            //        break;
            //    case (int)StatusEnum.Published:
            //        newPub.PubDate = pubDate;
            //        break;
            //    default:
            //        newPub.SubmitDate = pubDate;
            //        break;
            //}

            if (pub != null && pub.Id > 0)
            {
                newPub.ParentId = pub.Id;
            }

            //if (PubStatus!=(int)StatusEnum.SubResub) // sub publication
            //{
            //    newPub.ProjectId = pub.ProjectId;
            //    newPub.Title = pub.Title;
            //    newPub.Author = pub.Author;
            //    newPub.SubmitDate = pub.SubmitDate;

            //    if (pub.ConferenceId != null)
            //    {
            //        newPub.ConferenceId = pub.ConferenceId;
            //    }

            //    if (pub.JounralId != null)
            //    {
            //        newPub.JounralId = pub.JounralId;
            //    }

            //    //check if newpub already exists based on type, projectid, status and submitdate
            //    bool existingPub = context.Publications
            //                         .Any(p => p.ProjectId == ProjectId && p.Status == PubStatus && p.SubmitDate == newPub.SubmitDate);
            //    if (existingPub)
            //    {
            //        return false;
            //    }

            //    context.Publications.Add(newPub);
            //    //context.SaveChanges();

            //    //ddlChildPublication.Enabled = true;
            //    //ddlChildPublication.Items.Insert(ddlChildPublication.Items.Count, new ListItem(newPub.Id.ToString() + " " + newPub.Title, newPub.Id.ToString()));
            //}
            //else // new publication
            //{

            //check if pub already exists
            bool existingPub = context.Publications
                               .Any(p => p.ProjectId == ProjectId && p.Status == PubStatus && p.SubmitDate == newPub.SubmitDate);

            if (existingPub)
            {
                return(false);
            }

            newPub.ProjectId = ProjectId;
            newPub.Title     = TextBoxTitle.Text;
            newPub.Author    = TextAreaAuthors.InnerText;

            if (IsAbstract)
            {
                // save conference
                if (ddlConference.SelectedValue.Equals(string.Empty))
                {
                    Conference newConf = GetConference();

                    context.Conferences.Add(newConf);
                    context.SaveChanges();
                    newPub.ConferenceId = newConf.Id;
                }
                else
                {
                    newPub.ConferenceId = Convert.ToInt32(ddlConference.SelectedValue);
                }
            }

            //journal
            if (ValidateJournalControl())
            {
                bool journal = context.Journals
                               .Any(j => j.JournalName == TextBoxJournal.Text);

                if (journal)
                {
                    Journal x = context.Journals
                                .First(j => j.JournalName == TextBoxJournal.Text);

                    newPub.JounralId = x.Id;
                }
                else
                {
                    //new journal
                    Journal newJ = GetJournal();

                    context.Journals.Add(newJ);

                    context.SaveChanges();

                    newPub.JounralId = newJ.Id;
                }
            }
            else
            {
                newPub.JounralId = -1;
            }

            context.Publications.Add(newPub);
            context.SaveChanges();

            ddlPublication.Items.Insert(ddlPublication.Items.Count, new ListItem(newPub.Id.ToString() + " " + newPub.Title, newPub.Id.ToString()));

            //PublicationBioStat
            ICollection <PublicationBioStat> pubBiostatList = new Collection <PublicationBioStat>();

            foreach (GridViewRow row in GridViewBioStat.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
                    if (chkRow.Checked)
                    {
                        Label lblId     = row.FindControl("lblId") as Label;
                        int   biostatId = Convert.ToInt32(lblId.Text);

                        PublicationBioStat pubBiostat = new PublicationBioStat()
                        {
                            BioStats_Id     = biostatId,
                            Publications_Id = newPub.Id,
                            StartDate       = Convert.ToDateTime(HiddenFieldCurrentDate.Value),
                            EndDate         = Convert.ToDateTime("2099-01-01")
                        };

                        pubBiostatList.Add(pubBiostat);
                    }
                }
            }

            newPub.PublicationBioStats = pubBiostatList;

            //PublicationGrant
            ICollection <PublicationGrant> pubGrantList = new Collection <PublicationGrant>();

            foreach (GridViewRow row in GridViewGrant.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
                    if (chkRow.Checked)
                    {
                        Label lblId   = row.FindControl("lblId") as Label;
                        int   grantId = Convert.ToInt32(lblId.Text);

                        PublicationGrant pubGrant = new PublicationGrant()
                        {
                            GrantAffilId  = grantId,
                            PublicationId = newPub.Id,
                            StartDate     = Convert.ToDateTime(HiddenFieldCurrentDate.Value),
                            EndDate       = Convert.ToDateTime("2099-01-01")
                        };

                        pubGrantList.Add(pubGrant);
                    }
                }
            }

            newPub.PublicationGrants = pubGrantList;

            //}

            context.SaveChanges();

            return(true);
        }