public static PropertySearchInfo[] GetPropertyByIndex(Type type, int index) { var searchAttributesProps = type.GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(SearchAttribute), true)); var elemTypeInput = AgroHelper.GetEntityType((EntityRelated)index, typeof(BarrackInput), "trifenix.agro.model.external.Input"); var elemTypeInputProps = elemTypeInput.GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(SearchAttribute), true)) .Select(s => new { info = s, search = s.GetAttribute <SearchAttribute>(), required = s.GetAttribute <RequiredAttribute>(), unique = s.GetAttribute <UniqueAttribute>() }); var props = searchAttributesProps.Select(s => { var searchAttribute = (SearchAttribute)s.GetCustomAttributes(typeof(SearchAttribute), true).FirstOrDefault(); var searchAttributeInput = elemTypeInputProps.FirstOrDefault(p => p.search.Index == searchAttribute.Index && p.search.Related == searchAttribute.Related); return(new PropertySearchInfo { IsEnumerable = IsEnumerableProperty(s), Name = s.Name, Index = searchAttribute.Index, Related = searchAttribute.Related, Enums = searchAttribute.Related == Related.ENUM ? GetDescription(s.PropertyType) : new Dictionary <int, string>(), IndexClass = index, Info = ResourceExtension.ResourceModel(searchAttribute.Related, searchAttribute.Index), IsRequired = searchAttributeInput?.required != null, IsUnique = searchAttributeInput?.unique != null, AutoNumeric = searchAttribute.GetType() == typeof(AutoNumericSearchAttribute), Visible = searchAttribute.Visible, HasInput = searchAttributeInput != null }); }).ToArray(); //si existe alguna propiead que esté en el input y no esté en la entidad. if (elemTypeInputProps.Any(s => !props.Any(a => a.Name.Equals(s.info.Name)))) { var extra = elemTypeInputProps.Where(s => !props.Any(a => a.Name.Equals(s.info.Name))); var list = props.ToList(); foreach (var item in extra) { list.Add(new PropertySearchInfo { IsEnumerable = IsEnumerableProperty(item.info), Name = item.info.Name, Index = item.search.Index, Related = item.search.Related, Enums = item.search.Related == Related.ENUM ? GetDescription(item.info.PropertyType) : new Dictionary <int, string>(), IndexClass = index, Info = ResourceExtension.ResourceModel(item.search.Related, item.search.Index), IsRequired = item?.required != null, IsUnique = item?.unique != null, }); } props = list.ToArray(); } return(props); }
public static ModelDictionary GetModel(IEnumerable <PropertySearchInfo> propertySearchInfos, EntityRelated index) { var propByRelatedAndIndex = propertySearchInfos.GroupBy(s => new { s.Related, s.IndexClass, s.Index }).Select(s => s.FirstOrDefault()); var enumEmun = GetDescription(typeof(EnumRelated)); var modelInfo = ResourceExtension.ResourceModel(Related.REFERENCE, propByRelatedAndIndex.FirstOrDefault().IndexClass); var modelDictionary = new ModelDictionary() { Index = index, Description = modelInfo.Description, ShortName = modelInfo.ShortName, Title = modelInfo.Title, BoolData = GetDictionaryFromRelated(propByRelatedAndIndex, Related.BOOL), StringData = GetDictionaryFromRelated(propByRelatedAndIndex, Related.STR), DateData = GetDictionaryFromRelated(propByRelatedAndIndex, Related.DATE), DoubleData = GetDictionaryFromRelated(propByRelatedAndIndex, Related.DBL), EnumData = GetEnumDictionaryFromRelated(propByRelatedAndIndex, enumEmun), GeoData = GetDictionaryFromRelated(propByRelatedAndIndex, Related.GEO), NumData = GetDictionaryFromRelated(propByRelatedAndIndex, Related.NUM32), relData = GetDictionaryFromRelated(propByRelatedAndIndex, Related.REFERENCE), }; var suggestions = GetDictionaryFromRelated(propByRelatedAndIndex, Related.SUGGESTION); var num64 = GetDictionaryFromRelated(propByRelatedAndIndex, Related.NUM64); var suggestionNotInString = suggestions.Where(sg => !modelDictionary.StringData.Any(s => s.Key == sg.Key)); var num64NotInNum = num64.Where(sg => !modelDictionary.NumData.Any(s => s.Key == sg.Key)); var relLocal = GetDictionaryFromRelated(propByRelatedAndIndex, Related.LOCAL_REFERENCE); if (suggestionNotInString.Any()) { foreach (var item in suggestionNotInString) { modelDictionary.StringData.Add(item.Key, item.Value); } } if (num64NotInNum.Any()) { foreach (var item in num64NotInNum) { modelDictionary.NumData.Add(item.Key, item.Value); } } if (relLocal.Any()) { foreach (var item in relLocal) { modelDictionary.relData.Add(item.Key, item.Value); } } return(modelDictionary); }