private void InvokeNextButton_Complete(IAsyncResult result)
        {
            try
            {
                IEnumerable <FacetedSearch3.CUAHSIFacetedSearch.OntologyElement> MyRemainingFacets = new List <FacetedSearch3.CUAHSIFacetedSearch.OntologyElement>();
                FacetedSearch3.CUAHSIFacetedSearch.OntologyEnvelope env = new CUAHSIFacetedSearch.OntologyEnvelope();
                using (FacetedSearch3.CUAHSIFacetedSearch.MultiFacetedHISSvcClient cl = (FacetedSearch3.CUAHSIFacetedSearch.MultiFacetedHISSvcClient)result.AsyncState)
                {
                    env = cl.EndGetOntologyElementsGivenConstraints(result);
                    MyRemainingFacets = env.OntologyElements;
                }

                this.Invoke(new MethodInvoker(delegate
                {
                    if (IncludeSpatialResults == true)
                    {
                        PutReturnOnMap(env.Sites);
                    }

                    FacetFlowPanel.Enabled = true;
                    FacetFlowPanel.Controls.Add(new SearchFacetSpecifier(this, TopLevelFacets, SelectedFacets, MyRemainingFacets, FacetFlowPanel.Controls.Count));
                    ResizeSearchSpecifiers();
                }));
            }
            catch (Exception exc)
            {
                MessageBox.Show(string.Format("Error completing Next request with message {0}, inner ex {1}", exc.Message, exc.InnerException));
            }
        }
 /// <summary>
 /// Sets the MaxItemsInObjectGraph behavior to permit sufficiently-large transactions for faceted search
 /// </summary>
 /// <param name="cl"></param>
 private static void ConfigureCUAHSIChannelFactory(FacetedSearch3.CUAHSIFacetedSearch.MultiFacetedHISSvcClient cl)
 {
     foreach (var operation in cl.ChannelFactory.Endpoint.Contract.Operations)
     {
         var behavior = operation.Behaviors.Find <DataContractSerializerOperationBehavior>() as DataContractSerializerOperationBehavior;
         if (behavior != null)
         {
             behavior.MaxItemsInObjectGraph = 2147483647;
         }
     }
 }
        private void LoadTotalFacetCollection_Complete(IAsyncResult result)
        {
            try
            {
                using (FacetedSearch3.CUAHSIFacetedSearch.MultiFacetedHISSvcClient cl = (FacetedSearch3.CUAHSIFacetedSearch.MultiFacetedHISSvcClient)result.AsyncState)
                {
                    TopLevelFacets = cl.EndGetOntologyElements(result);
                }

                using (FacetedSearch3.CUAHSIFacetedSearch.MultiFacetedHISSvcClient cl = GetMultiFacetedHISSvcClient())
                {
                    this.Invoke(new MethodInvoker(delegate
                    {
                        #region Initialize form with retrieved data
                        if (InputsSufficientForFacetedSearch())
                        {
                            SpatialTemporalCommitted = true;
                            RemoveDownStreamSearchFacetSpecifiers(0);        // remove all searchfacetspecifers
                            ResetFacetFlowPanel();
                            try
                            {
                                cl.BeginGetOntologyElementsGivenConstraints(SelectedFacets.ToArray(), BeginDateTime, EndDateTime, SrchExt.MinY, SrchExt.MaxY, SrchExt.MinX, SrchExt.MaxX, false,
                                                                            InitializeFacetedSearch_Complete, cl);
                            }
                            catch (Exception e)
                            {
#if DEBUG
                                MessageBox.Show(string.Format("Error in transit layer with message {0} and inner exception {1}", e.Message, e.InnerException));
#else
                                MessageBox.Show("Error in transit layer. Please try again.");
#endif
                            }
                        }
                        else
                        {
                            MessageBox.Show("Please select a spatial region to bound your search prior to initiating faceted search");
                        }
                        #endregion
                    }));
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(string.Format("Error initializing total list of ontology elements with message {0}, inner ex: {1}", e.Message, e.InnerException));
            }
        }
        /// <summary>
        /// Called on client initialization. Loads the entire set of possible ontology terms to client-side memory. All mechanisms to display "what's left" will prune this collection.
        /// </summary>
        public void LoadTotalFacetCollection()
        {
            App.ProgressHandler.Progress(String.Empty, 0, "Initializing Faceted Search ... Please Wait");
            try
            {
                using (FacetedSearch3.CUAHSIFacetedSearch.MultiFacetedHISSvcClient cl = GetMultiFacetedHISSvcClient())
                {
                    /*bool configed = false;
                     * try
                     * {
                     *  ConfigureCUAHSIChannelFactory(cl);
                     *  configed = true;
                     * }
                     * catch (Exception ex)
                     * {
                     *  MessageBox.Show(string.Format("Error configuring channel factory with message {0}, inner ex: {1}", ex.Message, ex.InnerException));
                     * }*/
                    try
                    {
                        // if (configed)
                        // {
                        cl.BeginGetOntologyElements(LoadTotalFacetCollection_Complete, cl);
                        // }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(string.Format("Error refreshing ontology from metadata source with message {0}, {1}.", e.Message, e.InnerException));
                    }
                }
            }
            catch (Exception exc)
            {
#if DEBUG
                MessageBox.Show("Error connecting to server. Please verify you have access to the internet and try again. Message: " + exc.Message);
#else
                MessageBox.Show("Error connecting to server. Please verify you have access to the internet and try again.");
#endif
            }
            App.ProgressHandler.Progress(String.Empty, 0, "Initializing Faceted Search - Populating the Tree View");
        }
 private void InitializeFacetedSearch_Complete(IAsyncResult result)
 {
     try
     {
         FacetedSearch3.CUAHSIFacetedSearch.MultiFacetedHISSvcClient      cl  = (FacetedSearch3.CUAHSIFacetedSearch.MultiFacetedHISSvcClient)result.AsyncState;
         FacetedSearch3.CUAHSIFacetedSearch.OntologyEnvelope              env = cl.EndGetOntologyElementsGivenConstraints(result);
         IEnumerable <FacetedSearch3.CUAHSIFacetedSearch.OntologyElement> MyRemainingFacets = new List <FacetedSearch3.CUAHSIFacetedSearch.OntologyElement>();
         MyRemainingFacets = env.OntologyElements;
         this.Invoke(new MethodInvoker(delegate
         {
             FacetFlowPanel.Enabled = true;
             FacetFlowPanel.Controls.Add(new SearchFacetSpecifier(this, TopLevelFacets, SelectedFacets, MyRemainingFacets, FacetFlowPanel.Controls.Count));
             ResizeSearchSpecifiers();
             MakeSearchPanelVisible();
             App.ProgressHandler.Progress(String.Empty, 0, String.Empty);
         }));
     }
     catch (Exception e)
     {
         MessageBox.Show(string.Format("Error in transit layer with message {0} and inner exception {1}", e.Message, e.InnerException));
     }
 }
        private void InvokeSearchButton_Complete(IAsyncResult result)
        {
            try
            {
                List <FacetedSearch3.CUAHSIFacetedSearch.OntologyElement>     MyRemainingFacets = new List <FacetedSearch3.CUAHSIFacetedSearch.OntologyElement>();
                List <FacetedSearch3.CUAHSIFacetedSearch.SeriesCatalogRecord> SearchRes         = new List <CUAHSIFacetedSearch.SeriesCatalogRecord>();
                using (FacetedSearch3.CUAHSIFacetedSearch.MultiFacetedHISSvcClient cl = (FacetedSearch3.CUAHSIFacetedSearch.MultiFacetedHISSvcClient)result.AsyncState)
                {
                    SearchRes = cl.EndGetSeriesGivenConstraints(result).OrderByDescending(r => r.ValueCount).ToList();
                }

                this.Invoke(new MethodInvoker(delegate
                {
                    PutReturnOnMap(SearchRes);
                    FacetFlowPanel.Enabled = true;
                    FacetFlowPanel.Controls.Add(new SearchFacetSpecifier(this, TopLevelFacets, SelectedFacets, MyRemainingFacets, FacetFlowPanel.Controls.Count));
                    ResizeSearchSpecifiers();
                }));
            }
            catch (Exception exc)
            {
                MessageBox.Show(string.Format("Error completing Search request with message {0}, inner ex {1}", exc.Message, exc.InnerException));
            }
        }
        /// <summary>
        /// Method to call series search that returns sql that would be executed, rather than resultset.
        /// </summary>
        /// <param name="AllSelectedFacets"></param>
        public void InvokeSearchSQLButton(List <FacetedSearch3.CUAHSIFacetedSearch.OntologyElement> AllSelectedFacets)
        {
            SelectedFacets = AllSelectedFacets;
            List <FacetedSearch3.CUAHSIFacetedSearch.OntologyElement> MyRemainingFacets = new List <FacetedSearch3.CUAHSIFacetedSearch.OntologyElement>();

            if (InputsSufficientForFacetedSearch())
            {
                try
                {
                    SpatialTemporalCommitted = true;
                    // SrchExt = SelectedShapes.Envelope.ToExtent();
                    string SQLRes;
                    //using (FacetedSearch3.CUAHSIFacetedSearch.MultiFacetedHISSvcClient cl = new FacetedSearch3.CUAHSIFacetedSearch.MultiFacetedHISSvcClient())
                    using (FacetedSearch3.CUAHSIFacetedSearch.MultiFacetedHISSvcClient cl = GetMultiFacetedHISSvcClient())
                    {
                        ConfigureCUAHSIChannelFactory(cl);
                        SQLRes = cl.GetSQLOfSearchQuery(SelectedFacets.ToArray(), BeginDateTime, EndDateTime, SrchExt.MinY, SrchExt.MaxY, SrchExt.MinX, SrchExt.MaxX, IncludeSpatialResults);
                    }

                    MessageBox.Show(SQLRes);
                }
                catch (Exception e)
                {
#if DEBUG
                    MessageBox.Show(string.Format("Error in transit layer with message {0} and inner exception {1}", e.Message, e.InnerException));
#else
                    MessageBox.Show("Error in transit layer. Please try again.");
#endif
                }
            }
            else
            {
                MessageBox.Show("Please select a spatial region to bound your search prior to initiating faceted search");
            }
        }
        /// <summary>
        /// Executes search for SeriesCatalog
        /// </summary>
        /// <param name="AllSelectedFacets"></param>
        public void InvokeSearchButton(List <FacetedSearch3.CUAHSIFacetedSearch.OntologyElement> AllSelectedFacets)
        {
            SelectedFacets = AllSelectedFacets;
            if (InputsSufficientForFacetedSearch())
            {
                try
                {
                    SpatialTemporalCommitted = true;
                    // SrchExt = SelectedShapes.Envelope.ToExtent();
                    //using (FacetedSearch3.CUAHSIFacetedSearch.MultiFacetedHISSvcClient cl = new FacetedSearch3.CUAHSIFacetedSearch.MultiFacetedHISSvcClient())

                    using (FacetedSearch3.CUAHSIFacetedSearch.MultiFacetedHISSvcClient cl = GetMultiFacetedHISSvcClient())
                    {
                        cl.BeginGetSeriesGivenConstraints(SelectedFacets.ToArray(), BeginDateTime, EndDateTime, SrchExt.MinY, SrchExt.MaxY, SrchExt.MinX, SrchExt.MaxX, InvokeSearchButton_Complete, cl);
                    }
                }
                catch (Exception e)
                {
#if DEBUG
                    MessageBox.Show(string.Format("Error in transit layer with message {0} and inner exception {1}", e.Message, e.InnerException));
#else
                    MessageBox.Show("Error in transit layer. Please try again.");
#endif
                }
            }
            else
            {
                MessageBox.Show("Please select a spatial region to bound your search prior to initiating faceted search");
            }
        }
        /// <summary>
        /// Provides surface for SearchFacetSpecifiers to specify the next facet of the search, executed by spawning a new SearchFacetSpecifier.
        /// Uses CUAHSI Faceted Search webservices to determine what options are available, given the user's selections.
        /// Regenerates spatial-temporal options, then submits faceted search
        /// </summary>
        /// <param name="AllSelectedFacets"></param>
        public void InvokeNextButton(List <FacetedSearch3.CUAHSIFacetedSearch.OntologyElement> AllSelectedFacets, int MyIndex)
        {
            SelectedFacets = AllSelectedFacets;
            if (InputsSufficientForFacetedSearch())
            {
                try
                {
                    RemoveDownStreamSearchFacetSpecifiers(MyIndex + 1);
                    SpatialTemporalCommitted = true;

                    using (FacetedSearch3.CUAHSIFacetedSearch.MultiFacetedHISSvcClient cl = GetMultiFacetedHISSvcClient())
                    {
                        cl.BeginGetOntologyElementsGivenConstraints(SelectedFacets.ToArray(), BeginDateTime, EndDateTime, SrchExt.MinY, SrchExt.MaxY, SrchExt.MinX, SrchExt.MaxX, IncludeSpatialResults, InvokeNextButton_Complete, cl);
                        // FacetedSearch3.CUAHSIFacetedSearch.OntologyEnvelope env = cl.GetTypedOntologyElementsGivenConstraints(SelectedFacets, BeginDateTime, EndDateTime, SrchExt.MinY, SrchExt.MaxY, SrchExt.MinX, SrchExt.MaxX, IncludeSpatialResults);
                    }
                }
                catch (Exception e)
                {
#if DEBUG
                    MessageBox.Show(string.Format("Error in transit layer with message {0} and inner exception {1}", e.Message, e.InnerException));
#else
                    MessageBox.Show("Error in transit layer. Please try again.");
#endif
                }
            }
            else
            {
                MessageBox.Show("Please select a spatial region to bound your search prior to initiating faceted search");
            }
        }