/// <summary> /// This is the method that renders the resulting array into the console view /// </summary> /// <param name="searchResults">An array of SearchResults populated with values for the page</param> /// <param name="searchWrapper">The wrapper class containing all relevant parameters for the search.</param> /// <param name="searchPageIDs">The list of IDs of search pages</param> private static void HandleSearchResults(SearchResult[] searchResults, SearchWrapper searchWrapper, IList <Guid> searchPageIDs) { if ((searchResults == null) || (searchResults.Length == 0)) { Console.WriteLine("No Results"); return; } switch (searchWrapper.Mode) { //This add the id of the last node in the current set to the list. Now the user can move from page 1 to page 2. case SearchMode.NewSearch: searchPageIDs.Add(searchResults[searchResults.Length - 1]._node._id._nodeid_id); break; //If this is the last (highest) page in the list add the id of the last node in the current set to the list. Now the user can move from page n to page n+1. case SearchMode.NextPage: if (!searchPageIDs.Contains(searchResults[searchResults.Length - 1]._node._id._nodeid_id)) { searchPageIDs.Add(searchResults[searchResults.Length - 1]._node._id._nodeid_id); } break; } Console.WriteLine("Page Number: " + searchWrapper.CurrentPageNumber + "\n"); ShowSearchResults(searchResults, searchWrapper); }
/// <summary> /// This is a method to show print node names of current result set to screen. /// </summary> /// <param name="searchResults">An array of SearchResults populated with values for the page</param> /// <param name="searchWrapper">The wrapper class containing all relevant parameters for the search.</param> private static void ShowSearchResults(SearchResult[] searchResults, SearchWrapper searchWrapper) { for (int index = 0; index < searchResults.Length; index++) { var searchResult = searchResults[index]; int counter = (searchWrapper.CurrentPageNumber - 1) * searchWrapper.Parameters._nPageSize + index + 1; Console.WriteLine(counter + ": " + searchResult._node._type + ": " + searchResult._node.name); } }
/// <summary> /// This method will set the Search Page number for search method and UI, and ensure paging is correct /// </summary> /// <param name="searchWrapper">The wrapper class containing all relevant parameters for the search.</param> /// <param name="searchPageIDs">The list of IDs of search pages</param> /// <param name="searchMode">Seach mode command detailing what action to take</param> private static void PrepareSearch(SearchWrapper searchWrapper, IList <Guid> searchPageIDs, SearchMode searchMode) { searchWrapper.Mode = searchMode; int index = 0; switch (searchMode) { case SearchMode.NewSearch: //the start ID provides the clue to the search engine about which page is to be displayed. //since the search wrapper and page ID list are reset before this call, it means //that exactly 1 entry will exist, and it will be an emtpy GUID. This is required //for new searches. searchPageIDs.Add(searchWrapper.Parameters._guid_start_id); break; case SearchMode.NextPage: //This will safely increment the page index to the next item Guid. //The current index of the last item of the current page should be located and incremented. index = searchPageIDs.IndexOf(searchWrapper.Parameters._guid_start_id); index++; //check we have not gone past the last item if ((index <= 0) || (index >= searchPageIDs.Count - 1)) { index = searchPageIDs.Count - 1; } //reset the ID to really be the ID of the first item of the next page searchWrapper.Parameters._guid_start_id = searchPageIDs[index]; break; case SearchMode.PreviousPage: //same as next, but doing the inverse index = searchPageIDs.IndexOf(searchWrapper.Parameters._guid_start_id); index--; if ((index < 0) || (index >= searchPageIDs.Count)) { index = 0; } searchWrapper.Parameters._guid_start_id = searchPageIDs[index]; break; } searchWrapper.CurrentPageNumber = index + 1; }
/// <summary> /// This is the method for executing the actual search query on the server. /// </summary> /// <param name="cinegyClient">The instance of the proxy class to use to connect with</param> /// <param name="clientContext">The active context to use for the query</param> /// <param name="searchWrapper">The wrapper class containing all relevant parameters for the search.</param> /// <returns>An array of SearchResults populated with values for the page</returns> private static SearchResult[] Search(CinegyDataAccessServiceClient cinegyClient, ConnectContext clientContext, SearchWrapper searchWrapper) { try { string errorMsg; //Pepare the array for containing the result set. SearchResult[] searchResults; int returnCode; //Execute the actual search method against CAS, passing the prepared parameters. cinegyClient.Search(clientContext, searchWrapper.Parameters, out searchResults, out returnCode, out errorMsg); Console.WriteLine("Return Code: " + returnCode); Console.WriteLine("Message: " + errorMsg); return(searchResults); } catch (Exception ex) { Console.WriteLine("Search failed. Exception: " + ex.Message); return(null); } }
/// <summary> /// The method used to allow interactive commanding of the search process. Will repeat /// inside a 'do' loop until the 'Q' key is pressed, whereupon it will quit the loop (ultimately /// leading to the application terminating). /// </summary> /// <param name="cinegyClient">The instance of the proxy class to use to connect with</param> /// <param name="clientContext">The active context to use for the query</param> private static void HandleConsoleInput(CinegyDataAccessServiceClient cinegyClient, ConnectContext clientContext) { ConsoleKeyInfo keyInfo = new ConsoleKeyInfo(); //This is a List of node IDs for moving back and forward by the pageful in the result set. //These IDs will be used as the _guid_start_id property of the SearchParameters object. IList <Guid> searchPageIDs = new List <Guid>(); //Create an instance of the SearchWrapper class, used to command the search operation SearchWrapper searchWrapper = new SearchWrapper(); SearchResult[] searchResults; //main 'do' loop for console input do { Console.WriteLine("\n--------------------------------------------------------"); Console.WriteLine("Press the 's' key to start new search."); Console.WriteLine("Press the 'n' key to go to next search result page."); Console.WriteLine("Press the 'p' key to go to previous search result page."); Console.WriteLine("Press the 'q' key to quit\n"); while (Console.KeyAvailable == false) { } keyInfo = Console.ReadKey(true); switch (keyInfo.Key) { //The user wants to start a new search. This will reset the wrapper, and set //a new general string search paramter containing the entered text. case ConsoleKey.S: Console.Write("Keyword: "); //this list will be populated with IDs referring to every page of //results that are returned. searchPageIDs = new List <Guid>(); searchWrapper = new SearchWrapper(); searchWrapper.Parameters._str_search = Console.ReadLine(); Console.WriteLine("Searching...\n"); //Call to a local method to organise the values collected ready to run the actual search. PrepareSearch(searchWrapper, searchPageIDs, SearchMode.NewSearch); //Call to a local method to execute the prepared seach operation. searchResults = Search(cinegyClient, clientContext, searchWrapper); //Call to a local method to process the results and respond. HandleSearchResults(searchResults, searchWrapper, searchPageIDs); break; //Pages to the nex search page returned from CAS, so the screen is not overfilled case ConsoleKey.N: Console.WriteLine("Retrieving next Page for '" + searchWrapper.Parameters._str_search + "'....\n"); //Again, calls to the PrepareSearch local method, but specifying the next page PrepareSearch(searchWrapper, searchPageIDs, SearchMode.NextPage); //And then re-executes the search method to get the filled results searchResults = Search(cinegyClient, clientContext, searchWrapper); //Before processing the display HandleSearchResults(searchResults, searchWrapper, searchPageIDs); break; //Pages to the prvious search page returned from CAS. case ConsoleKey.P: Console.WriteLine("Retrieving previous Page for '" + searchWrapper.Parameters._str_search + "'....\n"); //Same as next page, but now with a different mode (PreviousPage) PrepareSearch(searchWrapper, searchPageIDs, SearchMode.PreviousPage); searchResults = Search(cinegyClient, clientContext, searchWrapper); HandleSearchResults(searchResults, searchWrapper, searchPageIDs); break; //Quit application. case ConsoleKey.Q: Console.WriteLine("Quitting..."); break; default: Console.WriteLine("Unknown Key Command"); break; } } while (keyInfo.Key != ConsoleKey.Q); }