public bool Process(IWorksheet worksheet)
        {
            bool contains_data = false;
            int  start_row     = -1;

            try
            {
                Logger.Aquire();
                Logger.Write("Worksheet.Process", "", System.Diagnostics.TraceEventType.Information, 2, 0, SharedData.LogCategory);
                Logger.Write("Worksheet.Process", "PROCESSING WORKSHEET: " + worksheet.Name, System.Diagnostics.TraceEventType.Information, 2, 0, SharedData.LogCategory);
                Logger.Write("Worksheet.Process", "       SOURCE MODULE: " + SourceDataTable.Name, System.Diagnostics.TraceEventType.Information, 2, 0, SharedData.LogCategory);
                Logger.Write("Worksheet.Process", "              FILTER: " + SourceDataTable.FilterExpression, System.Diagnostics.TraceEventType.Information, 2, 0, SharedData.LogCategory);

                if (StartRow.ToLower() == "%lastusedrow%")
                {
                    start_row = worksheet.UsedRange.RowCount;
                }
                else if (StartRow.ToLower() == "%firstnewrow%")
                {
                    start_row = worksheet.UsedRange.Row + 1;
                }
                else
                {
                    start_row = Convert.ToInt32(StartRow);
                }

                Logger.Write("Worksheet.Process", "    START COLUMN|ROW: " + StartColumn + start_row, System.Diagnostics.TraceEventType.Information, 2, 0, SharedData.LogCategory);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Logger.Release();
            }

            // Rename the worksheet if applicable.
            if (!string.IsNullOrEmpty(NewSheetName))
            {
                Logger.WriteLine("Worksheet.Process", "       CHANGING NAME: " + TextParser.Parse(NewSheetName, DrivingData, SharedData, ModuleCommands), System.Diagnostics.TraceEventType.Information, 2, 0, SharedData.LogCategory);
                worksheet.Name = TextParser.Parse(NewSheetName, DrivingData, SharedData, ModuleCommands);

                if (worksheet.Name.Length > 31)
                {
                    throw new Exception("The worksheet name (" + worksheet.Name + ") cannot have more than 31 characters.");
                }
            }

            // If the worksheet does not have a source table defined then return false.
            // Without a source table the worksheet will be empty.
            if (HasSourceTable)
            {
                if (!SharedData.Data.Contains(SourceDataTable.Name))
                {
                    throw new Exception(string.Format("The source data table '{0}' for sheet at index '{1}' was not found in the global cache set.", SourceDataTable.Name, SheetNumber));
                }

                // Load the worksheet with the source table's data.
                if (!Load(SourceDataTable.Process(ConditionalTables), worksheet))
                {
                    // No data wasloaded into the worksheet.
                    contains_data = false;
                }
                else
                {
                    // Perform any required formatting.
                    Format(worksheet);

                    contains_data = true;
                }
            }
            else
            {
                contains_data = false;
            }

            return(contains_data);
        }
