Ejemplo n.º 1
0
        public Medicine Find(int key)
        {
            try
            {
                XmlDocument xml = new XmlDocument();
                xml.Load(Config.Settings["XML_medicines"]);
                XmlNodeList meds = xml.DocumentElement.SelectNodes(string.Format("//medicine[id[text()={0}]]", key.ToString()));

                if (meds.Count == 0)
                    return null;

                XmlNode node = meds[0];
                int id;
                if (!Int32.TryParse(node.ChildNodes[0].InnerText, out id))
                    throw new ApplicationException(ErrorMessages.Messages["MED_S_xml_id"] + node.ChildNodes[0].InnerText);
                string name = node.ChildNodes[1].InnerText;
                string description = node.ChildNodes[2].InnerText;
                List<string> allergens = new List<string>();
                foreach (XmlNode a in node.ChildNodes[3].ChildNodes)
                {
                    allergens.Add(a.InnerText);
                }
                int package;
                if (!Int32.TryParse(node.ChildNodes[4].InnerText, out package))
                    throw new ApplicationException(ErrorMessages.Messages["MED_S_xml_package"] + node.ChildNodes[4].InnerText);
                float price;
                if (!Single.TryParse(node.ChildNodes[5].InnerText, out price))
                    throw new ApplicationException(ErrorMessages.Messages["MED_S_xml_price"] + node.ChildNodes[5].InnerText);

                Medicine m = new Medicine();
                m.Id = id;
                m.Name = name;
                m.Description = description;
                m.Allergens = allergens;
                m.PackageSize = package;
                m.Price = price;

                return m;
            }
            catch (Exception e)
            {
                throw new ApplicationException(ErrorMessages.Messages["MED_S_xml"] + e.Message);
            }
        }
Ejemplo n.º 2
0
        private Dictionary<int, Medicine> Read(DbDataReader reader, IDatabase db)
        {
            Dictionary<int, Medicine> medicines = new Dictionary<int, Medicine>();

            while (reader.Read())
            {
                Medicine m = new Medicine();

                m.Id = reader.GetInt32(0);
                m.Name = reader.GetString(1);
                m.Description = reader.GetString(2);
                m.PackageSize = reader.GetInt32(3);
                m.Price = Decimal.ToSingle(reader.GetDecimal(4));

                m.Allergens = this.ReadAllergens(m, db);

                medicines.Add(m.Id, m);
            }

            return medicines;
        }
Ejemplo n.º 3
0
 public void Add(Medicine m)
 {
     this.mMedicines.Add(m);
 }
Ejemplo n.º 4
0
        private List<string> ReadAllergens(Medicine m, IDatabase db)
        {
            DbCommand command = db.CreateCommand(sqlSELECTALLERGENS);
            command.Parameters.Add(db.CreateParameter("@id", "int"));
            command.Parameters["@id"].Value = m.Id;

            DbDataReader areader = db.Select(command);

            List<string> allergens = new List<string>();
            while (areader.Read())
            {
                allergens.Add(areader.GetString(0));
            }

            areader.Close();

            return allergens;
        }
Ejemplo n.º 5
0
 private void Add(Medicine m)
 {
     this.medicines.Add(m.Id, m);
 }
Ejemplo n.º 6
0
        public Dictionary<int, Medicine> LoadAsDict()
        {
            try
            {
                Dictionary<int, Medicine> result = new Dictionary<int, Medicine>();
                XmlDocument xml = new XmlDocument();
                xml.Load(Config.Settings["XML_medicines"]);
                XmlNodeList meds = xml.DocumentElement.SelectNodes("//medicine");

                foreach (XmlNode node in meds)
                {
                    int id;
                    if (!Int32.TryParse(node.ChildNodes[0].InnerText, out id))
                        throw new ApplicationException(ErrorMessages.Messages["MED_S_xml_id"] + node.ChildNodes[0].InnerText);
                    string name = node.ChildNodes[1].InnerText;
                    string description = node.ChildNodes[2].InnerText;
                    List<string> allergens = new List<string>();
                    foreach (XmlNode a in node.ChildNodes[3].ChildNodes)
                    {
                        allergens.Add(a.InnerText);
                    }
                    int package;
                    if (!Int32.TryParse(node.ChildNodes[4].InnerText, out package))
                        throw new ApplicationException(ErrorMessages.Messages["MED_S_xml_package"] + node.ChildNodes[4].InnerText);
                    float price;
                    if (!Single.TryParse(node.ChildNodes[5].InnerText, out price))
                        throw new ApplicationException(ErrorMessages.Messages["MED_S_xml_price"] + node.ChildNodes[5].InnerText);

                    Medicine m = new Medicine();
                    m.Id = id;
                    m.Name = name;
                    m.Description = description;
                    m.Allergens = allergens;
                    m.PackageSize = package;
                    m.Price = price;
                    result.Add(m.Id, m);
                }
                return result;
            }
            catch (Exception e)
            {
                throw new ApplicationException(ErrorMessages.Messages["MED_S_xml"] + e.Message);
            }
        }