private void GenerateButtonClick(object sender, EventArgs e) { var selectedFilter = FiltersComboBox.SelectedItem as FilterComboItem; if (selectedFilter == null) { return; } if (string.IsNullOrEmpty(SaveToTextBox.Text)) { SaveToTextBox.Focus(); return; } SetBusy("Generating Pivot report..."); var facets = new FacetCollection(); foreach (var facet in FacetsListBox.CheckedItems.Cast <Facet>()) { facets.Add(facet); } if (!Directory.Exists(SaveToTextBox.Text)) { Directory.CreateDirectory(SaveToTextBox.Text); } CopyDirectory(Path.GetFullPath(".\\pivot_files"), Path.Combine(SaveToTextBox.Text, ".\\pivot_files")); var path = Path.Combine(SaveToTextBox.Text, "pivot.cxml"); Task.Factory.StartNew(() => { // You need to set current filter first var setFilterDocument = GetCommandResultsAsDocument( "setCurrentFilter", new Dictionary <string, string> { { "sFilter", selectedFilter.Id } }); if (HasResponseError(setFilterDocument)) { return; } var cols = string.Join(",", facets.Select(f => f.Name).Where(f => f != "ixBug")); using (var stream = GetCommandResultsAsStream("search", new Dictionary <string, string> { { "cols", cols } })) { GeneratePivotReport(stream, facets, path, selectedFilter.Name); } }).ContinueWith(task => { SetAvailable(); if (HasTaskException(task)) { return; } }, TaskScheduler.FromCurrentSynchronizationContext()); }
private static void GeneratePivotReport(Stream stream, FacetCollection facets, string path, string collectionName) { using (var reader = XmlReader.Create(stream)) using (var writer = new XmlTextWriter(path, Encoding.UTF8)) { reader.Read(); reader.ReadStartElement("response"); reader.ReadElementContentAsString("description", string.Empty); if ("sFilter".Equals(reader.Name)) { reader.ReadElementContentAsString("sFilter", string.Empty); } reader.ReadStartElement("cases"); writer.Formatting = Formatting.Indented; writer.WriteStartDocument(); writer.WriteStartElement("Collection", "http://schemas.microsoft.com/collection/metadata/2009"); writer.WriteAttributeString("xmlns", "p", null, "http://schemas.microsoft.com/livelabs/pivot/collection/2009"); writer.WriteAttributeString("SchemaVersion", "1.0"); writer.WriteAttributeString("Name", collectionName); writer.WriteStartElement("FacetCategories"); foreach (var facet in facets) { writer.WriteStartElement("FacetCategory"); writer.WriteAttributeString("Name", facet.DisplayName); writer.WriteAttributeString("Type", facet.Type); writer.WriteAttributeString("IsFilterVisible", "http://schemas.microsoft.com/livelabs/pivot/collection/2009", facet.IsFilter ? "true" : "false"); writer.WriteAttributeString("IsWordWheelVisible", "http://schemas.microsoft.com/livelabs/pivot/collection/2009", "true"); writer.WriteAttributeString("IsMetaDataVisible", "http://schemas.microsoft.com/livelabs/pivot/collection/2009", "true"); writer.WriteEndElement(); // FacetCategory } writer.WriteEndElement(); // FacetCategories writer.WriteStartElement("Items"); writer.WriteAttributeString("ImgBase", ".\\pivot_files\\images.xml"); while (reader.Name == "case") { var id = reader.GetAttribute("ixBug"); reader.ReadStartElement("case"); writer.WriteStartElement("Item"); writer.WriteAttributeString("Id", id); writer.WriteAttributeString("Img", "#0"); var properties = new Dictionary <string, string> { { "ixBug", id }, }; while (reader.NodeType != XmlNodeType.EndElement) { var name = reader.Name; var content = reader.ReadElementContentAsString(name, string.Empty); properties.Add(name, content); } if (properties.ContainsKey("sTitle")) { writer.WriteElementString("Description", properties["sTitle"]); } writer.WriteStartElement("Facets"); foreach (var facet in facets) { facet.WriteTo(writer, properties[facet.Name]); } writer.WriteEndElement(); // Facets writer.WriteEndElement(); // Item reader.ReadEndElement(); // case } writer.WriteEndElement(); // Items reader.ReadEndElement(); // cases reader.ReadEndElement(); // response } }