/// <summary>
        /// Initializes a new instance of the <see cref="RadioButtonEditorAttribute"/> class.
        /// </summary>
        /// <param name="enumOrLookupType">Type of the enum or lookup.</param>
        /// <exception cref="ArgumentNullException">enumOrLookupType</exception>
        /// <exception cref="ArgumentException">lookupType</exception>
        public RadioButtonEditorAttribute(Type enumOrLookupType)
            : base("RadioButton")
        {
            if (enumOrLookupType == null)
            {
                throw new ArgumentNullException("enumOrLookupType");
            }

            if (enumOrLookupType.IsEnum)
            {
                var ek = enumOrLookupType.GetCustomAttribute <EnumKeyAttribute>(false);
                if (ek == null)
                {
                    EnumKey = enumOrLookupType.FullName;
                }
                else
                {
                    EnumKey = ek.Value;
                }

                return;
            }

            var lk = enumOrLookupType.GetCustomAttribute <LookupScriptAttribute>(false);

            if (lk == null)
            {
                throw new ArgumentException(string.Format(
                                                "'{0}' type doesn't have a [LookupScript] attribute, so it can't " +
                                                "be used with a RadioButtonEditor!",
                                                enumOrLookupType.FullName), "lookupType");
            }

            LookupKey = lk.Key ?? LookupScriptAttribute.AutoLookupKeyFor(enumOrLookupType);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LookupFilteringAttribute"/> class.
        /// </summary>
        /// <param name="lookupType">Type of the lookup to get lookup key from. Can be
        /// a row with [LookupScript] attribute or a custom lookup script.</param>
        /// <exception cref="ArgumentOutOfRangeException">lookupType is null</exception>
        public LookupFilteringAttribute(Type lookupType)
            : base("Lookup")
        {
            var attr = lookupType.GetCustomAttribute <LookupScriptAttribute>(false);

            if (attr == null)
            {
                throw new ArgumentOutOfRangeException("lookupType");
            }

            SetOption("lookupKey", attr.Key ??
                      LookupScriptAttribute.AutoLookupKeyFor(lookupType));
        }
        /// <summary>
        /// If you use this constructor, lookupKey will be determined by [LookupScript] attribute
        /// on specified lookup type. If this is a row type, make sure it has [LookupScript] attribute
        /// on it.
        /// </summary>
        public CheckLookupEditorAttribute(Type lookupType)
            : base("CheckLookup")
        {
            if (lookupType == null)
            {
                throw new ArgumentNullException("lookupType");
            }

            var attr = lookupType.GetCustomAttribute <LookupScriptAttribute>(false);

            if (attr == null)
            {
                throw new ArgumentException(string.Format(
                                                "'{0}' type doesn't have a [LookupScript] attribute, so it can't " +
                                                "be used with a CheckLookupEditor!",
                                                lookupType.FullName), "lookupType");
            }

            SetOption("lookupKey", attr.Key ??
                      LookupScriptAttribute.AutoLookupKeyFor(lookupType));
        }
        public static void RegisterLookupScripts(IDynamicScriptManager scriptManager,
                                                 ITypeSource typeSource, IServiceProvider serviceProvider)
        {
            if (scriptManager == null)
            {
                throw new ArgumentNullException(nameof(scriptManager));
            }

            if (typeSource == null)
            {
                throw new ArgumentNullException(nameof(typeSource));
            }

            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            var registeredType = new Dictionary <string, Type>(StringComparer.OrdinalIgnoreCase);

            foreach (var type in typeSource.GetTypesWithAttribute(typeof(LookupScriptAttribute)))
            {
                var          attr = type.GetCustomAttribute <LookupScriptAttribute>();
                LookupScript script;

                if (typeof(IRow).IsAssignableFrom(type))
                {
                    if (attr.LookupType == null)
                    {
                        script = (LookupScript)ActivatorUtilities.CreateInstance(serviceProvider,
                                                                                 typeof(RowLookupScript <>).MakeGenericType(type));
                    }
                    else if (attr.LookupType.IsGenericType)
                    {
                        script = (LookupScript)ActivatorUtilities.CreateInstance(serviceProvider,
                                                                                 attr.LookupType.MakeGenericType(type));
                    }
                    else if (attr.LookupType.GetCustomAttribute <LookupScriptAttribute>() == null)
                    {
                        script = (LookupScript)ActivatorUtilities.CreateInstance(serviceProvider,
                                                                                 attr.LookupType);
                    }
                    else
                    {
                        // lookup script type already has a LookupScript attribute,
                        // so it's dynamic script will be generated on itself
                        continue;
                    }
                }
                else if (!typeof(LookupScript).IsAssignableFrom(type) ||
                         type.IsAbstract)
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                                                                      "Type {0} can't be registered as a lookup script!", type.FullName));
                }
                else
                {
                    script = (LookupScript)ActivatorUtilities.CreateInstance(serviceProvider, type);
                }

                script.LookupKey = attr.Key ??
                                   LookupScriptAttribute.AutoLookupKeyFor(type);

                if (registeredType.TryGetValue(script.LookupKey, out Type otherType))
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                                                                      "Types {0} and {1} has the same lookup key (\"{2}\"). " +
                                                                      "\r\n\r\nPlease remove LookupScript attribute from one of them or change the lookup key!",
                                                                      type.FullName, otherType.FullName, script.LookupKey));
                }

                registeredType[script.LookupKey] = type;

                if (attr.Permission != null)
                {
                    script.Permission = attr.Permission;
                }

                if (attr.Expiration != 0)
                {
                    script.Expiration = TimeSpan.FromSeconds(attr.Expiration);
                }

                scriptManager.Register(script.ScriptName, script);
            }
        }
