Webmasters, gagnez de l'argent en affichant des bannières contextuelles Oxado
SourceForge.net Logo
Support This Project
   LeCouteauSuisse Project
Welcome ! [ Home ]  ·  [ Downloads ]  ·  [ Forums ]  ·  [ News ]  ·  [ Documentations ]  
[ Home ]

[ Documentations ]

Create Order Receiver


Create the receiver class

The receiver will receive an XMLEvent class for each order. We use the 'Node' propertie to have XML information on order. It will looks like :

<order>
	<reference>C2132</reference>
	<name>Brasil Coffee</name>
	<quantity>4</quantity>
	<price>1.2</price>
</order>
We will use an XSL transformation to produce the body of the mail to send. The XSL transformation is saved in a file and read from the receiver configuration. We want use the code from the MailReceiver component to configure and send mail.
We want to produce this kind of HTML presentation :
Order detail :
  • Product reference : C2132
  • Product name : Brasil Coffee
  • Quantity : 4
  • Unit price : 1.2

We describe only code specific for XSL transformation, the code to send mail will be retreive from the MailReceiver class. Follow this steps :
  1. Add a new class named 'OrderReceiver'
  2. Add namespace System.Net.Mail, System.Xml, System.Xml.Xsl, LeCouteauSuisse.Components and LeCouteauSuisse.API with the using directive
  3. Change class to public
  4. This class must derived from 'BaseReceiver'
  5. Add private fields from mail receiver.
  6. Copy the method Initialize from mail receiver.
  7. Copy the method Run from mail receiver.
  8. Remove bodyFormat and bodyArguments fields and lines referencing these fileds in Initialize method
  9. Add a private string field named xslBodyTransformation.
  10. Change the method Initialize. In this method we will read configuration from the .config file in the <settings> section of the receiver. We want to add the XSL file name with a parameter named 'xslBodyTransformation'. To read configuration we used 'context.Initialization.GetAttribute' method.
  11. Override the method Run. In this method we will check if we receive an XMLEvent, format the 'Node' property and use the result for the body of the mail.
This class will looks like :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LeCouteauSuisse.API;
using LeCouteauSuisse.Components;
using System.Net.Mail;
using System.Xml;
using System.Xml.Xsl;

namespace CoffeeSupplyChainLibrary
{
    public class OrderReceiver : BaseReceiver
    {
        const int smtpDefaultPort = 25;
        string toFormat, toArguments, fromFormat, fromArguments;
        string subjectFormat, subjectArguments;
        string server;
        int port;

        string xslBodyTransformation;

        public override void Initialize(ReceiverContext ctx)
        {
            string val;

            base.Initialize(ctx);

            server = context.Initialization.GetAttribute("smtpServer");
            val = context.Initialization.GetAttribute("smtpPort");
            if (!int.TryParse(val, out port))
                port = smtpDefaultPort;

            server = context.Initialization.GetAttribute("smtpServer");
            toFormat = context.Initialization.GetAttribute("toFormat");
            toArguments = context.Initialization.GetAttribute("toArguments");
            fromFormat = context.Initialization.GetAttribute("fromFormat");
            fromArguments = context.Initialization.GetAttribute("fromArguments");
            subjectFormat = context.Initialization.GetAttribute("subjectFormat");
            subjectArguments = context.Initialization.GetAttribute("subjectArguments");

            xslBodyTransformation = context.Initialization.GetAttribute("xslBodyTransformation");

            context.Logger.Write(LoggerLevel.Information, "OrderReceiver " + context.Name + " initialized");
        }

        public override void Run(GeneratorEvent generatorEvent, EventContext eventContext)
        {
            string from, to, subject, body;
            MailMessage message;
            SmtpClient smtp;
            XMLEvent xmlEvent;
            XslCompiledTransform transform;
            XmlWriter writer;
            XmlWriterSettings settings;
            StringBuilder bodyBuilder;

            // check if we have an XMLEvent
            if (generatorEvent.GetType() == typeof(XMLEvent))
            {
                xmlEvent = (XMLEvent)generatorEvent;

                from = FormatArguments.Format(fromFormat, fromArguments, generatorEvent, eventContext, context);
                to = FormatArguments.Format(toFormat, toArguments, generatorEvent, eventContext, context);
                subject = FormatArguments.Format(subjectFormat, subjectArguments, generatorEvent, eventContext, context);

                // load the XSL file
                transform = new XslCompiledTransform();
                transform.Load(xslBodyTransformation);

                // prepare string to receive result of transformation
                bodyBuilder = new StringBuilder();
                settings = new XmlWriterSettings();
                settings.ConformanceLevel = ConformanceLevel.Auto;
                writer = XmlWriter.Create(bodyBuilder, settings);

                // transform the 'Node' property and get result
                transform.Transform(xmlEvent.Node, writer);
                body = bodyBuilder.ToString();

                message = new MailMessage(from, to, subject, body);
                message.IsBodyHtml = true;
                if (string.IsNullOrEmpty(server))
                    smtp = new SmtpClient();
                else
                    smtp = new SmtpClient(server, port);
                smtp.Send(message);
                context.Logger.Write(LoggerLevel.Information, "OrderReceiver " + context.Name + " mail sent");
            }
        }
    }
}

Create the Order.xslt

This file will format a Order node. Follow this steps :

  1. Add a new item named 'Order.xslt' of type 'Data' - 'XSLT File' in your project.
  2. Put this content :
    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    	<xsl:output method="html" indent="yes"/>
    	<xsl:template match="* | node()">
    		<html>
    			<body>
    				<b>
    					Order detail :
    					<ul>
    						<li>
    							Product reference : <xsl:value-of select="reference"/>
    						</li>
    						<li>
    							Product name : <xsl:value-of select="name"/>
    						</li>
    						<li>
    							Quantity : <xsl:value-of select="quantity"/>
    						</li>
    						<li>
    							Unit price : <xsl:value-of select="price"/>
    						</li>
    					</ul>
    				</b>
    			</body>
    		</html>
    	</xsl:template>
    </xsl:stylesheet>
    


LeCouteauSuisse