using System;

using System.Data;

using System.Data.OleDb;

using System.IO;

using System.Xml;

using System.Xml.XPath;

using System.Xml.Xsl;

using System.Text.RegularExpressions;

using System.Collections;

using System.Net;

 

namespace Deliverable3

{

      class Deliverable3

      {

            static System.Data.OleDb.OleDbConnection myConnection;

            static System.Data.OleDb.OleDbDataAdapter adapter;

            static System.Data.DataSet ds;

     

            [STAThread]

            static void Main(string[] args)

            {

                  Deliverable3 d3 = new Deliverable3();

                  if(args.Length > 0)

                  {

                        if(args[0] == "XML")

                        {

                              d3.generateXML();

                        }

                        else if(args[0] == "HTML")

                        {

                              if(args.Length != 2)

                              {

                                    Console.WriteLine("You need to enter in a color.");

                                    Console.WriteLine("Acceptable values are: red, yellow, pink, white, blue, or index");

                                    return;

                              }

                              else if(args[1].Equals("all"))

                              {

                                    d3.makeHTML("index");

                                    d3.makeHTML("red");

                                    d3.makeHTML("yellow");

                                    d3.makeHTML("pink");

                                    d3.makeHTML("blue");

                                    d3.makeHTML("white");

                                    Console.WriteLine("HTML pages for all of the colors + index are complete!");

                              }

                              else

                              {

                                    d3.makeHTML(args[1]);

                                    Console.WriteLine("HTML page for " + args[1] + " is complete!");

                              }

                        }

                        else if(args[0].Equals("DB"))

                        {

                              d3.insertToAccess();

                        }

                        else

                        {

                              Console.WriteLine("You have input an incorrect set of values");

                              Console.WriteLine("The format is <XML|HTML|DB>");

                        }

                  }

                  else

                  {

                        Console.WriteLine("You have input an incorrect set of values");

                        Console.WriteLine("The format is <XML|HTML|DB>");

                  }

            }

 

            public static void flower(XmlTextWriter w, string name, string id, string description, string type, string shipmentID, string vendor)

            {

                  Regex regularExpression;

                  Match m;

                  regularExpression = new Regex("(pink|orange|yellow|white|red|peach|raspberry|scarlet|blue|purple|violet)", RegexOptions.IgnoreCase);

                  m = regularExpression.Match(description);

 

                  Hashtable colorTable = new Hashtable();

                  colorTable.Add("pink", "pink");

                  colorTable.Add("orange", "yellow");

                  colorTable.Add("yellow", "yellow");

                  colorTable.Add("white", "white");

                  colorTable.Add("red", "red");

                  colorTable.Add("peach", "pink");

                  colorTable.Add("raspberry", "red");

                  colorTable.Add("scarlet", "red");

                  colorTable.Add("blue", "blue");

                  colorTable.Add("purple", "blue");

                  colorTable.Add("violet", "blue");

 

                  string flowerColor = m.Value.ToLower();

                  string flowerColorXML = (string)colorTable[flowerColor];

 

                  w.WriteStartElement("flower");

                  w.WriteAttributeString("type", type);

                  w.WriteAttributeString("color", flowerColorXML);

                  w.WriteAttributeString("shipmentID", shipmentID);

                  w.WriteElementString("vendor", vendor);

                  w.WriteElementString("name", name);

                  w.WriteElementString("id", id);

                  w.WriteElementString("description", description);

                  w.WriteEndElement();

            }

 

            /// <summary>

            /// This will generate the HTML output for the pages based on the color that is input

            /// </summary>

            public void makeHTML(string colorOfPage)

