Пример #1
0
        /// <summary>
        /// Convert an import report into JSON data
        /// </summary>
        /// <param name="report"></param>
        /// <returns></returns>
        private static object ToJSON(CsvReport report)
        {
            var oSerializer = new JavaScriptSerializer();
            var json = new
            {
                HasErrors = report.Errors.Any(),
                HasWarnings = report.Warnings.Any(),
                Warnings = oSerializer.Serialize(report.Warnings.ExtractMessages()),
                Errors = oSerializer.Serialize(report.Errors.ExtractMessages()),
            };

            return json;
        }
Пример #2
0
        /// <summary>
        /// Extract users from CSV format and import them
        /// </summary>
        /// <returns></returns>
        public CsvReport FromCsv(List<string> allLines)
        {
            var usersProcessed = new List<string>();
            var commaSeparator = new[] { ',' };
            var report = new CsvReport();

            if (allLines == null || allLines.Count == 0)
            {
                report.Errors.Add(new CsvErrorWarning
                {
                    ErrorWarningType = CsvErrorWarningType.BadDataFormat,
                    Message = "No users found."
                });
                return report;
            }
            var settings = _settingsService.GetSettings(true);
            var lineCounter = 0;
            foreach (var line in allLines)
            {
                try
                {
                    lineCounter++;

                    // Each line is made up of n items in a predefined order

                    var values = line.Split(commaSeparator);

                    if (values.Length < 2)
                    {
                        report.Errors.Add(new CsvErrorWarning
                        {
                            ErrorWarningType = CsvErrorWarningType.MissingKeyOrValue,
                            Message = $"Line {lineCounter}: insufficient values supplied."
                        });

                        continue;
                    }

                    var userName = values[0];

                    if (userName.IsNullEmpty())
                    {
                        report.Errors.Add(new CsvErrorWarning
                        {
                            ErrorWarningType = CsvErrorWarningType.MissingKeyOrValue,
                            Message = $"Line {lineCounter}: no username supplied."
                        });

                        continue;
                    }

                    var email = values[1];
                    if (email.IsNullEmpty())
                    {
                        report.Errors.Add(new CsvErrorWarning
                        {
                            ErrorWarningType = CsvErrorWarningType.MissingKeyOrValue,
                            Message = $"Line {lineCounter}: no email supplied."
                        });

                        continue;
                    }

                    // get the user
                    var userToImport = GetUser(userName);

                    if (userToImport != null)
                    {
                        report.Errors.Add(new CsvErrorWarning
                        {
                            ErrorWarningType = CsvErrorWarningType.AlreadyExists,
                            Message = $"Line {lineCounter}: user already exists in forum."
                        });

                        continue;
                    }

                    if (usersProcessed.Contains(userName))
                    {
                        report.Errors.Add(new CsvErrorWarning
                        {
                            ErrorWarningType = CsvErrorWarningType.AlreadyExists,
                            Message = $"Line {lineCounter}: user already exists in import file."
                        });

                        continue;
                    }

                    usersProcessed.Add(userName);

                    userToImport = CreateEmptyUser();
                    userToImport.UserName = userName;
                    userToImport.Slug = ServiceHelpers.GenerateSlug(userToImport.UserName, GetUserBySlugLike(ServiceHelpers.CreateUrl(userToImport.UserName)), userToImport.Slug);
                    userToImport.Email = email;
                    userToImport.IsApproved = true;
                    userToImport.PasswordSalt = StringUtils.CreateSalt(AppConstants.SaltSize);

                    string createDateStr = null;
                    if (values.Length >= 3)
                    {
                        createDateStr = values[2];
                    }
                    userToImport.CreateDate = createDateStr.IsNullEmpty() ? DateTime.UtcNow : DateTime.Parse(createDateStr);

                    if (values.Length >= 4)
                    {
                        userToImport.Age = Int32.Parse(values[3]);
                    }
                    if (values.Length >= 5)
                    {
                        userToImport.Location = values[4];
                    }
                    if (values.Length >= 6)
                    {
                        userToImport.Website = values[5];
                    }
                    if (values.Length >= 7)
                    {
                        userToImport.Facebook = values[6];
                    }
                    if (values.Length >= 8)
                    {
                        userToImport.Signature = values[7];
                    }
                    userToImport.Roles = new List<MembershipRole> { settings.NewMemberStartingRole };
                    Add(userToImport);
                }
                catch (Exception ex)
                {
                    report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.GeneralError, Message = ex.Message });
                }
            }

            return report;
        }
