/ThinkEmailFomatter/Models/BusinessObjects/ThinkStreamToObject.cs
C# | 132 lines | 96 code | 19 blank | 17 comment | 18 complexity | 07debde784bc6b1710f2d5c3062c61f6 MD5 | raw file
Possible License(s): BSD-3-Clause
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.IO;
- using System.Xml;
- using System.Xml.Linq;
- using ThinkEmailFormatter.Models;
- namespace ThinkEmailFormatter.Models
- {
- /// <summary>
- /// Convert the HTTP-POSTed XML stream coming from THINK into
- /// a Business object
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public class ThinkStreamToObject<T> where T: new()
- {
- #region[ MEMBERS ]
- public readonly XElement _xmlElement;
-
- #endregion
- #region [ CONSTRUCTORS ]
- public ThinkStreamToObject(Stream xmlStream)
- {
- XmlDocument doc = new XmlDocument();
- doc.Load(xmlStream);
- _xmlElement = XElement.Load(new XmlNodeReader(doc));
- }
- public ThinkStreamToObject(XElement xmlElement)
- {
- _xmlElement = xmlElement;
- }
- #endregion
- #region [ METHODS ]
- /// <summary>
- /// Convert XML stream. So far that convertion can, at least,
- /// convert the XML related to the following THINK events:
- /// - Order Item Added
- /// - Customer Added
- /// - Order Renewed
- /// - Password Changed
- /// </summary>
- /// <returns></returns>
- public T Convert()
- {
- if (_xmlElement == null)
- {
- return default(T);
- }
- else
- {
- T convertedObj = new T();
- var properties = convertedObj.GetType().GetProperties();
- Type stringType = typeof(string);
- foreach (var property in properties)
- {
- dynamic propertyValue = null;
- // if this is a string, it means this is an attribute of node convertedObj.XmlElementName
- // Gets its value from the xmlStream
- if (property.PropertyType == stringType)
- {
- // TODO: FIx this horrible "td_email_recipient" hard-coded value
- if (property.Name == "td_email_recipient")
- {
- var emailAddress = _xmlElement.Element("td_email_recipient").FirstNode;
- propertyValue = emailAddress == null ? string.Empty : emailAddress.ToString();
- }
- else
- {
- var attrValue = _xmlElement.Attribute(property.Name);
- if (attrValue != null)
- propertyValue = (string)attrValue;
- }
- }
- else if (property.PropertyType.IsGenericType) // This is a children of node convertedObj.XmlElementName, and there may be more than one
- {
- var childrenElements = _xmlElement.Elements(property.Name);
- propertyValue = Activator.CreateInstance(property.PropertyType);
- foreach (XElement xelement in childrenElements)
- {
- if (xelement != null)
- {
- dynamic o = CreateInstance(property.PropertyType.GetGenericArguments()[0], xelement);
- propertyValue.Add(o.Convert());
- }
- }
- }
- else // This is a children of node convertedObj.XmlElementName
- {
- XElement childrenElement = _xmlElement.Elements(property.Name).FirstOrDefault();
- if (childrenElement != null)
- {
- dynamic o = CreateInstance(property.PropertyType, childrenElement);
- propertyValue = o.Convert();
- }
- }
- if (propertyValue != null)
- property.SetValue(convertedObj, propertyValue, null);
- }
- return convertedObj;
- }
- }
- private dynamic CreateInstance(Type genericType, XElement arg )
- {
- Type d1 = typeof(ThinkStreamToObject<>);
- Type[] typeArgs = { genericType };
- Type constructed = d1.MakeGenericType(typeArgs);
- object[] args = { arg };
- dynamic o = Activator.CreateInstance(constructed, args);
- return o;
- }
- #endregion
- }
- }