// To use a custom database schema with the Database Manager, you must define a custom MyPrepareSearch method
        // and assign it to the StorageDatabaseManager.PrepareSearch delegate. After doing this, the search fields in the Database Manager
        //will properly refine any database manager search.  The MyPrepareSearch() method gets the search fields specified in the database manager
        // by calling StorageDatabaseManager.GetDicomQueryParams().  This returns any query parameters specified.
        // The MyPrepareSearch() method then needs to create a MatchingParameterCollection that corresponds to the specified search.
        // Note that the database manager search fields correspond to items contained in the patient, study, and series tables only.
        // There are no search fields that correspond to the image table.
        // Therefore MyPrepareSearch() only needs to add MyPatient, MyStudy, and MySeries objects to the MatchingParameterList.
        // For more details, see the "Changing the LEAD Medical Storage Server to use a different database schema" tutorial.


        private void MyPrepareSearch(MatchingParameterCollection matchingCollection)
        {
            DicomQueryParams q = __DbManager.GetDicomQueryParams();

            try
            {
                MatchingParameterList matchingList = new MatchingParameterList();
                MyPatient             patient      = new MyPatient();
                MyStudy  study  = new MyStudy();
                MySeries series = new MySeries();

                matchingList.Add(patient);
                matchingList.Add(study);
                matchingList.Add(series);

                matchingCollection.Add(matchingList);
                study.StudyAccessionNumber    = q.AccessionNumber;
                patient.PatientIdentification = q.PatientId;

                if (!string.IsNullOrEmpty(q.PatientName.FamilyName))
                {
                    patient.PatientName = q.PatientName.FamilyName.TrimEnd('*') + "*";
                }

                if (!string.IsNullOrEmpty(q.PatientName.GivenName))
                {
                    patient.PatientName = q.PatientName.GivenName.TrimEnd('*') + "*";
                }

                if (!string.IsNullOrEmpty(q.Modalities))
                {
                    series.SeriesModality = q.Modalities.Replace(",", "\\");
                }
                ;

                if (!string.IsNullOrEmpty(q.SeriesDescription))
                {
                    series.SeriesSeriesDescription = q.SeriesDescription.TrimEnd('*') + "*";
                }

                if (!string.IsNullOrEmpty(q.ReferringPhysiciansName.FamilyName))
                {
                    study.StudyReferringPhysiciansName = q.ReferringPhysiciansName.FamilyName.TrimEnd('*') + "*";
                }
                ;

                if (!string.IsNullOrEmpty(q.ReferringPhysiciansName.GivenName))
                {
                    study.StudyReferringPhysiciansName = q.ReferringPhysiciansName.GivenName.TrimEnd('*') + "*";
                }
                ;

                if (q.StudyFromChecked || q.StudyToChecked)
                {
                    DateRange studyDateRange = new DateRange();

                    if (q.StudyFromChecked)
                    {
                        studyDateRange.StartDate = q.StudyFromDate;
                    }

                    if (q.StudyToChecked)
                    {
                        studyDateRange.EndDate = q.StudyToDate;
                    }

                    study.StudyStudyDate = studyDateRange;
                }

                if (q.StorageDateChecked)
                {
                    MyInstance      instance  = new MyInstance();
                    DateRange       dateRange = new DateRange();
                    DateRangeFilter StorageDateRangeFilter = q.StorageDateRange;
                    string          startDate = StorageDateRangeFilter.DateRangeFrom;
                    string          endDate   = StorageDateRangeFilter.DateRangeTo;

                    if (StorageDateRangeFilter.SelectedDateFilter == DateRangeFilter.RangeFilterType.DateRange)
                    {
                        if (!string.IsNullOrEmpty(startDate))
                        {
                            dateRange.StartDate = DateTime.Parse(startDate);
                        }

                        if (!string.IsNullOrEmpty(endDate))
                        {
                            dateRange.EndDate = DateTime.Parse(endDate);
                        }
                    }
                    else if (StorageDateRangeFilter.SelectedDateFilter == DateRangeFilter.RangeFilterType.Months)
                    {
                        DateTime lastMonthsDate = DateTime.Now.SubtractMonths(Convert.ToInt32(StorageDateRangeFilter.LastMonths));

                        dateRange.StartDate = lastMonthsDate;
                        dateRange.EndDate   = DateTime.Now;
                    }
                    else
                    {
                        TimeSpan subtractionDays = new TimeSpan(Convert.ToInt32(StorageDateRangeFilter.LastDays),
                                                                DateTime.Now.Hour,
                                                                DateTime.Now.Minute,
                                                                DateTime.Now.Second,
                                                                DateTime.Now.Millisecond);

                        dateRange.StartDate = DateTime.Now.Subtract(subtractionDays);
                        dateRange.EndDate   = DateTime.Now;
                    }

                    instance.ImageLastStoreDate = dateRange;
                    matchingList.Add(instance);
                }

                study.StudyStudyId = q.StudyId;
            }
            catch (Exception exception)
            {
                throw exception;
            }
            finally
            {
                // do nothing ;
            }
        }
        private void CreateContainerPages( )
        {
            ControlPanelView         controlPanelView  = new ControlPanelView( );
            ServerInformationView    serverInfoView    = new ServerInformationView( );
            ServerAddinsView         serverAddInView   = new ServerAddinsView( );
            ServerSettingsDialog     serverSettingsDlg = new ServerSettingsDialog( );
            ServerInformationControl serverInfoControl = new ServerInformationControl();

            ControlPanelPresenter      controlPanelPresenter   = new ControlPanelPresenter( );
            ServerInformationPresenter serverInfoPresenter     = new ServerInformationPresenter( );
            ServerAddinPresenter       serverAddInPresenter    = new ServerAddinPresenter( );
            ServerSettingsPresenter    serverSettingsPresenter = new ServerSettingsPresenter( );

#if LEADTOOLS_V19_OR_LATER
            RealTimeViewPresenter realTimePresenter = new RealTimeViewPresenter();
#endif

            StorageDatabaseManager dbManager = new StorageDatabaseManager( );
#if TUTORIAL_CUSTOM_DATABASE
            // To use a custom database schema with the Database Manager, you must define a custom MyPrepareSearch method
            // and assign it to the StorageDatabaseManager.PrepareSearch delegate.
            // For more details, see the "Changing the LEAD Medical Storage Server to use a different database schema" tutorial.

            dbManager.PrepareSearch = new PrepareSearchDelegate(MyPrepareSearch);
#endif
            EventLogViewer logViewer = new EventLogViewer( );

#if LEADTOOLS_V19_OR_LATER
            RealTimeViewerControl realTimeView = new RealTimeViewerControl()
            {
                Visible = false
            };
#endif

            dbManager.BackColor         = Color.FromArgb(75, 75, 75);
            logViewer.BackColor         = Color.White;
            logViewer.PathLogDump       = this.PathLogDump;
            serverInfoControl.BackColor = Color.White;

            ThemesManager.ApplyTheme(controlPanelView);
            ThemesManager.ApplyTheme(logViewer);
            ThemesManager.ApplyTheme(dbManager);
            ThemesManager.ApplyTheme(serverInfoControl);

#if LEADTOOLS_V19_OR_LATER
            ThemesManager.ApplyTheme(realTimeView);
#endif

            ConfigureServerInfoConrol(serverInfoControl);

            View.SetHeader(serverInfoView);

            AddPage(new PageData(controlPanelView, "Control Panel", null));

            //AddPage ( new PageData ( new Control ( ), "Security", null ) ) ;
            AddPage(new PageData(logViewer, "Event Log", null));
            AddPage(new PageData(serverInfoControl, "System Information", null));

            dbManager.MergeImportDicom = true;

#if (LEADTOOLS_V19_OR_LATER_EXPORT) || (LEADTOOLS_V19_OR_LATER)
            dbManager.EnableExport = true;
#endif // #if (LEADTOOLS_V19_OR_LATER_EXPORT) || (LEADTOOLS_V19_OR_LATER)

            dbManager.Enabled = ServerState.Instance.ServerService != null;

            if (ServerState.Instance.ServerService != null)
            {
                dbManager.AETitle = ServerState.Instance.ServerService.Settings.AETitle;
            }

            AddPage(new PageData(dbManager, "Database Manager", null));

            dbManager.Enabled = true;

#if LEADTOOLS_V19_OR_LATER
            AddPage(new PageData(realTimeView, "Live View", null));
#endif
            InitializeLicenseView();

            serverAddInView.Enabled = UserManager.User.IsAdmin();
            AddPage(new PageData(serverAddInView, "Add-ons", null));

            controlPanelPresenter.RunView(controlPanelView);
            serverInfoPresenter.RunView(serverInfoView);
            serverAddInPresenter.RunView(serverAddInView);
            serverSettingsPresenter.RunView(serverSettingsDlg);

#if LEADTOOLS_V19_OR_LATER
            realTimePresenter.RunView(realTimeView);
#endif

            if (DataAccessServices.IsDataAccessServiceRegistered <IStorageDataAccessAgent>())
            {
                _AccessAgent = DataAccessServices.GetDataAccessService <IStorageDataAccessAgent>();
            }

            dbManager.CancelStore           += new EventHandler <CancelStoreEventArgs>  (dbManager_CancelStore);
            dbManager.ConfigureStoreCommand += new EventHandler <StoreCommandEventArgs> (dbManager_ConfigureStoreCommand);

#if (LEADTOOLS_V19_OR_LATER_EXPORT) || (LEADTOOLS_V19_OR_LATER)
            dbManager.ConfigureStoreExportCommand += new EventHandler <StoreCommandEventArgs> (dbManager_ConfigureStoreCommandExport);
#endif // #if (LEADTOOLS_V19_OR_LATER_EXPORT) || (LEADTOOLS_V19_OR_LATER)


#if TUTORIAL_CUSTOM_DATABASE
            // To use a custom database schema with the Database Manager, you must define a custom MyPrepareSearch method
            // and assign it to the StorageDatabaseManager.PrepareSearch delegate. After doing this, the search fields in the Database Manager
            //will properly refine any database manager search.  The MyPrepareSearch() method gets the search fields specified in the database manager
            // by calling StorageDatabaseManager.GetDicomQueryParams().  This returns any query parameters specified.
            // For more details, see the "Changing the LEAD Medical Storage Server to use a different database schema" tutorial.

            dbManager.GetDicomQueryParams();
#endif
            __DbManager      = dbManager;
            __ServerInfoView = serverInfoView;

            ApplyPermissions( );

            ApplyStorageSettingsPermissions( );

            Instance_LicenseChanged(this, EventArgs.Empty);
            ServerState.Instance.LicenseChanged += new EventHandler(Instance_LicenseChanged);
        }