            {

                  string color = colorOfPage;

                  string outputHTML = color + ".html";

                  string stylesheet;

                  string filename;

 

                  if(color == "index")

                  {

                        stylesheet = "index.xsl";

                        filename = "StoreInfo.xml";

                  }

                  else if(color.Equals("red") || color.Equals("pink") || color.Equals("yellow") || color.Equals("white") || color.Equals("blue"))

                  {

                        stylesheet = "colors.xsl";

                        filename = "Flowers_d3.xml";

                  }

                  else

                  {

                        Console.WriteLine("You have input an invalid color");

                        Console.WriteLine("Valid colors are: red, pink, yellow, white, or index");

                        return;

                  }

 

                  XsltArgumentList xslArg = new XsltArgumentList();

                  xslArg.AddParam("Color","",color);

                 

                  XslTransform xslt = new XslTransform();

                  xslt.Load(stylesheet);

 

                  XPathDocument xpathdocument = new XPathDocument(filename);

 

                  XmlTextWriter writer = new XmlTextWriter(outputHTML,null);

                 

                  xslt.Transform(xpathdocument, xslArg, writer);

                  writer.Close();

            }

 

            /// <summary>

            /// This method will combine two XML files and generate a single output

            /// </summary>

            public void generateXML()

            {

           

                  try

                  {

                 

                        // Establish the writer

                        XmlTextWriter xwriter = new XmlTextWriter("Flowers_d3.xml", null);

                        xwriter.Formatting = Formatting.Indented;

                        xwriter.WriteStartDocument();

                        xwriter.WriteStartElement("FlowerStock");

                 

                        Deliverable3 d3XML = new Deliverable3();

                        d3XML.parseShipmentXML("shipment435.xml", "DahliaDescriptions.xml", "435-72", xwriter);

                        d3XML.parseShipmentXML("shipment435.xml", "BegoniaDescriptions.xml", "435-72", xwriter);

                        d3XML.parseShipmentXML("shipment435.xml", "ColeusDescriptions.xml", "435-72", xwriter);

                        d3XML.parseShipmentXML("Shipment588.xml", "VerbenaDescriptions.xml", "588-76", xwriter);

                        d3XML.parseShipmentXML("Shipment588.xml", "BellflowerDescriptions.xml", "588-76", xwriter);

                        d3XML.parseShipmentXML("Shipment588.xml", "DianthusDescriptions.xml", "588-76", xwriter);

 

                        // Close the writer

                        xwriter.WriteEndDocument();

                        xwriter.Close();

                  }

 

 

                  catch (IOException e)

                  {

                        Console.WriteLine(e);

                  }

            }

 

            public void parseShipmentXML(string shipmentFile, string flowerFile, string shipmentID, XmlTextWriter xwriter)

            {

                  // Get the input data

                  XmlDocument flowerXML = new XmlDocument();

                  XmlDocument shipmentXML = new XmlDocument();

                  flowerXML.Load(flowerFile);

                  shipmentXML.Load(shipmentFile);

 

                  // Collect target nodes

                  XmlNodeList shipmentList;

                  XmlElement flowerRoot = flowerXML.DocumentElement;

                  XmlElement shipmentRoot = shipmentXML.DocumentElement;

                  shipmentList = shipmentRoot.SelectNodes("//Plant");

 

                        // Process target nodes

                        foreach (XmlNode d in shipmentList)

                        {

                              // Get the vender from the ParentNode

                              string vendor = d.ParentNode.Attributes["Name"].InnerText;

                             

                              // Get the different elements within this node

                              string name = d.ChildNodes[0].InnerText;

                       

                              // Get the attribute data of node

                              XmlAttributeCollection ac;

                              ac = d.Attributes;

                       

                              // Get the type of the flower (e.g. rose)

                              string id = ac["ID"].InnerText;

                              string type = ac["Type"].InnerText;

 

                              // make description

                              string description = "No Description Provided";

 

                              XmlNode myNode = flowerRoot.SelectSingleNode("/FlowerMania/Descriptions/ID[@Num = '" + id + "']");

                              if(myNode != null)

                              {

                                    description = myNode["Text"].InnerText;

                                                     

                                    // Send information to employee method

                                    flower(xwriter, name, id, description, type, shipmentID, vendor);

                              }

                        }          

            }

 

            /// <summary>

