Esempio n. 1
0
        public IGenericIOManager OpenConnection()
        {
            //Validate Attributes
            foreach (PropertyInfo property in this._properties.Keys)
            {
                ConnectionAttribute attr = this._properties[property];

                switch (attr.Type)
                {
                case ConnectionSettingType.Boolean:
                    bool b = (bool)property.GetValue(this, null);
                    break;

                case ConnectionSettingType.Integer:
                    int i = (int)property.GetValue(this, null);
                    if (attr.IsValueRestricted)
                    {
                        if (i < attr.MinValue)
                        {
                            throw new Exception(attr.DisplayName + " must be an Integer above " + attr.MinValue);
                        }
                        else if (i > attr.MaxValue)
                        {
                            throw new Exception(attr.DisplayName + " must be an Integer below " + attr.MaxValue);
                        }
                    }
                    break;

                case ConnectionSettingType.Password:
                case ConnectionSettingType.String:
                case ConnectionSettingType.File:
                    String s = (String)property.GetValue(this, null);
                    if (attr.IsRequired)
                    {
                        if (s == null || (s.Equals(String.Empty) && !attr.AllowEmptyString))
                        {
                            if (attr.NotRequiredIf != null)
                            {
                                PropertyInfo notReqProp = this._properties.Keys.Where(p => p.Name.Equals(attr.NotRequiredIf)).FirstOrDefault();
                                if (notReqProp != null)
                                {
                                    bool notReq = (bool)notReqProp.GetValue(this, null);
                                    if (!notReq)
                                    {
                                        throw new Exception(attr.DisplayName + " is a required connection setting!");
                                    }
                                }
                                else
                                {
                                    throw new Exception("Value for NotRequiredIf property is invalid");
                                }
                            }
                            else
                            {
                                throw new Exception(attr.DisplayName + " is a required connection setting!");
                            }
                        }
                    }
                    if (s != null && attr.IsValueRestricted)
                    {
                        if (s.Length < attr.MinValue)
                        {
                            throw new Exception(attr.DisplayName + " must be at least " + attr.MinValue + " characters long");
                        }
                        else if (s.Length > attr.MaxValue)
                        {
                            throw new Exception(attr.DisplayName + " must be no more than " + attr.MaxValue + " characters long");
                        }
                    }
                    break;

                case ConnectionSettingType.Enum:
                    Enum e = (Enum)property.GetValue(this, null);
                    break;

                default:
                    throw new Exception("Not a valid Connection Setting Type");
                }
            }
            return(this.OpenConnectionInternal());
        }
        /// <summary>
        /// Populates the settings from an existing serialized configuration
        /// </summary>
        /// <param name="g">Graph</param>
        /// <param name="objNode">Object Node</param>
        public virtual void PopulateFrom(IGraph g, INode objNode)
        {
            foreach (PropertyInfo property in this._properties.Keys)
            {
                ConnectionAttribute attr = this._properties[property];

                if (!String.IsNullOrEmpty(attr.PopulateFrom))
                {
                    INode n = objNode;

                    if (!String.IsNullOrEmpty(attr.PopulateVia))
                    {
                        n = ConfigurationLoader.GetConfigurationNode(g, n, g.CreateUriNode(UriFactory.Create(attr.PopulateVia)));
                        if (n == null)
                        {
                            continue;
                        }
                    }

                    switch (attr.Type)
                    {
                    case ConnectionSettingType.Boolean:
                        bool b = ConfigurationLoader.GetConfigurationBoolean(g, n, g.CreateUriNode(UriFactory.Create(attr.PopulateFrom)), (bool)property.GetValue(this, null));
                        property.SetValue(this, b, null);
                        break;

                    case ConnectionSettingType.File:
                    case ConnectionSettingType.Password:
                    case ConnectionSettingType.String:
                        String s = ConfigurationLoader.GetConfigurationString(g, n, g.CreateUriNode(UriFactory.Create(attr.PopulateFrom)));
                        if (!String.IsNullOrEmpty(s))
                        {
                            property.SetValue(this, s, null);
                        }
                        else
                        {
                            //May be a URI as the object
                            IUriNode u = ConfigurationLoader.GetConfigurationNode(g, n, g.CreateUriNode(UriFactory.Create(attr.PopulateFrom))) as IUriNode;
                            if (u != null)
                            {
                                property.SetValue(this, u.Uri.AbsoluteUri, null);
                            }
                        }
                        break;

                    case ConnectionSettingType.Integer:
                        int i = ConfigurationLoader.GetConfigurationInt32(g, n, g.CreateUriNode(UriFactory.Create(attr.PopulateFrom)), (int)property.GetValue(this, null));
                        property.SetValue(this, i, null);
                        break;

                    case ConnectionSettingType.Enum:
                        String enumStr = ConfigurationLoader.GetConfigurationString(g, n, g.CreateUriNode(UriFactory.Create(attr.PopulateFrom)));
                        if (!String.IsNullOrEmpty(enumStr))
                        {
                            try
                            {
                                Object val = Enum.Parse(property.GetValue(this, null).GetType(), enumStr, false);
                                property.SetValue(this, val, null);
                            }
                            catch
                            {
                                //Ignore errors
                            }
                        }
                        break;
                    }
                }
            }
        }