Ejemplo n.º 5
0
        private void GenerateRowMembers(Type rowType)
        {
            bool anyMetadata   = false;
            var  codeNamespace = GetNamespace(rowType);
            Row  row           = (Row)Activator.CreateInstance(rowType);

            var idRow        = row as IIdRow;
            var isActiveRow  = row as IIsActiveRow;
            var isDeletedRow = row as IIsDeletedRow;
            var nameRow      = row as INameRow;
            var lookupAttr   = rowType.GetCustomAttribute <LookupScriptAttribute>();

            if (lookupAttr == null)
            {
                var script = lookupScripts.FirstOrDefault(x =>
                                                          x.BaseType != null &&
                                                          x.BaseType.IsGenericType &&
                                                          x.BaseType.GetGenericArguments().Any(z => z == rowType));

                if (script != null)
                {
                    lookupAttr = script.GetCustomAttribute <LookupScriptAttribute>();
                }
            }

            if (idRow != null)
            {
                cw.Indented("[InlineConstant] public const string IdProperty = \"");
                var field = ((Field)idRow.IdField);
                sb.Append(field.PropertyName ?? field.Name);
                sb.AppendLine("\";");
                anyMetadata = true;
            }

            if (isActiveRow != null)
            {
                cw.Indented("[InlineConstant] public const string IsActiveProperty = \"");
                var field = (isActiveRow.IsActiveField);
                sb.Append(field.PropertyName ?? field.Name);
                sb.AppendLine("\";");
                anyMetadata = true;
            }

            if (isDeletedRow != null)
            {
                cw.Indented("[InlineConstant] public const string IsDeletedProperty = \"");
                var field = (isDeletedRow.IsDeletedField);
                sb.Append(field.PropertyName ?? field.Name);
                sb.AppendLine("\";");
                anyMetadata = true;
            }

            if (nameRow != null)
            {
                cw.Indented("[InlineConstant] public const string NameProperty = \"");
                var field = (nameRow.NameField);
                sb.Append(field.PropertyName ?? field.Name);
                sb.AppendLine("\";");
                anyMetadata = true;
            }

            var localTextPrefix = row.GetFields().LocalTextPrefix;

            if (!string.IsNullOrEmpty(localTextPrefix))
            {
                cw.Indented("[InlineConstant] public const string LocalTextPrefix = \"");
                sb.Append(localTextPrefix);
                sb.AppendLine("\";");
                anyMetadata = true;
            }

            if (lookupAttr != null)
            {
                cw.Indented("[InlineConstant] public const string LookupKey = \"");
                sb.Append(lookupAttr.Key ??
                          LookupScriptAttribute.AutoLookupKeyFor(rowType));
                sb.AppendLine("\";");

                sb.AppendLine();
                cw.Indented("public static Lookup<");
                MakeFriendlyName(rowType, codeNamespace, null);
                sb.Append("> Lookup { [InlineCode(\"Q.getLookup('");
                sb.Append(lookupAttr.Key ?? LookupScriptAttribute.AutoLookupKeyFor(rowType));
                sb.AppendLine("')\")] get { return null; } }");

                anyMetadata = true;
            }

            if (anyMetadata)
            {
                sb.AppendLine();
            }

            foreach (var field in row.GetFields())
            {
                cw.Indented("public ");

                var enumField = field as IEnumTypeField;
                if (enumField != null && enumField.EnumType != null)
                {
                    HandleMemberType(enumField.EnumType, codeNamespace);
                    sb.Append('?');
                }
                else
                {
                    var dataType = field.ValueType;
                    HandleMemberType(dataType, codeNamespace);
                }

                sb.Append(" ");
                sb.Append(field.PropertyName ?? field.Name);
                sb.AppendLine(" { get; set; }");
            }

            sb.AppendLine();
            cw.IndentedLine("[Imported, PreserveMemberCase]");
            cw.IndentedLine("public static class Fields");
            cw.InBrace(delegate
            {
                foreach (var field in row.GetFields())
                {
                    cw.Indented("[InlineConstant] public const string ");
                    sb.Append(field.PropertyName ?? field.Name);
                    sb.Append(" = \"");
                    sb.Append(field.PropertyName ?? field.Name);
                    sb.AppendLine("\";");
                }
            });
        }
