Exemplo n.º 1
0
        public async Task <ActionResult> AvailableLanguages(bool enforce = false)
        {
            if (!_services.Permissions.Authorize(StandardPermissionProvider.ManageLanguages))
            {
                return(Content(T("Admin.AccessDenied.Description")));
            }

            var languages   = _languageService.GetAllLanguages(true);
            var languageDic = languages.ToDictionarySafe(x => x.LanguageCulture, StringComparer.OrdinalIgnoreCase);

            var downloadState = _asyncState.Get <LanguageDownloadState>();
            var translatedPercentageAtLastImport = _genericAttributeService.GetAttributes("TranslatedPercentageAtLastImport", "Language").ToDictionarySafe(x => x.EntityId);

            var checkResult = await CheckAvailableResources(enforce);

            var model = new AvailableLanguageListModel();

            model.Languages     = new List <AvailableLanguageModel>();
            model.Version       = checkResult.Version;
            model.ResourceCount = checkResult.ResourceCount;

            foreach (var resources in checkResult.Resources)
            {
                if (resources.Language.Culture.HasValue())
                {
                    languageDic.TryGetValue(resources.Language.Culture, out Language language);

                    var alModel = new AvailableLanguageModel();
                    PrepareAvailableLanguageModel(alModel, resources, translatedPercentageAtLastImport, language, downloadState);

                    model.Languages.Add(alModel);
                }
            }

            return(PartialView(model));
        }
Exemplo n.º 2
0
        private Dictionary <int, LastResourcesImportInfo> GetLastResourcesImportInfos()
        {
            Dictionary <int, LastResourcesImportInfo> result = null;

            try
            {
                var attributes = _genericAttributeService.GetAttributes("LastResourcesImportInfo", "Language").ToList();
                result = attributes.ToDictionarySafe(x => x.EntityId, x => JsonConvert.DeserializeObject <LastResourcesImportInfo>(x.Value));
            }
            catch (Exception exception)
            {
                Logger.Error(exception);
            }

            return(result ?? new Dictionary <int, LastResourcesImportInfo>());
        }
Exemplo n.º 3
0
        protected override void Import(ImportExecuteContext context)
        {
            var customer = _services.WorkContext.CurrentCustomer;
            var allowManagingCustomerRoles = _services.Permissions.Authorize(StandardPermissionProvider.ManageCustomerRoles, customer);

            var allAffiliateIds = _affiliateService.GetAllAffiliates(true)
                                  .Select(x => x.Id)
                                  .ToList();

            var allCountries = new Dictionary <string, int>();

            foreach (var country in _countryService.GetAllCountries(true))
            {
                if (!allCountries.ContainsKey(country.TwoLetterIsoCode))
                {
                    allCountries.Add(country.TwoLetterIsoCode, country.Id);
                }

                if (!allCountries.ContainsKey(country.ThreeLetterIsoCode))
                {
                    allCountries.Add(country.ThreeLetterIsoCode, country.Id);
                }
            }

            var allStateProvinces = _stateProvinceService.GetAllStateProvinces(true)
                                    .ToDictionarySafe(x => new Tuple <int, string>(x.CountryId, x.Abbreviation), x => x.Id);

            var allCustomerNumbers = new HashSet <string>(
                _genericAttributeService.GetAttributes(SystemCustomerAttributeNames.CustomerNumber, _attributeKeyGroup).Select(x => x.Value),
                StringComparer.OrdinalIgnoreCase);

            var allCustomerRoles = _customerRoleRepository.Table.ToDictionarySafe(x => x.SystemName, StringComparer.OrdinalIgnoreCase);

            using (var scope = new DbContextScope(ctx: _services.DbContext, autoDetectChanges: false, proxyCreation: false, validateOnSave: false, autoCommit: false))
            {
                var segmenter = context.DataSegmenter;

                Initialize(context);
                AddInfoForDeprecatedFields(context);

                while (context.Abort == DataExchangeAbortion.None && segmenter.ReadNextBatch())
                {
                    var batch = segmenter.GetCurrentBatch <Customer>();

                    _customerRepository.Context.DetachAll(true);

                    context.SetProgress(segmenter.CurrentSegmentFirstRowIndex - 1, segmenter.TotalRows);

                    // ===========================================================================
                    // Process customers
                    // ===========================================================================
                    try
                    {
                        ProcessCustomers(context, batch, allAffiliateIds);
                    }
                    catch (Exception exception)
                    {
                        context.Result.AddError(exception, segmenter.CurrentSegment, "ProcessCustomers");
                    }

                    // reduce batch to saved (valid) records.
                    // No need to perform import operations on errored records.
                    batch = batch.Where(x => x.Entity != null && !x.IsTransient).ToArray();

                    // update result object
                    context.Result.NewRecords      += batch.Count(x => x.IsNew && !x.IsTransient);
                    context.Result.ModifiedRecords += batch.Count(x => !x.IsNew && !x.IsTransient);

                    // ===========================================================================
                    // Process customer roles
                    // ===========================================================================
                    try
                    {
                        _customerRepository.Context.AutoDetectChangesEnabled = true;
                        ProcessCustomerRoles(context, batch, allCustomerRoles);
                    }
                    catch (Exception exception)
                    {
                        context.Result.AddError(exception, segmenter.CurrentSegment, "ProcessCustomerRoles");
                    }
                    finally
                    {
                        _customerRepository.Context.AutoDetectChangesEnabled = false;
                    }

                    // ===========================================================================
                    // Process generic attributes
                    // ===========================================================================
                    try
                    {
                        ProcessGenericAttributes(context, batch, allCountries, allStateProvinces, allCustomerNumbers);
                    }
                    catch (Exception exception)
                    {
                        context.Result.AddError(exception, segmenter.CurrentSegment, "ProcessGenericAttributes");
                    }

                    // ===========================================================================
                    // Process avatars
                    // ===========================================================================
                    if (_customerSettings.AllowCustomersToUploadAvatars)
                    {
                        try
                        {
                            ProcessAvatars(context, batch);
                        }
                        catch (Exception exception)
                        {
                            context.Result.AddError(exception, segmenter.CurrentSegment, "ProcessAvatars");
                        }
                    }

                    // ===========================================================================
                    // Process addresses
                    // ===========================================================================
                    try
                    {
                        _services.DbContext.AutoDetectChangesEnabled = true;
                        ProcessAddresses(context, batch, allCountries, allStateProvinces);
                    }
                    catch (Exception exception)
                    {
                        context.Result.AddError(exception, segmenter.CurrentSegment, "ProcessAddresses");
                    }
                    finally
                    {
                        _services.DbContext.AutoDetectChangesEnabled = false;
                    }
                }
            }
        }
