|
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using AndrewGolik.DaoTraining.TransferObjects;
using AndrewGolik.DaoTraining.Common.Utilities;
namespace AndrewGolik.DaoTraining.DataAccessLayer.XmlRepository
{
public class XmlOrderDao:OrderDao
{
private Object thisLock = new Object();
private bool IsCustomerExist(int customerID)
{
XmlDocument xmlCustomers = new XmlDocument();
xmlCustomers.Load(XmlDaoFactory.PathToCustomersFile);
if (xmlCustomers.DocumentElement.SelectSingleNode("Customer[CustomerID="+customerID+"]") != null)
return true;
else
return false;
}
public int CreateOrder(Order order)
{
lock (thisLock)
{
if (!IsCustomerExist(order.CustomerID))
throw new ArgumentException("The customerID is invalid.");
XmlDocument xmlOrders = new XmlDocument();
xmlOrders.Load(XmlDaoFactory.PathToOrdersFile);
//generate the new id for the order
XmlNode xmlLastGeneratedId = xmlOrders.DocumentElement.Attributes.GetNamedItem("LastGeneratedId");
int lastGeneratedIdValue = Convert.ToInt32(xmlLastGeneratedId.Value) + 1;
xmlLastGeneratedId.InnerText = lastGeneratedIdValue.ToString();
//assign the new generated id to the order then serialize it to xml
order.OrderID = lastGeneratedIdValue;
XmlDocument xmlOrder = Serialization.FillXmlDocFromObject<Order>(order);
//import new order xml to orders list
xmlOrders.DocumentElement.AppendChild(xmlOrders.ImportNode(xmlOrder.DocumentElement, true));
xmlOrders.Save(XmlDaoFactory.PathToOrdersFile);
return lastGeneratedIdValue;
}
}
public bool DeleteOrder(int orderID)
{
lock (thisLock)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(XmlDaoFactory.PathToOrdersFile);
XmlNode xmlOrderNode = xmlDoc.DocumentElement.SelectSingleNode("Order[OrderID = " + orderID + "]");
if (xmlOrderNode != null)
{
xmlOrderNode.ParentNode.RemoveChild(xmlOrderNode);
xmlDoc.Save(XmlDaoFactory.PathToOrdersFile);
return true;
}
else
return false;
}
}
public bool UpdateOrder(Order order)
{
lock (thisLock)
{ //first check if customer id passed exists
if(!IsCustomerExist(order.CustomerID))
throw new ArgumentException("The customerID is invalid.");
if (DeleteOrder(order.OrderID))
{
XmlDocument xmlOrder = Serialization.FillXmlDocFromObject<Order>(order);
//import order xml updated to orders list
XmlDocument xmlOrders = new XmlDocument();
xmlOrders.Load(XmlDaoFactory.PathToOrdersFile);
xmlOrders.DocumentElement.AppendChild(xmlOrders.ImportNode(xmlOrder.DocumentElement, true));
xmlOrders.Save(XmlDaoFactory.PathToOrdersFile);
return true;
}
else
return false;
}
}
public List<Order> GetOrders()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(XmlDaoFactory.PathToOrdersFile);
XmlNodeList xmlOrderList = xmlDoc.DocumentElement.SelectNodes("Order");
List<Order> orders = new List<Order>();
for (int index = 0; index < xmlOrderList.Count; index++)
{
XmlDocument xmlOrder = new XmlDocument();
xmlOrder.LoadXml(xmlOrderList[index].OuterXml);
Order order = Serialization.FillObjectFromXMLDoc<Order>(xmlOrder);
orders.Add(order);
}
return orders;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Andrew Golik is a software professional working in Minsk, Belarus.
He enjoys design infrastructures based on object oriented paradigm. His programming experience includes ASP, ASP.NET, .NET, COM, JAVA, PHP, DHTML, AJAX, blah blah blah....
Andrew Golik's Blog