using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Xml.Serialization;
using Vrh.XmlProcessing;
namespace Vrh.OneMessage
{
///
/// OneMessage üzenetküldő paraméterező XML fájl leképezése.
///
public sealed class OneMessageXmlParser : XmlParser
{
private const string ELEMENT_NOTFOUND = "Element \"{0}\" not found where \"{1}\" attribute equals \"{2}\"!";
#region Private variables
private readonly XElement m_MessageXE = null;
#endregion Private variables
#region ElementNames private classes
private new class ElementNames : XmlLinqBase.ElementNames
{
///
/// 'Attachment' elem név.
///
public const string ATTACHMENT = "Attachment";
///
/// 'Attachments' elem név.
///
public const string ATTACHMENTS = "Attachments";
///
/// 'DeliveryDefinition' elem név.
///
public const string DELIVERYDEFINITION = "DeliveryDefinition";
///
/// 'DeliveryDefinitions' elem név.
///
public const string DELIVERYDEFINITIONS = "DeliveryDefinitions";
///
/// 'MessageBody' elem név.
///
public const string MESSAGEBODY = "MessageBody";
///
/// 'OneMessage' elem név.
///
public const string ONEMESSAGE = "OneMessage";
///
/// 'OneMessageDefinitions' elem név.
///
public const string ONEMESSAGEDEFINITIONS = "OneMessageDefinitions";
///
/// 'SendTo' elem név.
///
public const string SENDTO = "SendTo";
///
/// 'Subject' elem név.
///
public const string SUBJECT = "Subject";
}
#endregion ElementNames private classes
#region AttributeNames static class
///
/// XML fájlokban ilyen attribútum nevek találhatóak egy MenuItem-ben.
///
public static class AttributeNames
{
///
/// 'DeleteAfterSend' attribútum név.
///
public const string DELETEAFTERSEND = "DeleteAfterSend";
///
/// 'Delivery' attribútum név.
///
public const string DELIVERY = "Delivery";
///
/// 'Folder' attribútum név.
///
public const string FOLDER = "Folder";
///
/// 'Id' attribútum név.
///
public const string ID = "Id";
///
/// 'LCID' attribútum név.
///
public const string LCID = "LCID";
///
/// 'Name' attribútum név.
///
public const string NAME = "Name";
}
#endregion AttributeNames static class
#region Properties
///
/// A már beazonosított (hozzárendelt) szállítási definició.
///
public DeliveryDefinition Delivery { get; private set; }
#region SendTo property
///
/// Címzettek vesszővel elválasztott listája.
///
public string SendTo
{
get
{
if (m_SendTo == null)
{
m_SendTo = GetValue(m_MessageXE.Element(ElementNames.SENDTO), "");
}
return m_SendTo;
}
}
private string m_SendTo = null;
#endregion SendTo property
#region Subject property
///
/// A levél tárgya.
///
public string Subject
{
get
{
if (m_Subject == null)
{
m_Subject = GetValue(m_MessageXE.Element(ElementNames.SUBJECT), "");
}
return m_Subject;
}
}
private string m_Subject = null;
#endregion Subject property
#region MessageBody property
///
/// Az üzenet tartalma.
///
public string MessageBody
{
get
{
if (m_MessageBody == null)
{
m_MessageBody = GetValue(m_MessageXE.Element(ElementNames.MESSAGEBODY), "");
}
return m_MessageBody;
}
}
private string m_MessageBody = null;
#endregion MessageBody property
#region Attachments property
///
/// A paraméterező fájlban megadott csatolmányok.
///
public List Attachments
{
get
{
if (m_Attachments == null)
{
m_Attachments = new List();
XElement xe = m_MessageXE.Element(ElementNames.ATTACHMENTS);
if (xe != null)
{
string folder = GetValue(AttributeNames.FOLDER, xe, "");
m_IsDeleteAfterSend = GetValue(AttributeNames.DELETEAFTERSEND, xe, false);
m_IsInitializedDeleteAfterSend = true;
IEnumerable attachmentsXE = xe.Elements(ElementNames.ATTACHMENT);
if (attachmentsXE != null && attachmentsXE.Any())
{
foreach (XElement attXE in attachmentsXE)
{
var ae = new AttachmentElement(attXE, folder);
if (!string.IsNullOrWhiteSpace(ae.File))
{ // Ha van érték is File tulajdonságokban, csak akkor adjuk hozzá
m_Attachments.Add(ae);
}
}
}
}
}
return m_Attachments;
}
}
private List m_Attachments = null;
#endregion Attachments property
#region IsDeleteAfterSend property
///
/// A csatolmányokat kell-e törölni a küldés után.
///
public bool IsDeleteAfterSend
{
get
{
if (!m_IsInitializedDeleteAfterSend)
{
XElement xe = m_MessageXE.Element(ElementNames.ATTACHMENTS);
if (xe != null)
{
m_IsDeleteAfterSend = GetValue(AttributeNames.DELETEAFTERSEND, xe, false);
}
m_IsInitializedDeleteAfterSend = true;
}
return m_IsDeleteAfterSend;
}
}
private bool m_IsDeleteAfterSend = false;
private bool m_IsInitializedDeleteAfterSend = false;
#endregion IsDeleteAfterSend property
#endregion Properties
#region Constructor
///
/// XML paraméterező fájl feldolgozásának konstruktora.
///
/// XmlParser kapcsolati sztring (connection string).
/// Az alkalmazás alap mappája. Ez alatt van az App_Data.
/// A környezetben érvényes nyelvi kód. Ha üres, akkor az XmlParser-ben beállított lesz érvényes.
/// A környezetben az XmlParser részére létrehozott változók.
///
///
public OneMessageXmlParser(
XmlConnection xmlc,
string appPath,
string lcid,
Dictionary otherVars,
string messageId,
string delivery = null)
: base(xmlc, appPath, lcid, otherVars)
{
try
{
#region Üzenetdefiníciós XElement megkeresése és megjegyzése
XElement defXE = this.RootElement.Element(ElementNames.ONEMESSAGEDEFINITIONS);
if (defXE == null)
{
throw new ApplicationException(string.Format(Messages.ERR_MISSINGELEMENT, ElementNames.ONEMESSAGEDEFINITIONS));
}
IEnumerable messagesXE = defXE.Elements(ElementNames.ONEMESSAGE);
if (messagesXE == null || !messagesXE.Any())
{
throw new ApplicationException(string.Format(Messages.ERR_MISSINGELEMENT, ElementNames.ONEMESSAGE));
}
foreach (XElement messXE in messagesXE)
{
string messId = GetValue(AttributeNames.ID, messXE, "", true, true);
string messLCID = GetValue(AttributeNames.LCID, messXE, "");
if (messId == messageId && (string.IsNullOrWhiteSpace(messLCID) || messLCID == this.LCID))
{
m_MessageXE = messXE;
}
}
if (m_MessageXE == null)
{
throw new ApplicationException(string.Format(ELEMENT_NOTFOUND, ElementNames.ONEMESSAGE, AttributeNames.ID, messageId));
}
#endregion Üzenetdefiníciós XElement megkeresése és megjegyzése
#region Delivery megkeresése és beállítása
string deliveryName = delivery;
if (string.IsNullOrWhiteSpace(deliveryName))
{
deliveryName = GetValue(AttributeNames.DELIVERY, m_MessageXE, "", true, true);
}
XElement deliveryDefsXE = this.RootElement.Element(ElementNames.DELIVERYDEFINITIONS);
if (deliveryDefsXE == null)
{
throw new ApplicationException(string.Format(Messages.ERR_MISSINGELEMENT, ElementNames.DELIVERYDEFINITIONS));
}
IEnumerable deliveriesXE = deliveryDefsXE.Elements(ElementNames.DELIVERYDEFINITION);
if (deliveriesXE == null || !deliveriesXE.Any())
{
throw new ApplicationException(string.Format(Messages.ERR_MISSINGELEMENT, ElementNames.DELIVERYDEFINITION));
}
foreach (XElement delXE in deliveriesXE)
{
string delName = GetValue(AttributeNames.NAME, delXE, "", true, true);
if (delName == deliveryName)
{
this.Delivery = new DeliveryDefinition(delXE);
}
}
if (this.Delivery == null)
{
throw new ApplicationException(string.Format(ELEMENT_NOTFOUND, ElementNames.DELIVERYDEFINITION, AttributeNames.NAME, deliveryName));
}
#endregion Delivery megkeresése és beállítása
}
catch (Exception ex)
{
throw new ApplicationException("Error occured when OneMessageXmlParser initialization!", ex);
}
}
#endregion Constructor
}
}