/// <summary>
 /// This method will be called in SummaryQuery only the zero-ing threshold is greater than 0,
 /// a status message is generated, and the user wants to zero out results under the threshold.
 /// </summary>
 /// <param name="requestId"></param>
 public void PostProcess(string requestId)
 {
     SummaryQueryUtil.ZeroLowCellCountsInQueryResult(viewableResultDataset.Tables[0], CellThreshHold);
     status.Code        = RequestStatus.StatusCode.Complete;
     status.Message     = "";
     status.PostProcess = false;
 }
        private void BuildResponseDocuments()
        {
            if (responseDocument == null)
            {
                // Metadata request returns two documents, a display version of the metadata result, and processable version.
                // Summary query requests returns two documents if necessary, the query result and the style document.
                responseDocument               = new Document[IsMetadataRequest || hasCellCountAlert ? 2 : 1];
                responseDocument[0]            = new Document("0", "x-application/lpp-dns-table", "SummaryQueryResponse.xml");
                responseDocument[0].IsViewable = true;
                responseDocument[0].Size       = viewableResultDataset.GetXml().Length;

                if (hasCellCountAlert)
                {
                    ViewableDocumentStyle style = new ViewableDocumentStyle
                    {
                        Css        = ".StyledRows { background-color: yellow; color: red }",
                        StyledRows = SummaryQueryUtil.FindLowCellCountsInQueryResult(viewableResultDataset.Tables[0], CellThreshHold)
                    };

                    XmlSerializer serializer = new XmlSerializer(typeof(ViewableDocumentStyle));
                    using (StringWriter sw = new StringWriter())
                    {
                        using (XmlWriter xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings {
                            OmitXmlDeclaration = true
                        }))
                        {
                            serializer.Serialize(xmlWriter, style, null);
                            styleXml = sw.ToString();
                        }
                    }

                    responseDocument[1]            = new Document("1", "application/xml", "ViewableDocumentStyle.xml");
                    responseDocument[1].IsViewable = false;
                    responseDocument[1].Size       = styleXml.Length;
                }
                else if (IsMetadataRequest)
                {
                    responseDocument[1]            = new Document("1", "application/xml", "RefreshDatesResponse.xml");
                    responseDocument[1].IsViewable = false;
                    responseDocument[1].Size       = metadataResultDataset == null ? 0 : metadataResultDataset.GetXml().Length;
                }
            }
        }
        public void Start(string requestId, bool viewSQL)
        {
            try
            {
                log.Debug("SummaryQueryModelProcessor:Start: RequestId=" + requestId + ", viewSQL=" + viewSQL.ToString());
                if (Settings.Count == 0)
                {
                    throw new Exception(CommonMessages.Exception_MissingSettings);
                }

                status.Code = RequestStatus.StatusCode.InProgress;
                //styleXml = null;
                DataSet   combinedDataset = new DataSet();
                DataTable dtSQL           = new DataTable();
                dtSQL.Columns.Add("SQL");
                foreach (string query in queries)
                {
                    if (!string.IsNullOrEmpty(query.Trim()))
                    {
                        if (!viewSQL)
                        {
                            var ds    = ExecuteQuery(query);
                            var table = ds.Tables[0];
                            combinedDataset.Merge(table);
                        }
                        else
                        {
                            dtSQL.Rows.Add(query);
                        }
                    }
                }
                if (viewSQL)
                {
                    combinedDataset.Merge(dtSQL);
                }

                hasCellCountAlert = SummaryQueryUtil.CheckCellCountsInQueryResult(combinedDataset, CellThreshHold);

                DataTable datatable;

                if (IsMetadataRequest)
                {
                    //datatable = combinedDataset.Tables[0];
                    datatable             = AugmentAdminQuery(combinedDataset);
                    metadataResultDataset = combinedDataset;
                }
                else
                {
                    //datatable = AugmentSummaryQuery(combinedDataset);
                    datatable             = combinedDataset.Tables[0].Copy();
                    metadataResultDataset = null;
                }

                // Log the DataSet result.
                MemoryStream s = new MemoryStream();
                combinedDataset.WriteXml(s, XmlWriteMode.WriteSchema);
                s.Seek(0, SeekOrigin.Begin);
                string x = new StreamReader(s).ReadToEnd();
                // Can never leave this code enabled with a production build
                //log.Debug("Result of SQL Query");
                //log.Debug(x);

                viewableResultDataset.Tables.Add(datatable);

                if (hasCellCountAlert)
                {
                    status.Code        = RequestStatus.StatusCode.CompleteWithMessage;
                    status.Message     = "The query results have rows with low cell counts. You can choose to set the low cell count data to 0 by clicking the [Post Process] button.";
                    status.PostProcess = true;
                }
                else
                {
                    status.Code    = RequestStatus.StatusCode.Complete;
                    status.Message = "";
                }
            }
            catch (Exception e)
            {
                status.Code    = RequestStatus.StatusCode.Error;
                status.Message = e.Message;
                throw e;
            }
        }