Пример #3
0
        /// <summary>
        /// Import a language from CSV
        /// </summary>
        /// <param name="langKey"> </param>
        /// <param name="allLines"></param>
        /// <returns>A report on the import</returns>
        public CsvReport FromCsv(string langKey, List<string> allLines)
        {
            var commaSeparator = new[] { ',' };
            var report = new CsvReport();

            if (allLines == null || allLines.Count == 0)
            {
                report.Errors.Add(new CsvErrorWarning
                {
                    ErrorWarningType = CsvErrorWarningType.BadDataFormat,
                    Message = "No language keys or values found."
                });
                return report;
            }

            // Look up the language and culture
            Language language;

            try
            {
                var cultureInfo = LanguageUtils.GetCulture(langKey);

                if (cultureInfo == null)
                {
                    report.Errors.Add(new CsvErrorWarning
                    {
                        ErrorWarningType = CsvErrorWarningType.DoesNotExist,
                        Message = string.Format("The language culture '{0}' does not exist.", langKey)
                    });

                    return report;
                }

                // See if this language exists already, and if not then create it
                language = GetLanguageByLanguageCulture(langKey) ?? Add(cultureInfo);
            }
            catch (LanguageOrCultureAlreadyExistsException ex)
            {
                report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.AlreadyExists, Message = ex.Message });
                return report;
            }
            catch (Exception ex)
            {
                report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.ItemBad, Message = ex.Message });
                return report;
            }

            return FromCsv(language, allLines);
        }
Пример #4
0
        public CsvReport FromCsv(Language language, List<string> allLines)
        {
            var commaSeparator = new[] { ',' };
            var report = new CsvReport();

            if (allLines == null || allLines.Count == 0)
            {
                report.Errors.Add(new CsvErrorWarning
                {
                    ErrorWarningType = CsvErrorWarningType.BadDataFormat,
                    Message = "No language keys or values found."
                });
                return report;
            }

            try
            {
                //var allResourceKeys = _localizationRepository.GetAllResourceKeys();
                var lineCounter = 0;
                foreach (var line in allLines)
                {
                    lineCounter++;

                    //var keyValuePair = line.Split(commaSeparator);
                    var keyValuePair = line.Split(commaSeparator, 2, StringSplitOptions.None);

                    if (keyValuePair.Length < 2)
                    {
                        report.Errors.Add(new CsvErrorWarning
                        {
                            ErrorWarningType = CsvErrorWarningType.MissingKeyOrValue,
                            Message = string.Format("Line {0}: a key and a value are required.", lineCounter)
                        });

                        continue;
                    }

                    var key = keyValuePair[0];

                    if (string.IsNullOrEmpty(key))
                    {
                        // Ignore empty keys
                        continue;
                    }
                    key = key.Trim();

                    var value = keyValuePair[1];

                    var resourceKey = _localizationRepository.GetResourceKey(key);

                    if (language == null)
                    {
                        throw new ApplicationException(string.Format("Unable to create language"));
                    }

                    // If key does not exist, it is a new one to be created
                    if (resourceKey == null)
                    {
                        resourceKey = new LocaleResourceKey
                        {
                            Name = key,
                            DateAdded = DateTime.UtcNow,
                        };

                        Add(resourceKey);
                        report.Warnings.Add(new CsvErrorWarning
                        {
                            ErrorWarningType = CsvErrorWarningType.NewKeyCreated,
                            Message = string.Format("A new key named '{0}' has been created, and will require a value in all languages.", key)
                        });
                    }

                    // In the new language (only) set the value for the resource
                    var stringResource = language.LocaleStringResources.FirstOrDefault(res => res.LocaleResourceKey.Name == resourceKey.Name);
                    if (stringResource != null)
                    {
                        if (!stringResource.ResourceValue.Equals(value))
                        {
                            stringResource.ResourceValue = value;   
                        }                     
                    }
                    else
                    {
                        // No string resources have been created, so most probably
                        // this is the installer creating the keys so we need to create the 
                        // string resource to go with it and add it
                        stringResource = new LocaleStringResource
                        {
                            Language = language,
                            LocaleResourceKey = resourceKey,
                            ResourceValue = value
                        };
                        _localizationRepository.Add(stringResource);
                    }
                }
            }
            catch (Exception ex)
            {
                report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.GeneralError, Message = ex.Message });
            }

            _cacheService.ClearStartsWith(AppConstants.LocalisationCacheName);
            return report;
        }