Exemplo n.º 4
0
        protected override void Import(IImportExecuteContext context)
        {
            var customer = _services.WorkContext.CurrentCustomer;
            var allowManagingCustomerRoles = _services.Permissions.Authorize(StandardPermissionProvider.ManageCustomerRoles, customer);

            var allCustomerRoles = _customerService.GetAllCustomerRoles(true);

            var allAffiliateIds = _affiliateService.GetAllAffiliates(true)
                                  .Select(x => x.Id)
                                  .ToList();

            var allCountryIds = _countryService.GetAllCountries(true)
                                .Select(x => x.Id)
                                .ToList();

            var allStateProvinceIds = _stateProvinceService.GetAllStateProvinces(true)
                                      .Select(x => x.Id)
                                      .ToList();

            var allCustomerNumbers = _genericAttributeService.GetAttributes(SystemCustomerAttributeNames.CustomerNumber, _attributeKeyGroup)
                                     .Select(x => x.Value)
                                     .ToList();

            using (var scope = new DbContextScope(ctx: _services.DbContext, autoDetectChanges: false, proxyCreation: false, validateOnSave: false, autoCommit: false))
            {
                var segmenter = context.GetSegmenter <Customer>();

                Init(context, _dataExchangeSettings);

                context.Result.TotalRecords = segmenter.TotalRows;

                while (context.Abort == DataExchangeAbortion.None && segmenter.ReadNextBatch())
                {
                    var batch = segmenter.CurrentBatch;

                    _customerRepository.Context.DetachAll(false);

                    context.SetProgress(segmenter.CurrentSegmentFirstRowIndex - 1, segmenter.TotalRows);

                    try
                    {
                        ProcessCustomers(context, batch, allAffiliateIds, allCustomerRoles);
                    }
                    catch (Exception exception)
                    {
                        context.Result.AddError(exception, segmenter.CurrentSegment, "ProcessCustomers");
                    }

                    // reduce batch to saved (valid) records.
                    // No need to perform import operations on errored records.
                    batch = batch.Where(x => x.Entity != null && !x.IsTransient).ToArray();

                    // update result object
                    context.Result.NewRecords      += batch.Count(x => x.IsNew && !x.IsTransient);
                    context.Result.ModifiedRecords += batch.Count(x => !x.IsNew && !x.IsTransient);

                    try
                    {
                        _services.DbContext.AutoDetectChangesEnabled = true;

                        ProcessGenericAttributes(context, batch, allCountryIds, allStateProvinceIds, allCustomerNumbers);
                    }
                    catch (Exception exception)
                    {
                        context.Result.AddError(exception, segmenter.CurrentSegment, "ProcessGenericAttributes");
                    }
                    finally
                    {
                        _services.DbContext.AutoDetectChangesEnabled = false;
                    }
                }
            }
        }