protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(IsAuthorized))
            {
                if (Context.Items["Authorized"] == null || !Context.Items["Authorized"].Equals("true"))
                {
                    Server.Transfer("~/Pages/Error/WebViewerAuthorizationErrorPage.aspx");
                }
                else
                {
                    IsAuthorized = "true";
                }
            }

            if (string.IsNullOrEmpty(WebViewerInitString))
            {
                WebViewerInitString = Request.Params[ImageServerConstants.WebViewerQueryStrings.WebViewerInitParams];
            }

            //Extract the WebViewer Init Parameters for retreiving studies
            var initParams = new WebViewerInitParams();

            string[] vals = HttpUtility.UrlDecode(WebViewerInitString).Split(new[] { '?', ';', '=', ',', '&' });
            for (int i = 0; i < vals.Length - 1; i++)
            {
                if (String.IsNullOrEmpty(vals[i]))
                {
                    continue;
                }

                if (vals[i].Equals(ImageServerConstants.WebViewerStartupParameters.Study))
                {
                    i++;
                    initParams.StudyInstanceUids.Add(vals[i]);
                }
                else if (vals[i].Equals(ImageServerConstants.WebViewerStartupParameters.PatientID))
                {
                    i++;
                    initParams.PatientIds.Add(vals[i]);
                }
                else if (vals[i].Equals(ImageServerConstants.WebViewerStartupParameters.AeTitle))
                {
                    i++;
                    initParams.AeTitle = vals[i];
                }
                else if (vals[i].Equals(ImageServerConstants.WebViewerStartupParameters.AccessionNumber))
                {
                    i++;
                    initParams.AccessionNumbers.Add(vals[i]);
                }
            }

            SearchPanel.InitParams = initParams;
            SearchPanel.Username   = Context.Items[ImageServerConstants.WebViewerQueryStrings.Username] as String;
            SearchPanel.SessionId  = Context.Items[ImageServerConstants.WebViewerStartupParameters.Session] as String;
        }
        private static void ValidateParameters(WebViewerInitParams parameters)
        {
            if (parameters == null)
            {
                throw new Exception("Parameters are missing");
            }


            // According to the techinical document, AE Title is optional
            //
            // if (string.IsNullOrEmpty(parameters.AeTitle.Trim()))
            // {
            //    throw new Exception("'aetitle' cannot be empty");
            // }

            if (parameters.AccessionNumbers != null)
            {
                foreach (var value in parameters.AccessionNumbers)
                {
                    if (string.IsNullOrEmpty(value.Trim()))
                    {
                        throw new Exception("'accession' is specified but contain an empty value");
                    }
                }
            }

            if (parameters.PatientIds != null)
            {
                foreach (var value in parameters.PatientIds)
                {
                    if (string.IsNullOrEmpty(value.Trim()))
                    {
                        throw new Exception("'patientid' is specified but contain an empty value");
                    }
                }
            }

            if (parameters.StudyInstanceUids != null)
            {
                foreach (var value in parameters.StudyInstanceUids)
                {
                    if (string.IsNullOrEmpty(value.Trim()))
                    {
                        throw new Exception("'study' is specified but contain an empty value");
                    }
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            UserID              = Request.Params[ImageServerConstants.WebViewerQueryStrings.Username];
            Password            = Request.Params[ImageServerConstants.WebViewerQueryStrings.Password];
            AppName             = Request.Params[ImageServerConstants.WebViewerQueryStrings.ApplicationName];
            ListStudies         = Request.Params[ImageServerConstants.WebViewerQueryStrings.ListStudies];
            WebViewerInitString = Request.Params[ImageServerConstants.WebViewerQueryStrings.WebViewerInitParams];

            //Try to authenticate the user
            if (!string.IsNullOrEmpty(UserID) && !string.IsNullOrEmpty(Password))
            {
                if (String.IsNullOrEmpty(AppName))
                {
                    int start = WebViewerInitString.IndexOf(ImageServerConstants.WebViewerQueryStrings.ApplicationName + "=");

                    if (start < 0)
                    {
                        AppName = ImageServerConstants.DefaultApplicationName;
                    }
                    else
                    {
                        start  += (ImageServerConstants.WebViewerQueryStrings.ApplicationName + "=").Length;
                        AppName = WebViewerInitString.Substring(start);
                        int end = AppName.IndexOf(',');
                        AppName = AppName.Substring(0, end);
                        if (string.IsNullOrEmpty(AppName))
                        {
                            AppName = ImageServerConstants.DefaultApplicationName;
                        }
                    }
                }

                AppName = String.Format("{0}@{1}", AppName, HttpContext.Current.Request.UserHostName);

                try
                {
                    using (LoginService service = new LoginService())
                    {
                        SessionInfo session = service.Login(UserID, Password, AppName);
                        Platform.Log(LogLevel.Info, "[{0}]: {1} has successfully logged in.", AppName, UserID);

                        WebViewerInitString += string.Format(",{0}={1},{2}={3}",
                                                             ImageServerConstants.WebViewerQueryStrings.Username, UserID,
                                                             ImageServerConstants.WebViewerQueryStrings.Session,
                                                             session.Credentials.SessionToken.Id);

                        //_sessionId = session.Credentials.SessionToken.Id;

                        //Add the session information to the context in case we redirect to the studies page.
                        //We need these to properly launch the WebViewer
                        Context.Items.Add(ImageServerConstants.WebViewerQueryStrings.Username, UserID);
                        Context.Items.Add(ImageServerConstants.WebViewerQueryStrings.Session, session.Credentials.SessionToken.Id);
                        Context.Items.Add("Authorized", false);

                        foreach (string role in session.Credentials.Authorities)
                        {
                            if (role.Equals(Macro.ImageServer.Enterprise.Authentication.AuthorityTokens.Study.ViewImages))
                            {
                                Context.Items["Authorized"] = "true";
                                break;
                            }
                        }
                    }
                }
                catch (PasswordExpiredException)
                {
                    Platform.Log(LogLevel.Info, "[{0}]: {1} encountered PasswordExpiredException.", AppName, UserID);
                    Server.Transfer(ImageServerConstants.PageURLs.WebViewerAuthorizationErrorPage, true);
                }
                catch (UserAccessDeniedException)
                {
                    Platform.Log(LogLevel.Info, "[{0}]: {1} encountered UserAccessDeniedException.", AppName, UserID);
                    Server.Transfer(ImageServerConstants.PageURLs.WebViewerAuthorizationErrorPage, true);
                }
                catch (Exception ex)
                {
                    Platform.Log(LogLevel.Info, "[{0}]: {1} encountered exception {2} - {3}.", AppName, UserID, ex.GetType(), ex.Message);
                    Server.Transfer(ImageServerConstants.PageURLs.WebViewerAuthorizationErrorPage, true);
                }

                if (!string.IsNullOrEmpty(ListStudies) && ListStudies.Equals("true"))
                {
                    //Extract the WebViewer Init Parameters to determine whether or not we need
                    //to redirect to the Studies page.
                    var      initParams = new WebViewerInitParams();
                    string[] vals       = HttpUtility.UrlDecode(WebViewerInitString).Split(new[] { '?', ';', '=', ',', '&' });
                    for (int i = 0; i < vals.Length - 1; i++)
                    {
                        if (String.IsNullOrEmpty(vals[i]))
                        {
                            continue;
                        }

                        if (vals[i].Equals(ImageServerConstants.WebViewerStartupParameters.Study))
                        {
                            i++;
                            initParams.StudyInstanceUids.Add(vals[i]);
                        }
                        else if (vals[i].Equals(ImageServerConstants.WebViewerStartupParameters.PatientID))
                        {
                            i++;
                            initParams.PatientIds.Add(vals[i]);
                        }
                        else if (vals[i].Equals(ImageServerConstants.WebViewerStartupParameters.AeTitle))
                        {
                            i++;
                            initParams.AeTitle = vals[i];
                        }
                        else if (vals[i].Equals(ImageServerConstants.WebViewerStartupParameters.AccessionNumber))
                        {
                            i++;
                            initParams.AccessionNumbers.Add(vals[i]);
                        }
                    }

                    //Check if there are multiple studies to be displayed.
                    var controller        = new StudyController();
                    var partitionAdapter  = new ServerPartitionDataAdapter();
                    var partitionCriteria = new ServerPartitionSelectCriteria();
                    StudySelectCriteria studyCriteria;
                    ServerPartition     partition = null;
                    int studyCount = 0;

                    if (!string.IsNullOrEmpty(initParams.AeTitle))
                    {
                        partitionCriteria.AeTitle.EqualTo(initParams.AeTitle);
                        IList <ServerPartition> partitions = partitionAdapter.GetServerPartitions(partitionCriteria);

                        //TODO: What if the AE Title is invalid?

                        if (partitions.Count == 1)
                        {
                            partition = partitions[0];
                        }
                    }

                    //TODO: The logic below is very weird.

                    foreach (string patientId in initParams.PatientIds)
                    {
                        studyCriteria = new StudySelectCriteria();
                        if (partition != null)
                        {
                            studyCriteria.ServerPartitionKey.EqualTo(partition.Key);
                        }
                        SetStringCondition(studyCriteria.PatientId, patientId);
                        studyCount += controller.GetStudyCount(studyCriteria);
                    }

                    if (studyCount < 2)
                    {
                        foreach (string accession in initParams.AccessionNumbers)
                        {
                            studyCriteria = new StudySelectCriteria();
                            if (partition != null)
                            {
                                studyCriteria.ServerPartitionKey.EqualTo(partition.Key);
                            }
                            SetStringCondition(studyCriteria.AccessionNumber, accession);

                            // TODO: studyCount is either 0 or 1  entering this block. If the same study is found, studyCount is incremented to 2, which is wrong
                            studyCount += controller.GetStudyCount(studyCriteria);
                        }
                    }

                    if (studyCount < 2 && initParams.StudyInstanceUids.Count > 0)
                    {
                        studyCriteria = new StudySelectCriteria();
                        if (partition != null)
                        {
                            studyCriteria.ServerPartitionKey.EqualTo(partition.Key);
                        }
                        studyCriteria.StudyInstanceUid.In(initParams.StudyInstanceUids);

                        // TODO: studyCount is either 0 or 1 entering this block. If the same study is found, studyCount is incremented to 2, which is wrong
                        studyCount += controller.GetStudyCount(studyCriteria);
                    }

                    if (studyCount > 1)
                    {
                        Server.Transfer(ImageServerConstants.PageURLs.WebViewerStudiesPage, true);
                    }
                }


                if (string.IsNullOrEmpty(WebViewerInitString))
                {
                    Response.Redirect(ImageServerConstants.PageURLs.WebViewerDefaultPage, true);
                }
                else
                {
                    Response.Redirect(ImageServerConstants.PageURLs.WebViewerDefaultPage + "?" + ImageServerConstants.WebViewerQueryStrings.WebViewerInitParams + "=" + WebViewerInitString, true);
                }
            }
            else
            {
                Server.Transfer(ImageServerConstants.PageURLs.WebViewerAuthorizationErrorPage, true);
            }
        }
        private static IList <StudySummary> LoadStudies(WebViewerInitParams initParams)
        {
            ValidateParameters(initParams);

            var controller       = new StudyController();
            var partitionAdapter = new ServerPartitionDataAdapter();
            StudySelectCriteria studyCriteria;
            var                 partitionCriteria = new ServerPartitionSelectCriteria();
            ServerPartition     partition         = null;
            IList <Study>       studies;
            List <StudySummary> totalStudies = new List <StudySummary>();

            if (!string.IsNullOrEmpty(initParams.AeTitle))
            {
                partitionCriteria.AeTitle.EqualTo(initParams.AeTitle);
                IList <ServerPartition> partitions = partitionAdapter.GetServerPartitions(partitionCriteria);
                if (partitions.Count == 1)
                {
                    partition = partitions[0];
                }
            }

            foreach (string patientId in initParams.PatientIds)
            {
                studyCriteria = new StudySelectCriteria();
                if (partition != null)
                {
                    studyCriteria.ServerPartitionKey.EqualTo(partition.Key);
                }
                SetStringCondition(studyCriteria.PatientId, patientId);
                studyCriteria.StudyDate.SortDesc(0);
                studies = controller.GetStudies(studyCriteria);

                foreach (Study study in studies)
                {
                    totalStudies.Add(StudySummaryAssembler.CreateStudySummary(HttpContextData.Current.ReadContext, study));
                }
            }

            foreach (string accession in initParams.AccessionNumbers)
            {
                studyCriteria = new StudySelectCriteria();
                if (partition != null)
                {
                    studyCriteria.ServerPartitionKey.EqualTo(partition.Key);
                }
                SetStringCondition(studyCriteria.AccessionNumber, accession);
                studyCriteria.StudyDate.SortDesc(0);
                studies = controller.GetStudies(studyCriteria);

                foreach (Study study in studies)
                {
                    totalStudies.Add(StudySummaryAssembler.CreateStudySummary(HttpContextData.Current.ReadContext, study));
                }
            }

            if (initParams.StudyInstanceUids.Count > 0)
            {
                studyCriteria = new StudySelectCriteria();
                if (partition != null)
                {
                    studyCriteria.ServerPartitionKey.EqualTo(partition.Key);
                }
                studyCriteria.StudyInstanceUid.In(initParams.StudyInstanceUids);
                studyCriteria.StudyDate.SortDesc(0);
                studies = controller.GetStudies(studyCriteria);

                foreach (Study study in studies)
                {
                    totalStudies.Add(StudySummaryAssembler.CreateStudySummary(HttpContextData.Current.ReadContext, study));
                }
            }

            totalStudies.Sort((a, b) => a.StudyDate.CompareTo(b.StudyDate) * -1);

            return(totalStudies);
        }