/// <summary> /// Generate a CSW request string. /// </summary> /// <remarks> /// The CSW request string is built. /// The request is string is build based on the request xslt. /// </remarks> /// <param name="param1">The search criteria</param> /// <returns>The request string</returns> public string GenerateCSWGetRecordsRequest(CswSearchCriteria searchCriteria) { XmlElement root = null; if ((getRecordsRequest == null)||!searchCriteria.RefinePreviousFilter) { getRecordsRequest = new XmlDocument(); XmlDeclaration declaration = getRecordsRequest.CreateXmlDeclaration("1.0", "UTF-8", null); getRecordsRequest.AppendChild(declaration); // Create the root element root = getRecordsRequest.CreateElement("GetRecords"); getRecordsRequest.AppendChild(root); } SetFilterCriteria(getRecordsRequest, searchCriteria); if (searchCriteria.RefinePreviousFilter) { //All And|Or|Not child elements of the GetRecords-root element need to be group in and And element root = getRecordsRequest.DocumentElement; //First get rid of childs that will be reset later XmlNode node = root.SelectSingleNode("StartPosition"); if (node != null ) root.RemoveChild(node); node = root.SelectSingleNode("MaxRecords"); if (node != null) root.RemoveChild(node); XmlElement xmlRefineLogicalOperator = getRecordsRequest.CreateElement("And"); while (root.ChildNodes.Count > 0) { xmlRefineLogicalOperator.AppendChild(root.FirstChild); } root.AppendChild(xmlRefineLogicalOperator); } XmlElement xmlElement = getRecordsRequest.CreateElement("StartPosition"); xmlElement.InnerText = searchCriteria.StartPosition.ToString(); root.AppendChild(xmlElement); xmlElement = getRecordsRequest.CreateElement("MaxRecords"); xmlElement.InnerText = searchCriteria.MaxRecords.ToString(); root.AppendChild(xmlElement); return TransformRequest(getRecordsRequest); }
private void SetFilterCriteria(XmlDocument doc, CswSearchCriteria searchCriteria) { XmlElement root = doc.DocumentElement; //The logical operator between the filter properties XmlElement xmlFilterLogicalOperator = doc.CreateElement(Enum.GetName(typeof(CswSearchCriteria.LogicalOperator), searchCriteria.FilterLogicalOperator)); root.AppendChild(xmlFilterLogicalOperator); //Regular expression instead of Split, in order to keep word combinations between double quotes //"Bob the Builder" construction ---> gives 2 matches: '"Bob the Builder"' and 'construction'. Regex regex = new Regex(@"((""((?<token>.*?)(?<!\\)"")|(?<token>[\S]+))(\s)*)"); MatchCollection matches = regex.Matches(searchCriteria.SearchText); XmlElement searchTextLogicalOperator=null; for (int i = 0; i < matches.Count; i++) { XmlElement xmlKeyWord = doc.CreateElement("KeyWord"); xmlKeyWord.InnerText = this.XmlEscape(matches[i].Value.Trim().Replace(@"""", String.Empty)); if ((matches.Count > 1)||(searchCriteria.SearchTextLogicalOperator == CswSearchCriteria.LogicalOperator.Not)) { if (i == 0) { searchTextLogicalOperator = doc.CreateElement(Enum.GetName(typeof(CswSearchCriteria.LogicalOperator), searchCriteria.SearchTextLogicalOperator)); xmlFilterLogicalOperator.AppendChild(searchTextLogicalOperator); } searchTextLogicalOperator.AppendChild(xmlKeyWord); } else { xmlFilterLogicalOperator.AppendChild(xmlKeyWord); } } //XmlElement xmlLiveDataMap = doc.CreateElement("LiveDataMap"); //xmlLiveDataMap.InnerText = searchCriteria.LiveDataAndMapOnly.ToString(); //xmlFilterLogicalOperator.AppendChild(xmlLiveDataMap); XmlElement xmlTypesOfUseLogicalOperator=null; for (int i = 0; i < searchCriteria.TypesOfUse.Count; i++) { XmlElement xmlTypeOfUse = doc.CreateElement("TypeOfUse"); xmlTypeOfUse.InnerText = searchCriteria.TypesOfUse[i]; if (searchCriteria.TypesOfUse.Count > 1) { if (i == 0) { xmlTypesOfUseLogicalOperator = doc.CreateElement("Or"); xmlFilterLogicalOperator.AppendChild(xmlTypesOfUseLogicalOperator); } xmlTypesOfUseLogicalOperator.AppendChild(xmlTypeOfUse); } else { xmlFilterLogicalOperator.AppendChild(xmlTypeOfUse); } } if (!String.IsNullOrEmpty(searchCriteria.Organisation)) { XmlElement xmlOrganisation = doc.CreateElement("Organisation"); xmlOrganisation.InnerText = this.XmlEscape(searchCriteria.Organisation.Trim().Replace(@"""", String.Empty)); xmlFilterLogicalOperator.AppendChild(xmlOrganisation); } if (searchCriteria.Envelope != null) { XmlElement xmlEnvelope = doc.CreateElement("Envelope"); xmlFilterLogicalOperator.AppendChild(xmlEnvelope); XmlElement xe = doc.CreateElement("MinX"); xe.InnerText=searchCriteria.Envelope.MinX.ToString(); xmlEnvelope.AppendChild(xe); xe = doc.CreateElement("MinY"); xe.InnerText = searchCriteria.Envelope.MinY.ToString(); xmlEnvelope.AppendChild(xe); xe = doc.CreateElement("MaxX"); xe.InnerText = searchCriteria.Envelope.MaxX.ToString(); xmlEnvelope.AppendChild(xe); xe = doc.CreateElement("MaxY"); xe.InnerText = searchCriteria.Envelope.MaxY.ToString(); xmlEnvelope.AppendChild(xe); } }
/// <summary> /// Search CSW catalog with the criteria defined by user /// </summary> /// <param name="sender">event sender</param> /// <param name="e">event args</param> private void FindButton_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); try { // Todo: add more validation. only minimum validation at this point. if (catalogComboBox.SelectedIndex == -1) { ShowErrorMessageBox(StringResources.PleaseSelectACswCatalog); return; } CswCatalog catalog = (CswCatalog)catalogComboBox.SelectedItem; if (catalog == null) { throw new NullReferenceException(StringResources.CswCatalogIsNull); } // reset GUI for search results ResetSearchResultsGUI(); System.Windows.Forms.Cursor.Current = Cursors.WaitCursor; if (!catalog.IsConnected()) { string errMsg = ""; try { catalog.Connect(_isWriteLogs); } catch (Exception ex) { errMsg = ex.Message; } if (!catalog.IsConnected()) { sb.AppendLine("Failed to connect to Catalog"); ShowErrorMessageBox (StringResources.ConnectToCatalogFailed + "\r\n" + errMsg); return; } } // todo: need paging maganism. update SearchCriteria.StartPoistion // generate search criteria CswSearchCriteria searchCriteria = new CswSearchCriteria(); searchCriteria.SearchText = searchPhraseTextBox.Text; searchCriteria.SearchTextLogicalOperator = (CswSearchCriteria.LogicalOperator)cbSearchTextLO.SelectedIndex; searchCriteria.FilterLogicalOperator = CswSearchCriteria.LogicalOperator.And; searchCriteria.Organisation = tbOrganisation.Text; if (cbDownload.Checked) searchCriteria.TypesOfUse.Add(cbDownload.Text); if (cbRaadplegen.Checked) searchCriteria.TypesOfUse.Add(cbRaadplegen.Text); if (cbAchtergrond.Checked) searchCriteria.TypesOfUse.Add(cbAchtergrond.Text); searchCriteria.StartPosition = 1; searchCriteria.MaxRecords = (int)maxResultsNumericUpDown.Value; //searchCriteria.LiveDataAndMapOnly = (liveDataAndMapsOnlyCheckBox.Checked); searchCriteria.RefinePreviousFilter = ((cbRefine.Checked) && (cbRefine.Enabled)); //if (useCurrentExtentCheckBox.Checked) //{ // try { searchCriteria.Envelope = CurrentMapViewExtent(); } // catch (Exception ex) // { // String errMsg = StringResources.GetCurrentExtentFailed + "\r\n" + // ex.Message + "\r\n" + "\r\n" + // StringResources.UncheckCurrentExtentAndTryAgain; // sb.AppendLine(errMsg); // ShowErrorMessageBox(errMsg); // return; // } //} //else //{ searchCriteria.Envelope = null; //} // search if (_searchRequest == null) { _searchRequest = new CswSearchRequest(_isWriteLogs); } _searchRequest.Catalog = catalog; _searchRequest.Criteria = searchCriteria; _searchRequest.Search(); ShowSearchResults(sb); } catch (Exception ex) { sb.AppendLine(ex.Message); ShowErrorMessageBox (ex.Message); } finally { if (_isWriteLogs) Utils.writeFile(sb.ToString()); System.Windows.Forms.Cursor.Current = Cursors.Default; } }