public async Task <VersionedField> GetVersionedFieldAsync <TEntity, TId>(TEntity owner, string fieldName) where TEntity : IEntity <TId> { var config = _entityConfigurationStore.Get(typeof(TEntity)); return(await _fieldRepository.GetAll() .FirstOrDefaultAsync(f => f.OwnerId == owner.Id.ToString() && f.OwnerType == config.TypeShortAlias && f.Name == fieldName)); }
/// <inheritdoc/> public async Task <object> GetAsync(string entityTypeShortAlias, string id) { var entityConfiguration = _entityConfigurationStore.Get(entityTypeShortAlias); if (entityConfiguration == null) { throw new Exception($"Failed to get a configuration of an entity with {nameof(EntityAttribute.TypeShortAlias)} = '{entityTypeShortAlias}'"); } return(await GetAsync(entityConfiguration.EntityType, id)); }
public async Task <List <AutocompleteItemDto> > ListAsync(string term, string typeShortAlias, bool allowInherited, Guid?selectedValue, string filter, CancellationToken cancellationToken) { try { if (string.IsNullOrWhiteSpace(typeShortAlias)) { return(new List <AutocompleteItemDto>()); } var entityConfig = _configurationStore.Get(typeShortAlias); if (entityConfig.DisplayNamePropertyInfo == null) { throw new Exception($"EntityDisplayName not found for entity of type `{entityConfig.EntityType.FullName}`, one of properties should be marked with the `EntityDisplayNameAttribute` to use generic autocomplete"); } var method = this.GetType().GetMethod(nameof(GetList)); if (method == null) { throw new Exception($"{nameof(GetList)} not found"); } var idProp = entityConfig.EntityType.GetProperty("Id"); if (idProp == null) { throw new Exception("Id property not found"); } var genericMethod = method.MakeGenericMethod(entityConfig.EntityType, idProp.PropertyType); var task = (Task)genericMethod.Invoke(this, new object[] { term, allowInherited, selectedValue, filter, cancellationToken }); await task.ConfigureAwait(false); var resultProperty = task.GetType().GetProperty("Result"); if (resultProperty == null) { throw new Exception("Result property not found"); } return(resultProperty.GetValue(task) as List <AutocompleteItemDto>); } catch (Exception e) { throw; } }
/// inheritedDoc public PropertyMetadataDto GetPropertyMetadata(PropertyInfo property) { var path = property.Name; var entityConfig = property.DeclaringType != null && property.DeclaringType.IsEntityType() ? _entityConfigurationStore.Get(property.DeclaringType) : null; var epc = entityConfig?[property.Name]; var dataType = GetDataType(property); var result = new PropertyMetadataDto { Path = path, Label = ReflectionHelper.GetDisplayName(property), Description = ReflectionHelper.GetDescription(property), IsVisible = property.GetAttribute <BrowsableAttribute>()?.Browsable ?? true, Required = property.HasAttribute <RequiredAttribute>(), Readonly = !property.CanWrite || (property.GetAttribute <ReadOnlyAttribute>()?.IsReadOnly ?? false), DataType = dataType.DataType, DataFormat = dataType.DataFormat, EntityTypeShortAlias = property.PropertyType.IsEntityType() ? _entityConfigurationStore.Get(property.PropertyType)?.SafeTypeShortAlias : null, ReferenceListName = epc?.ReferenceListName, ReferenceListNamespace = epc?.ReferenceListNamespace, EnumType = epc?.EnumType, OrderIndex = property.GetAttribute <DisplayAttribute>()?.GetOrder() ?? -1, IsFrameworkRelated = IsFrameworkRelatedProperty(property), //ConfigurableByUser = property.GetAttribute<BindableAttribute>()?.Bindable ?? true, //GroupName = ReflectionHelper.get(declaredProperty ?? property), }; if (dataType.DataType == DataTypes.Array) { result.ItemsType = GetItemsType(property); } return(result); }
protected override Task <List <ModelDto> > FetchModelsAsync() { var types = _typeFinder.FindAll().Where(t => t.IsEntityType()) .Select(t => { var config = _entityConfigurationStore.Get(t); return(new ModelDto { ClassName = t.FullName, Type = t, Description = ReflectionHelper.GetDescription(t), Alias = config?.SafeTypeShortAlias }); }) .ToList(); return(Task.FromResult(types)); }
private List <DataTableStoredFilter> GetStoredFilters(DataTableConfig config) { var filters = new List <DataTableStoredFilter>(); var roleRepo = IocManager.Resolve <IRepository <ShaRole, Guid> >(); var rolePersonRepo = IocManager.Resolve <IRepository <ShaRoleAppointedPerson, Guid> >(); var containerRepo = IocManager.Resolve <IRepository <StoredFilterContainer, Guid> >(); var mapper = IocManager.Resolve <IMapper>(); var existingFilters = containerRepo.GetAll() .Where(f => f.OwnerType == "" && f.OwnerId == config.Id && !f.Filter.IsDeleted).Select(f => f.Filter) .OrderBy(f => f.OrderIndex).ToList(); foreach (var filter in existingFilters) { // Security: when visibility conditions are provided, restrict the filter var hasAccess = true; if (filter.VisibleBy.Any()) { var shaRoleType = _entityConfigurationStore.Get(typeof(ShaRole))?.TypeShortAlias; var visibleByRoles = filter.VisibleBy.Where(v => v.OwnerType == shaRoleType) .Select(v => roleRepo.Get(v.OwnerId.ToGuid())).ToList(); hasAccess = false; var currentUser = GetCurrentUser(); foreach (var role in visibleByRoles) { if (rolePersonRepo.GetAll().Any(c => c.Role == role && c.Person == currentUser)) { hasAccess = true; break; } } } if (hasAccess) { filters.Add(mapper.Map <DataTableStoredFilter>(filter)); } } return(filters); }
private Type GetEntityReferenceType(EntityPropertyDto propertyDto, DynamicDtoTypeBuildingContext context) { if (propertyDto.DataType != DataTypes.EntityReference) { throw new NotSupportedException($"DataType {propertyDto.DataType} is not supported. Expected {DataTypes.EntityReference}"); } if (string.IsNullOrWhiteSpace(propertyDto.EntityType)) { return(null); } var entityConfig = _entityConfigurationStore.Get(propertyDto.EntityType); if (entityConfig == null) { return(null); } return(context.UseDtoForEntityReferences ? typeof(EntityWithDisplayNameDto <>).MakeGenericType(entityConfig.IdType) : entityConfig?.IdType); }
private async Task ProcessAssemblyAsync(Assembly assembly) { var entityTypes = assembly.GetTypes().Where(t => MappingHelper.IsEntity(t)).ToList(); // todo: remove usage of IEntityConfigurationStore var entitiesConfigs = entityTypes.Select(t => { var config = _entityConfigurationStore.Get(t); var codeProperties = _metadataProvider.GetProperties(t); return(new { Config = config, Properties = codeProperties, PropertiesMD5 = GetPropertiesMD5(codeProperties), }); }).ToList(); var dbEntities = await _entityConfigRepository.GetAll().ToListAsync(); // Update out-of-date configs var toUpdate = dbEntities .Select( ec => new { db = ec, code = entitiesConfigs.FirstOrDefault(c => c.Config.EntityType.Name == ec.ClassName && c.Config.EntityType.Namespace == ec.Namespace) }) .Where( c => c.code != null && (c.db.FriendlyName != c.code.Config.FriendlyName || c.db.TableName != c.code.Config.TableName || c.db.TypeShortAlias != c.code.Config.SafeTypeShortAlias || c.db.DiscriminatorValue != c.code.Config.DiscriminatorValue || c.db.PropertiesMD5 != c.code.PropertiesMD5)) .ToList(); foreach (var config in toUpdate) { config.db.FriendlyName = config.code.Config.FriendlyName; config.db.TableName = config.code.Config.TableName; config.db.TypeShortAlias = config.code.Config.SafeTypeShortAlias; config.db.DiscriminatorValue = config.code.Config.DiscriminatorValue; await _entityConfigRepository.UpdateAsync(config.db); if (config.db.PropertiesMD5 != config.code.PropertiesMD5) { await UpdatePropertiesAsync(config.db, config.code.Config.EntityType, config.code.Properties, config.code.PropertiesMD5); } } // Add news configs var toAdd = entitiesConfigs.Where(c => !dbEntities.Any(ec => ec.ClassName == c.Config.EntityType.Name && ec.Namespace == c.Config.EntityType.Namespace)).ToList(); foreach (var config in toAdd) { var ec = new EntityConfig() { FriendlyName = config.Config.FriendlyName, TableName = config.Config.TableName, TypeShortAlias = config.Config.SafeTypeShortAlias, DiscriminatorValue = config.Config.DiscriminatorValue, ClassName = config.Config.EntityType.Name, Namespace = config.Config.EntityType.Namespace, Source = Domain.Enums.MetadataSourceType.ApplicationCode, }; await _entityConfigRepository.InsertAsync(ec); await UpdatePropertiesAsync(ec, config.Config.EntityType, config.Properties, config.PropertiesMD5); } // Inactivate deleted entities // todo: write changelog }
/// <summary> /// Get entity configuration by type short alias /// </summary> public static EntityConfiguration GetEntityConfiguration(this string typeShortAlias) { return(_configurationStore.Get(typeShortAlias)); }
private List <KeyValuePair <string, GeneralDataType> > DoGetPropertiesForSqlQuickSearch(Type rowType, List <DataTableColumn> columns) { var entityConfig = _entityConfigurationStore.Get(rowType); var props = columns .OfType <DataTablesDisplayPropertyColumn>() .Select(c => { var currentEntityConfig = entityConfig; PropertyConfiguration property = null; if (c.PropertyName.Contains('.')) { var parts = c.PropertyName.Split('.'); for (int i = 0; i < parts.Length; i++) { if (!currentEntityConfig.Properties.TryGetValue(parts[i], out property)) { return(null); } if (!property.IsMapped) { return(null); } // all parts except the latest - entity reference // all parts mapped if (property.GeneralType == GeneralDataType.EntityReference) { currentEntityConfig = _entityConfigurationStore.Get(property.PropertyInfo.PropertyType); } else if (i != parts.Length - 1) { return(null); // only last part can be not an entity reference } } } else { currentEntityConfig.Properties.TryGetValue(c.PropertyName, out property); } if (property == null) { return(null); } if (property.PropertyInfo.Name == currentEntityConfig.CreatedUserPropertyInfo?.Name || property.PropertyInfo.Name == currentEntityConfig.LastUpdatedUserPropertyInfo?.Name || property.PropertyInfo.Name == currentEntityConfig.InactivateUserPropertyInfo?.Name) { return(null); } if (!property.IsMapped) { return(null); } return(new { Path = c.PropertyName, Property = property }); }) .Where(i => i != null) .Select(i => new KeyValuePair <string, GeneralDataType>(i.Path, i.Property.GeneralType)) .ToList(); return(props); }
public async Task <object> GetEntityPropertyAsync <TEntity, TId>(TEntity entity, string propertyName) where TEntity : class, IEntity <TId> { var dynamicProperty = (await DtoTypeBuilder.GetEntityPropertiesAsync(entity.GetType())) .FirstOrDefault(p => p.Source == MetadataSourceType.UserDefined && p.Name == propertyName); if (dynamicProperty == null) { return(null); } var serializedValue = await GetValueAsync(entity, dynamicProperty); if (serializedValue == null) { return(null); } Type simpleType = null; switch (dynamicProperty.DataType) { case DataTypes.EntityReference: { var entityConfig = _entityConfigurationStore.Get(dynamicProperty.EntityType); var id = SerializationManager.DeserializeProperty(entityConfig.IdType, serializedValue); if (id == null) { return(null); } return(await _dynamicRepository.GetAsync(entityConfig.EntityType, id.ToString())); } case DataTypes.Boolean: simpleType = typeof(bool?); break; case DataTypes.Guid: simpleType = typeof(Guid?); break; case DataTypes.String: simpleType = typeof(string); break; case DataTypes.Date: case DataTypes.DateTime: simpleType = typeof(DateTime?); break; case DataTypes.Number: { switch (dynamicProperty.DataFormat) { case NumberFormats.Int32: simpleType = typeof(int?); break; case NumberFormats.Int64: simpleType = typeof(Int64?); break; case NumberFormats.Double: simpleType = typeof(double?); break; case NumberFormats.Float: simpleType = typeof(float?); break; } break; } case DataTypes.Time: simpleType = typeof(TimeSpan?); break; case DataTypes.Object: simpleType = typeof(string); break; } var rawValue = serializedValue != null && simpleType != null ? SerializationManager.DeserializeProperty(simpleType, serializedValue) : null; return(rawValue); }