private void CheckTranslation(VmTreeItem source, ServiceServiceClass target)
 {
     target.ServiceClassId.Should().Be(source.Id);
 }
        public void ImportDataFromJSON()
        {
//            var importedGeneralDescriptions = JsonConvert.DeserializeObject<List<ImportStatutoryServiceGeneralDescription>>(File.ReadAllText(CreateGeneralDescriptionsJsonTask.GeneralDescriptionsGeneratedFile), new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });
            string langCode = LanguageCode.fi.ToString();
//
            //List<Guid> municipalityOrganizationIds = new List<Guid>();
            List <StatutoryServiceGeneralDescriptionVersioned> statutoryServiceGeneralDescriptions = new List <StatutoryServiceGeneralDescriptionVersioned>();
//            Language defaultLanguage = null;
//
            string draftCode = PublishingStatus.Draft.ToString();
//            //PublishingStatusType defaultPublishingType = null;
//
            var nameTypeName = NameTypeEnum.Name.ToString();
//            //NameType defaultNameType = null;
//
//
            var serviceTypeService = ServiceTypeEnum.Service.ToString();

//
//            using (var serviceScope = _serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
//            {
//                var service = serviceScope.ServiceProvider.GetService<IGeneralDescriptionUpdateService>();
//                service?.CreateOrUpdateGeneralDescriptions(importedGeneralDescriptions);
//            }


            // for performance/speed reason read these into memory (there are only ~130 entries on each table currently)

            Console.WriteLine();

            using (var serviceScope = _serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                var scopedCtxMgr = serviceScope.ServiceProvider.GetService <IContextManager>();

                scopedCtxMgr.ExecuteWriter(unitOfWork =>
                {
                    var municipalityRepository     = unitOfWork.CreateRepository <IMunicipalityRepository>();
                    var organizationRepository     = unitOfWork.CreateRepository <IOrganizationVersionedRepository>();
                    var organizationTypeRepository = unitOfWork.CreateRepository <IOrganizationTypeRepository>();
                    var statutoryServiceGeneralDescriptionRepository = unitOfWork.CreateRepository <IStatutoryServiceGeneralDescriptionVersionedRepository>();

                    // Exclude Helsinki and Mikkeli
                    var excludedMunicipalityIds = municipalityRepository.All().Where(x => x.MunicipalityNames.Any(y => y.Name.Equals("Helsinki") || y.Name.Equals("Mikkeli"))).Select(m => m.Id).ToList();

                    // Get all organizations that are municipalities (exclude Mikkeli and Helsinki)
                    var organizationMunicipalityTypeCode = OrganizationTypeEnum.Municipality.ToString();
                    var organizationMunicipalityTypeId   = organizationTypeRepository.All().Where(x => x.Code == organizationMunicipalityTypeCode).First().Id;

                    var municipalityOrganizationsUnificRoots =
                        organizationRepository.All()
                        .Where(x => x.TypeId == organizationMunicipalityTypeId && ((x.MunicipalityId == null) || ((x.MunicipalityId != null) && !excludedMunicipalityIds.Contains(x.MunicipalityId.Value))))
                        .Select(m => m.UnificRootId)
                        .ToList();

                    statutoryServiceGeneralDescriptions = statutoryServiceGeneralDescriptionRepository.All().ToList();

                    var existingStatutoryServiceNames   = unitOfWork.CreateRepository <IStatutoryServiceNameRepository>().All().ToList();
                    var existingStatutoryServiceClasses = unitOfWork.CreateRepository <IStatutoryServiceServiceClassRepository>().All().ToList();

                    var serviceRepository             = unitOfWork.CreateRepository <IServiceVersionedRepository>();
                    var serviceNameRepository         = unitOfWork.CreateRepository <IServiceNameRepository>();
                    var organizationServiceRepository = unitOfWork.CreateRepository <IOrganizationServiceRepository>();
                    var serviceServiceClassRepository = unitOfWork.CreateRepository <IServiceServiceClassRepository>();

                    var defaultPublishingType =
                        unitOfWork
                        .CreateRepository <IPublishingStatusTypeRepository>()
                        .All()
                        .FirstOrDefault(m => m.Code == draftCode);
                    var defaultLanguage =
                        unitOfWork.CreateRepository <ILanguageRepository>().All().First(x => x.Code == langCode);
                    var defaultNameType =
                        unitOfWork
                        .CreateRepository <INameTypeRepository>()
                        .All()
                        .FirstOrDefault(x => x.Code == nameTypeName);

                    var defaultAreaInformationType = unitOfWork
                                                     .CreateRepository <IAreaInformationTypeRepository>()
                                                     .All()
                                                     .FirstOrDefault(x => x.Code == AreaInformationTypeEnum.WholeCountry.ToString());

                    var defaultFundingType = unitOfWork.CreateRepository <IServiceFundingTypeRepository>()
                                             .All()
                                             .FirstOrDefault(x => x.Code == ServiceFundingTypeEnum.PubliclyFunded.ToString());


                    foreach (var statutoryServiceGeneralDescription in statutoryServiceGeneralDescriptions)
                    {
                        Console.Write("#");
                        // Link the created general description to each municipality by creating a new service
                        foreach (var municipalityOrganizationRootId in municipalityOrganizationsUnificRoots)
                        {
                            // Create service for each municipality organization
                            var service = new ServiceVersioned()
                            {
                                Created   = DateTime.UtcNow,
                                CreatedBy = CreateServiceDataForGeneralDescriptionsJsonTask.DefaultCreatedBy,
                                Id        = Guid.NewGuid(),
                                StatutoryServiceGeneralDescriptionId = statutoryServiceGeneralDescription.UnificRootId,
                                //ServiceCoverageTypeId = null,
                                Type                = null,
                                FundingType         = defaultFundingType,
                                AreaInformationType = defaultAreaInformationType,
                                PublishingStatus    = defaultPublishingType,
                                UnificRoot          = new Service()
                                {
                                    Id = Guid.NewGuid()
                                },
                                OrganizationId = municipalityOrganizationRootId
                            };

                            var serviceName = new ServiceName()
                            {
                                Created            = DateTime.UtcNow,
                                CreatedBy          = CreateServiceDataForGeneralDescriptionsJsonTask.DefaultCreatedBy,
                                ServiceVersionedId = service.Id,
                                Name =
                                    existingStatutoryServiceNames.FirstOrDefault(
                                        x => x.StatutoryServiceGeneralDescriptionVersionedId == statutoryServiceGeneralDescription.Id).Name,
                                Localization = defaultLanguage,
                                Type         = defaultNameType
                            };

                            // Create OrganizationService linking the service to organization (municipality)
                            var organizationService = new OrganizationService()
                            {
                                Created   = DateTime.UtcNow,
                                CreatedBy = CreateServiceDataForGeneralDescriptionsJsonTask.DefaultCreatedBy,

                                ServiceVersionedId = service.Id,
                                OrganizationId     = municipalityOrganizationRootId,
                            };

                            var statutoryServiceClass = existingStatutoryServiceClasses.FirstOrDefault(x => x.StatutoryServiceGeneralDescriptionVersionedId == statutoryServiceGeneralDescription.Id);
                            // Create ServiceServiceClass
                            if (statutoryServiceClass != null)
                            {
                                var serviceServiceClass = new ServiceServiceClass()
                                {
                                    Created            = DateTime.UtcNow,
                                    CreatedBy          = CreateServiceDataForGeneralDescriptionsJsonTask.DefaultCreatedBy,
                                    ServiceVersionedId = service.Id,
                                    ServiceClassId     = statutoryServiceClass.ServiceClassId
                                };
                                serviceServiceClassRepository.Add(serviceServiceClass);
                            }
                            serviceRepository.Add(service);
                            serviceNameRepository.Add(serviceName);
                            organizationServiceRepository.Add(organizationService);
                        }
                        unitOfWork.Save(SaveMode.NonTrackedDataMigration);
                    }
                });
            }
        }
 private void CheckTranslation(string source, ServiceServiceClass target)
 {
     target.ServiceClass.Uri.Should().Be(source);
 }