private static Collection <Historie_stavby> Read(OracleDataReader reader, bool complete)
        {
            Collection <Historie_stavby> Historie_staveb = new Collection <Historie_stavby>();

            while (reader.Read())
            {
                int             i = -1;
                Historie_stavby Historie_stavby = new Historie_stavby();
                Historie_stavby.Id_zmeny      = reader.GetInt32(++i);
                Historie_stavby.Typ_stavby    = reader.GetString(++i);
                Historie_stavby.Ulice         = reader.GetString(++i);
                Historie_stavby.Cislo_popisne = reader.GetInt32(++i);
                if (complete)
                {
                    Historie_stavby.Cislo_stavby_na_KU   = reader.GetInt32(++i);
                    Historie_stavby.Nazev_KU             = reader.GetString(++i);
                    Historie_stavby.Datum_kolaudace      = reader.GetDateTime(++i);
                    Historie_stavby.Casovy_okamzik_zmeny = reader.GetDateTime(++i);
                }
                Historie_stavby.Id_vlastnika = reader.GetInt32(++i);
                Historie_stavby.Id_stavby    = reader.GetInt32(++i);

                Historie_staveb.Add(Historie_stavby);
            }
            return(Historie_staveb);
        }
        public void Insert(Historie_stavby historie_stavby)
        {
            Database db = new Database();

            db.Connect();
            OracleCommand command = db.CreateCommand(SQL_INSERT);

            PrepareCommand(command, historie_stavby);
            int ret = db.ExecuteNonQuery(command);

            db.Close();
        }
        /*
         * public static int Sequence(Database Db = null)
         * {
         *  Database db;
         *  if (Db == null)
         *  {
         *      db = new Database();
         *      db.Connect();
         *  }
         *  else
         *  {
         *      db = (Database)Db;
         *  }
         *
         *  OracleCommand command = db.CreateCommand(SQL_SEQUENCE);
         *  OracleDataReader reader = db.Select(command);
         *
         *  int hodnota = 0;
         *  while (reader.Read() != false)
         *  {
         *      hodnota = reader.GetInt32(0);
         *  }
         *
         *  reader.Close();
         *
         *  if (Db == null)
         *  {
         *      db.Close();
         *  }
         *
         *  return hodnota;
         * }*/

        /*
         * public static int Insert(Historie_stavby historie_stavby)
         * {
         *  Database db = new Database();
         *  db.Connect();
         *  OracleCommand command = db.CreateCommand(SQL_INSERT);
         *  PrepareCommand(command, historie_stavby);
         *  int ret = db.ExecuteNonQuery(command);
         *  db.Close();
         *  return ret;
         * }*/

        /*
         * public static Collection<Historie_stavby> Select(Database Db = null)
         * {
         *  Database db;
         *  if (Db == null)
         *  {
         *      db = new Database();
         *      db.Connect();
         *  }
         *  else
         *  {
         *      db = (Database)Db;
         *  }
         *
         *  OracleCommand command = db.CreateCommand(SQL_SELECT);
         *  OracleDataReader reader = db.Select(command);
         *
         *  Collection<Historie_stavby> Historie_staveb = Read(reader, false);
         *  reader.Close();
         *
         *  if (Db == null)
         *  {
         *      db.Close();
         *  }
         *
         *  return Historie_staveb;
         * }*/

        /*
         * public static Historie_stavby Select_id(int idZmeny, Database Db = null)
         * {
         *  Database db = new Database();
         *  db.Connect();
         *  OracleCommand command = db.CreateCommand(SQL_SELECT_ID);
         *
         *  command.Parameters.AddWithValue(":id", idZmeny);
         *  OracleDataReader reader = db.Select(command);
         *
         *  Collection<Historie_stavby> historie_staveb = Read(reader, true);
         *  Historie_stavby historie_stavby = null;
         *  if (historie_staveb.Count == 1)
         *  {
         *      historie_stavby = historie_staveb[0];
         *  }
         *  reader.Close();
         *  db.Close();
         *  return historie_stavby;
         * }*/

        private static void PrepareCommand(OracleCommand command, Historie_stavby Historie_stavby)
        {
            command.BindByName = true;
            command.Parameters.AddWithValue(":id", Historie_stavby.Id_zmeny);
            command.Parameters.AddWithValue(":typ", Historie_stavby.Typ_stavby);
            command.Parameters.AddWithValue(":ulice", Historie_stavby.Ulice);
            command.Parameters.AddWithValue(":cislo_popisne", Historie_stavby.Cislo_popisne);
            command.Parameters.AddWithValue(":cislo_stavby", Historie_stavby.Cislo_stavby_na_KU);
            command.Parameters.AddWithValue(":nazev_KU", Historie_stavby.Nazev_KU);
            command.Parameters.AddWithValue(":datum_kolaudace", Historie_stavby.Datum_kolaudace);
            command.Parameters.AddWithValue(":okamzik_zmeny", Historie_stavby.Casovy_okamzik_zmeny);
            command.Parameters.AddWithValue(":id_vlastnika", Historie_stavby.Id_vlastnika);
            command.Parameters.AddWithValue(":id_stavby", Historie_stavby.Id_stavby);
        }
        public Historie_stavby Select_id(int idZmeny)
        {
            Collection <Historie_stavby> vsechnyHistorie = this.Select();
            Historie_stavby vybranaHistorie = null;

            foreach (Historie_stavby historie in vsechnyHistorie)
            {
                if (historie.Id_zmeny == idZmeny)
                {
                    vybranaHistorie = historie;
                }
            }

            return(vybranaHistorie);
        }
        public Collection <Historie_stavby> Select()
        {
            XDocument xDoc = XDocument.Load(ConstantsXml.FilePath);

            List <XElement> elementy = xDoc.Descendants("Historie_staveb").Descendants("Historie_stavby").ToList();

            Collection <Historie_stavby> vsechnyHistorieStaveb = new Collection <Historie_stavby>();
            int      id;
            int      cislo_popisne;
            int      cislo_stavby;
            DateTime datum;
            DateTime okamzikZmeny;
            int      idVlastnika;
            int      idStavby;

            foreach (XElement element in elementy)
            {
                Historie_stavby historieStavby = new Historie_stavby();

                int.TryParse(element.Attribute("Id_zmeny").Value, out id);
                historieStavby.Typ_stavby = element.Attribute("Typ_stavby").Value;
                historieStavby.Ulice      = element.Attribute("Ulice").Value;
                int.TryParse(element.Attribute("Cislo_popisne").Value, out cislo_popisne);
                int.TryParse(element.Attribute("Cislo_stavby_na_KU").Value, out cislo_stavby);
                historieStavby.Nazev_KU = element.Attribute("Nazev_KU").Value;
                DateTime.TryParse(element.Attribute("Datum_kolaudace").Value, out datum);
                DateTime.TryParse(element.Attribute("Casovy_okamzik_zmeny").Value, out okamzikZmeny);
                int.TryParse(element.Attribute("Id_vlastnika").Value, out idVlastnika);
                int.TryParse(element.Attribute("Id_stavby").Value, out idStavby);

                historieStavby.Id_zmeny             = id;
                historieStavby.Cislo_popisne        = cislo_popisne;
                historieStavby.Cislo_stavby_na_KU   = cislo_stavby;
                historieStavby.Datum_kolaudace      = datum;
                historieStavby.Casovy_okamzik_zmeny = okamzikZmeny;
                historieStavby.Id_vlastnika         = idVlastnika;
                historieStavby.Id_stavby            = idStavby;

                vsechnyHistorieStaveb.Add(historieStavby);
                historieStavby = null;
            }

            return(vsechnyHistorieStaveb);
        }
        public void Insert(Historie_stavby historie_stavby)
        {
            XDocument xDoc = XDocument.Load(ConstantsXml.FilePath);

            XElement result = new XElement("Historie_stavby",
                                           new XAttribute("Id_zmeny", historie_stavby.Id_zmeny),
                                           new XAttribute("Typ_stavby", historie_stavby.Typ_stavby),
                                           new XAttribute("Ulice", historie_stavby.Ulice),
                                           new XAttribute("Cislo_popisne", historie_stavby.Cislo_popisne),
                                           new XAttribute("Cislo_stavby_na_KU", historie_stavby.Cislo_stavby_na_KU),
                                           new XAttribute("Nazev_KU", historie_stavby.Nazev_KU),
                                           new XAttribute("Datum_kolaudace", historie_stavby.Datum_kolaudace.ToShortDateString()),
                                           new XAttribute("Casovy_okamzik_zmeny", historie_stavby.Casovy_okamzik_zmeny.ToShortDateString()),
                                           new XAttribute("Id_vlastnika", historie_stavby.Id_vlastnika),
                                           new XAttribute("Id_stavby", historie_stavby.Id_stavby));

            xDoc.Root.Element("Historie_staveb").Add(result);
            xDoc.Save(ConstantsXml.FilePath);
        }
        public Historie_stavby Select_id(int idZmeny)
        {
            Database db = new Database();

            db.Connect();
            OracleCommand command = db.CreateCommand(SQL_SELECT_ID);

            command.Parameters.AddWithValue(":id", idZmeny);
            OracleDataReader reader = db.Select(command);

            Collection <Historie_stavby> historie_staveb = Read(reader, true);
            Historie_stavby historie_stavby = null;

            if (historie_staveb.Count == 1)
            {
                historie_stavby = historie_staveb[0];
            }

            reader.Close();
            db.Close();

            return(historie_stavby);
        }
        public void Update(StavbaVlastnik stavbaVlastnik)
        {
            //XDocument xDoc = XDocument.Load(Constants.FilePath);

            //List<XElement> elementy = xDoc.Descendants("StavbyVlastnici").Descendants("StavbaVlastnik").ToList();

            //vytvoreni DTO pro praci s daty
            Stavba   vybranaStavba     = new Stavba();
            Vlastnik vlastnikProUpravu = new Vlastnik();

            //pristup k jinym tridam
            Stavba_XmlMapper          stavbaGateway         = new Stavba_XmlMapper();
            Historie_stavby_XmlMapper historieStavbyGateway = new Historie_stavby_XmlMapper();
            Vlastnik_XmlMapper        vlastnikGateway       = new Vlastnik_XmlMapper();

            //nahrani vsech informaci o stavbe, ktera je aktualizovana
            vybranaStavba = stavbaGateway.Select_id(stavbaVlastnik.Id_stavby);

            //vyber vsech vlastniku staveb
            Collection <StavbaVlastnik> stavbyVlastnici = this.Select();

            foreach (StavbaVlastnik vlastnik in stavbyVlastnici)
            {
                //vyber vlastniku, kteri vlastni upravovanou stavbu
                if (vlastnik.Id_stavby == stavbaVlastnik.Id_stavby)
                {
                    //nacitani z xml a hledani polozky s id upravovane stavby a id vlastniku, kteri ji vlastni, pro mozne smazani
                    XDocument xDoc = XDocument.Load(ConstantsXml.FilePath);
                    var       q    = from node in xDoc.Descendants("StavbyVlastnici").Descendants("StavbaVlastnik")
                                     let attr                         = node.Attribute("Id_stavby")
                                                            let attr1 = node.Attribute("Id_vlastnika")
                                                                        where (attr != null && attr.Value == stavbaVlastnik.Id_stavby.ToString()) && (attr1 != null && attr1.Value == vlastnik.Id_vlastnika.ToString())
                                                                        select node;
                    q.ToList().ForEach(x => x.Remove());
                    xDoc.Save(ConstantsXml.FilePath);

                    Historie_stavby historieStavby = new Historie_stavby();

                    historieStavby.Id_zmeny             = historieStavbyGateway.Sequence();
                    historieStavby.Typ_stavby           = vybranaStavba.Typ_stavby;
                    historieStavby.Ulice                = vybranaStavba.Ulice;
                    historieStavby.Cislo_popisne        = vybranaStavba.Cislo_popisne;
                    historieStavby.Cislo_stavby_na_KU   = vybranaStavba.Cislo_stavby_na_KU;
                    historieStavby.Nazev_KU             = vybranaStavba.Nazev_KU;
                    historieStavby.Datum_kolaudace      = vybranaStavba.Datum_kolaudace;
                    historieStavby.Casovy_okamzik_zmeny = DateTime.Now;
                    historieStavby.Id_vlastnika         = vlastnik.Id_vlastnika;
                    historieStavby.Id_stavby            = stavbaVlastnik.Id_stavby;

                    //vlozeni smazaneho zaznamu do archivace
                    historieStavbyGateway.Insert(historieStavby);

                    //znovunacteni vsech vlastniku staveb
                    Collection <StavbaVlastnik> upraveniStavbyVlastnici = this.Select();
                    int pocetZaznamuVlastnika = 0;
                    foreach (StavbaVlastnik stavbyVlastnik in upraveniStavbyVlastnici)
                    {
                        //kontrola, jestli odstraneny vlastnik vlastni jeste nejakou stavbu nebo uz ne
                        if (stavbyVlastnik.Id_vlastnika == vlastnik.Id_vlastnika)
                        {
                            pocetZaznamuVlastnika++;
                        }
                    }

                    //pokud nevlastni uz zadnou stavbu, pak se zrusi jeho aktualnost
                    if (pocetZaznamuVlastnika == 0)
                    {
                        vlastnikProUpravu.Id_vlastnika      = vlastnik.Id_vlastnika;
                        vlastnikProUpravu.Aktualni_vlastnik = "N";

                        vlastnikGateway.Delete(vlastnikProUpravu);
                    }
                }
            }

            //vlozeni noveho upraveneho zaznamu
            this.Insert(stavbaVlastnik);

            //nastaveni aktualniho vlastnika
            vlastnikProUpravu.Id_vlastnika      = stavbaVlastnik.Id_vlastnika;
            vlastnikProUpravu.Aktualni_vlastnik = "A";
            vlastnikGateway.Delete(vlastnikProUpravu);
        }