Пример #5
0
        /// <summary>
        /// Import a language from CSV
        /// </summary>
        /// <param name="langKey"> </param>
        /// <param name="allLines"></param>
        /// <returns>A report on the import</returns>
        public CsvReport FromCsv(string langKey, List<string> allLines)
        {
            var commaSeparator = new[] { ',' };
            var report = new CsvReport();

            if (allLines == null || allLines.Count == 0)
            {
                report.Errors.Add(new CsvErrorWarning
                {
                    ErrorWarningType = CsvErrorWarningType.BadDataFormat,
                    Message = "No language keys or values found."
                });
                return report;
            }

            // Look up the language and culture
            Language language;

            try
            {
                var cultureInfo = LanguageUtils.GetCulture(langKey);

                if (cultureInfo == null)
                {
                    report.Errors.Add(new CsvErrorWarning
                    {
                        ErrorWarningType = CsvErrorWarningType.DoesNotExist,
                        Message = string.Format("The language culture '{0}' does not exist.", langKey)
                    });

                    return report;
                }

                // See if this language exists already, and if not then create it
                language = GetLanguageByLanguageCulture(langKey) ?? Add(cultureInfo);
            }
            catch (LanguageOrCultureAlreadyExistsException ex)
            {
                report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.AlreadyExists, Message = ex.Message });
                return report;
            }
            catch (Exception ex)
            {
                report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.ItemBad, Message = ex.Message });
                return report;
            }

            try
            {
                var lineCounter = 0;
                foreach (var line in allLines)
                {
                    lineCounter++;

                    var keyValuePair = line.Split(commaSeparator);

                    if (keyValuePair.Length < 2)
                    {
                        report.Errors.Add(new CsvErrorWarning
                        {
                            ErrorWarningType = CsvErrorWarningType.MissingKeyOrValue,
                            Message = string.Format("Line {0}: a key and a value are required.", lineCounter)
                        });

                        continue;
                    }

                    var key = keyValuePair[0];

                    if (string.IsNullOrEmpty(key))
                    {
                        // Ignore empty keys
                        continue;
                    }
                    key = key.Trim();

                    var value = keyValuePair[1];

                    var resourceKey = _localizationRepository.GetResourceKey(key);

                    if (language == null)
                    {
                        throw new ApplicationException(string.Format("Unable to create language"));
                    }

                    // If key does not exist, it is a new one to be created
                    if (resourceKey == null)
                    {
                        resourceKey = new LocaleResourceKey
                                          {
                                              Name = key,
                                              DateAdded = DateTime.UtcNow,
                                          };

                        Add(resourceKey);
                        report.Warnings.Add(new CsvErrorWarning
                        {
                            ErrorWarningType = CsvErrorWarningType.NewKeyCreated,
                            Message = string.Format("A new key named '{0}' has been created, and will require a value in all languages.", key)
                        });
                    }

                    // In the new language (only) set the value for the resource
                    var stringResources = language.LocaleStringResources.Where(res => res.LocaleResourceKey.Name == resourceKey.Name).ToList();
                    if (stringResources.Any())
                    {
                        foreach (var res in stringResources)
                        {
                            res.ResourceValue = value;
                            break;
                        }
                    }
                    else
                    {
                        // No string resources have been created, so most probably
                        // this is the installer creating the keys so we need to create the
                        // string resource to go with it and add it
                        var stringResource = new LocaleStringResource
                            {
                                Language = language,
                                LocaleResourceKey = resourceKey,
                                ResourceValue = value
                            };
                        _localizationRepository.Add(stringResource);
                    }
                }
            }
            catch (Exception ex)
            {
                report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.GeneralError, Message = ex.Message });
            }

            return report;
        }
