125 lines
5.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data.SqlClient;
using System.Reflection;
namespace XMLExtractor
{
public class Paragraph {
public string id { get; set; }
public int idCapitolo { get; set; }
public int idSezione { get; set; }
public int idPagina { get; set; }
public int idOrdinamento { get; set; }
public string descrizione { get; set; }
public string posizione { get; set; }
public string[] sottoparagrafi { get; set; }
public void writeDB(SqlConnection conn)
{
// Le sezioni di Immobiliare Monitoraggio su DB partono da 201 (compreso)
// I dati sono estratti dallXML con indice che parte da 1
int rowsUpdatedInserted = 0;
if(this.idPagina == 12)
rowsUpdatedInserted = 0;
SqlCommand updInsCommand;
SqlCommand cmdCount = new SqlCommand("SELECT count(*) from REP_IMM_MON_TestiParagrafo WHERE titolo = @titolo", conn);
cmdCount.Parameters.AddWithValue("@titolo", this.descrizione);
int count = (int)cmdCount.ExecuteScalar();
int i = 1;
string sqlCommand = "";
int numeroColonneTesto = 15;
// Se esiste, faccio l'update, altrimenti creo una nuova riga
//if (count > 0)
//{
// for (i = 1; i <= numeroColonneTesto; i++)
// {
// sqlCommand += "Testo" + i + " = " + (i <= this.sottoparagrafi.Length ? "@Testo" + i : "@Null") + (i != numeroColonneTesto ? ", " : "");
// }
// foreach (string _string in this.sottoparagrafi)
// {
// sqlCommand += ("Testo" + i + "='" + _string + "'") + (i != this.sottoparagrafi.Length ? ", " : "");
// i++;
// }
// updInsCommand = new SqlCommand("UPDATE TestiParagrafo SET titolo = @titolo, Posizione = @Posizione, " + sqlCommand + " WHERE idPagina = @idPagina", conn);
//}
//else
{
string defColumn = "";
for (i = 1; i <= numeroColonneTesto; i++)
{
defColumn += ("Testo" + i) + (i != numeroColonneTesto ? ", " : "");
sqlCommand += (i <= this.sottoparagrafi.Length ? ("@Testo" + i) : "@Null") + (i != numeroColonneTesto ? ", " : "");
}
updInsCommand = new SqlCommand("INSERT into REP_IMM_MON_TestiParagrafo (idREP_IMM_MON_TestoParagrafo, idREP_IMM_MON_Pagina, titolo, Posizione, " + defColumn + " ) "
+ "VALUES (@ID, @idPagina, @titolo, @Posizione, "+sqlCommand+")", conn);
updInsCommand.Parameters.AddWithValue("@ID", Convert.ToInt32(new SqlCommand("SELECT count(*) FROM REP_IMM_MON_TestiParagrafo", conn).ExecuteScalar())+1);
updInsCommand.Parameters.AddWithValue("@idPagina", this.idPagina);
i = 1;
foreach (string _string in this.sottoparagrafi)
{
updInsCommand.Parameters.AddWithValue("@Testo" + i, _string);
i++;
}
updInsCommand.Parameters.AddWithValue("@Null", DBNull.Value);
updInsCommand.Parameters.AddWithValue("@Posizione", this.posizione);
updInsCommand.Parameters.AddWithValue("@titolo", this.descrizione);
rowsUpdatedInserted = updInsCommand.ExecuteNonQuery();
}
}
public static void writeParagraph(XmlNode attribute, List<string> tagList, string nextTag, Data data)
{
List<String> paragraphAttributes = new List<String>(new[] { "id", "descrizione", "posizione", "sottoparagrafi" });
Paragraph paragraph = new Paragraph();
foreach (XmlNode attr in attribute.ChildNodes)
if (!tagList.Contains(attr.Name.ToString()))
{
PropertyInfo propertyInfo = paragraph.GetType().GetProperty(attr.Name.ToLower());
if (!attr.Name.Equals("sottoParagrafi"))
propertyInfo.SetValue(paragraph, Convert.ChangeType(attr.InnerText, propertyInfo.PropertyType), null);
else
propertyInfo.SetValue(paragraph, Convert.ChangeType(getSubparagraph(attr), propertyInfo.PropertyType), null);
paragraphAttributes.Remove(attr.Name.ToLower());
if (!(paragraphAttributes.Count > 0))
{
paragraph.idCapitolo = ReadXML.idCapitolo;
paragraph.idSezione = ReadXML.idSezione;
paragraph.idPagina = ReadXML.idPagina;
paragraph.idOrdinamento = ReadXML.idOrdinamento;
ReadXML.idOrdinamento++;
data.paragraphList.Add(paragraph);
paragraph = new Paragraph();
paragraphAttributes = new List<String>(new[] { "id", "descrizione", "posizione", "sottoparagrafi" });
}
}
else ReadXML.extractTag(attr, attr.Name.ToString());
}
public static string[] getSubparagraph(XmlNode attribute)
{
List<String> subparagraphList = new List<String>();
foreach (XmlNode attr in attribute.ChildNodes)
subparagraphList.Add(attr.InnerText);
return subparagraphList.ToArray();
}
}
}