DataTplIfStartsWith.cs
//
// This code is part of Document Solutions for Word demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using GrapeCity.Documents.Word;

namespace DsWordWeb.Demos
{
    // This example demonstrates the use of 'if/else' construct in report templates
    // to filter data source on a condition (that a certain field starts with the letter 'S').
    public class DataTplIfStartsWith
    {
        public GcWordDocument CreateDocx()
        {
            // The data source (ocean and sea data from Wikipedia):
            var oceans = new[]
            {
                new { name = "Pacific", areaOfWorldOcean = 0.466, volumeOfWorldOcean = 0.501, seas = new[]
                    { new {name = "Australasian Mediterranean Sea" }, new { name = "Philippine Sea" }, new { name = "Coral Sea" }, new { name = "South China Sea"}  }
                },
                new { name = "Atlantic", areaOfWorldOcean = 0.235, volumeOfWorldOcean = 0.233, seas = new[]
                    { new {name = "Sargasso Sea" }, new { name = "Caribbean Sea" }, new { name = "Mediterranean Sea" }, new { name = "Gulf of Guinea" } }
                },
                new { name = "Indian", areaOfWorldOcean = 0.195, volumeOfWorldOcean = 0.198, seas = new[]
                    { new {name = "Arabian Sea" }, new { name = "Bay of Bengal" }, new { name = "Andaman Sea" }, new { name = "Laccadive Sea" } }
                },
                new { name = "Southern", areaOfWorldOcean = 0.061, volumeOfWorldOcean = 0.054, seas = new[]
                    { new {name = "Weddell Sea" }, new { name = "Somov Sea" }, new { name = "Riiser-Larsen Sea" }, new { name = "Lazarev Sea" } }
                },
                new { name = "Arctic", areaOfWorldOcean = 0.043, volumeOfWorldOcean = 0.014, seas = new[]
                    { new {name = "Barents Sea" }, new { name = "Greenland Sea" }, new { name = "East Siberian Sea" }, new { name = "Kara Sea" } }
                },
            };

            var doc = new GcWordDocument();

            // Add the data source:
            doc.DataTemplate.DataSources.Add("ds", oceans);

            // Add a list template so that the data is formatted as a list:
            var myListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "myListTemplate");

            // Filter output using if/then/else - print only names starting with an 's':
            var p = doc.Body.Paragraphs.Add("{{if StartsWith(UCase(ds.seas.name),\"S\")}}{{ds.seas.name}}{{endif}}", doc.Styles[BuiltInStyleId.ListParagraph]);
            p.ListFormat.Template = myListTemplate;

            // Expand the templates:
            doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));

            // Add a short note describing the demo at the top of the document:
            doc.Body.Paragraphs.Insert(
                "This example demonstrates the use of 'if/else' construct in report templates " +
                "to filter data source on a condition (that a certain field starts with the letter 'S')." +
                "Please see this sample source code for full details.",
                InsertLocation.Start);
            doc.Body.Paragraphs.Insert("Report templates: if data starts with 'S'", doc.Styles[BuiltInStyleId.Heading1], InsertLocation.Start);

            // Done:
            return doc;
        }
    }
}