//--------------------------------------------------------------------------------------------------------------------- private void InitializeData(int count) { DataSets = new CatalogueDataSetInfo[count]; DataSetIdentifiers = new string[count]; DataSetResources = new string[count]; DataSetSizes = new long[count]; foreach (KeyValuePair <string, string> kvp in MetadataElementNames) { metadataValues[kvp.Key] = new TypedValue[count]; } }
//--------------------------------------------------------------------------------------------------------------------- /// <summary>Adds data sets from the specified URL.</summary> /// <returns><c>true</c>, if the request was successful, <c>false</c> if there was an error.</returns> /// <param name="url">A URL at which RDF metadata content for the data sets can be obtained.</param> /// <param name="startIndex">The start index of the current partial request.</param> /// <param name="count">The number of data sets to be received with the current partial request.</param> protected bool AddResultsFromRdf(string url, int startIndex, int count) { /*url = Regex.Replace(url, @"([^\&\?]+)=\{startIndex\??\}", "$1=" + startIndex); * url = Regex.Replace(url, @"([^\&\?]+)=\{count\??\}", "$1=" + count);*/ url = Regex.Replace(url, @"([\?&])startIndex=", (startIndex == -1 ? "" : "$1startIndex=" + startIndex)); url = Regex.Replace(url, @"([\?&])count=", "$1count=" + count); context.AddDebug(3, "Catalogue query (page): " + url); try { XmlDocument doc = new XmlDocument(); doc.Load(url); if (namespaceManager == null) { namespaceManager = new XmlNamespaceManager(doc.NameTable); foreach (XmlAttribute attr in doc.DocumentElement.Attributes) { if (attr.Prefix == "xmlns") { namespaceManager.AddNamespace(attr.LocalName, attr.Value); } } } // In the first request, retrieve meta information on results if (startIndex == 0) { // Extract the number of results from the retreived XML document and add it to the local totalResults XmlNode node = doc.SelectSingleNode("//rdf:Description/os:totalResults", namespaceManager); int tr = 0; if (node != null) { Int32.TryParse(node.InnerText, out tr); } TotalResults = tr; // Extract the first start index (i.e. the index offset) node = doc.SelectSingleNode("//rdf:Description/os:startIndex", namespaceManager); startIndex = 0; if (node != null) { Int32.TryParse(node.InnerText, out startIndex); } // Extract the actual number of items per page, in case it does not correspond to the requested count value node = doc.SelectSingleNode("//rdf:Description/os:itemsPerPage", namespaceManager); //if (node != null) Int32.TryParse(node.InnerText, out dataSetsPerRequest); // Set length of dataSets array and of metadata arrays count = (TotalResults <= maxCount ? TotalResults : maxCount); InitializeData(count); ReceivedResults = 0; } if (count == 0 || TotalResults < minCount) { return(true); // continue only if results are requested } XmlNodeList nodes = doc.SelectNodes(DataSetXmlName, namespaceManager); for (int i = 0; i < nodes.Count; i++) { DataSets[ReceivedResults] = new CatalogueDataSetInfo(this, ReceivedResults); GetDataSetInformation(ReceivedResults, nodes[i] as XmlElement); ReceivedResults++; } context.AddDebug(3, "Results: " + ReceivedResults); } catch (Exception e) { context.AddError("Catalogue query failed: " + e.Message + " " + url); return(false); } return(true); }
//--------------------------------------------------------------------------------------------------------------------- /// <summary>Queries the catalogue and receives the data sets to build the result.</summary> /// <param name="minCount">Minimum number of data sets to be included. If the minimum number is not reached, the result will be empty.</param> /// <param name="maxCount">Maxium number of data sets to be included.</param> public void GetDataSets(int minCount, int maxCount) { this.minCount = minCount; this.maxCount = maxCount; if (Url != null) { // Get the element names from the configuration file XmlDocument configDoc = new XmlDocument(); XmlElement catalogueElem = null; bool success = false; try { configDoc.Load(context.SiteConfigFolder + "/services.xml"); success = ((catalogueElem = configDoc.SelectSingleNode("/services/dataset/catalogue[@type='application/rdf+xml']") as XmlElement) != null); } catch (Exception) { } if (success) { for (int i = 0; i < catalogueElem.ChildNodes.Count; i++) { XmlElement elem = catalogueElem.ChildNodes[i] as XmlElement; if (elem == null || !elem.HasAttribute("name")) { continue; } switch (elem.Attributes["name"].Value) { case "dataset": DataSetXmlName = elem.InnerXml; break; case "value": ResourceXmlName = elem.InnerXml; break; case "_identifier": IdentifierXmlName = elem.InnerXml; break; case "_size": SizeXmlName = elem.InnerXml; break; } } } else if (context.UserLevel == UserLevel.Administrator) { context.AddWarning("Invalid catalogue metadata configuration"); } // Get first result context.AddDebug(2, "Catalogue query (base): " + Url); if (!AddResultsFromRdf(Url, 0, (maxCount > ResultsPerRequest ? ResultsPerRequest : maxCount))) { context.WriteInfo(String.Format("Return 1")); return; } if (maxCount == 0 || TotalResults < minCount) { InitializeData(0); return; } // If necessary, do more requests if (maxCount > TotalResults) { maxCount = TotalResults; } while (ReceivedResults < maxCount) { if (!AddResultsFromRdf(Url, ReceivedResults, (maxCount > ReceivedResults + ResultsPerRequest ? ResultsPerRequest : maxCount - ReceivedResults))) { break; } } context.AddDebug(2, "Catalogue results: " + ReceivedResults + " (total results: " + TotalResults + ")"); } else if (DataSetParameter != null && context is IfyWebContext) { TotalResults = DataSetParameter.Values.Length; ReceivedResults = TotalResults; InitializeData(ReceivedResults); string[] paramValues; paramValues = StringUtils.Split((context as IfyWebContext).GetParamValue(DataSetParameter.Name + ":_identifier"), DataSetParameter.Separator); if (paramValues.Length == ReceivedResults) { for (int i = 0; i < ReceivedResults; i++) { DataSetIdentifiers[i] = paramValues[i]; } } paramValues = StringUtils.Split((context as IfyWebContext).GetParamValue(DataSetParameter.Name + ":_size"), DataSetParameter.Separator); if (paramValues.Length == ReceivedResults) { for (int i = 0; i < ReceivedResults; i++) { Int64.TryParse(paramValues[i], out DataSetSizes[i]); } } /*for (int i = 0; i < dataSetTotalCount; i++) DataSets[i] = new DataSetInfo(this, i, DataSetParameter.Values[i], sizes[i], startTimes[i], endTimes[i]); */ for (int i = 0; i < ReceivedResults; i++) { DataSets[i] = new CatalogueDataSetInfo(this, i); DataSetResources[i] = DataSetParameter.Values[i]; } foreach (KeyValuePair <string, string> kvp in MetadataElementNames) { metadataValues[kvp.Key] = new TypedValue[ReceivedResults]; paramValues = StringUtils.Split((context as IfyWebContext).GetParamValue(DataSetParameter.Name + ":" + kvp.Key), DataSetParameter.Separator); if (paramValues.Length == ReceivedResults) { for (int i = 0; i < ReceivedResults; i++) { SetMetadataValue(i, kvp.Key, paramValues[i]); } } } /*string paramValue = context.GetParamValue(paramName); * datasetParam * string[] paramValues = StringUtils.Split(paramValue, ';'); * DataSets = new DataSetInfo[]; * int[] fileSizes; * DateTime[] startDates; * DateTime[] endDates;*/ } }