Esempio n. 2
0
        protected void TransformData()
        {
            MemoryStream         xslt_stream, xml_stream;
            XmlTextReader        xml_reader;
            XsltSettings         xslt_settings;
            XPathDocument        doc;
            DataTable            DataSource = null;
            XslCompiledTransform xslt;
            XsltArgumentList     arg_list = null;
            string template_path          = null;

            try
            {
                CreateResultsTable();

                xslt          = new XslCompiledTransform();
                xslt_settings = new XsltSettings(true, true);
                xslt_stream   = new MemoryStream();
                xml_stream    = new MemoryStream();

                if (HasTemplate)
                {
                    Logger.WriteLine("XsltWriter.TransformData", "", System.Diagnostics.TraceEventType.Information, 2, 0, SharedData.LogCategory);

                    // Determine if we are using an XSLT template on disk or if we are getting it from another module.
                    if (!string.IsNullOrEmpty(TemplateName))
                    {
                        // Parse the templates path.
                        template_path = TextParser.Parse(TemplateName, DrivingData, SharedData, ModuleCommands);

                        // Check for the file expecting relative path.
                        if (System.IO.File.Exists(System.IO.Path.GetFullPath(template_path)))
                        {
                            TemplateName = System.IO.Path.GetFullPath(template_path);
                        }
                        // Check for the file from absolute path.
                        else if (System.IO.File.Exists(template_path))
                        {
                            TemplateName = template_path;
                        }
                        // Check for the file two layers up incase we are in the bin/debug|release folder.
                        else if (System.IO.File.Exists(@"..\..\" + template_path))
                        {
                            TemplateName = System.IO.Path.GetFullPath(@"..\..\" + template_path);
                        }
                        else
                        {
                            throw new Exception(string.Format("The XSLT template file '{0}' was not found.", template_path));
                        }

                        Logger.WriteLine("XsltWriter.TransformData", "PHYSICAL STYLE SHEET: ", System.Diagnostics.TraceEventType.Information, 2, 0, SharedData.LogCategory);

                        // Load the style sheet from the referenced path.
                        xslt.Load(TextParser.Parse(TemplateName, DrivingData, SharedData, ModuleCommands), xslt_settings, null);
                    }
                    else
                    {
                        Logger.WriteLine("XsltWriter.TransformData", " DYNAMIC STYLE SHEET: ", System.Diagnostics.TraceEventType.Information, 2, 0, SharedData.LogCategory);

                        // Load the style sheet from the referenced module.
                        xslt.Load(new XmlTextReader(new StringReader(TextParser.Parse(TemplateBody, DrivingData, SharedData, ModuleCommands))), xslt_settings, null);
                    }

                    if (SourceDataTable != null)
                    {
                        Logger.WriteLine("XsltWriter.TransformData", "          DATASOURCE: " + TextParser.Parse(SourceDataTable.Name, DrivingData, SharedData, ModuleCommands), System.Diagnostics.TraceEventType.Information, 2, 0, SharedData.LogCategory);
                        //Logger.WriteLine("XsltWriter.TransformData", "              FILTER: " + SourceDataTable.FilterExpression, System.Diagnostics.TraceEventType.Information, 2, 0, SharedData.LogCategory);

                        // Get the data source table.
                        DataSource = SourceDataTable.Process(DestinationDataTable.CacheTableCollection);
                    }

                    // Check to see if the XSLT writer has any arguments defined.
                    if (ArgumentContainer != null && ArgumentContainer.Count > 0)
                    {
                        Logger.WriteLine("XsltWriter.TransformData", "", System.Diagnostics.TraceEventType.Information, 2, 0, SharedData.LogCategory);
                        Logger.WriteLine("XsltWriter.TransformData", "      ARGUMENT COUNT: " + ArgumentContainer.Count, System.Diagnostics.TraceEventType.Information, 2, 0, SharedData.LogCategory);

                        arg_list = new XsltArgumentList();

                        // Load the argument list.
                        foreach (Argument arg in ArgumentContainer.Arguments)
                        {
                            Logger.WriteLine("XsltWriter.TransformData", "", System.Diagnostics.TraceEventType.Information, 2, 0, SharedData.LogCategory);
                            Logger.WriteLine("XsltWriter.TransformData", "       ARGUMENT NAME: " + arg.Name, System.Diagnostics.TraceEventType.Information, 2, 0, SharedData.LogCategory);
                            Logger.WriteLine("XsltWriter.TransformData", "               VALUE: " + TextParser.Parse(arg.Value, DrivingData, SharedData, ModuleCommands), System.Diagnostics.TraceEventType.Information, 2, 0, SharedData.LogCategory);

                            arg_list.AddParam(arg.Name, "", TextParser.Parse(arg.Value, DrivingData, SharedData, ModuleCommands));
                        }
                    }

                    if ((DataSource != null && DataSource.Rows.Count > 0) || AllowEmptyReport)
                    {
                        if (DataSource == null)
                        {
                            DataSource = new DataTable("NOT_USED");
                        }

                        if (AllowEmptyReport && !string.IsNullOrEmpty(EmptyMessage) && DataSource.Rows.Count == 0)
                        {
                            DataSource.Columns.Add(new DataColumn("Message", typeof(string)));

                            var row = DataSource.NewRow();
                            row["Message"] = EmptyMessage;
                            DataSource.Rows.Add(row);
                        }

                        // Get the data from the source table in XML form.
                        DataSource.WriteXml(xml_stream);

                        // Make sure we are looking at the begining of our data stream.
                        xml_stream.Position = 0;

                        // Load the xml from our xml stream into our xml reader.
                        xml_reader = new XmlTextReader(xml_stream);

                        // Create an xml document from the xml reader.
                        doc = new XPathDocument(xml_reader);

                        // Transform the xml document with the xslt template and load it into our xslt_stream.
                        xslt.Transform(doc, arg_list, xslt_stream);

                        xslt_stream.Flush();
                        xslt_stream.Position = 0;

                        Logger.WriteLine("XsltWriter.TransformData", "", TraceEventType.Information, 2, 0, SharedData.LogCategory);
                        Logger.WriteLine("XsltWriter.TransformData", "    CREATE TRANSFORM: SUCCESSFULL", TraceEventType.Information, 2, 0, SharedData.LogCategory);

                        Save(xslt_stream);
                    }
                    else
                    {
                        Logger.WriteLine("XsltWriter.TransformData", "", TraceEventType.Information, 2, 0, SharedData.LogCategory);
                        Logger.WriteLine("XsltWriter.TransformData", "DISCARDING TRANSFORM: NO DATA", TraceEventType.Information, 2, 0, SharedData.LogCategory);
                    }
                }
                else
                {
                    throw new Exception("XSLT writer must have a template defined.");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }