private static void FillSearchableAttributesOptions <T>(SearchableAttribute searchableAttribute, StringBuilder sb, List <string> searchableAttributeOptions)
            where T : class, new()
        {
            var propertyInfo = typeof(T).GetProperty(searchableAttribute.Attribute);

            if (propertyInfo == null)
            {
                throw new ArgumentNullException("Searchable attribute cannot be null");
            }
            //Sample => ordered(Productname)
            if (searchableAttribute.SearchableAttributeFunctionType != null)
            {
                sb.Append(searchableAttribute.SearchableAttributeFunctionType.ToString().ToLower());
                sb.Append("(");
                sb.Append(searchableAttribute.Attribute);
                sb.Append(")");
            }
            else
            {
                sb.Append(searchableAttribute.Attribute);
            }

            searchableAttributeOptions.Add(sb.ToString());
            sb.Clear();
        }
        /// <summary>
        /// Generates a search type parameter which is determined by the search field of the object
        /// </summary>
        /// <param name="objectType">The type of the object whose field is provided</param>
        /// <param name="searchField">The field (property) to be searched</param>
        /// <param name="required">Specifies if these parameters are mandatory</param>
        /// <returns></returns>
        internal static RuntimeDefinedParameter SearchType(Type objectType, string searchField, bool required = false)
        {
            if (objectType != null)
            {
                if (!String.IsNullOrEmpty(searchField))
                {
                    RuntimeDefinedParameter Param = new RuntimeDefinedParameter()
                    {
                        Name          = "SearchType",
                        ParameterType = typeof(String)
                    };

                    foreach (string Set in _PARAMETER_SETS)
                    {
                        Param.Attributes.Add(new ParameterAttribute()
                        {
                            ParameterSetName = Set,
                            Mandatory        = required,
                            HelpMessage      = "Select the type of search you want to perform."
                        });
                    }


                    SearchableAttribute Search = objectType.GetProperty(searchField).GetCustomAttribute <SearchableAttribute>();

                    List <string> SearchesAllowed = new List <string>();

                    if (Search != null)
                    {
                        foreach (PropertyInfo Prop in Search.GetType().GetProperties())
                        {
                            if (Prop.PropertyType.Equals(typeof(bool)))
                            {
                                if ((bool)Prop.GetValue(Search) == true)
                                {
                                    SearchesAllowed.Add(Prop.Name);
                                }
                            }
                        }
                    }

                    Param.Attributes.Add(new ValidateSetAttribute(SearchesAllowed.ToArray()));

                    return(Param);
                }
                else
                {
                    throw new ArgumentNullException("searchField", "The search field cannot be null or empty.");
                }
            }
            else
            {
                throw new ArgumentNullException("objectType", "The object type cannot be null");
            }
        }
Пример #3
0
        private static void CollectHandlers()
        {
            NetworkMessageHandlerAttribute.clientMessageHandlers.Clear();
            NetworkMessageHandlerAttribute.serverMessageHandlers.Clear();
            HashSet <short> hashSet = new HashSet <short>();

            foreach (SearchableAttribute searchableAttribute in SearchableAttribute.GetInstances <NetworkMessageHandlerAttribute>())
            {
                NetworkMessageHandlerAttribute networkMessageHandlerAttribute = (NetworkMessageHandlerAttribute)searchableAttribute;
                MethodInfo methodInfo = networkMessageHandlerAttribute.target as MethodInfo;
                if (!(methodInfo == null) && methodInfo.IsStatic)
                {
                    networkMessageHandlerAttribute.messageHandler = (NetworkMessageDelegate)Delegate.CreateDelegate(typeof(NetworkMessageDelegate), methodInfo);
                    if (networkMessageHandlerAttribute.messageHandler != null)
                    {
                        if (networkMessageHandlerAttribute.client)
                        {
                            NetworkMessageHandlerAttribute.clientMessageHandlers.Add(networkMessageHandlerAttribute);
                            hashSet.Add(networkMessageHandlerAttribute.msgType);
                        }
                        if (networkMessageHandlerAttribute.server)
                        {
                            NetworkMessageHandlerAttribute.serverMessageHandlers.Add(networkMessageHandlerAttribute);
                            hashSet.Add(networkMessageHandlerAttribute.msgType);
                        }
                    }
                    if (networkMessageHandlerAttribute.messageHandler == null)
                    {
                        Debug.LogWarningFormat("Could not register message handler for {0}. The function signature is likely incorrect.", new object[]
                        {
                            methodInfo.Name
                        });
                    }
                    if (!networkMessageHandlerAttribute.client && !networkMessageHandlerAttribute.server)
                    {
                        Debug.LogWarningFormat("Could not register message handler for {0}. It is marked as neither server nor client.", new object[]
                        {
                            methodInfo.Name
                        });
                    }
                }
            }
            for (short num = 48; num < 75; num += 1)
            {
                if (!hashSet.Contains(num))
                {
                    Debug.LogWarningFormat("Network message MsgType.Highest + {0} is unregistered.", new object[]
                    {
                        (int)(num - 47)
                    });
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Initialize property metadata.
        /// </summary>
        /// <param name="propertyInfo">Property info.</param>
        public ClrPropertyMetadata(PropertyInfo propertyInfo)
            : base(propertyInfo.Name, propertyInfo.PropertyType)
        {
            Property = propertyInfo;
            CanGet   = propertyInfo.GetGetMethod() != null;
            CanSet   = propertyInfo.GetSetMethod() != null;
            if (CanGet)
            {
                _GetValue = propertyInfo.GetGetMethodDelegate();
            }
            if (CanSet)
            {
                _SetValue = propertyInfo.GetSetMethodDelegate();
            }
            SetMetadata();

            HideAttribute hide = propertyInfo.GetCustomAttribute <HideAttribute>();

            if (hide == null)
            {
                if (propertyInfo.PropertyType.IsGenericType)
                {
                    IsHiddenOnView = true;
                }
            }

            string customType;
            bool   isFileUpload;

            Type         = propertyInfo.GetCustomDataType(out customType, out isFileUpload);
            CustomType   = customType;
            IsFileUpload = isFileUpload;

            if (Type != CustomDataType.Image && Type != CustomDataType.Password && Type != CustomDataType.Time)
            {
                if (CustomType == null || CustomType == "Entity" || CustomType == "Enum")
                {
                    SearchableAttribute searchable = propertyInfo.GetCustomAttribute <SearchableAttribute>();
                    Searchable = searchable != null;
                }
            }
        }
Пример #5
0
        public IDictionary <string, ISearchableAttribute> GetAttributes(Type type)
        {
            PropertyInfo[] props      = type.GetProperties();
            var            attributes = new Dictionary <string, ISearchableAttribute>();

            foreach (PropertyInfo prop in props)
            {
                object[] attrs = prop.GetCustomAttributes(true);
                foreach (object attr in attrs)
                {
                    SearchableAttribute searchableAttribute = attr as SearchableAttribute;
                    if (searchableAttribute != null)
                    {
                        attributes.Add(prop.Name, searchableAttribute);
                    }
                }
            }

            return(attributes);
        }
Пример #6
0
        /// <summary>
        /// Initialize property metadata.
        /// </summary>
        /// <param name="propertyInfo">Property info.</param>
        public ClrPropertyMetadata(PropertyInfo propertyInfo)
            : base(propertyInfo.Name, propertyInfo.PropertyType)
        {
            Property = propertyInfo;
            CanGet   = propertyInfo.GetGetMethod() != null;
            CanSet   = propertyInfo.GetSetMethod() != null;
            if (CanGet)
            {
                _GetValue = propertyInfo.GetGetMethodDelegate();
            }
            if (CanSet)
            {
                _SetValue = propertyInfo.GetSetMethodDelegate();
            }
            SetMetadata();

            HideAttribute hide = propertyInfo.GetCustomAttribute <HideAttribute>();

            if (hide == null)
            {
                if (!propertyInfo.PropertyType.GetTypeInfo().IsValueType&& propertyInfo.PropertyType.GetTypeInfo().IsGenericType)
                {
                    IsHiddenOnView = true;
                }
            }

            string customType;
            bool   isFileUpload;

            Type         = propertyInfo.GetCustomDataType(out customType, out isFileUpload);
            CustomType   = customType;
            IsFileUpload = isFileUpload;
            Converter    = TypeDescriptor.GetConverter(ClrType);

            SearchableAttribute searchable = propertyInfo.GetCustomAttribute <SearchableAttribute>();

            Searchable = searchable != null;
        }
Пример #7
0
        public async Task <IActionResult> UpdateIndexSettings()
        {
            //This settings can be edit on Algolia dashboard
            IndexOptions options = new IndexOptions();
            List <SearchableAttribute>    searchableAttributes    = new List <SearchableAttribute>();
            List <CustomRankingAttribute> customRankingAttributes = new List <CustomRankingAttribute>();

            CustomRankingAttribute customAttributeSalary = new CustomRankingAttribute();

            customAttributeSalary.Attribute   = "Salary";
            customAttributeSalary.RankingType = RankingFunctionsEnum.Asc;

            SearchableAttribute attributeSalary = new SearchableAttribute();

            attributeSalary.Attribute = "Salary";
            attributeSalary.SearchableAttributeFunctionType = SearchableAttributeFunctionsEnum.Ordered;

            SearchableAttribute attributeFirstname = new SearchableAttribute();

            attributeFirstname.Attribute = "Firstname";

            searchableAttributes.Add(attributeSalary);
            searchableAttributes.Add(attributeFirstname);
            customRankingAttributes.Add(customAttributeSalary);
            options.CustomRankingAttributes = customRankingAttributes;
            options.SearchableAttributes    = searchableAttributes;

            var response = await _indexService.UpdateIndexSettings <Employee>(new UpdateIndexSettingsRequestDto()
            {
                IndexName    = "Employees",
                IndexOptions = options
            });

            TempData["Message"] = response.Message;
            return(RedirectToAction("Index", "Home"));
        }
Пример #8
0
        /// <summary>
        /// Initialize property metadata.
        /// </summary>
        /// <param name="propertyInfo">Property info.</param>
        public PropertyMetadata(PropertyInfo propertyInfo)
        {
            Property = propertyInfo;

            DisplayAttribute display = propertyInfo.GetCustomAttribute <DisplayAttribute>();

            if (display != null)
            {
                Name = display.Name == null ? propertyInfo.Name : display.Name;
                if (display.Description != null)
                {
                    Description = display.Description;
                }
                Order     = display.GetOrder().HasValue ? display.Order : 0;
                ShortName = display.ShortName == null ? Name : display.ShortName;
            }
            else
            {
                Name = propertyInfo.Name;
            }

            RequiredAttribute required = propertyInfo.GetCustomAttribute <RequiredAttribute>();

            IsRequired = required != null;

            HideAttribute hide = propertyInfo.GetCustomAttribute <HideAttribute>();

            if (hide != null)
            {
                IsHiddenOnEdit   = hide.IsHiddenOnEdit;
                IsHiddenOnView   = hide.IsHiddenOnView;
                IsHiddenOnDetail = hide.IsHiddenOnDetail;
            }
            else
            {
                if (propertyInfo.PropertyType.IsGenericType)
                {
                    IsHiddenOnView = true;
                }
            }

            var customDataType = propertyInfo.GetCustomAttribute <CustomDataTypeAttribute>();

            if (customDataType != null)
            {
                Type       = customDataType.Type;
                CustomType = customDataType.Custom;
            }
            else
            {
                Type type = propertyInfo.PropertyType;
                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    type = type.GetGenericArguments()[0];
                }
                if (type == typeof(DateTime))
                {
                    Type = CustomDataType.Date;
                }
                if (type == typeof(TimeSpan))
                {
                    Type = CustomDataType.Time;
                }
                else if (type == typeof(bool))
                {
                    Type = CustomDataType.Boolean;
                }
                else if (type == typeof(short) || type == typeof(int) || type == typeof(long))
                {
                    Type = CustomDataType.Integer;
                }
                else if (type == typeof(float) || type == typeof(double))
                {
                    Type = CustomDataType.Number;
                }
                else if (type == typeof(decimal))
                {
                    Type = CustomDataType.Currency;
                }
                else if (type == typeof(byte[]))
                {
                    Type = CustomDataType.File;
                }
                else if (type.IsEnum)
                {
                    Type       = CustomDataType.Other;
                    CustomType = "Enum";
                }
                else if (type.IsGenericType)
                {
                    Type       = CustomDataType.Other;
                    CustomType = "Collection";
                }
                else if (typeof(IEntity).IsAssignableFrom(type))
                {
                    Type       = CustomDataType.Other;
                    CustomType = "Entity";
                }
            }

            if (Type != CustomDataType.Image && Type != CustomDataType.Password && Type != CustomDataType.Time)
            {
                if (CustomType == null || CustomType == "Entity" || CustomType == "Enum")
                {
                    SearchableAttribute searchable = propertyInfo.GetCustomAttribute <SearchableAttribute>();
                    Searchable = searchable != null;
                }
            }

            IsDistinct = propertyInfo.GetCustomAttribute <DistinctAttribute>() != null;
        }
Пример #9
0
 public static bool IsSearchTypeAllowed(this SearchType searchType, Type type, string searchField)
 {
     return(SearchableAttribute.IsSearchTypeAllowed(type, searchType, searchField));
 }
Пример #10
0
 public static IEnumerable <string> GetSearchableProperties <T>(this T obj)
 {
     return(SearchableAttribute.GetSearchableProperties <T>());
 }