public async Task <int> ChangeNameStatusAsync(int nameId, int status)
        {
            var name = await _context.Names.FindAsync(nameId);

            name.Status = (ENameStatus)status;

            if (status.Equals((int)ENameStatus.Reserved))
            {
                await _context.Entry(name).Reference(n => n.NameSearch).LoadAsync();

                await _context.Entry(name.NameSearch).Collection(n => n.Names).LoadAsync();

                var unconsideredNames = name.NameSearch.Names.Where(n =>
                                                                    !n.EntityNameId.Equals(name.EntityNameId) && n.Status.Equals(ENameStatus.Pending))
                                        .ToList();
                foreach (var unconsideredName in unconsideredNames)
                {
                    unconsideredName.Status = ENameStatus.NotConsidered;
                }
            }

            return(await _context.SaveChangesAsync());
        }
        private void AddCoreData(MainDatabaseContext context)
        {
            if (!context.Languages.Any())
            {
                Language polish = new Language()
                {
                    CultureSymbol      = "pl-PL",
                    LanguageDictionary = LanguageDictionary.Polish
                };
                context.Languages.Add(polish);
                context.SaveChanges();
            }
            if (!context.SystemUsers.Any())
            {
                Language   polish = context.Languages.FirstOrDefault(x => x.LanguageDictionary == LanguageDictionary.Polish);
                SystemUser admin  = new SystemUser()
                {
                    CreatedDate = DateTime.Now,
                    Email       = SystemUsers.SystemUserEmail,
                    FirstName   = SystemUsers.SystemUserName,
                    IsActive    = true,
                    LastName    = "",
                    LanguageId  = polish.Id,
                    Name        = SystemUsers.SystemUserName
                };
                context.SystemUsers.Add(admin);
                context.SaveChanges();
                admin.CreatedById          = admin.Id;
                context.Entry(admin).State = EntityState.Modified;

                SystemUser unknownUser = new SystemUser()
                {
                    CreatedDate = DateTime.Now,
                    CreatedById = admin.Id,
                    Email       = SystemUsers.UnknownUserEmail,
                    FirstName   = SystemUsers.UnknownUserName,
                    IsActive    = true,
                    LastName    = "",
                    LanguageId  = polish.Id,
                    Name        = SystemUsers.UnknownUserName
                };
                context.SystemUsers.Add(unknownUser);

                context.SaveChanges();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Updates the database based upon SessionDetails in the UpdatesToStore queue.
        /// If the SessioStoreData.SessionData entry is null, the data is removed.
        /// Runs in a loop until either Token.IsCancellationRequested is true.
        /// Will finish updating the database when we are disposing and there are still updates to store
        /// </summary>
        private void UpdateDatabase()
        {
            while (!this.token.IsCancellationRequested || (this.disposing && this.updatesToStore.Count > 0))
            {
                MainDatabaseContext context = new MainDatabaseContext();

                int count = 0;
                if (this.updatesToStore.Count > 0)
                {
                    while (this.updatesToStore.Count > 0 && count < 100)
                    {
                        count++;
                        SessionDetails session = this.updatesToStore.Dequeue();
                        if (session.Role == null)
                        {
                            if (context.SessionStore.Any(s => s.SessionId == session.SessionId))
                            {
                                context.SessionStore.Local.Where(s => s.SessionId == session.SessionId).ToList().ForEach(s => context.Entry(s).State = System.Data.Entity.EntityState.Deleted);
                            }
                            else
                            {
                                // Crashed on duplicate key error
                                context.Entry(session).State = System.Data.Entity.EntityState.Deleted;
                            }
                        }
                        else
                        {
                            SessionDetails newSession = new SessionDetails();
                            newSession.SessionId  = session.SessionId;
                            newSession.Role       = session.Role;
                            newSession.LastAccess = session.LastAccess;

                            if (context.SessionStore.Local.Any(s => s.SessionId == newSession.SessionId) && context.Entry(context.SessionStore.Local.Where(s => s.SessionId == newSession.SessionId).Single()).State != System.Data.Entity.EntityState.Deleted)
                            {
                                SessionDetails details = context.SessionStore.Local.Where(s => s.SessionId == newSession.SessionId).Single();
                                details.Role       = newSession.Role;
                                details.LastAccess = newSession.LastAccess;
                            }
                            else if (context.SessionStore.Any(s => s.SessionId == newSession.SessionId))
                            {
                                context.Entry(newSession).State = System.Data.Entity.EntityState.Modified;
                            }
                            else
                            {
                                context.Entry(newSession).State = System.Data.Entity.EntityState.Added;
                            }
                        }
                    }

                    if (count > 0)
                    {
                        try
                        {
                            context.SaveChanges();
                        }
                        catch (DbEntityValidationException e)
                        {
                            string errorResult = string.Empty;
                            foreach (var eve in e.EntityValidationErrors)
                            {
                                errorResult += "Entity of type \" " + eve.Entry.Entity.GetType().Name + "\" in state \"" + eve.Entry.State + "\" has the following validation errors: \n";
                                foreach (var ve in eve.ValidationErrors)
                                {
                                    errorResult += "- Property: \"" + ve.PropertyName + "\", Error: \"" + ve.ErrorMessage + "\" \n";
                                }
                            }

                            throw new DbEntityValidationException(errorResult, e);
                        }
                    }
                }
                else
                {
                    Thread.Sleep(100);
                }
            }
        }
        public async Task <ApplicationResponseDto> InsertMembersAsync(Guid user, NewShareHoldersRequestDto dto)
        {
            // Get application from db
            var application = await GetPrivateEntityApplicationAsync(user, dto.ApplicationId);

            await LoadSavedMemorandumAsync(application);

            await _context.Entry(application.PrivateEntity.MemorandumOfAssociation)
            .Collection(m => m.ShareClauses)
            .LoadAsync();

            // Get share clause
            var shareClauses = application.PrivateEntity.MemorandumOfAssociation.ShareClauses;

            // Map shareholding pple
            foreach (var personDto in dto.People)
            {
                var person = MapPerson(personDto, shareClauses);
                await _context.AddAsync(person);

                if (personDto.HasBeneficiaries())
                {
                    foreach (var benefactor in personDto.PeopleRepresented)
                    {
                        var beneficiary = MapPerson(benefactor, shareClauses);
                        await _context.AddAsync(beneficiary);

                        person.PersonRepresentsPersonss.Add(new PersonRepresentsPerson(person,
                                                                                       beneficiary));
                    }
                }

                AddPrivateEntityMember(application, person);
            }

            // Map shareholding Entities
            foreach (var entity in dto.Entities)
            {
                if (entity.IsRepresented())
                {
                    if (entity.CountryCode.Equals("ZWE"))
                    {
                        var registeredEntity = await _context.PrivateEntities
                                               .Include(p => p.Name)
                                               .ThenInclude(a => a.NameSearch)
                                               .ThenInclude(n => n.Names)
                                               .SingleOrDefaultAsync(p =>
                                                                     p.Name.NameSearch.Names
                                                                     .Single(n => n.Status.Equals(ENameStatus.Used)).Value == entity.Name &&
                                                                     p.Reference.Equals(entity.CompanyReference));

                        if (!registeredEntity.Equals(null))
                        {
                            foreach (var nominee in entity.Nominees)
                            {
                                var person = MapPerson(nominee, shareClauses);
                                AddPrivateEntityMember(application, person);

                                registeredEntity.PersonRepresentsPrivateEntity.Add(
                                    new PersonRepresentsPrivateEntity(registeredEntity, person));
                            }
                        }
                        else
                        {
                            throw new Exception("One of the local shareholding entities is not registered.");
                        }
                    }
                    else
                    {
                        var shareholdingForeignEntity = _mapper.Map <ForeignEntity>(entity);

                        foreach (var subscription in entity.Subs)
                        {
                            shareholdingForeignEntity.ForeignEntitySubscriptions.Add(
                                new ForeignEntitySubscription(shareholdingForeignEntity,
                                                              shareClauses.Single(s => s.Title.Equals(subscription.Title)), subscription.Amount));
                        }

                        foreach (var nominee in entity.Nominees)
                        {
                            var person = MapPerson(nominee, shareClauses);
                            shareholdingForeignEntity.PersonRepresentsForeignEntities.Add(
                                new PersonRepresentsForeignEntity(shareholdingForeignEntity, person));
                            await _context.AddAsync(shareholdingForeignEntity);

                            AddPrivateEntityMember(application, person);
                        }

                        // await _context.SaveChangesAsync();
                    }
                }
                else
                {
                    throw new Exception("One of the shareholding entities is not represented.");
                }
            }

            return(await ReturnApplicationResponse(application));
        }