Пример #6
0
        public WrappedJsonResult ImportLanguage(string languageCulture, HttpPostedFileBase file)
        {
            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                var report = new CsvReport();

                //http://www.dustinhorne.com/post/2011/11/16/AJAX-File-Uploads-with-jQuery-and-MVC-3.aspx
                try
                {
                    // Verify that the user selected a file
                    if (file != null && file.ContentLength > 0)
                    {
                        // Unpack the data
                        var allLines = new List<string>();
                        using (var streamReader = new StreamReader(file.InputStream, System.Text.Encoding.UTF8, true))
                        {
                            while (streamReader.Peek() >= 0)
                            {
                                allLines.Add(streamReader.ReadLine());
                            }
                        }

                        // Read the CSV file and generate a language
                        report = _localizationService.FromCsv(languageCulture, allLines);
                        unitOfWork.Commit();
                    }
                    else
                    {
                        report.Errors.Add(new CsvErrorWarning
                        {
                            ErrorWarningType = CsvErrorWarningType.BadDataFormat,
                            Message = "File does not contain a language."
                        });
                    }
                }
                catch (Exception ex)
                {
                    unitOfWork.Rollback();
                    report.Errors.Add(new CsvErrorWarning
                    {
                        ErrorWarningType = CsvErrorWarningType.GeneralError,
                        Message = string.Format("Unable to import language: {0}", ex.Message)
                    });
                }

                return new WrappedJsonResult { Data = ToJSON(report) };
            }
        }
Пример #7
0
        private InstallerResult AddOrUpdateTheDefaultLanguageStrings(InstallerResult installerResult)
        {            
            // Read in CSV and import like it does normally in the admin section
            using (var unitOfWork = _UnitOfWorkManager.NewUnitOfWork())
            {
                var report = new CsvReport();

                try
                {
                        // Get the base language file
                        var file = System.Web.HttpContext.Current.Server.MapPath(@"~/Installer/en-GB.csv");

                        // Verify that the user selected a file
                        if (file != null)
                        {
                            // Unpack the data
                            var allLines = new List<string>();
                            using (var streamReader = new StreamReader(file, System.Text.Encoding.UTF8, true))
                            {
                                while (streamReader.Peek() >= 0)
                                {
                                    allLines.Add(streamReader.ReadLine());
                                }
                            }

                            // Read the CSV file and generate a language
                            report = _localizationService.FromCsv("en-GB", allLines);
                        }

                        unitOfWork.Commit();
                }
                catch (Exception ex)
                {
                    unitOfWork.Rollback();

                    //Loop through report errors and spit them out in the installer result message?
                    var sb = new StringBuilder();
                    foreach (var error in report.Errors)
                    {
                        if (error.ErrorWarningType == CsvErrorWarningType.BadDataFormat ||
                            error.ErrorWarningType == CsvErrorWarningType.GeneralError)
                        {
                            sb.AppendFormat("{0}<br />", error.Message);
                        }
                    }

                    installerResult.Exception = ex.InnerException;
                    installerResult.Message = "Error creating the initial data >>  Language Strings";
                    if (!string.IsNullOrEmpty(sb.ToString()))
                    {
                        installerResult.Message += string.Concat("<br />", sb.ToString());
                    }
                    installerResult.Successful = false;
                }
                return installerResult; 
            }
        }