Exemple #1
0
 public SearchViewAdapterModel(ElasticStatUnit view, IEnumerable <string> personNames, IEnumerable <CodeLookupVm> mainActivities, RegionLookupVm region)
 {
     Mapper.Map(view, this);
     Persons                   = new PersonAdapterModel(string.Join(", ", personNames));
     Activities                = new ActivityAdapterModel(string.Join(", ", mainActivities.Select(x => x.Name)), string.Join(", ", mainActivities.Select(x => x.NameLanguage1)), string.Join(", ", mainActivities.Select(x => x.NameLanguage2)));
     Address.FullPath          = region?.FullPath;
     Address.FullPathLanguage1 = region?.FullPathLanguage1;
     Address.FullPathLanguage2 = region?.FullPathLanguage2;
 }
Exemple #2
0
        public async Task AddDocument(ElasticStatUnit elasticItem)
        {
            try
            {
                var insertResult = await _elasticClient.IndexAsync(elasticItem, i => i.Index(StatUnitSearchIndexName));

                if (!insertResult.IsValid)
                {
                    throw new Exception(insertResult.DebugInformation);
                }
            }
            catch (Exception)
            {
                await Synchronize();
            }
        }
Exemple #3
0
        /// <summary>
        /// Removing statunit from elastic
        /// </summary>
        /// <param name="statId">index of item in elastic</param>
        /// <param name="statUnitTypes">types of statunits</param>
        /// <returns></returns>
        public async Task DeleteDocumentAsync(ElasticStatUnit elasticItem)
        {
            try
            {
                var deleteResponse = await _elasticClient.DeleteAsync <ElasticStatUnit>(elasticItem.Id,
                                                                                        u => u.Index(StatUnitSearchIndexName));

                if (!deleteResponse.IsValid)
                {
                    throw new Exception(deleteResponse.DebugInformation);
                }
            }
            catch (Exception)
            {
                await Synchronize();
            }
        }
Exemple #4
0
        public async Task EditDocument(ElasticStatUnit elasticItem)
        {
            try
            {
                var updateResult = await _elasticClient.UpdateAsync <ElasticStatUnit, ElasticStatUnit>(elasticItem.Id,
                                                                                                       u => u.Index(StatUnitSearchIndexName).Doc(elasticItem));

                if (!updateResult.IsValid)
                {
                    throw new Exception(updateResult.DebugInformation);
                }
            }
            catch (Exception)
            {
                await Synchronize();
            }
        }
Exemple #5
0
        /// <summary>
        /// Method to get view model
        /// </summary>
        /// <param name = "id"> Id stat. units </param>
        /// <param name = "type"> Type of stat. units </param>
        /// <param name = "userId"> User Id </param>
        /// <param name = "ignoredActions"> </param>
        /// <returns> </returns>
        public async Task <StatUnitViewModel> GetViewModel(int?id, StatUnitTypes type, string userId, ActionsEnum ignoredActions)
        {
            bool isEmployee = await _userService.IsInRoleAsync(userId, DefaultRoleNames.Employee);

            var item = id.HasValue
                ? await _commonSvc.GetStatisticalUnitByIdAndType(id.Value, type, false)
                : GetDefaultDomainForType(type);

            if (item == null)
            {
                throw new BadRequestException(nameof(Resource.NotFoundMessage));
            }

            if ((ignoredActions == ActionsEnum.Edit) && isEmployee)
            {
                var helper     = new StatUnitCheckPermissionsHelper(_context);
                var mappedItem = new ElasticStatUnit();
                Mapper.Map(item, mappedItem);
                helper.CheckRegionOrActivityContains(userId, mappedItem.RegionIds, mappedItem.ActivityCategoryIds);
            }

            var dataAccess = await _userService.GetDataAccessAttributes(userId, item.UnitType);

            var config = type == StatUnitTypes.EnterpriseGroup
                ? _mandatoryFields.EnterpriseGroup
                : (object)_mandatoryFields.StatUnit;
            var mandatoryDict = config.GetType().GetProperties().ToDictionary(x => x.Name, GetValueFrom(config));

            if (type != StatUnitTypes.EnterpriseGroup)
            {
                object subConfig;
                switch (type)
                {
                case StatUnitTypes.LocalUnit:
                    subConfig = _mandatoryFields.LocalUnit;
                    break;

                case StatUnitTypes.LegalUnit:
                    subConfig = _mandatoryFields.LegalUnit;
                    break;

                case StatUnitTypes.EnterpriseUnit:
                    subConfig = _mandatoryFields.Enterprise;
                    break;

                default: throw new Exception("bad statunit type");
                }
                var getValue = GetValueFrom(subConfig);
                subConfig.GetType().GetProperties().ForEach(x => mandatoryDict[x.Name] = getValue(x));
            }

            //The LegalUnits field of the EnterpriseUnit type is required by business logic
            if (type == StatUnitTypes.EnterpriseUnit && mandatoryDict.ContainsKey("LegalUnits"))
            {
                mandatoryDict["LegalUnits"] = true;
            }

            return(StatUnitViewModelCreator.Create(item, dataAccess, mandatoryDict, ignoredActions));

            Func <PropertyInfo, bool> GetValueFrom(object source) => prop =>
            {
                var value = prop.GetValue(source);
                return(value is bool b && b);
            };
        }