using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Reflection; using System.Data.SqlClient; namespace XMLExtractor { public class Chart { public string id { get; set; } public int idCapitolo { get; set; } public int idSezione { get; set; } public int idOrdinamento { get; set; } public int idPagina { get; set; } public int decimali { get; set; } public bool percentuale { get; set; } public string asseY { get; set; } public string descrizione { get; set; } public string posizione { get; set; } public Dictionary items { get; set; } public List note { get; set; } public void writeDB(SqlConnection conn) { string sqlCommand = "(idREP_IMM_MON_Grafico, idREP_IMM_MON_Pagina, idTipologia, descrizione, asseY, decimali, percentuale, posizione, " + "nota1, nota2, nota3, nota4, nota5) VALUES (@idREP_IMM_MON_Grafico, @idREP_IMM_MON_Pagina, @idTipologia, @descrizione, " + "@asseY, @decimali, @percentuale, @posizione, @nota1, @nota2, @nota3, @nota4, @nota5)"; SqlCommand insertTo = new SqlCommand("INSERT INTO REP_IMM_MON_Grafici " + sqlCommand, conn); int primaryKey = Convert.ToInt32((new SqlCommand("SELECT count(*) from REP_IMM_MON_Grafici", conn)).ExecuteScalar()) + 1; insertTo.Parameters.AddWithValue("@idREP_IMM_MON_Grafico", primaryKey); insertTo.Parameters.AddWithValue("@idREP_IMM_MON_Pagina", this.idPagina); insertTo.Parameters.AddWithValue("@idTipologia", 1); insertTo.Parameters.AddWithValue("@descrizione", this.descrizione); insertTo.Parameters.AddWithValue("@asseY", this.asseY); insertTo.Parameters.AddWithValue("@decimali", this.decimali); insertTo.Parameters.AddWithValue("@percentuale", this.percentuale); insertTo.Parameters.AddWithValue("@posizione", this.posizione); for (int i = 1; i <= 5; i++) { if (this.note != null && (i - 1) < this.note.Count) insertTo.Parameters.AddWithValue("@nota" + i, note[i - 1].valore); else insertTo.Parameters.AddWithValue("@nota" + i, DBNull.Value); } int rowsUpdatedInserted = insertTo.ExecuteNonQuery(); if (rowsUpdatedInserted != 0 || this.items != null) writePuntiGrafico(conn, primaryKey); } public void writePuntiGrafico(SqlConnection conn, int primaryKey) { string sqlCommand = "(idREP_IMM_MON_PuntiGrafico, idREP_IMM_MON_Grafico, chiave, valMin) " + "VALUES (@idREP_IMM_MON_PuntiGrafico, @idREP_IMM_MON_Grafico, @chiave, @valMin) "; foreach (var item in this.items) { SqlCommand insertTo = new SqlCommand("INSERT INTO REP_IMM_MON_PuntiGrafico " + sqlCommand, conn); insertTo.Parameters.AddWithValue("@idREP_IMM_MON_Grafico", primaryKey); insertTo.Parameters.AddWithValue("@idREP_IMM_MON_PuntiGrafico", Convert.ToInt32((new SqlCommand("SELECT count(*) from REP_IMM_MON_PuntiGrafico", conn)).ExecuteScalar()) + 1); insertTo.Parameters.AddWithValue("@chiave", item.Key); insertTo.Parameters.AddWithValue("@valMin", item.Value); int rowsUpdatedInserted = insertTo.ExecuteNonQuery(); } } public static List getProperties() { List properties = new List(); Chart objectChart = new Chart(); foreach (var prop in objectChart.GetType().GetProperties()) { properties.Add(prop.Name.ToLower()); } properties.Remove("idcapitolo"); properties.Remove("idsezione"); properties.Remove("idordinamento"); properties.Remove("idpagina"); properties.Remove("note"); return properties; } public static void writeChart(XmlNode attribute, List tagList, string nextTag, Data data) { List chartAttributes = getProperties(); bool presenzaNota = attribute.ChildNodes.Cast().Any(x => x.Name.Equals("note")); if (presenzaNota) chartAttributes.Add("note"); Chart chart = new Chart(); foreach (XmlNode attr in attribute.ChildNodes) if (!tagList.Contains(attr.Name.ToString())) { PropertyInfo propertyInfo = chart.GetType().GetProperty(attr.Name); if (attr.Name.ToLower().Equals("note")) propertyInfo.SetValue(chart, Convert.ChangeType(Nota.getNote(attr), propertyInfo.PropertyType), null); else if (!attr.Name.ToLower().Equals("items")) propertyInfo.SetValue(chart, Convert.ChangeType(attr.InnerText, propertyInfo.PropertyType), null); else propertyInfo.SetValue(chart, Convert.ChangeType(getItems(attr), propertyInfo.PropertyType), null); chartAttributes.Remove(attr.Name.ToLower()); if (!(chartAttributes.Count > 0)) { chart.idCapitolo = ReadXML.idCapitolo; chart.idSezione = ReadXML.idSezione; chart.idPagina = ReadXML.idPagina; chart.idOrdinamento = ReadXML.idOrdinamento; ReadXML.idOrdinamento++; data.chartList.Add(chart); chart = new Chart(); chartAttributes = getProperties(); if (presenzaNota) chartAttributes.Add("note"); } } else ReadXML.extractTag(attr, attr.Name.ToString()); } public static Dictionary getItems(XmlNode attribute) { Dictionary pointList = new Dictionary(); string chartString = ""; decimal chartValues = 0; foreach (XmlNode attr in attribute.ChildNodes) { int i = 0; foreach (XmlNode value in attr.ChildNodes) { if (i == 0) chartString = value.InnerText; else chartValues = Convert.ToDecimal(value.InnerText.Replace(".", ","), new System.Globalization.CultureInfo("it-IT")); i++; } pointList.Add(chartString, chartValues); } return pointList; } } }