context: #region License
  
  // Copyright (c) 2013, ClearCanvas Inc.
  // All rights reserved.
  // http://www.clearcanvas.ca
  //
  // This file is part of the ClearCanvas RIS/PACS open source project.
  //
  // The ClearCanvas RIS/PACS open source project is free software: you can
  // redistribute it and/or modify it under the terms of the GNU General Public
  // License as published by the Free Software Foundation, either version 3 of the
  // License, or (at your option) any later version.
  //
  // The ClearCanvas RIS/PACS open source project is distributed in the hope that it
  // will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
  // Public License for more details.
  //
  // You should have received a copy of the GNU General Public License along with
  // the ClearCanvas RIS/PACS open source project.  If not, see
  // <http://www.gnu.org/licenses/>.
  
  #endregion
  
  using System;
  using System.Collections.Generic;
  using System.Configuration;
  using System.Text;
  using System.Threading;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using AjaxControlToolkit;
  using ClearCanvas.Common;
  using ClearCanvas.Common.Utilities;
  using ClearCanvas.Dicom.Audit;
  using ClearCanvas.ImageServer.Common;
  using ClearCanvas.ImageServer.Model;
  using ClearCanvas.ImageServer.Web.Application.Controls;
  using ClearCanvas.ImageServer.Web.Application.Helpers;
  using ClearCanvas.ImageServer.Web.Common;
  using ClearCanvas.ImageServer.Web.Common.Data;
  using ClearCanvas.ImageServer.Web.Common.Data.DataSource;
  using ClearCanvas.ImageServer.Web.Common.Security;
  using ClearCanvas.ImageServer.Web.Common.WebControls.UI;
  using AuthorityTokens=ClearCanvas.ImageServer.Enterprise.Authentication.AuthorityTokens;
  using Resources;
  using SR=Resources.SR;
  
  [assembly: WebResource("ClearCanvas.ImageServer.Web.Application.Pages.Studies.SearchPanel.js", "application/x-javascript")]
  
  namespace ClearCanvas.ImageServer.Web.Application.Pages.Studies
  {
      public class SearchPanelButtonClickedEventArgs:EventArgs
      {
          public IEnumerable<StudySummary> SelectedStudies { get; set; }
      }
  
      [ClientScriptResource(ComponentType="ClearCanvas.ImageServer.Web.Application.Pages.Studies.SearchPanel", ResourcePath="ClearCanvas.ImageServer.Web.Application.Pages.Studies.SearchPanel.js")]
      public partial class SearchPanel : AJAXScriptControl, IStudySearchPage
      {
          #region Private members
  
          private readonly StudyController _controller = new StudyController();
          private EventHandler<SearchPanelButtonClickedEventArgs> _deleteButtonClickedHandler;
          private EventHandler<SearchPanelButtonClickedEventArgs> _assignAuthorityGroupsButtonClickedHandler;
          private static IStudySearchPageExtension _customizer;
          #endregion Private members
  
          #region Class Constructor
  
          static SearchPanel()
          {
              LoadCustomzier();
          }
  
          #endregion
  
          #region Events
          public event EventHandler<SearchPanelButtonClickedEventArgs> DeleteButtonClicked
          {
              add { _deleteButtonClickedHandler += value; }
              remove { _deleteButtonClickedHandler -= value; }
          }
  
          public event EventHandler<SearchPanelButtonClickedEventArgs> AssignAuthorityGroupsButtonClicked
          {
              add { _assignAuthorityGroupsButtonClickedHandler += value; }
              remove { _assignAuthorityGroupsButtonClickedHandler -= value; }
          }
          #endregion
  
          #region Private Properties
          
          private bool DisplaySearchWarning
          {
              get
              {
                  return String.IsNullOrEmpty(PatientId.TrimText) &&
                         String.IsNullOrEmpty(PatientName.TrimText) &&
                         String.IsNullOrEmpty(AccessionNumber.TrimText) &&
                         String.IsNullOrEmpty(ToStudyDate.Text) &&
                         String.IsNullOrEmpty(FromStudyDate.Text) &&
                         String.IsNullOrEmpty(StudyDescription.TrimText) &&
                         String.IsNullOrEmpty(ReferringPhysiciansName.TrimText) &&
                         String.IsNullOrEmpty(ResponsibleOrganization.TrimText) &&
                         String.IsNullOrEmpty(ResponsiblePerson.TrimText) &&
                         ModalityListBox.SelectedIndex < 0 &&
                         StatusListBox.SelectedIndex < 0 &&
                         ConfigurationManager.AppSettings["DisplaySearchWarning"].ToLower().Equals("true");
              }
          }
  
          #endregion
  
          #region Public Properties
  
          [ExtenderControlProperty]
          [ClientPropertyName("DeleteButtonClientID")]
          public string DeleteButtonClientID
          {
              get { return DeleteStudyButton.ClientID; }
          }
  
          [ExtenderControlProperty]
          [ClientPropertyName("OpenButtonClientID")]
          public string OpenButtonClientID
          {
              get { return ViewStudyDetailsButton.ClientID; }
          }
  
  		[ExtenderControlProperty]
  		[ClientPropertyName("RestoreButtonClientID")]
  		public string RestoreButtonClientID
  		{
  			get { return RestoreStudyButton.ClientID; }
  		}
  
          [ExtenderControlProperty]
          [ClientPropertyName("SendButtonClientID")]
          public string SendButtonClientID
          {
              get { return MoveStudyButton.ClientID; }
          }
  
          [ExtenderControlProperty]
          [ClientPropertyName("AssignAuthorityGroupsButtonClientID")]
          public string AssignAuthorityGroupsButtonClientID
          {
              get { return AssignAuthorityGroupsButton.ClientID; }
          }
  
          [ExtenderControlProperty]
          [ClientPropertyName("StudyListClientID")]
          public string StudyListClientID
          {
              get { return StudyListGridView.TheGrid.ClientID; }
          }
          
          [ExtenderControlProperty]
          [ClientPropertyName("OpenStudyPageUrl")]
          public string OpenStudyPageUrl
          {
              get { return Page.ResolveClientUrl(ImageServerConstants.PageURLs.StudyDetailsPage); }
          }
  
          [ExtenderControlProperty]
          [ClientPropertyName("SendStudyPageUrl")]
          public string SendStudyPageUrl
          {
              get { return Page.ResolveClientUrl(ImageServerConstants.PageURLs.MoveStudyPage); }
          }
  
          [ExtenderControlProperty]
          [ClientPropertyName("ViewImagePageUrl")]
          public string ViewImagePageUrl
          {
              get
              {
  //                if (!Thread.CurrentPrincipal.IsInRole(ClearCanvas.ImageViewer.AuthorityTokens.ViewerClinical))
  //                    return Page.ResolveClientUrl(ImageServerConstants.PageURLs.EmbeddedViewImagesPage);
  
                  return Page.ResolveClientUrl(ConfigurationManager.AppSettings["WebViewerPage"] ?? ImageServerConstants.PageURLs.ViewImagesPage);
              }
          }
          
          [ExtenderControlProperty]
          [ClientPropertyName("CanViewImages")]
          public bool CanViewImages
          {
              get { return Thread.CurrentPrincipal.IsInRole(AuthorityTokens.Study.ViewImages); }
          }
  
          [ExtenderControlProperty]
          [ClientPropertyName("OpenNewWindow")]
          public bool OpenNewWindow
          {
              get { return false; }
          }
  
  
          [ExtenderControlProperty]
          [ClientPropertyName("ViewImageButtonClientID")]
          public string ViewImageButtonClientID
          {
              get { return ViewImagesButton.ClientID; }
          }
  
          public ServerPartition ServerPartition { get; set; }
  
          Control IStudySearchPage.SearchFieldsContainer
          {
              get { return SearchFieldsContainer; }
          }
  
          #endregion Public Properties  
  
          #region Private Methods
  
          private static void LoadCustomzier()
          {
              try
              {
                  _customizer = new StudySearchPageExtensionPoint().CreateExtension() as IStudySearchPageExtension;
              }
              catch (Exception ex)
              {
                  Platform.Log(LogLevel.Debug, ex);
              }
          }
  
          private void SetupChildControls()
          {
              foreach(StudyStatusEnum s in  StudyStatusEnum.GetAll())
              {
                  StatusListBox.Items.Add(new ListItem { Text = ServerEnumDescription.GetLocalizedDescription(s), Value = s.Lookup});
              }
  
              ClearToStudyDateButton.Attributes["onclick"] = ScriptHelper.ClearDate(ToStudyDate.ClientID, ToStudyDateCalendarExtender.ClientID);
              ClearFromStudyDateButton.Attributes["onclick"] = ScriptHelper.ClearDate(FromStudyDate.ClientID, FromStudyDateCalendarExtender.ClientID);
              ToStudyDate.Attributes["OnChange"] = ScriptHelper.CheckDateRange(FromStudyDate.ClientID, ToStudyDate.ClientID, ToStudyDate.ClientID, ToStudyDateCalendarExtender.ClientID, "To Date must be greater than From Date");
              FromStudyDate.Attributes["OnChange"] = ScriptHelper.CheckDateRange(FromStudyDate.ClientID, ToStudyDate.ClientID, FromStudyDate.ClientID, FromStudyDateCalendarExtender.ClientID, "From Date must be less than To Date");
              
              GridPagerTop.InitializeGridPager(SR.GridPagerStudySingleItem, SR.GridPagerStudyMultipleItems, StudyListGridView.TheGrid,
                                               () => StudyListGridView.ResultCount, ImageServerConstants.GridViewPagerPosition.Top);
              StudyListGridView.Pager = GridPagerTop;
  
              ConfirmStudySearchMessageBox.Confirmed += delegate
                                                            {
                                                                StudyListGridView.DataBindOnPreRender =
                                                                    true;
                                                                StudyListGridView.Refresh();
                                                                if (SearchUpdatePanel.UpdateMode ==
                                                                    UpdatePanelUpdateMode.Conditional)
                                                                    SearchUpdatePanel.Update();
                                                            };
              ConfirmStudySearchMessageBox.Cancel += delegate
                                                         {
                                                             StudyListGridView.DataBindOnPreRender = false;
                                                         };
  
              RestoreMessageBox.Confirmed += delegate(object data)
                              {
                                  if (data is IList<Study>)
                                  {
                                      var studies = data as IList<Study>;
                                      foreach (Study study in studies)
                                      {
                                          _controller.RestoreStudy(study);
                                      }
                                  }
  								else if (data is IList<StudySummary>)
  								{
  									var studies = data as IList<StudySummary>;
  									foreach (StudySummary study in studies)
  									{
                                          _controller.RestoreStudy(study.TheStudy);
  									}
  								}
                                  else if (data is Study)
                                  {
                                      var study = data as Study;
                                      _controller.RestoreStudy(study);
                                  }
  
                                  DataBind();
                                  SearchUpdatePanel.Update(); // force refresh
                              };
  
              StudyListGridView.DataSourceCreated += delegate(StudyDataSource source)
                                          {
                                              source.Partition = ServerPartition;
                                              source.DateFormats = ToStudyDateCalendarExtender.Format;
  
                                              if (!String.IsNullOrEmpty(PatientId.TrimText))
                                                  source.PatientId = SearchHelper.TrailingWildCard(PatientId.TrimText);
                                              if (!String.IsNullOrEmpty(PatientName.TrimText))
                                                  source.PatientName = SearchHelper.NameWildCard(PatientName.TrimText);
                                              if (!String.IsNullOrEmpty(AccessionNumber.TrimText))
                                                  source.AccessionNumber = SearchHelper.TrailingWildCard(AccessionNumber.TrimText);
                                              if (!String.IsNullOrEmpty(ToStudyDate.Text))
                                                  source.ToStudyDate = ToStudyDate.Text;
                                              if (!String.IsNullOrEmpty(FromStudyDate.Text))
                                                  source.FromStudyDate = FromStudyDate.Text;
                                              if (!String.IsNullOrEmpty(StudyDescription.TrimText))
                                                  source.StudyDescription = SearchHelper.LeadingAndTrailingWildCard(StudyDescription.TrimText);
                                              if (!String.IsNullOrEmpty(ReferringPhysiciansName.TrimText))
                                                  source.ReferringPhysiciansName = SearchHelper.NameWildCard(ReferringPhysiciansName.TrimText);
                                              if (!String.IsNullOrEmpty(ResponsiblePerson.TrimText))
                                                  source.ResponsiblePerson = SearchHelper.NameWildCard(ResponsiblePerson.TrimText);
                                              if (!String.IsNullOrEmpty(ResponsibleOrganization.TrimText))
                                                  source.ResponsibleOrganization = SearchHelper.NameWildCard(ResponsibleOrganization.TrimText);
                                              
                                              if (ModalityListBox.SelectedIndex > -1)
                                              {
                                                  var modalities = new List<string>();
                                                  foreach (ListItem item in ModalityListBox.Items)
                                                  {
                                                      if (item.Selected)
                                                      {
                                                          modalities.Add(item.Value);
                                                      }
                                                  }
                                                  source.Modalities = modalities.ToArray();
                                              }
  
  
gt:                                             if (StatusListBox.SelectedIndex > -1)