Ejemplo n.º 6
0
        public static void RegisterLookupScripts()
        {
            var assemblies     = ExtensibilityHelper.SelfAssemblies;
            var registeredType = new Dictionary <string, Type>(StringComparer.OrdinalIgnoreCase);

            foreach (var assembly in assemblies)
            {
                foreach (var type in assembly.GetTypes())
                {
                    var attr = type.GetCustomAttribute <LookupScriptAttribute>();
                    if (attr == null)
                    {
                        continue;
                    }

                    LookupScript script;

                    if (typeof(Row).IsAssignableFrom(type))
                    {
                        if (attr.LookupType == null)
                        {
                            script = (LookupScript)Activator.CreateInstance(typeof(RowLookupScript <>).MakeGenericType(type));
                        }
                        else if (attr.LookupType.IsGenericType)
                        {
                            script = (LookupScript)Activator.CreateInstance(attr.LookupType.MakeGenericType(type));
                        }
                        else if (attr.LookupType.GetCustomAttribute <LookupScriptAttribute>() == null)
                        {
                            script = (LookupScript)Activator.CreateInstance(attr.LookupType);
                        }
                        else
                        {
                            // lookup script type already has a LookupScript attribute,
                            // so it's dynamic script will be generated on itself
                            continue;
                        }
                    }
                    else if (!typeof(LookupScript).IsAssignableFrom(type) ||
                             type.IsAbstract)
                    {
                        throw new InvalidOperationException(String.Format("Type {0} can't be registered as a lookup script!", type.FullName));
                    }
                    else
                    {
                        script = (LookupScript)Activator.CreateInstance(type);
                    }

                    script.LookupKey = attr.Key ??
                                       LookupScriptAttribute.AutoLookupKeyFor(type);

                    Type otherType;
                    if (registeredType.TryGetValue(script.LookupKey, out otherType))
                    {
                        throw new InvalidOperationException(String.Format("Types {0} and {1} has the same lookup key (\"{2}\"). " +
                                                                          "\r\n\r\nPlease remove LookupScript attribute from one of them or change the lookup key!",
                                                                          type.FullName, otherType.FullName, script.LookupKey));
                    }

                    registeredType[script.LookupKey] = type;

                    if (attr.Permission != null)
                    {
                        script.Permission = attr.Permission;
                    }

                    if (attr.Expiration != 0)
                    {
                        script.Expiration = TimeSpan.FromSeconds(attr.Expiration);
                    }

                    DynamicScriptManager.Register(script.ScriptName, script);
                }
            }
        }