Esempio n. 1
0
        public static StatUnitViewModel Create(
            IStatisticalUnit domainEntity,
            DataAccessPermissions dataAccess,
            IReadOnlyDictionary <string, bool> mandatoryFields,
            ActionsEnum ignoredActions)
        {
            var properties = GetFilteredProperties(domainEntity.GetType())
                             .Select(x => PropertyMetadataFactory.Create(
                                         x.PropInfo, domainEntity, x.Writable,
                                         mandatoryFields.TryGetValue(x.PropInfo.Name, out var mandatory) ? mandatory : (bool?)null));

            return(new StatUnitViewModel
            {
                StatUnitType = StatisticalUnitsTypeHelper.GetStatUnitMappingType(domainEntity.GetType()),
                Properties = properties,
                Permissions = dataAccess.Permissions.Where(x => properties.Any(d => x.PropertyName.EndsWith($".{d.LocalizeKey}"))).ToList() //TODO: Filter By Type (Optimization)
            });

            IEnumerable <(PropertyInfo PropInfo, bool Writable)> GetFilteredProperties(Type type)
            => type.GetProperties(BindingFlags.Instance | BindingFlags.Public)
            .Where(x =>
                   dataAccess.HasWriteOrReadPermission(DataAccessAttributesHelper.GetName(type, x.Name)) &&
                   x.CanRead &&
                   x.CanWrite &&
                   (x.GetCustomAttribute <NotMappedForAttribute>(true) == null ||
                    !x.GetCustomAttribute <NotMappedForAttribute>(true).Actions.HasFlag(ignoredActions))
                   )
            .OrderBy(x => ((DisplayAttribute)x.GetCustomAttribute(typeof(DisplayAttribute)))?.GetOrder() ?? int.MaxValue)
            .Select(x => (x, dataAccess.HasWritePermission(DataAccessAttributesHelper.GetName(type, x.Name))));
        }
Esempio n. 2
0
        private object GetValueOrDefault(string name)
        {
            var property = _statUnit.GetType().GetProperty(name);

            if (property == null)
            {
                _logger.Info($"{_statUnit.GetType().FullName} class doesn't have property {name}");
            }
            return(property?.GetValue(_statUnit));
        }
Esempio n. 3
0
        public void CheckBeforeDelete(IStatisticalUnit unit, bool toDelete)
        {
            if (toDelete)
            {
                switch (unit.GetType().Name)
                {
                case nameof(LocalUnit):
                {
                    var localUnit = unit as LocalUnit;
                    var legalUnit = _dbContext.LegalUnits.Include(x => x.LocalUnits).FirstOrDefault(x => localUnit.LegalUnitId == x.RegId && !x.IsDeleted);
                    if (legalUnit != null && legalUnit.LocalUnits.Count == 1)
                    {
                        throw new BadRequestException(nameof(Resource.DeleteLocalUnit));
                    }
                    break;
                }

                case nameof(EnterpriseUnit):
                {
                    var entUnit = unit as EnterpriseUnit;
                    if (entUnit.LegalUnits.Count > 0)
                    {
                        throw new BadRequestException(nameof(Resource.DeleteEnterpriseUnit));
                    }
                    break;
                }
                }
            }
            else
            {
                switch (unit.GetType().Name)
                {
                case nameof(LocalUnit):
                {
                    var localUnit = unit as LocalUnit;
                    var legalUnit = _dbContext.LegalUnits.Include(x => x.LocalUnits).FirstOrDefault(x => localUnit.LegalUnitId == x.RegId);
                    if (legalUnit != null && legalUnit.IsDeleted == true)
                    {
                        throw new BadRequestException(nameof(Resource.RestoreLocalUnit));
                    }
                    break;
                }

                case nameof(EnterpriseUnit):
                {
                    var entUnit = unit as EnterpriseUnit;
                    if (entUnit.LegalUnits.Any(x => x.IsDeleted == true))
                    {
                        throw new BadRequestException(nameof(Resource.RestoreEnterpriseUnit));
                    }
                    break;
                }
                }
            }
        }
Esempio n. 4
0
 private static string SimpleValueExtractor(IStatisticalUnit unit, FieldEnum field)
 {
     return(unit.GetType().GetProperty(field.ToString())?.GetValue(unit, null)?.ToString() ?? string.Empty);
 }