Ejemplo n.º 1
0
        private void SearchBoxPreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (SearchTextList.SelectedIndex == -1)
            {
                SearchTextList.SelectedIndex = 0;
            }

            switch (e.Key)
            {
            case Key.Down:
                if (SearchTextList.ItemsSource != null)
                {
                    int nItems = (SearchTextList.ItemsSource as List <SearchItem>).Count;
                    if (SearchTextList.SelectedIndex < nItems - 1)
                    {
                        SearchTextList.SelectedIndex++;
                        SearchTextList.ScrollIntoView(SearchTextList.SelectedItem);
                    }
                }
                break;

            case Key.Up:
                if (SearchTextList.SelectedIndex > 0)
                {
                    SearchTextList.SelectedIndex--;
                    SearchTextList.ScrollIntoView(SearchTextList.SelectedItem);
                }
                break;

            case Key.Enter:
                Keyboard.ClearFocus();
                break;
            }
        }
        private async void OnFindCommandExecuted()
        {
            /* Search lots and send back results */
            // _inventories = new ObservableCollection<Inventory>(InventoryFactory.GetInventories());

            //string[] lines = ListId.Trim().Split(new[] { "\r\n", "\r", "\n" },StringSplitOptions.None);
            //searchText = string.Join(",", lines.Distinct().ToArray());

            //Settings.Filter = Filter;

            string ggInventoryIdList = string.Empty;
            string LotIdList         = string.Empty;

            try
            {
                string   searchText      = string.Empty;
                string[] searchTextArray = null;
                string   query           = string.Empty;

                if (!IsMultiline && string.IsNullOrEmpty(SearchText))
                {
                    throw new Exception("Search text is empty");
                }
                if (IsMultiline && string.IsNullOrEmpty(SearchTextList))
                {
                    throw new Exception("Search text list is empty");
                }
                if (Workgroup == null)
                {
                    throw new Exception("Workgroup is empty");
                }
                if (Filter == null)
                {
                    throw new Exception("Filter is empty");
                }
                if (SearchOperator == null)
                {
                    throw new Exception("Search operator is empty");
                }
                if (string.IsNullOrEmpty(Filter.filter_type))
                {
                    throw new Exception("Filter type is empty.\nReview dataview in AdminTool");
                }
                if (string.IsNullOrEmpty(Filter.filter_is_pre_query))
                {
                    throw new Exception("Filter is_pre_query is empty.\nReview dataview in AdminTool");
                }

                Settings.Filter = Filter.filter_name;
                if (IsMultiline)
                {
                    searchTextArray = SearchTextList.Split(new[] { "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
                    if (searchTextArray.Length == 0)
                    {
                        throw new Exception("Search text list is empty");
                    }
                }
                else
                {
                    Settings.SearchText = SearchText;
                    searchText          = SearchText.Trim();
                }

                int limit = 0;
                if (!int.TryParse(MaxResults, out limit))
                {
                    MaxResults = "0";
                }
                if (limit > 2000)
                {
                    throw new Exception("Max results cannot be greater than 2000");
                }

                string querySearchOperator = string.Empty;
                switch (SearchOperator.value_member)
                {
                case "equals":
                    querySearchOperator = " = '{0}'";
                    break;

                case "contains":
                    querySearchOperator = " like '%{0}%'";
                    break;

                case "startswith":
                    querySearchOperator = " like '{0}%'";
                    break;

                case "endswith":
                    querySearchOperator = " like '%{0}'";
                    break;

                case "lessthan":
                    querySearchOperator = " < {0}";
                    break;

                case "morethan":
                    querySearchOperator = " > {0}";
                    break;

                default:
                    throw new Exception("Search operator not supported");
                }

                List <int> preResultIds = null;
                List <int> resultIds    = new List <int>();
                switch (Filter.filter_type)
                {
                case "dataview":
                    if (string.IsNullOrEmpty(Filter.filter_source))
                    {
                        throw new Exception("Filter source is empty.\nReview dataview in AdminTool");
                    }
                    if (!IsMultiline)
                    {
                        preResultIds = await _restClient.SearchLookup(Filter.filter_source, SearchOperator.value_member, searchText);

                        if (preResultIds != null && preResultIds.Count > 0)
                        {
                            searchText = string.Join(",", preResultIds);
                            query      = "@inventory.accession_id IN ({0})";
                        }
                        else
                        {
                            await _pageDialogService.DisplayAlertAsync("Error", Filter.filter_name + " not found", "OK");

                            return;
                        }
                    }
                    else
                    {
                        foreach (var text in searchTextArray)
                        {
                            var partialResultIds = await _restClient.SearchLookup(Filter.filter_source, SearchOperator.value_member, text);

                            if (partialResultIds != null && partialResultIds.Count() > 0)
                            {
                                resultIds.AddRange(partialResultIds);
                            }
                        }
                        if (resultIds.Count == 0)
                        {
                            await _pageDialogService.DisplayAlertAsync("Error", Filter.filter_name + " not found", "OK");

                            return;
                        }
                        query      = "@inventory.accession_id IN ({0})";
                        searchText = string.Join(",", resultIds);
                    }
                    break;

                case "query":
                    if (string.IsNullOrEmpty(Filter.filter_query))
                    {
                        throw new Exception("Filter query is empty.\nReview dataview in AdminTool");
                    }
                    if (!IsMultiline)
                    {
                        if (Filter.filter_is_pre_query.Equals("Y"))
                        {
                            if (string.IsNullOrEmpty(Filter.filter_pre_query_table))
                            {
                                throw new Exception("Filter pre result table_name is empty.\nReview dataview in AdminTool");
                            }

                            query        = string.Format(Filter.filter_query + querySearchOperator, searchText);
                            preResultIds = await _restClient.SearchKeys(query, Filter.filter_pre_query_table);

                            if (preResultIds != null && preResultIds.Count > 0)
                            {
                                searchText = string.Join(",", preResultIds);
                                query      = "@inventory.accession_id IN ({0})";
                            }
                            else
                            {
                                await _pageDialogService.DisplayAlertAsync("Error", Filter.filter_name + " not found", "OK");

                                return;
                            }
                        }
                        else
                        {
                            query = Filter.filter_query + querySearchOperator;
                        }
                    }
                    else      //Multiline
                    {
                        query = Filter.filter_query + querySearchOperator;

                        if (Filter.filter_is_pre_query.Equals("Y"))
                        {
                            if (string.IsNullOrEmpty(Filter.filter_pre_query_table))
                            {
                                throw new Exception("Filter pre result table_name is empty.\nReview dataview in AdminTool");
                            }

                            foreach (var text in searchTextArray)
                            {
                                preResultIds = await _restClient.SearchKeys(string.Format(query, text), Filter.filter_pre_query_table);

                                if (preResultIds != null && preResultIds.Count > 0)
                                {
                                    searchText = string.Join(",", preResultIds);
                                    var finalQuery = "@inventory.accession_id IN ({0})";

                                    if (!string.IsNullOrEmpty(Location1))
                                    {
                                        finalQuery += string.Format(" and @inventory.storage_location_part1 = '{0}'", Location1);
                                    }

                                    /*if (Workgroup != null && Workgroup.group_owned_by.HasValue)
                                     *  finalQuery += string.Format(" and @inventory.owned_by = {0}", Workgroup.group_owned_by.Value);*/
                                    finalQuery += " and @inventory.form_type_code <> '**'";

                                    var partialResultIds = await _restClient.SearchKeys(string.Format(finalQuery, searchText), "inventory", 0);

                                    if (partialResultIds != null && partialResultIds.Count() > 0)
                                    {
                                        resultIds.AddRange(partialResultIds);
                                    }
                                }
                            }
                            query      = "@inventory.inventory_id in ({0})";
                            searchText = string.Join(",", resultIds);
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(Location1))
                            {
                                query += string.Format(" and @inventory.storage_location_part1 = '{0}'", Location1);
                            }

                            /*if (Workgroup != null && Workgroup.group_owned_by.HasValue)
                             *  query += string.Format(" and @inventory.owned_by = {0}", Workgroup.group_owned_by.Value);*/
                            query += " and @inventory.form_type_code <> '**'";

                            foreach (var text in searchTextArray)
                            {
                                var partialResultIds = await _restClient.SearchKeys(string.Format(query, text), "inventory", 0);

                                if (partialResultIds != null && partialResultIds.Count() > 0)
                                {
                                    resultIds.AddRange(partialResultIds);
                                }
                            }
                            query      = "@inventory.inventory_id in ({0})";
                            searchText = string.Join(",", resultIds);
                        }
                    }
                    break;

                /*case "lookup":
                 *  throw new Exception("Filter type not supported");
                 *  break;*/
                default:
                    throw new Exception("Filter type not supported");
                }

                /*switch (Filter.filter_name)
                 * {
                 *  case "Accession Number":
                 *      SearchText = SearchText.Replace("CIP ", "");
                 *      int dotIndex = SearchText.IndexOf('.');
                 *      string accession_number_part2 = string.Empty;
                 *      string accession_number_part3 = string.Empty;
                 *      if (dotIndex > -1)
                 *      {
                 *          accession_number_part2 = searchText.Substring(0, dotIndex);
                 *          accession_number_part3 = searchText.Substring(dotIndex + 1, SearchText.Length - dotIndex - 1);
                 *          query = string.Format("@accession.accession_number_part2 = '{0}' and @accession.accession_number_part3 = '{1}'"
                 *                              , accession_number_part2, accession_number_part3);
                 *      }
                 *      else
                 *      {
                 *          accession_number_part2 = searchText;
                 *          query = string.Format("@accession.accession_number_part2 = '{0}' and @accession.accession_number_part3 < '0'", accession_number_part2);
                 *      }
                 *
                 *      preResult = await _restClient.SearchAccession(query, "", "accession");
                 *      if (preResult != null && preResult.Count > 0)
                 *      {
                 *          searchText = preResult.First().accession_id.ToString();
                 *          query = "@inventory.accession_id = {0}";
                 *      }
                 *      else
                 *      {
                 *          await _pageDialogService.DisplayAlertAsync("Error", "Accession number not found", "OK");
                 *          return;
                 *      }
                 *
                 *      break;
                 *  case "Inventory Id / Lot Id":
                 *      if (!IsMultiline)
                 *      {
                 *          if (searchText.StartsWith("gg"))
                 *          {
                 *              searchText = searchText.Replace("gg", "");
                 *              query = "@inventory.inventory_id = {0}";
                 *          }
                 *          else
                 *          {
                 *              query = "@inventory.inventory_number_part2 = {0}";
                 *          }
                 *      }
                 *      else
                 *      {
                 *          ggInventoryIdList = string.Join(",", UniqueList.Where(x => x.StartsWith("gg")).Select(y => y.Replace("gg", "")));
                 *          LotIdList = string.Join(",", UniqueList.Where(x => !x.StartsWith("gg")));
                 *
                 *          if (ggInventoryIdList != string.Empty && LotIdList != string.Empty)
                 *          {
                 *              query = string.Format("@inventory.inventory_id IN ({0}) OR @inventory.inventory_number_part2 IN ({1})", ggInventoryIdList, LotIdList);
                 *          }
                 *          else if (ggInventoryIdList == string.Empty && LotIdList == string.Empty)
                 *          {
                 *              await _pageDialogService.DisplayAlertAsync("Error", "Empty values", "OK");
                 *              return;
                 *          }
                 *          else if (ggInventoryIdList == string.Empty)
                 *          {
                 *              query = string.Format("@inventory.inventory_number_part2 IN ({0})", LotIdList);
                 *          }
                 *          else
                 *          {
                 *              query = string.Format("@inventory.inventory_id IN ({0})", ggInventoryIdList);
                 *          }
                 *          searchText = string.Empty;
                 *      }
                 *      break;
                 *  case "Storage location level 2":
                 *      query = "@inventory.storage_location_part2 like '%{0}%'";
                 *      break;
                 *  case "Storage location level 3":
                 *      query = "@inventory.storage_location_part3 like '%{0}%'";
                 *      break;
                 *  case "Accession Name":
                 *      query = string.Format("@accession_inv_name.category_code = 'CULTIVAR' and @accession_inv_name.plant_name = '{0}'", searchText);
                 *      preResult = await _restClient.SearchAccession(query, "", "accession");
                 *      if (preResult != null && preResult.Count > 0)
                 *      {
                 *          searchText = string.Join(",", preResult.Select(x => x.accession_id.ToString()).ToList());
                 *          query = "@inventory.accession_id IN ({0})";
                 *      }
                 *      else
                 *      {
                 *          await _pageDialogService.DisplayAlertAsync("Error", "Accession name not found", "OK");
                 *          return;
                 *      }
                 *      break;
                 *  case "Collecting number":
                 *      query = string.Format("@accession_inv_name.category_code = 'COLLECTOR' and @accession_inv_name.plant_name = '{0}'", searchText);
                 *      preResult = await _restClient.SearchAccession(query, "", "accession");
                 *      if (preResult != null && preResult.Count > 0)
                 *      {
                 *          searchText = preResult.First().accession_id.ToString();
                 *          query = "@inventory.accession_id = '{0}'";
                 *      }
                 *      else
                 *      {
                 *          await _pageDialogService.DisplayAlertAsync("Error", "Accession name not found", "OK");
                 *          return;
                 *      }
                 *      break;
                 *  case "Note":
                 *      query = "@inventory.note like '%{0}%'";
                 *      break;
                 *  case "Taxon Code":
                 *      query = "@taxonomy_species.alternate_name = '{0}'";
                 *      break;
                 *  case "Order request - Order Id":
                 *      query = "@order_request.order_request_id = '{0}'";
                 *      break;
                 *  case "Order request - Local number":
                 *      query = "@order_request.local_number = '%{0}%'";
                 *      break;
                 * }
                 */
                if (!string.IsNullOrEmpty(Location1))
                {
                    query += string.Format(" and @inventory.storage_location_part1 = '{0}'", Location1);
                }
                // Support user in multiples workgroups
                if (Workgroup != null && !string.IsNullOrEmpty(Workgroup.inv_maint_policy_ids))
                {
                    query += string.Format(" and @inventory.inventory_maint_policy_id in ({0})", Workgroup.inv_maint_policy_ids);
                }
                query += " and @inventory.form_type_code <> '**'";

                List <InventoryThumbnail> result;
                if (!string.IsNullOrEmpty(searchText))
                {
                    result = await _restClient.Search(string.Format(query, searchText), "", "inventory", limit);
                }
                else
                {
                    result = await _restClient.Search(string.Format(query), "", "inventory", limit);
                }

                if (result != null)
                {
                    if (!Filter.Equals("Inventory Id / Lot Id") || IsMultiline)
                    {
                        await _pageDialogService.DisplayAlertAsync("Message", string.Format("Found {0} matches in the database.", result.Count), "OK");
                    }

                    var navigationParams = new NavigationParameters();
                    navigationParams.Add("InventoryThumbnailList", result);
                    await NavigationService.GoBackAsync(navigationParams);
                }
                else
                {
                    await _pageDialogService.DisplayAlertAsync("Message", "Sin resultados", "OK");
                }
            }
            catch (Exception e)
            {
                await _pageDialogService.DisplayAlertAsync("Error", e.Message, "OK");
            }
        }