private void ParseMultipleProps() { var unit = new LegalUnit {Name = "1", NumOfPeopleEmp = 1, EmployeesDate = DateTime.Now.AddYears(-1)}; var sourceProps = new[] {"namee", "peopleNum", "emp_date", "address_id"}; var mapping = new Dictionary<string, string[]> { [sourceProps[0]] = new[] {nameof(unit.Name)}, [sourceProps[1]] = new[] {nameof(unit.NumOfPeopleEmp)}, [sourceProps[2]] = new[] {nameof(unit.EmployeesDate)}, [sourceProps[3]] = new[] {nameof(unit.AddressId)}, }; var expected = new[] { "new name", 100500.ToString(), DateTime.Now.ToString(CultureInfo.InvariantCulture), "1", }; var raw = new Dictionary<string, object> { [sourceProps[0]] = expected[0], [sourceProps[1]] = expected[1], [sourceProps[2]] = expected[2], [sourceProps[3]] = expected[3], }; StatUnitKeyValueParser.ParseAndMutateStatUnit(mapping, raw, unit); Assert.Equal(expected[0], unit.Name); Assert.Equal(expected[1], unit.NumOfPeopleEmp.ToString()); Assert.Equal(expected[2], unit.EmployeesDate?.ToString(CultureInfo.InvariantCulture)); Assert.Equal(string.IsNullOrEmpty(expected[3]), !unit.AddressId.HasValue); }
/// <summary> /// Creating a legal unit with a local unit and an enterprise /// </summary> /// <param name="legalUnit"></param> /// <returns></returns> public async Task CreateLegalWithEnterpriseAndLocal(LegalUnit legalUnit) { LegalUnit createdLegal; EnterpriseUnit createdEnterprise = null; LocalUnit createdLocal = null; using (var transaction = _dbContext.Database.BeginTransaction()) { try { createdLegal = await CreateStatUnitAsync(legalUnit); if (legalUnit.EnterpriseUnitRegId == null || legalUnit.EnterpriseUnitRegId == 0) { var sameStatIdEnterprise = _dbContext.EnterpriseUnits.FirstOrDefault(eu => eu.StatId == legalUnit.StatId); if (sameStatIdEnterprise != null) { await LinkEnterpriseToLegalAsync(sameStatIdEnterprise, createdLegal); } else { createdEnterprise = await CreateEnterpriseForLegalAsync(createdLegal); } } var addressIds = legalUnit.LocalUnits.Where(x => x.AddressId != null).Select(x => x.AddressId).ToList(); var addresses = _dbContext.Address.Where(x => addressIds.Contains(x.Id)).ToList(); var sameAddresses = addresses.Where(x => x.RegionId == legalUnit.Address.RegionId && x.AddressPart1 == legalUnit.Address.AddressPart1 && x.AddressPart2 == legalUnit.Address.AddressPart2 && x.AddressPart3 == legalUnit.Address.AddressPart3 && x.Latitude == legalUnit.Address.Latitude && x.Longitude == legalUnit.Address.Longitude).ToList(); if (!sameAddresses.Any()) { createdLocal = await CreateLocalForLegalAsync(createdLegal); } transaction.Commit(); } catch (Exception e) { throw new BadRequestException(nameof(Resource.SaveError), e); } } await _elasticService.AddDocument(Mapper.Map <IStatisticalUnit, ElasticStatUnit>(createdLegal)); if (createdLocal != null) { await _elasticService.AddDocument(Mapper.Map <IStatisticalUnit, ElasticStatUnit>(createdLocal)); } if (createdEnterprise != null) { await _elasticService.AddDocument(Mapper.Map <IStatisticalUnit, ElasticStatUnit>(createdEnterprise)); } }
private async Task LogStatUnitUplaodTest(DataUploadingLogStatuses status) { var unit = new LegalUnit { StatId = "123", Name = "name42" }; var started = DateTime.Now; var ended = DateTime.Now; DataUploadingLog actual; using (var ctx = CreateDbContext()) { var queueItem = new DataSourceQueue(); ctx.DataSourceQueues.Add(queueItem); await ctx.SaveChangesAsync(); await new QueueService(ctx).LogUnitUpload( queueItem, JsonConvert.SerializeObject(unit), started, unit, ended, status, string.Empty, null, null); actual = queueItem.DataUploadingLogs.FirstOrDefault(); } Assert.NotNull(actual); Assert.Equal(started, actual.StartImportDate); Assert.Equal(ended, actual.EndImportDate); Assert.Equal(status, actual.Status); }
private void ParseMultipleFieldsOnFromOneSourceAttributeForComplexEntitiesInList() { const string source = "Persons"; const string nameValue = "MyName"; var expected = new List<KeyValuePair<string, Dictionary<string, string>>> { (new KeyValuePair<string, Dictionary<string, string>>("Person", new Dictionary<string, string> { { "Name", nameValue } })) }; var unit = new LegalUnit(); var mapping = new Dictionary<string, string[]> { ["Persons.Person.Name"] = new[] { $"{nameof(unit.Persons)}.{nameof(Person.GivenName)}", $"{nameof(unit.Persons)}.{nameof(Person.Surname)}" }, }; var raw = new Dictionary<string, object> { [source] = expected, }; StatUnitKeyValueParser.ParseAndMutateStatUnit(mapping, raw, unit); Assert.Single(unit.Persons); Assert.Equal(nameValue, unit.Persons.First().GivenName); Assert.Equal(nameValue, unit.Persons.First().Surname); }
public DataAccessPermissions ToPermissionsModel() { var attributes = LegalUnit .Concat(LocalUnit) .Concat(EnterpriseUnit) .Concat(EnterpriseGroup) .ToList(); return(new DataAccessPermissions(Mapper.Map <List <Permission> >(attributes))); }
private void ParseIntProp() { var unit = new LegalUnit {NumOfPeopleEmp = 2}; const string sourceProp = "peopleNum"; var mapping = new Dictionary<string, string[]> {[sourceProp] = new[] {nameof(unit.NumOfPeopleEmp)}}; const int expected = 17; var raw = new Dictionary<string, object> {[sourceProp] = expected.ToString()}; StatUnitKeyValueParser.ParseAndMutateStatUnit(mapping, raw, unit); Assert.Equal(expected, unit.NumOfPeopleEmp); }
private void ParseComplexFieldShouldPassForActualAddress() { const string expected = "some", sourceProp = "actualAddress"; var propPath = $"{nameof(StatisticalUnit.ActualAddress)}.{nameof(Address.Region)}.{nameof(Region.Code)}"; var unit = new LegalUnit(); var mapping = new Dictionary<string, string[]> {[sourceProp] = new[] {propPath}}; var raw = new Dictionary<string, object> {[sourceProp] = expected}; StatUnitKeyValueParser.ParseAndMutateStatUnit(mapping, raw, unit); Assert.NotNull(unit.ActualAddress); Assert.NotNull(unit.ActualAddress.Region); Assert.Equal(expected, unit.ActualAddress.Region.Code); }
private static async Task CreateLegalUnitAsync(NSCRegDbContext context, LegalUnit legalUnit) { context.LegalUnits.Add(legalUnit); await context.SaveChangesAsync(); var activity = await CreateActivityAsync(context); context.ActivityStatisticalUnits.Add(new ActivityStatisticalUnit { ActivityId = activity.Id, UnitId = (await context.LegalUnits.FirstOrDefaultAsync(x => x.Name == legalUnit.Name)).RegId }); await context.SaveChangesAsync(); }
/// <summary> /// Convert method to string collection /// </summary> /// <param name="validate">Flag of validity</param> /// <returns></returns> public IEnumerable <string> ToStringCollection(bool validate = true) { var attributes = LegalUnit.Concat(LocalUnit) .Concat(EnterpriseUnit) .Concat(EnterpriseGroup) .Where(v => v.Allowed) .Select(v => v.Name); if (validate) { attributes = attributes.Where(v => DataAccessAttributesProvider.Find(v) != null); } return(attributes); }
private void ParseComplexFieldShouldPassForPersons() { var expected = new List<KeyValuePair<string, Dictionary<string, string>>>{(new KeyValuePair<string,Dictionary<string,string>>("Person",new Dictionary<string, string>{{"Name","MyName"}}))}; const string sourceProp = "Persons.Person.Name"; var propPath = $"{nameof(StatisticalUnit.Persons)}.{nameof(Person)}.{nameof(Person.GivenName)}"; var unit = new LegalUnit(); var mapping = new Dictionary<string, string[]> {[sourceProp] = new[] {propPath}}; var raw = new Dictionary<string, object> {["Persons"] = expected}; StatUnitKeyValueParser.ParseAndMutateStatUnit(mapping, raw, unit); Assert.NotNull(unit.Persons); Assert.NotEmpty(unit.Persons); Assert.NotNull(unit.Persons.First()); }
private async Task CheckIfUnitExistsOnExistingUnit() { var unit = new LegalUnit { StatId = "1" }; bool exists; using (var ctx = CreateDbContext()) { ctx.LegalUnits.Add(unit); await ctx.SaveChangesAsync(); exists = await new QueueService(ctx).CheckIfUnitExists(StatUnitTypes.LegalUnit, unit.StatId); } Assert.True(exists); }
private void ParseComplexFieldShouldPassForActivities() { const string value = "01.11.9"; var expected = new List<KeyValuePair<string, Dictionary<string, string>>> { (new KeyValuePair<string, Dictionary<string, string>>("Activity", new Dictionary<string, string> { { "Code", value } })) }; const string sourceProp = "Activities"; var propPath = $"{nameof(StatisticalUnit.Activities)}.{nameof(Activity.ActivityCategory)}.{nameof(ActivityCategory.Code)}"; var unit = new LegalUnit(); var mapping = new Dictionary<string, string[]> {["Activities.Activity.Code"] = new[] {propPath}}; var raw = new Dictionary<string, object> {[sourceProp] = expected}; StatUnitKeyValueParser.ParseAndMutateStatUnit(mapping, raw, unit); Assert.NotNull(unit.Activities); Assert.NotEmpty(unit.Activities); Assert.NotNull(unit.Activities.First()); Assert.NotNull(unit.Activities.First().ActivityCategory); Assert.Equal(value, unit.Activities.First().ActivityCategory.Code); }
private async Task <LocalUnit> CreateLocalForLegalAsync(LegalUnit legalUnit) { var localUnit = new LocalUnit { AddressId = legalUnit.AddressId, ActualAddressId = legalUnit.ActualAddressId, LegalUnitId = legalUnit.RegId }; Mapper.Map(legalUnit, localUnit); _dbContext.LocalUnits.Add(localUnit); await _dbContext.SaveChangesAsync(); legalUnit.HistoryLocalUnitIds = localUnit.RegId.ToString(); _dbContext.LegalUnits.Update(legalUnit); await _dbContext.SaveChangesAsync(); CreateActivitiesAndPersonsAndForeignParticipations(legalUnit.Activities, legalUnit.PersonsUnits, legalUnit.ForeignParticipationCountriesUnits, localUnit.RegId); await _dbContext.SaveChangesAsync(); return(localUnit); }
private async Task <EnterpriseUnit> CreateEnterpriseForLegalAsync(LegalUnit legalUnit) { var enterpriseUnit = new EnterpriseUnit { AddressId = legalUnit.AddressId, ActualAddressId = legalUnit.ActualAddressId, HistoryLegalUnitIds = legalUnit.RegId.ToString() }; Mapper.Map(legalUnit, enterpriseUnit); _dbContext.EnterpriseUnits.Add(enterpriseUnit); await _dbContext.SaveChangesAsync(); legalUnit.EnterpriseUnitRegId = enterpriseUnit.RegId; _dbContext.LegalUnits.Update(legalUnit); await _dbContext.SaveChangesAsync(); CreateActivitiesAndPersonsAndForeignParticipations(legalUnit.Activities, legalUnit.PersonsUnits, legalUnit.ForeignParticipationCountriesUnits, enterpriseUnit.RegId); await _dbContext.SaveChangesAsync(); return(enterpriseUnit); }
protected void SearchButton_Click(object sender, EventArgs e) { using (var dataContext = new CvrDataContext()) { LegalUnit unit; Dictionary <String, String> nameAddressMap = new Dictionary <String, String>(); List <Position> namePositionMap = null; decimal cvrNum; if (Decimal.TryParse(CvrSearchBox.Text, out cvrNum)) { unit = dataContext.Units.Where(u => u.LegalUnitIdentifier == cvrNum && u.ProductionUnitIdentifier == 0).FirstOrDefault() as LegalUnit; nameAddressMap.Add(unit.Name, "vejnavn=" + HttpUtility.UrlEncode(unit.AddressOfficialStreetName) + "&husnr=" + HttpUtility.UrlEncode(unit.AddressOfficialStreetBuildingIdentifier) + "&postnr=" + unit.AddressOfficialPostCodeIdentifier ); if (unit != null) { var prod = unit.ProductionUnits.Where(pU => pU != null); foreach (var pU in prod) { object o = pU.Name; if (!nameAddressMap.ContainsKey(pU.Name)) { nameAddressMap.Add(pU.Name, "vejnavn=" + HttpUtility.UrlEncode(pU.AddressOfficialStreetName) + "&husnr=" + HttpUtility.UrlEncode(pU.AddressOfficialStreetBuildingIdentifier) + "&postnr=" + pU.AddressOfficialPostCodeIdentifier ); } } var owner = unit.Owners.Where(o => o != null); foreach (var ow in owner) { object o = ow.Ajourføringsmarkering; } } else { unit = new LegalUnit(); unit.Name = " CVR-nummer ukendt"; unit.TelephoneNumberIdentifier = ""; } } else { unit = new LegalUnit(); unit.Name = " Forkert input!"; unit.TelephoneNumberIdentifier = ""; } frmLegalUnit.DataSource = new LegalUnit[] { unit }; frmLegalUnit.DataBind(); namePositionMap = getPositions(nameAddressMap); JavaScriptSerializer serializer = new JavaScriptSerializer(); //this.ClientScript.RegisterClientScriptBlock(this.GetType(), "addresses", serializer.Serialize(nameAddressMap), true); /* * if (namePositionMap.Count > 1) * { * decimal[,] bounds = findBounds(namePositionMap); * Page.ClientScript.RegisterStartupScript(this.GetType(), "Call setBounds function", "setBounds(" + serializer.Serialize(bounds) + ");", true); * } */ Page.ClientScript.RegisterStartupScript(this.GetType(), "Call addPosiotn function", "addPositionData(" + serializer.Serialize(namePositionMap) + ");", true); Page.ClientScript.RegisterStartupScript(this.GetType(), "Call map function", "showPositionsOnMap();", true); } }
private async Task LinkLocalsToLegalAsync(IEnumerable <LocalUnit> sameStatIdLocalUnits, LegalUnit legalUnit) { foreach (var localUnit in sameStatIdLocalUnits) { localUnit.LegalUnitId = legalUnit.RegId; _dbContext.LocalUnits.Update(localUnit); } await _dbContext.SaveChangesAsync(); var localsOfLegal = _dbContext.LocalUnits.Where(lou => lou.RegId == legalUnit.RegId) .Select(x => x.RegId).ToList(); legalUnit.HistoryLocalUnitIds = string.Join(",", localsOfLegal); _dbContext.Update(legalUnit); await _dbContext.SaveChangesAsync(); }
public static void AddStatUnits(NSCRegDbContext context) { var roleId = context.Roles.FirstOrDefault(r => r.Name == DefaultRoleNames.Administrator)?.Id; var adminId = context.UserRoles.FirstOrDefault(x => x.RoleId == roleId)?.UserId; var sysAdminUser = context.Users.FirstOrDefault(u => u.Id == adminId); context.StatisticalUnits.AddRange(new LocalUnit { Name = "local unit 1", UserId = sysAdminUser.Id, RegIdDate = DateTime.Now, StartPeriod = DateTime.Now, EndPeriod = DateTime.MaxValue, Address = new Address { AddressPart1 = "local address 1", RegionId = 1 }, }, new LocalUnit { Name = "local unit 2", StatId = "OKPO2LU", UserId = sysAdminUser.Id, RegIdDate = DateTime.Now, StartPeriod = DateTime.Now, EndPeriod = DateTime.MaxValue, Address = new Address { AddressPart1 = "local address 2", RegionId = 1 }, }); var le1 = new LegalUnit { Name = "legal unit 1", UserId = sysAdminUser.Id, RegIdDate = DateTime.Now, StatId = "OKPO2LEGALU", StartPeriod = DateTime.Now, EndPeriod = DateTime.MaxValue, Address = new Address { AddressPart1 = "legal address 1", RegionId = 1 }, ActivitiesUnits = new List <ActivityStatisticalUnit> { new ActivityStatisticalUnit { Activity = new Activity { IdDate = new DateTime(2017, 03, 17), Turnover = 2000, ActivityType = ActivityTypes.Primary, UpdatedByUser = sysAdminUser, ActivityYear = DateTime.Today.Year, ActivityCategory = context.ActivityCategories.Single(v => v.Code == "11.07.9") }, }, new ActivityStatisticalUnit { Activity = new Activity { IdDate = new DateTime(2017, 03, 28), Turnover = 4000, ActivityType = ActivityTypes.Secondary, UpdatedByUser = sysAdminUser, ActivityYear = 2006, ActivityCategory = context.ActivityCategories.Single(v => v.Code == "91.01.9") } } }, }; context.StatisticalUnits.AddRange(le1, new LegalUnit { Name = "legal unit 2", UserId = sysAdminUser.Id, IsDeleted = true, RegIdDate = DateTime.Now, StartPeriod = DateTime.Now, EndPeriod = DateTime.MaxValue, Address = new Address { AddressPart1 = "legal address 2", RegionId = 1 }, }); var eu1 = new EnterpriseUnit { Name = "enterprise unit 1", StatId = "OKPO1EU", UserId = sysAdminUser.Id, RegIdDate = DateTime.Now, StartPeriod = DateTime.Now, EndPeriod = DateTime.MaxValue, }; var eu2 = new EnterpriseUnit { Name = "enterprise unit 2", StatId = "OKPO2EU", UserId = sysAdminUser.Id, RegIdDate = DateTime.Now, StartPeriod = DateTime.Now, EndPeriod = DateTime.MaxValue, Address = new Address { AddressPart1 = "enterprise address 2", RegionId = 1 }, }; context.EnterpriseUnits.AddRange(eu1, eu2, new EnterpriseUnit { Name = "enterprise unit 3", StatId = "OKPO3EU", UserId = sysAdminUser.Id, IsDeleted = true, RegIdDate = DateTime.Now, StartPeriod = DateTime.Now, EndPeriod = DateTime.MaxValue, Address = new Address { AddressPart1 = "enterprise address 2", RegionId = 1 }, }, new EnterpriseUnit { StatId = "OKPO4EU", Name = "enterprise unit 4", UserId = sysAdminUser.Id, RegIdDate = DateTime.Now, StartPeriod = DateTime.Now, EndPeriod = DateTime.MaxValue, Address = new Address { AddressPart1 = "enterprise address 2", RegionId = 1 }, }, new EnterpriseUnit { Name = "enterprise unit 5", UserId = sysAdminUser.Id, RegIdDate = DateTime.Now, StartPeriod = DateTime.Now, EndPeriod = DateTime.MaxValue, Address = new Address { AddressPart1 = "enterprise address 2", RegionId = 1 }, }, new EnterpriseUnit { Name = "enterprise unit 6", UserId = sysAdminUser.Id, RegIdDate = DateTime.Now, StartPeriod = DateTime.Now, EndPeriod = DateTime.MaxValue, Address = new Address { AddressPart1 = "enterprise address 2", RegionId = 1 }, }); var eg1 = new EnterpriseGroup { Name = "enterprise group 1", UserId = sysAdminUser.Id, StatId = "EG1", RegIdDate = DateTime.Now, StartPeriod = DateTime.Now, EndPeriod = DateTime.MaxValue, Address = new Address { AddressPart1 = "ent. group address 1", RegionId = 1 }, }; var eg2 = new EnterpriseGroup { Name = "enterprise group 2", StatId = "EG2", UserId = sysAdminUser.Id, RegIdDate = DateTime.Now, StartPeriod = DateTime.Now, EndPeriod = DateTime.MaxValue, Address = new Address { AddressPart1 = "ent. group address 2", RegionId = 1 } }; context.EnterpriseGroups.AddRange(eg1, eg2); //Links: eu1.EnterpriseGroup = eg1; le1.EnterpriseUnit = eu1; context.SaveChanges(); }
private async Task LinkEnterpriseToLegalAsync(EnterpriseUnit sameStatIdEnterprise, LegalUnit legalUnit) { legalUnit.EnterpriseUnitRegId = sameStatIdEnterprise.RegId; _dbContext.LegalUnits.Update(legalUnit); await _dbContext.SaveChangesAsync(); var legalsOfEnterprise = _dbContext.LegalUnits.Where(leu => leu.RegId == sameStatIdEnterprise.RegId) .Select(x => x.RegId).ToList(); sameStatIdEnterprise.HistoryLegalUnitIds += string.Join(",", legalsOfEnterprise); _dbContext.EnterpriseUnits.Update(sameStatIdEnterprise); await _dbContext.SaveChangesAsync(); }