/// <summary>
        /// Constructor that find and initialize the first chart found in the content control
        /// </summary>
        /// <param name="content_control">SdtElement instance represent the content control</param>
        public Helper_WordBarChart(SdtElement content_control)
        {
            rotated_table = StaticValues.barchart_rotatedtable;
            if (!Init(content_control))
            {
                return;
            }

            // Get the first ChartPart instance found in the content control
            XElement chart_content_control = Helper_WordBase.GetContentControlByTag(Helper_WordBase.GetContentControlXMLBySdtElement(content_control), StaticValues.content_cc_name);
            string   chart_id = (string)chart_content_control.Descendants(StaticValues.c + "chart").Attributes(StaticValues.r + "id").FirstOrDefault <XAttribute>().Value;

            chart_part = (ChartPart)WordTemplateManager.document.MainDocumentPart.GetPartById(chart_id);

            if (chart_part == null)
            {
                StaticValues.logs += "[Error][WordChart]Can't initialize required data using the input content control" + Environment.NewLine;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// The function used to initialize the required data
        /// </summary>
        /// <param name="content_control">SdtElement instance for the content control</param>
        public virtual bool Init(SdtElement content_control)
        {
            this.content_control = content_control;
            if (this.content_control == null)
            {
                StaticValues.logs += "[Error]Can't get the content control" + Environment.NewLine;
                return(false);
            }

            // Get the SQL statement in the nested content control
            XElement sql_content_control = Helper_WordBase.GetContentControlByTag(Helper_WordBase.GetContentControlXMLBySdtElement(content_control), StaticValues.sql_cc_name);

            if (sql_content_control != null)
            {
                sql_query = sql_content_control.Value.ToString();
            }

            if (sql_query != null && !StaticValues.use_default_tablename_in_sql)
            {
                sql_query = sql_query.Replace(StaticValues.dummy_table_name, StaticValues.project_table_name);
            }

            return(true);
        }
        public void Execute()
        {
            SdtElement repeat_sql_cc = content_control.Descendants <SdtElement>().Where(s => s.Descendants <SdtAlias>().FirstOrDefault().Val.ToString().ToLower() == StaticValues.repeatsql_cc_name).FirstOrDefault();

            sql_query = GetContentControlContents(repeat_sql_cc);
            if (!StaticValues.use_default_tablename_in_sql)
            {
                sql_query = sql_query.Replace(StaticValues.dummy_table_name, StaticValues.project_table_name);
            }
            DataTable result = GetDataFromDatabaseUsingSQL();

            for (int i = 0; i < result.Rows.Count - 1; i++)
            {
                CloneData();
            }

            List <string> columnNames = (from dc in result.Columns.Cast <DataColumn>()
                                         select dc.ColumnName.ToLower()).ToList();

            WordTemplateManager sub_manager;
            int row_count = 0;

            foreach (SdtElement repeated_cc in content_control.Descendants <SdtElement>().Where(s => s.Descendants <SdtAlias>().FirstOrDefault().Val.ToString().ToLower() == StaticValues.repeatcontent_cc_name))
            {
                foreach (SdtElement nested_cc in repeated_cc.Descendants <SdtElement>())
                {
                    SdtAlias alias = nested_cc.Descendants <SdtAlias>().FirstOrDefault();
                    if (alias.Val.ToString().ToLower() == StaticValues.repeatval_cc_name)
                    {
                        string sql_content = GetContentControlContents(nested_cc);

                        string append_data = "";
                        // If the content sql specify the data from the above sql
                        if (columnNames.Contains(sql_content.ToLower()))
                        {
                            append_data = result.Rows[row_count][sql_content.ToLower()].ToString();
                        }
                        else
                        {
                            // If the content sql specify a query
                            if (!StaticValues.use_default_tablename_in_sql)
                            {
                                sql_content = sql_content.Replace(StaticValues.dummy_table_name, StaticValues.project_table_name);
                            }

                            DataRow conditional = result.Rows[row_count];
                            sql_content = Helper_WordBase.AddCondtionalToSQLQuery(sql_content, conditional);

                            DataTable selectvalue_result = GetDataFromDatabaseUsingSQL(sql_content);
                            if (selectvalue_result != null)
                            {
                                // TODO: Print out more values if possible
                                // Only print the 1st value found
                                append_data = selectvalue_result.Rows[0][0].ToString();
                            }
                        }
                        ReplaceContentsInContentControl(nested_cc, append_data);
                    }
                    else
                    {
                        DataRow conditional = result.Rows[row_count];
                        sub_manager = new WordTemplateManager(nested_cc);
                        sub_manager.Process(conditional);
                    }
                }
                row_count++;
            }
        }