Esempio n. 1
0
        public static OfxInstitutionInfo GetProviderInformation(OfxInstitutionInfo provider)
        {
            if (provider == null || string.IsNullOrEmpty(provider.OfxHomeId))
            {
                return(null);
            }

            // update the cached information.
            string url = string.Format(OfxHomeProviderInfo, provider.OfxHomeId);

            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.Method = "GET";
                HttpWebResponse r = request.GetResponse() as HttpWebResponse;
                using (Stream s = r.GetResponseStream())
                {
                    XDocument doc = XDocument.Load(s);
                    if (provider.AddInfoFromOfxHome(doc.Root))
                    {
                        UpdateCachedProfile(provider);
                    }
                }
            }
            catch
            {
                // todo: report error
            }
            return(provider);
        }
Esempio n. 2
0
        private static void UpdateCachedProfile(OfxInstitutionInfo profile)
        {
            List <OfxInstitutionInfo> list = GetCachedBankList();

            if (list.Contains(profile))
            {
                // already there.
                SaveList(list);
                return;
            }
            else
            {
                // find the same provider in our cacheList and merge the info
                foreach (OfxInstitutionInfo item in list)
                {
                    if (string.Compare(item.MoneyDanceId, profile.MoneyDanceId) == 0 ||
                        string.Compare(item.OfxHomeId, profile.OfxHomeId) == 0)
                    {
                        if (item.Merge(profile))
                        {
                            SaveList(list);
                        }
                        break;
                    }
                }
            }
        }
Esempio n. 3
0
        private static List <OfxInstitutionInfo> LoadProviderList(XDocument doc)
        {
            List <OfxInstitutionInfo> list = new List <OfxInstitutionInfo>();
            Dictionary <string, OfxInstitutionInfo> map = new Dictionary <string, OfxInstitutionInfo>();

            foreach (XElement e in doc.Root.Elements())
            {
                OfxInstitutionInfo info = OfxInstitutionInfo.Create(e);
                if (info != null && info.Name != null)
                {
                    OfxInstitutionInfo other;
                    if (map.TryGetValue(info.Name, out other))
                    {
                        other.Merge(info);
                    }
                    else
                    {
                        list.Add(info);
                        map[info.Name] = info;
                    }
                }
            }

            return(list);
        }
Esempio n. 4
0
        private static void MergeProviderList(List <OfxInstitutionInfo> result, XDocument doc)
        {
            try
            {
                /* The financial institution list from http://www.ofxhome.com/api.php?all=yes
                 * is in this format:
                 *  <institutionlist>
                 *      <institutionid id="555" name="121 Financial Credit Union"/>
                 *      ...
                 */
                foreach (XElement institution in doc.Root.Elements())
                {
                    OfxInstitutionInfo c = OfxInstitutionInfo.Create(institution);

                    string name = c.Name;
                    if (!string.IsNullOrEmpty(name))
                    {
                        bool exists = false;
                        foreach (OfxInstitutionInfo i in result)
                        {
                            bool?mergable = IsMergable(i.MoneyDanceId, c.MoneyDanceId);
                            if (!mergable.HasValue)
                            {
                                mergable = IsMergable(i.OfxHomeId, c.OfxHomeId);
                            }
                            if (!mergable.HasValue)
                            {
                                mergable = IsMergable(i.Name, c.Name);
                            }

                            if (mergable == true)
                            {
                                exists = true;
                                i.Merge(c);
                                break;
                            }
                        }
                        if (!exists)
                        {
                            // found a new one!
                            result.Add(c);
                        }
                    }
                }
            }
            catch
            {
                // todo: error handling...
            }
        }
        internal static OfxInstitutionInfo Create(XElement e)
        {
            OfxInstitutionInfo result = new OfxInstitutionInfo();

            foreach (XElement child in e.Elements())
            {
                DateTime?lastChanged = null;
                string   date        = (string)child.Attribute("changed");
                if (!string.IsNullOrEmpty(date))
                {
                    DateTime dt;
                    if (DateTime.TryParse(date, out dt))
                    {
                        lastChanged = dt;
                    }
                }
                result.SetValue(child.Name.LocalName, child.Value, lastChanged);
            }
            return(result);
        }
        internal bool Merge(OfxInstitutionInfo profile)
        {
            Debug.Assert((string.IsNullOrEmpty(this.MoneyDanceId) || string.IsNullOrEmpty(profile.MoneyDanceId) || string.Compare(this.MoneyDanceId, profile.MoneyDanceId) == 0) &&
                         (string.IsNullOrEmpty(this.OfxHomeId) || string.IsNullOrEmpty(profile.OfxHomeId) || string.Compare(this.OfxHomeId, profile.OfxHomeId) == 0));

            bool changed = false;

            foreach (ChangeTrackedField field in profile.fields.Values)
            {
                bool setit = false;
                ChangeTrackedField ourField;
                if (fields.TryGetValue(field.Name, out ourField))
                {
                    if (ourField.Value != field.Value)
                    {
                        // see which one is newer.
                        setit = field.LastChange.HasValue && (!ourField.LastChange.HasValue || ourField.LastChange.Value < field.LastChange.Value);
                    }
                    else
                    {
                        // values are equal, no need for a change then.
                    }
                }
                else
                {
                    // new to us
                    setit = true;
                }
                if (setit)
                {
                    SetValue(field.Name, field.Value, field.LastChange);
                    changed = true;
                }
            }

            return(changed);
        }
        /// <summary>
        /// This code can parse the Python script for MoneyDance in order to glean OfxInstitutionInfo.
        /// You can then merge that with the existing ofx-index data from ofxhome.com
        /// </summary>
        /// <param name="filename">The text file containing MoneyDance python code</param>
        /// <returns>The list of OfxInstitutionInfo found in the MoneyDance file</returns>
        public static List <OfxInstitutionInfo> ParseMoneyDancePythonScript(string filename)
        {
            List <OfxInstitutionInfo> result = new List <OfxInstitutionInfo>();

            string text = null;

            using (StreamReader reader = new StreamReader(filename))
            {
                text = reader.ReadToEnd();
            }

            for (int i = 0, n = text.Length; i < n; i++)
            {
                char ch = text[i];
                if (ch == '{')
                {
                    OfxInstitutionInfo info = new OfxInstitutionInfo();
                    result.Add(info);

                    string name  = null;
                    string value = null;

                    for (i = i + 1; i < n; i++)
                    {
                        ch = text[i];
                        if (ch == '}')
                        {
                            // done
                            value = null;
                            break;
                        }
                        else if (ch == '"')
                        {
                            i++;
                            for (int j = i; i < n; j++)
                            {
                                ch = text[j];
                                if (ch == '"')
                                {
                                    value = text.Substring(i, j - i);
                                    i     = j;
                                    break;
                                }
                            }

                            if (name != null)
                            {
                                // map to ofx-index format.
                                switch (name)
                                {
                                case "fi_name":
                                    info.Name = value;
                                    break;

                                case "fi_org":
                                    info.Org = value;
                                    break;

                                case "id":
                                    info.MoneyDanceId = value;
                                    break;

                                case "bootstrap_url":
                                    info.ProviderURL = value;
                                    break;

                                case "fi_id":
                                    info.Fid = value;
                                    break;

                                case "app_id":
                                    info.AppId = value;
                                    break;

                                case "app_ver":
                                    info.AppVer = value;
                                    break;

                                case "broker_id":
                                    info.BrokerId = value;
                                    break;
                                }

                                name  = null;
                                value = null;
                            }
                        }
                        else if (ch == '=')
                        {
                            name  = value;
                            value = null;
                        }
                    }
                }
            }

            return(result);
        }