using System; using System.Globalization; using System.IO; using System.Linq; using System.Text.Json; using System.Threading.Tasks; using Kurs.Languages.Entities; using Kurs.Notifications.Entities; using Kurs.Platform.Blog; using Kurs.Platform.Charts.Dto; using Kurs.Platform.Entities; using Kurs.Platform.Enums; using Kurs.Platform.Forum; using Kurs.Platform.ListForms; using Kurs.Platform.Seeds; using Kurs.Settings.Entities; using Microsoft.Extensions.Configuration; using Volo.Abp.Data; using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Repositories; using Volo.Abp.MultiTenancy; using Volo.Abp.PermissionManagement; using static Kurs.Settings.SettingsConsts; namespace Kurs.Platform.Data.Seeds; public class PlatformDataSeeder : IDataSeedContributor, ITransientDependency { private readonly IRepository _languages; private readonly IRepository _languageKey; private readonly IRepository _languagesText; private readonly IRepository _dataSources; private readonly IRepository _settings; private readonly IRepository _charts; private readonly IRepository _globalSearch; private readonly IRepository _backgroundWorkerRepository; private readonly IRepository _notificationRuleRepository; private readonly IRepository _menuRepository; private readonly IRepository _permissionGroupRepository; private readonly IRepository _permissionRepository; private readonly IRepository _sectorRepository; private readonly IRepository _uomCategoryRepository; private readonly IRepository _uomRepository; private readonly IRepository _currencyRepository; private readonly IRepository _countryGroupRepository; private readonly IRepository _countryRepository; private readonly IRepository _stateRepository; private readonly IRepository _skillTypeRepository; private readonly IRepository _skillRepository; private readonly IRepository _skillLevelRepository; private readonly IRepository _contactTagRepository; private readonly IRepository _contactTitleRepository; private readonly IRepository _blogCategoryRepository; private readonly IRepository _blogPostsRepository; private readonly IRepository _forumCategoryRepository; public PlatformDataSeeder( IRepository languages, IRepository languageKey, IRepository languagesText, IRepository dataSource, IRepository settings, IRepository charts, IRepository globalSearch, IRepository backgroundWorkerRepository, IRepository notificationRuleRepository, IRepository menuRepository, IRepository permissionGroupRepository, IRepository permissionRepository, IRepository sectorRepository, IRepository uomCategoryRepository, IRepository uomRepository, IRepository currencyRepository, IRepository countryGroupRepository, IRepository countryRepository, IRepository stateRepository, IRepository skillTypeRepository, IRepository skillRepository, IRepository skillLevelRepository, IRepository contactTagRepository, IRepository contactTitleRepository, IRepository blogCategoryRepository, IRepository blogPostsRepository, IRepository forumCategoryRepository ) { _languages = languages; _languageKey = languageKey; _languagesText = languagesText; _dataSources = dataSource; _settings = settings; _charts = charts; _globalSearch = globalSearch; _backgroundWorkerRepository = backgroundWorkerRepository; _notificationRuleRepository = notificationRuleRepository; _menuRepository = menuRepository; _permissionGroupRepository = permissionGroupRepository; _permissionRepository = permissionRepository; _sectorRepository = sectorRepository; _uomCategoryRepository = uomCategoryRepository; _uomRepository = uomRepository; _currencyRepository = currencyRepository; _countryGroupRepository = countryGroupRepository; _countryRepository = countryRepository; _stateRepository = stateRepository; _skillTypeRepository = skillTypeRepository; _skillRepository = skillRepository; _skillLevelRepository = skillLevelRepository; _contactTagRepository = contactTagRepository; _contactTitleRepository = contactTitleRepository; _blogCategoryRepository = blogCategoryRepository; _blogPostsRepository = blogPostsRepository; _forumCategoryRepository = forumCategoryRepository; } private static IConfigurationRoot BuildConfiguration() { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? ""}.json", true); return builder.Build(); } public async Task SeedAsync(DataSeedContext context) { var settings = await _settings.GetListAsync(); var dataSources = await _dataSources.GetListAsync(); var languages = await _languages.GetListAsync(); var keys = await _languageKey.GetListAsync(); var texts = await _languagesText.GetListAsync(); var charts = await _charts.GetListAsync(); var configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile(Path.Combine("Seeds", "SeederData.json")) .AddJsonFile(Path.Combine("Seeds", $"SeederData.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? ""}.json"), true) .Build(); var items = configuration.Get(); foreach (var item in items.Charts) { if (!charts.Any(a => a.ChartCode == item.ChartCode)) { await _charts.InsertAsync(new() { ChartCode = item.ChartCode, CultureName = item.CultureName, DataSourceCode = item.DataSourceCode, UserId = item.UserId, RoleId = item.RoleId, TitleJson = JsonSerializer.Serialize(new ChartTitleDto { Text = item.Title.Text, Subtitle = item.Title.Subtitle, }), DataSourceJson = JsonSerializer.Serialize(new ChartDataSourceDto { Query = item.DataSource.Query }), SeriesJson = JsonSerializer.Serialize(item.Series), CrosshairJson = JsonSerializer.Serialize(new ChartCrosshairDto() { Enabled = item.Crosshair.Enabled, Color = item.Crosshair.Color, DashStyle = item.Crosshair.DashStyle, HorizontalLine = new ChartCrosshairLineDto() { DashStyle = item.Crosshair.HorizontalLine.DashStyle } }), ArgumentAxisJson = JsonSerializer.Serialize(new ChartArgumentAxisDto() { Label = item.ArgumentAxis.Label }), SizeJson = JsonSerializer.Serialize(new ChartSizeDto { Width = item.Size.Width, Height = item.Size.Height }), PermissionJson = JsonSerializer.Serialize(new PermissionCrudDto { C = item.Permission.C, R = item.Permission.R, U = item.Permission.U, D = item.Permission.D }), }); } } foreach (var item in items.Settings) { if (!settings.Any(a => a.Code == item.Code)) { await _settings.InsertAsync(new() { Code = item.Code, NameKey = item.NameKey, DescriptionKey = item.DescriptionKey, DefaultValue = item.DefaultValue.Replace("\r\n", Environment.NewLine), IsVisibleToClients = item.IsVisibleToClients, IsInherited = item.IsInherited, IsEncrypted = item.IsEncrypted, MainGroupKey = item.MainGroupKey, SubGroupKey = item.SubGroupKey, RequiredPermissionName = item.RequiredPermissionName, DataType = item.DataType, Providers = item.Providers, SelectOptions = item.SelectOptions, Order = item.Order }); } } if (!dataSources.Any(a => a.Code == SeedConsts.DataSources.DefaultCode)) { var config = BuildConfiguration(); await _dataSources.InsertAsync(new() { Code = SeedConsts.DataSources.DefaultCode, DataSourceType = DefaultDatabaseProvider == DatabaseProvider.SqlServer ? DataSourceTypeEnum.Mssql : DataSourceTypeEnum.Postgresql, ConnectionString = config.GetConnectionString(DefaultDatabaseProvider) }); } foreach (var item in items.Languages) { if (!languages.Any(a => a.CultureName == item.CultureName)) { await _languages.InsertAsync(new() { CultureName = item.CultureName, UiCultureName = item.UiCultureName, DisplayName = item.DisplayName, IsEnabled = item.IsEnabled, TwoLetterISOLanguageName = new CultureInfo(item.CultureName).TwoLetterISOLanguageName, MultipleCultures = item.MultipleCultures, }); } } foreach (var item in items.LanguageTexts) { if (!keys.Any(a => a.Key == item.Key)) { await _languageKey.InsertAsync(new() { Key = item.Key, ResourceName = item.ResourceName, }); } if (!texts.Any(a => a.CultureName == "en" && a.Key == item.Key)) { await _languagesText.InsertAsync(new() { CultureName = "en", Key = item.Key, Value = item.En, ResourceName = item.ResourceName, }); } if (!texts.Any(a => a.CultureName == "tr" && a.Key == item.Key)) { await _languagesText.InsertAsync(new() { CultureName = "tr", Key = item.Key, Value = item.Tr, ResourceName = item.ResourceName, }); } } foreach (var item in items.GlobalSearch) { if (!await _globalSearch.AnyAsync(x => x.System == item.System && x.Group == item.Group && x.Term == item.Term)) { await _globalSearch.InsertAsync(new GlobalSearch { System = item.System, Group = item.Group, Term = item.Term, Weight = item.Weight, Url = item.Url }); } } foreach (var item in items.BackgroundWorkers) { if (!await _backgroundWorkerRepository.AnyAsync(x => x.Name == item.Name)) { await _backgroundWorkerRepository.InsertAsync(new BackgroundWorker { Name = item.Name, Cron = item.Cron, WorkerType = Enum.Parse(item.WorkerType), IsActive = item.IsActive, DataSourceCode = item.DataSourceCode }); } } foreach (var item in items.NotificationRules) { var exists = await _notificationRuleRepository.AnyAsync(x => x.NotificationType == item.NotificationType && x.RecipientType == item.RecipientType && x.RecipientId == item.RecipientId && x.Channel == item.Channel); if (!exists) { await _notificationRuleRepository.InsertAsync(new NotificationRule { NotificationType = item.NotificationType, RecipientType = item.RecipientType, RecipientId = item.RecipientId, Channel = item.Channel, IsActive = item.IsActive, IsFixed = item.IsFixed, IsCustomized = item.IsCustomized }); } } foreach (var item in items.Menus) { var exists = await _menuRepository.AnyAsync(x => x.Code == item.Code); if (!exists) { await _menuRepository.InsertAsync(new Menu { ParentCode = string.IsNullOrWhiteSpace(item.ParentCode) ? null : item.ParentCode, Code = item.Code, DisplayName = item.DisplayName, Order = item.Order, Url = item.Url, Icon = item.Icon, RequiredPermissionName = item.RequiredPermissionName, IsDisabled = item.IsDisabled }); } } foreach (var item in items.PermissionGroupDefinitionRecords) { var exists = await _permissionGroupRepository.AnyAsync(x => x.Name == item.Name); if (!exists) { await _permissionGroupRepository.InsertAsync(new PermissionGroupDefinitionRecord { Name = item.Name, DisplayName = item.DisplayName }); } } foreach (var item in items.PermissionDefinitionRecords) { var exists = await _permissionRepository.AnyAsync(x => x.Name == item.Name); if (!exists) { await _permissionRepository.InsertAsync(new PermissionDefinitionRecord { GroupName = item.GroupName, Name = item.Name, ParentName = string.IsNullOrWhiteSpace(item.ParentName) ? null : item.ParentName, DisplayName = item.DisplayName, IsEnabled = item.IsEnabled, MultiTenancySide = (MultiTenancySides)item.MultiTenancySide }); } } foreach (var item in items.Sectors) { var exists = await _sectorRepository.AnyAsync(x => x.Name == item.Name); if (!exists) { await _sectorRepository.InsertAsync(new Sector { Name = item.Name, FullName = item.FullName }); } } foreach (var item in items.UomCategories) { var exists = await _uomCategoryRepository.AnyAsync(x => x.Name == item.Name); if (!exists) { await _uomCategoryRepository.InsertAsync(new UomCategory { Name = item.Name }); } } foreach (var item in items.Uoms) { var exists = await _uomRepository.AnyAsync(x => x.Name == item.Name); if (!exists) { await _uomRepository.InsertAsync(new Uom { Name = item.Name, Type = item.Type, Ratio = item.Ratio, IsActive = item.IsActive, Rounding = item.Rounding, CategoryName = item.CategoryName }); } } foreach (var item in items.Currencies) { var exists = await _currencyRepository.AnyAsync(x => x.Code == item.Code); if (!exists) { await _currencyRepository.InsertAsync(new Currency { Code = item.Code, Symbol = item.Symbol, Name = item.Name, IsActive = item.IsActive }); } } foreach (var item in items.CountryGroups) { var exists = await _countryGroupRepository.AnyAsync(x => x.Name == item.Name); if (!exists) { await _countryGroupRepository.InsertAsync(new CountryGroup { Name = item.Name, }); } } foreach (var item in items.Countries) { var exists = await _countryRepository.AnyAsync(x => x.Code == item.Code); if (!exists) { await _countryRepository.InsertAsync(new Country { Code = item.Code, Name = item.Name, CurrencyCode = item.CurrencyCode, PhoneCode = item.PhoneCode, TaxLabel = item.TaxLabel, GroupName = item.GroupName }); } } foreach (var item in items.States) { var exists = await _stateRepository.AnyAsync(x => x.Name == item.Name && x.CountryCode == item.CountryCode); if (!exists) { await _stateRepository.InsertAsync(new State { Code = item.Code, Name = item.Name, CountryCode = item.CountryCode }); } } foreach (var item in items.SkillTypes) { var exists = await _skillTypeRepository.AnyAsync(x => x.Name == item.Name); if (!exists) { await _skillTypeRepository.InsertAsync(new SkillType { Name = item.Name, }); } } foreach (var item in items.Skills) { var exists = await _skillRepository.AnyAsync(x => x.Name == item.Name && x.TypeName == item.TypeName); if (!exists) { await _skillRepository.InsertAsync(new Skill { Name = item.Name, TypeName = item.TypeName }); } } foreach (var item in items.SkillLevels) { var exists = await _skillLevelRepository.AnyAsync(x => x.Name == item.Name && x.TypeName == item.TypeName); if (!exists) { await _skillLevelRepository.InsertAsync(new SkillLevel { Name = item.Name, TypeName = item.TypeName, Progress = item.Progress, IsDefault = item.IsDefault }); } } foreach (var item in items.ContactTags) { var exists = await _contactTagRepository.AnyAsync(x => x.Name == item.Name); if (!exists) { await _contactTagRepository.InsertAsync(new ContactTag { Name = item.Name, Category = item.Category, }); } } foreach (var item in items.ContactTitles) { var exists = await _contactTitleRepository.AnyAsync(x => x.Title == item.Title); if (!exists) { await _contactTitleRepository.InsertAsync(new ContactTitle { Title = item.Title, Abbreviation = item.Abbreviation, }); } } foreach (var item in items.BlogCategories) { var exists = await _blogCategoryRepository.AnyAsync(x => x.Name == item.Name); if (!exists) { var newCategory = new BlogCategory( item.Id, item.Name, item.Slug, item.Description ) { DisplayOrder = item.DisplayOrder, PostCount = item.PostCount }; await _blogCategoryRepository.InsertAsync(newCategory); } } foreach (var item in items.BlogPosts) { var exists = await _blogPostsRepository.AnyAsync(x => x.Title == item.Title); if (!exists) { await _blogPostsRepository.InsertAsync(new BlogPost( item.Id, item.Title, item.Slug, item.ContentTr, item.ContentEn, item.Summary, item.ReadTime, item.CoverImage, item.CategoryId, item.AuthorId )); } } foreach (var item in items.ForumCategories) { var exists = await _forumCategoryRepository.AnyAsync(x => x.Name == item.Name); if (!exists) { var newCategory = new ForumCategory( item.Id, item.Name, item.Slug, item.Description, item.Icon, item.DisplayOrder ); await _forumCategoryRepository.InsertAsync(newCategory); } } } }