//
        // GET: /mylibrary/
        public ActionResult Index(int specid = 0, int oid = 0, int fid = 0, string selaction = null, string sort = "date", string searchfolder = "", string page = "", int start = 1)
        {
            int            qsSpecId           = specid;
            MyLibraryModel MainMyLibraryModel = new MyLibraryModel();
            var            UserSpecialityList = MyLibraryBL.GetUserSpecialities(CurrentUser.UserId);

            if (qsSpecId == 0)
            {
                qsSpecId = UserSpecialityList.Select(n => n.SpecialityId).FirstOrDefault();
            }
            var UserFolderList = MyLibraryBL.GetUserFolderList(qsSpecId, CurrentUser.UserId);

            MainMyLibraryModel.AllUserFolders      = UserFolderList;
            MainMyLibraryModel.AllUserSpecialities = UserSpecialityList;
            ViewBag.SpecId = qsSpecId;
            ViewBag.PrimarySpecialityId = UserSpecialityList.Select(n => n.SpecialityId).FirstOrDefault();

            // For Citation

            if (selaction == "saved")
            {
                MainMyLibraryModel.Citations = MyLibraryBL.GetCitations(fid, CurrentUser.UserId);
                if (MainMyLibraryModel.Citations.Count > 0)
                {
                    MainMyLibraryModel.CitationDetails = MyLibraryBL.GetCitationDetails(MainMyLibraryModel.Citations, sort, CurrentUser.UserId, 2);
                }
                else
                {
                    MainMyLibraryModel.CitationDetails = null;
                }
            }
            else if (selaction == "editor")
            {
                MainMyLibraryModel.PMIDs     = MyLibraryBL.GetEditorsChoicePMids(fid);
                MainMyLibraryModel.Citations = MyLibraryBL.GetEditorsChoiceCitations(fid, CurrentUser.UserId);
                if (MainMyLibraryModel.Citations.Count > 0)
                {
                    MainMyLibraryModel.CitationDetails = MyLibraryBL.GetCitationDetails(MainMyLibraryModel.Citations, sort, CurrentUser.UserId, 2);
                }
                else
                {
                    MainMyLibraryModel.CitationDetails = null;
                }
            }

            else if (selaction == "seminal")
            {
                MainMyLibraryModel.PMIDs     = MyLibraryBL.GetSeminalPMids(fid);
                MainMyLibraryModel.Citations = MyLibraryBL.GetSeminalCitations(fid, CurrentUser.UserId);
                if (MainMyLibraryModel.Citations.Count > 0)
                {
                    MainMyLibraryModel.CitationDetails = MyLibraryBL.GetCitationDetails(MainMyLibraryModel.Citations, sort, CurrentUser.UserId, 2);
                }
                else
                {
                    MainMyLibraryModel.CitationDetails = null;
                }
            }

            else if (selaction == "sponsor")
            {
                MainMyLibraryModel.PMIDs     = MyLibraryBL.GetSponsorPMids(fid);
                MainMyLibraryModel.Citations = MyLibraryBL.GetSponsorCitations(fid, CurrentUser.UserId);
                if (MainMyLibraryModel.Citations.Count > 0)
                {
                    MainMyLibraryModel.CitationDetails = MyLibraryBL.GetCitationDetails(MainMyLibraryModel.Citations, sort, CurrentUser.UserId, 2);
                }
                else
                {
                    MainMyLibraryModel.CitationDetails = null;
                }
            }

            else if (selaction == "acr")
            {
                MainMyLibraryModel.AcrDocumentsMyLibraryList = MyLibraryBL.getAcrDocumentsList(fid);
            }

            if (!string.IsNullOrEmpty(searchfolder))
            {
                MainMyLibraryModel.CitationDetails = MainMyLibraryModel.CitationDetails.Where(c => c.pmid.ToString() == searchfolder || (c.AuthorList ?? "").Contains(searchfolder) || (c.ArticleTitle ?? "").Contains(searchfolder) || (c.MedlinePgn ?? "").Contains(searchfolder) || (c.MedlineTA ?? "").Contains(searchfolder)).ToList();
            }

            if (MainMyLibraryModel.CitationDetails != null)
            {
                MainMyLibraryModel.CitationDetailsTotal = MainMyLibraryModel.CitationDetails;
                MainMyLibraryModel.CitationDetails      = MainMyLibraryModel.CitationDetailsTotal.Skip(start - 1).Take(25).ToList();

                using (Cogent3Entities entity = new Cogent3Entities())
                {
                    foreach (var item in MainMyLibraryModel.CitationDetails)
                    {
                        if ((!item.unicodeFixed.HasValue) || (item.unicodeFixed == false))
                        {
                            if ((item.ArticleTitle ?? "").Contains("?") || (item.ArticleTitle ?? "").Contains("="))
                            {
                                List <string> ArticleTitleWithNoIssue = MyLibraryBL.GetAbstractWithNoIssue(item.pmid);

                                if (ArticleTitleWithNoIssue.Count == 2)
                                {
                                    item.ArticleTitle = ArticleTitleWithNoIssue[1];

                                    int PMID       = Convert.ToInt32(item.pmid);
                                    var IwideTable = (from iw in entity.iWides where iw.PMID == item.pmid select iw).FirstOrDefault();

                                    if (IwideTable != null)
                                    {                                    //AbstractArticleTitleNew[0] is for Abstract Text and AbstractArticleTitleNew[1] is for Article Title;;
                                        item.ArticleTitle              = ArticleTitleWithNoIssue[1];
                                        IwideTable.ArticleTitle        = ArticleTitleWithNoIssue[1];
                                        entity.Entry(IwideTable).State = EntityState.Modified;
                                    }

                                    var IwideNewTable = (from iw in entity.iWideNews where iw.PMID == item.pmid select iw).FirstOrDefault();

                                    if (IwideNewTable != null)
                                    {
                                        item.ArticleTitle                 = ArticleTitleWithNoIssue[1];
                                        IwideNewTable.ArticleTitle        = ArticleTitleWithNoIssue[1];
                                        entity.Entry(IwideNewTable).State = EntityState.Modified;
                                    }
                                }
                            }
                        }
                    }

                    entity.SaveChanges();
                }
            }

            return(View(MainMyLibraryModel));
        }
        // To Get Citation Details
        public static List <CitationDetailsModel> GetCitationDetails(List <CitationsModel> CitationsPmids, string sort, int UserId, int displayMode, bool isAbstract = false)
        {
            using (Cogent3Entities entity = new Cogent3Entities())
            {
                StringBuilder str = new StringBuilder();
                foreach (var item in CitationsPmids)
                {
                    str.Append(item.pmid + ",");
                }

                // For sort order
                short sortorder = 1;
                switch (sort)
                {
                case "date":
                    sortorder = 1;
                    break;

                case "authors":
                    sortorder = 2;
                    break;

                case "title":
                    sortorder = 3;
                    break;

                case "journal":
                    sortorder = 4;
                    break;

                default:
                    sortorder = 1;
                    break;
                }

                string PMIDList = str.ToString();
                if (!string.IsNullOrEmpty(PMIDList))
                {
                    PMIDList = PMIDList.Remove(PMIDList.LastIndexOf(','), 1);
                }

                string       query              = "[ap_DisplayPMID_AJA_Dev] @UserID,@PMIDList,@DisplayMode,@SearchSort";
                SqlParameter UserID             = new SqlParameter("@UserID", UserId);
                SqlParameter Pmids              = new SqlParameter("@PMIDList", PMIDList);
                SqlParameter DisplayMode        = new SqlParameter("@DisplayMode", displayMode);
                SqlParameter SearchSort         = new SqlParameter("@SearchSort", sortorder);
                var          AllCitationDetails = entity.Database.SqlQuery <CitationDetailsModel>(query, UserID, Pmids, DisplayMode, SearchSort).ToList();


                if (isAbstract)
                {
                    try
                    {
                        string       query1       = "[ap_DisplayPMID_AJA_Dev_Detailed] @UserID,@PMIDList,@DisplayMode,@SearchSort";
                        SqlParameter UserID1      = new SqlParameter("@UserID", UserId);
                        SqlParameter Pmids1       = new SqlParameter("@PMIDList", PMIDList);
                        SqlParameter DisplayMode1 = new SqlParameter("@DisplayMode", displayMode);
                        SqlParameter SearchSort1  = new SqlParameter("@SearchSort", sortorder);
                        var          AllCitationDetailsDetailed = entity.Database.SqlQuery <CitationDetailsModelDetail>(query1, UserID1, Pmids1, DisplayMode1, SearchSort1).ToList();
                        AllCitationDetails[0].AuthorList = "";
                        foreach (var item in AllCitationDetailsDetailed)
                        {
                            AllCitationDetails[0].AuthorList += item.DisplayName + ", ";
                        }
                        AllCitationDetails[0].AuthorList = AllCitationDetails[0].AuthorList.Substring(0, AllCitationDetails[0].AuthorList.LastIndexOf(','));

                        if ((!AllCitationDetails[0].unicodeFixed.HasValue) || AllCitationDetails[0].unicodeFixed == false)
                        {
                            List <string> AbstractArticleTitleNew = GetAbstractWithNoIssue(AllCitationDetails[0].pmid);
                            if (AbstractArticleTitleNew.Count == 2)
                            {
                                int PMID       = Convert.ToInt32(AllCitationDetails[0].pmid);
                                var IwideTable = (from iw in entity.iWides where iw.PMID == PMID select iw).FirstOrDefault();

                                if (IwideTable != null)
                                {
                                    AllCitationDetails[0].AbstractText = AbstractArticleTitleNew[0];  //AbstractArticleTitleNew[0] is for Abstract Text and AbstractArticleTitleNew[1] is for Article Title;;
                                    IwideTable.AbstractText            = AbstractArticleTitleNew[0];
                                    AllCitationDetails[0].ArticleTitle = AbstractArticleTitleNew[1];
                                    IwideTable.ArticleTitle            = AbstractArticleTitleNew[1];
                                    IwideTable.unicodeFixed            = true;
                                    entity.Entry(IwideTable).State     = EntityState.Modified;
                                }

                                var IwideNewTable = (from iw in entity.iWideNews where iw.PMID == PMID select iw).FirstOrDefault();

                                if (IwideNewTable != null)
                                {
                                    AllCitationDetails[0].AbstractText = AbstractArticleTitleNew[0];
                                    IwideNewTable.AbstractText         = AbstractArticleTitleNew[0];

                                    AllCitationDetails[0].ArticleTitle = AbstractArticleTitleNew[1];
                                    IwideNewTable.ArticleTitle         = AbstractArticleTitleNew[1];
                                    IwideNewTable.unicodeFixed         = true;

                                    entity.Entry(IwideNewTable).State = EntityState.Modified;
                                }
                            }
                        }
                        entity.SaveChanges();
                    }
                    catch (Exception)
                    {
                    }
                }

                List <CitationDetailsModel> NonMedLineCitationsList = AllCitationDetails.Where(u => string.IsNullOrEmpty(u.ArticleTitle)).ToList();
                foreach (var item in NonMedLineCitationsList)
                {
                    using (EditorsEntities entityNM = new EditorsEntities())
                    {
                        CitationDetailsModel NMCitation = NonMedLineCitationsList.Find(u => u.pmid == item.pmid);
                        if (NMCitation != null)
                        {
                            NMCitation = (from nm in entityNM.NonMedlineCitations
                                          where nm.PMID == NMCitation.pmid
                                          select new CitationDetailsModel
                            {
                                AbstractText = nm.Abstract,
                                ArticleTitle = nm.ArticleTitle,
                                AuthorList = nm.AuthorList,
                                DisplayDate = nm.DisplayDate,
                                DisplayNotes = nm.DisplayNotes,
                                MedlinePgn = nm.MedlinePgn,
                                MedlineTA = nm.MedlineTA,
                                StatusDisplay = nm.StatusDisplay,
                                pmid = nm.PMID
                            }).FirstOrDefault();

                            if (NMCitation != null)
                            {
                                int Index = AllCitationDetails.FindIndex(u => u.pmid == item.pmid);
                                AllCitationDetails.RemoveAt(Index);
                                AllCitationDetails.Insert(Index, NMCitation);
                            }
                        }
                    }
                }


                return(AllCitationDetails);
            }
        }