            /// Insert the sales information into an Access Database

            /// </summary>

            public void insertToAccess()

            {

                  try

                  {

                        // Get the input from the XML document

                        //

                        // Read in the XML document

                        XmlDocument doc = new XmlDocument();

                        XmlDocument doc2 = new XmlDocument();

                        doc.Load("Sales1.xml");

                        doc2.Load("Sales2.xml");

 

                        // Collect target nodes from the XML document

                        XmlNodeList salesList;

                        XmlNodeList salesList2;

                        XmlElement root = doc.DocumentElement;

                        XmlElement root2 = doc2.DocumentElement;

                        salesList = root.SelectNodes("/Receipts/Sale");

                        salesList2 = root2.SelectNodes("/Receipts/Sale");

 

                        // Prepare the database

                        //      Delete old version of database table

                        //      Declare a new database table

                        //

                        // Open connection to database

                        myConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = deliverable3.mdb");

                        myConnection.Open();

     

                        // Delete existing Designers table

                        string deleteRyan = "DROP TABLE RyanPrins";

                        OleDbCommand dCommand = new OleDbCommand(deleteRyan);

                        dCommand.Connection = myConnection;

                        dCommand.ExecuteNonQuery();

 

                        // Create a new Designers table

                        //    Note: Command string formatted here for presentation convenience

                        string myNewTable = "CREATE TABLE RyanPrins (ItemID STRING, NumberSold INT, PriceOfItem CURRENCY)";

                        OleDbCommand myCommand = new OleDbCommand(myNewTable);

                        myCommand.Connection = myConnection;

                        myCommand.ExecuteNonQuery();

                       

                        // Create an adapter and select command

                        adapter = new System.Data.OleDb.OleDbDataAdapter("SELECT ItemID, NumberSold, PriceOfItem FROM RyanPrins", myConnection);

                        System.Data.OleDb.OleDbCommandBuilder cb = new

                        System.Data.OleDb.OleDbCommandBuilder(adapter);

                 

                        // Create and fill a dataset

                        ds = new DataSet();

                        adapter.Fill(ds, "SalesInfo");

 

                        // Process target nodes in XML node list

                        // Send XML content and DataSet to loadDesigner method

                        foreach (XmlNode d in salesList)

                        {

                              string itemID = d.ChildNodes[0].InnerText;

                              string numberSold = d.ChildNodes[1].InnerText;

                              string priceOfItem = d.ChildNodes[2].InnerText;

                             

                              // Send dataset and XML content to method for loading into dataset

                              loadDesigner(ds, itemID, numberSold, priceOfItem);

                        }    

                 

                        foreach (XmlNode d in salesList2)

                        {

                              // Get the attribute data of node

                              XmlAttributeCollection ac;

                              ac = d.Attributes;

                             

                              string itemID = ac["ItemID"].InnerText;

                              //string itemID = d.Attributes["ItemID"].InnerText;

                              string numberSold = d.ChildNodes[0].InnerText;

                              string priceOfItem = ac["PriceOfItem"].InnerText;

                             

                              // Send dataset and XML content to method for loading into dataset

                              loadDesigner(ds, itemID, numberSold, priceOfItem);

                        }

                  }

                  catch (IOException e)

                  {

                        Console.WriteLine(e);

                  }

     

                  // Update the database table

                  adapter.Update(ds, "SalesInfo");

                 

                  myConnection.Close();

            }

 

            public static void loadDesigner(System.Data.DataSet inDataSet, string itemID, string numberSold, string priceOfItem)

            {

                  // Create a new row in the Designers table

                  DataRow r = ds.Tables["SalesInfo"].NewRow();

                 

                  // Fill the new row

                  r["PriceOfItem"] = Convert.ToDecimal(priceOfItem);

                  r["ItemID"] = itemID;

                  r["NumberSold"] = numberSold;

                 

                  // Add the new row to the Designers table

                  inDataSet.Tables["SalesInfo"].Rows.Add(r);

 

            }

      }

 

}