DataTplUseCases.cs
//
// This code is part of GrapeCity Documents for Word samples.
// Copyright (c) GrapeCity, Inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Linq;
using GrapeCity.Documents.Word;

namespace GcWordWeb.Samples
{
    // This data template sample loads a DOCX that was created in MS Word,
    // and contains a table with data template tags. It then creates a DataSet,
    // loads the GcNwind.xml data base into it, and builds a product list
    // from tables in that data set. The product list is set as the template
    // data source on the document, and the template is processed to build
    // the final document with actual data.
    public class DataTplUseCases
    {
        public GcWordDocument CreateDocx(string[] sampleParams)
        {
            var doc = new GcWordDocument();

            // Load the template DOCX:
            doc.Load(Path.Combine("Resources", "WordDocs", sampleParams[3]));

            using (var ds = new DataSet())
            {
                // Load the data set:
                ds.ReadXml(Path.Combine("Resources", "data", "GcWordTplDataSet.xml"));

                // Return the unmodified template document for reference:
                if (sampleParams.Count() >= 6 && sampleParams[5] == "template")
                    return doc;

                    // Add the data source to the data template data sources:
                if (sampleParams.Count() >= 5 && !string.IsNullOrEmpty(sampleParams[4]))
                    doc.DataTemplate.DataSources.Add("ds", ds.Tables[sampleParams[4]]);
                else
                    doc.DataTemplate.DataSources.Add("ds", ds);

                // The document already has all the necessary bindings,
                // so we only need to process the data template:
                doc.DataTemplate.Process(System.Globalization.CultureInfo.GetCultureInfo("en-US"));
            }

            // Done:
            return doc;
        }

        public GcWordDocument CreateDocx(int parsIdx = 0)
        {
            return CreateDocx(GetSampleParamsList()[parsIdx]);
        }

        public static List<string[]> GetSampleParamsList()
        {
            // Mandatory: name, description, info
            // Custom: template DOCX [[, table] , "template"]
            return new List<string[]>()
            {
                new string[] { "@use-data-tpl/Lease Agreement", "Generate a lease agreement", null,
                    "Lease_Agreement_Template.docx", "LeaseAgreement" },
                new string[] { "@use-data-tpl/Consulting Agreement", "Generate a consultancy agreement", null,
                    "Consulting_Agreement_Template.docx", "ConsAgreement" },
                new string[] { "@use-data-tpl/Rental Agreement", "Generate a house rental agreement", null,
                    "House_Rental_Template.docx", "HouseRentalAgreement" },
                new string[] { "@use-data-tpl/Employment Contract", "Generate an employment contract", null,
                    "Employment_Contract_Template.docx", "EmploymentContract" },

                // Parameter sets below simply return the template used to produce the bound document in corresponding
                // data template samples:
                new string[] { "@use-data-tpl-src/Lease Agreement", "Template Lease_Agreement_Template.docx", null,
                    "Lease_Agreement_Template.docx", null, "template" },
                new string[] { "@use-data-tpl-src/Consulting Agreement", "Template Consulting_Agreement_Template.docx", null,
                    "Consulting_Agreement_Template.docx", null, "template" },
                new string[] { "@use-data-tpl-src/Rental Agreement", "Template House_Rental_Template.docx", null,
                    "House_Rental_Template.docx", null, "template" },
                new string[] { "@use-data-tpl-src/Employment Contract", "Template Employment_Contract_Template.docx", null,
                    "Employment_Contract_Template.docx", null, "template" },
                new string[] { "@use-data-tpl-src/Order Invoice", "Template InvoiceTemplate.docx", null,
                    "InvoiceTemplate.docx", null, "template" },
            };
        }
    }
}