public ViewResult CommitImportResources(bool?previewImport, bool?showMenu, ICollection <DetectedImportChange> changes)
        {
            var model = new ImportResourcesViewModel {
                ShowMenu = showMenu ?? false
            };

            try
            {
                // prepare incoming model a bit
                // if change is selected and translation and/or language is `null` -> most probably this means that translation was empty
                // but Mvc model binder set it to `null` -> we need to fix this to get functionality to set empty translations via import process
                var importer = new ResourceImportWorkflow();
                var detectedImportChanges = changes.Where(c => c.Selected)
                                            .ForEach(c =>
                {
                    c.ImportingResource.Translations.ForEach(t =>
                    {
                        t.Value    = t.Value ?? (t.Value = string.Empty);
                        t.Language = t.Language ?? (t.Language = string.Empty);
                    });
                })
                                            .ToList();

                var result = importer.ImportChanges(detectedImportChanges);

                ViewData["LocalizationProvider_ImportResult"] = string.Join("<br/>", result);
            }
            catch (Exception e)
            {
                ModelState.AddModelError("importFailed", $"Import failed! Reason: {e.Message}");
            }

            return(View("ImportResources", model));
        }
Exemple #2
0
        public ViewResult ImportResources(bool?previewImport, HttpPostedFileBase importFile, bool?showMenu)
        {
            var model = new ImportResourcesViewModel {
                ShowMenu = showMenu ?? false
            };

            if (importFile == null || importFile.ContentLength == 0)
            {
                return(View("ImportResources", model));
            }

            var fileInfo        = new FileInfo(importFile.FileName);
            var potentialParser = ConfigurationContext.Current.Import.Providers.FindByExtension(fileInfo.Extension);

            if (potentialParser == null)
            {
                ModelState.AddModelError("file", $"Unknown file extension - `{fileInfo.Extension}`");
                return(View("ImportResources", model));
            }

            var workflow     = new ResourceImportWorkflow();
            var streamReader = new StreamReader(importFile.InputStream);
            var fileContent  = streamReader.ReadToEnd();
            var allLanguages = new AvailableLanguages.Query().Execute();

            try
            {
                var parseResult        = potentialParser.Parse(fileContent);
                var differentLanguages = parseResult.DetectedLanguages.Except(allLanguages);
                if (differentLanguages.Any())
                {
                    ModelState.AddModelError("file", $"Importing language `{string.Join(", ", differentLanguages.Select(c => c.Name))}` is not available in current EPiServer installation");
                    return(View("ImportResources", model));
                }

                if (previewImport.HasValue && previewImport.Value)
                {
                    var changes          = workflow.DetectChanges(parseResult.Resources, new GetAllResources.Query(true).Execute());
                    var changedLanguages = changes.SelectMany(c => c.ChangedLanguages).Distinct().Select(l => new CultureInfo(l));

                    var previewModel = new PreviewImportResourcesViewModel(changes, showMenu ?? false, changedLanguages);

                    return(View("ImportPreview", previewModel));
                }

                var result = workflow.Import(parseResult.Resources, previewImport ?? true);
                ViewData["LocalizationProvider_ImportResult"] = result;
            }
            catch (Exception e)
            {
                ModelState.AddModelError("importFailed", $"Import failed! Reason: {e.Message}");
            }

            return(View("ImportResources", model));
        }
Exemple #3
0
        public ViewResult ImportResources(bool?previewImport, HttpPostedFileBase importFile, bool?showMenu)
        {
            var model = new ImportResourcesViewModel {
                ShowMenu = showMenu ?? false
            };

            if (importFile == null || importFile.ContentLength == 0)
            {
                return(View("ImportResources", model));
            }

            var fileInfo = new FileInfo(importFile.FileName);

            if (fileInfo.Extension.ToLower() != ".json")
            {
                ModelState.AddModelError("file", "Uploaded file has different extension. Json file expected");
                return(View("ImportResources", model));
            }

            var importer     = new ResourceImporter();
            var serializer   = new JsonDataSerializer();
            var streamReader = new StreamReader(importFile.InputStream);
            var fileContent  = streamReader.ReadToEnd();

            try
            {
                var newResources = serializer.Deserialize <IEnumerable <LocalizationResource> >(fileContent);

                if (previewImport.HasValue && previewImport.Value)
                {
                    var changes = importer.DetectChanges(newResources, new GetAllResources.Query().Execute());

                    var availableLanguagesQuery = new AvailableLanguages.Query();
                    var languages = availableLanguagesQuery.Execute();

                    var previewModel = new PreviewImportResourcesViewModel(changes, showMenu ?? false, languages);

                    return(View("ImportPreview", previewModel));
                }

                var result = importer.Import(newResources, previewImport ?? true);

                ViewData["LocalizationProvider_ImportResult"] = result;
            }
            catch (Exception e)
            {
                ModelState.AddModelError("importFailed", $"Import failed! Reason: {e.Message}");
            }

            return(View("ImportResources", model));
        }
Exemple #4
0
        public ViewResult CommitImportResources(bool?previewImport, bool?showMenu, ICollection <DetectedImportChange> changes)
        {
            var model = new ImportResourcesViewModel
            {
                ShowMenu = showMenu ?? false
            };

            try
            {
                var importer = new ResourceImportWorkflow();
                var result   = importer.ImportChanges(changes.Where(c => c.Selected).ToList());

                ViewData["LocalizationProvider_ImportResult"] = string.Join("<br/>", result);
            }
            catch (Exception e)
            {
                ModelState.AddModelError("importFailed", $"Import failed! Reason: {e.Message}");
            }

            return(View("ImportResources", model));
        }
Exemple #5
0
        public ViewResult ImportResources(bool?importOnlyNewContent, HttpPostedFileBase importFile, bool?showMenu)
        {
            var model = new ImportResourcesViewModel
            {
                ShowMenu = showMenu ?? false
            };

            if (importFile == null || importFile.ContentLength == 0)
            {
                return(View("ImportResources", model));
            }

            var fileInfo = new FileInfo(importFile.FileName);

            if (fileInfo.Extension.ToLower() != ".json")
            {
                ModelState.AddModelError("file", "Uploaded file has different extension. Json file expected");
                return(View("ImportResources", model));
            }

            var importer     = new ResourceImporter();
            var serializer   = new JsonDataSerializer();
            var streamReader = new StreamReader(importFile.InputStream);
            var fileContent  = streamReader.ReadToEnd();

            try
            {
                var newResources = serializer.Deserialize <IEnumerable <LocalizationResource> >(fileContent);
                var result       = importer.Import(newResources, importOnlyNewContent ?? true);

                ViewData["LocalizationProvider_ImportResult"] = result;
            }
            catch (Exception e)
            {
                ModelState.AddModelError("importFailed", $"Import failed! Reason: {e.Message}");
            }

            return(View("ImportResources", model));
        }