예제 #1
0
        private void getYearData(int year = -1)
        {
            if (-1 == year) {
                year = DateTime.Now.AddYears(-1).Year;
            }

            XmlParser yearXML = new XmlParser("http://www.bnro.ro/files/xml/years/nbrfxrates" + year + ".xml", "Cube", "date", "Rate", "currency");
            Dictionary<string, Dictionary<string, decimal>> yRates = yearXML.parse();
            Dictionary<string, string> d = new Dictionary<string, string>();

            foreach (var date in yRates) {
                d["date"] = date.Key;

                foreach (var currency in date.Value) {
                    d["rate"] = currency.Value.ToString();
                    db.insert(currency.Key, d, true);
                }
            }
        }
예제 #2
0
        private Dictionary<string, decimal> initData()
        {
            bool populate = false, no_fetch = false;
            DateTime? last_fetch = null;

            XmlParser currentXML = new XmlParser("http://www.bnr.ro/nbrfxrates.xml", "Cube", "date", "Rate", "currency");
            XmlParser dateXML = new XmlParser("http://www.bnr.ro/nbrfxrates.xml", "Cube", "date", "Rate", "currency");

            Dictionary<string, decimal> cRates = currentXML.parse().First().Value;

            DateTime cDate = dateXML.getDate();

            if (!db.DBexists()) {
                db.create();
                db.createTable("data(last_fetch DATETIME UNIQUE NOT NULL, oldest_date DATETIME)");
                db.createTable("currency(symbol NCHAR(3) NOT NULL, name NVARCHAR(35) NOT NULL)");

                populate = no_fetch = true;

                foreach (string table in cRates.Keys) {
                    db.createTable(table + "(rate MONEY NOT NULL, date DATETIME UNIQUE NOT NULL)");

                    if (currencies.ContainsKey(table)) {
                        db.insert("currency", new Dictionary<string, string>() {{"symbol", table}, {"name", currencies[table]}});
                    }
                }
            }
            else {
                SqlCeDataReader r = db.getData("SELECT last_fetch FROM data");

                if (r.Read()) {
                    last_fetch = r.GetDateTime(0);

                    if (last_fetch != cDate) {
                        no_fetch = true;
                    }
                }
                else {
                    no_fetch = true;
                }
            }

            if (no_fetch) {
                foreach (string table in cRates.Keys) {
                    Dictionary<string, string> d = new Dictionary<string, string>();
                    d.Add("rate", cRates[table].ToString());
                    d.Add("date", cDate.ToString());

                    db.insert(table, d, true);
                }

                if (null != last_fetch) {
                    db.delete(string.Format("data WHERE last_fetch='{0}'", last_fetch));
                }

                Dictionary<string, string> data = new Dictionary<string, string>();
                data.Add("last_fetch", cDate.ToString());
                db.insert("data", data, true);

                last_fetch = cDate;
            }

            TimeSpan ts = (TimeSpan)(cDate - last_fetch);

            if (populate || ts.Days >= 10) {
                get10DaysData();
            }

            if (populate) {
                getYearData();
            }

            cRates.Add(RON, 1);

            return cRates;
        }
예제 #3
0
        private void get10DaysData()
        {
            XmlParser last10XML = new XmlParser("http://www.bnro.ro/nbrfxrates10days.xml", "Cube", "date", "Rate", "currency");
            Dictionary<string, Dictionary<string, decimal>> last10Rates = last10XML.parse();

            Dictionary<string, string> d = new Dictionary<string, string>();

            foreach (var date in last10Rates) {
                d["date"] = date.Key;

                foreach (var currency in date.Value) {
                    d["rate"] = currency.Value.ToString();
                    db.insert(currency.Key, d, true);
                }
            }
        }