Beispiel #1
0
        private void ExecuteSaveUserWithAgencyWithAgentViewCommand(object obj)
        {
            try
            {
                if (SelectedUserWithAgencyWithAgent != null)// && SelectedUserWithAgencyWithAgent.AgencyWithAgent != null)
                {
                    var agencyWithAg = new AgencyAgentDTO
                    {
                        AgencyId = SelectedLocalAgencyDto.Id,
                        AgentId  = SelectedForeignAgentDto.Id
                    };

                    //SelectedUserWithAgencyWithAgent.AgencyWithAgent.AgentId = SelectedForeignAgentDto.Id;
                    _unitOfWork.Repository <AgencyAgentDTO>()
                    .InsertUpdate(agencyWithAg);
                    //_unitOfWork.Commit();

                    SelectedUserWithAgencyWithAgent.AgencyWithAgentId = agencyWithAg.Id;
                    _unitOfWork.UserRepository <UserAgencyAgentDTO>()
                    .Insert(SelectedUserWithAgencyWithAgent);

                    _unitOfWork.Commit();

                    //_unitOfWork.Dispose();

                    CloseWindow(obj);
                }
            }
            catch (Exception ex)
            {
            }
        }
Beispiel #2
0
        public static bool InsertAgencyWithAgents(string userName, string agencyName)
        {
            //try
            //{
            //    userName = Singleton.User.UserName;
            //    agencyName = Singleton.Agency.AgencyName;
            //}
            //catch
            //{
            //    userName = "******";
            //    agencyName = "Default Agency";
            //}

            try
            {
                IDbContext iDbContext = DbContextUtil.GetDbContextInstance();
                var        unitOfWork = new UnitOfWork(iDbContext);


                AgencyDTO agency = new LocalAgencyService(true).GetLocalAgency();
                IEnumerable <AgentDTO> agents = new ForeignAgentService(true, false).GetAll();

                foreach (AgentDTO foreignAgentDTO in agents)
                {
                    AgentDTO dto = foreignAgentDTO;
                    var      agencyWithAgents = unitOfWork.Repository <AgencyAgentDTO>()
                                                .Query()
                                                .FilterList(f => f.AgentId == dto.Id && f.AgencyId == agency.Id)
                                                .Get()
                                                .FirstOrDefault();

                    if (agencyWithAgents != null)
                    {
                        continue;
                    }

                    agencyWithAgents = new AgencyAgentDTO
                    {
                        AgencyId = agency.Id,
                        AgentId  = foreignAgentDTO.Id
                    };

                    unitOfWork.Repository <AgencyAgentDTO>().Insert(agencyWithAgents);
                }
                unitOfWork.Commit();

                unitOfWork.Dispose();

                return(true);
            }
            catch (Exception ex)
            {
                LogUtil.LogError(ErrorSeverity.Critical, "Insert Agency With Agents",
                                 ex.Message + Environment.NewLine + ex.InnerException, userName, agencyName);
                return(false);
            }
        }
Beispiel #3
0
        public static bool InsertUserWithAgencyWithAgents(string userName, string agencyName)
        {
            //string userName, agencyName;
            //try
            //{
            //    userName = Singleton.User.UserName;
            //    agencyName = Singleton.Agency.AgencyName;
            //}
            //catch
            //{
            //    userName = "******";
            //    agencyName = "Default Agency";
            //}

            try
            {
                IDbContext iDbContext = DbContextUtil.GetDbContextInstance();
                var        unitOfWork = new UnitOfWork(iDbContext);

                var users            = new UserService().GetAll().ToList();
                var agenctWithAgents = unitOfWork.Repository <AgencyAgentDTO>()
                                       .Query()
                                       .Get()
                                       .ToList();

                foreach (var userDTO in users)
                {
                    foreach (var agencyWithAgentDTO in agenctWithAgents)
                    {
                        UserDTO        dto      = userDTO;
                        AgencyAgentDTO agentDTO = agencyWithAgentDTO;

                        var userWithAgencyWithAgents = unitOfWork.UserRepository <UserAgencyAgentDTO>()
                                                       .Query()
                                                       .FilterList(f => f.UserId == dto.UserId && f.AgencyWithAgentId == agentDTO.Id)
                                                       .Get()
                                                       .FirstOrDefault();

                        if (userWithAgencyWithAgents != null)
                        {
                            continue;
                        }

                        userWithAgencyWithAgents = new UserAgencyAgentDTO()
                        {
                            UserId            = dto.UserId,
                            AgencyWithAgentId = agentDTO.Id
                        };

                        unitOfWork.UserRepository <UserAgencyAgentDTO>().Insert(userWithAgencyWithAgents);
                    }
                }

                unitOfWork.Commit();

                unitOfWork.Dispose();

                return(true);
            }
            catch (Exception ex)
            {
                LogUtil.LogError(ErrorSeverity.Critical, "Insert User With Agency With Agents",
                                 ex.Message + Environment.NewLine + ex.InnerException, userName, agencyName);
                return(false);
            }
        }
Beispiel #4
0
        public bool SyncAgencyWithAgents(IUnitOfWork sourceUnitOfWork, IUnitOfWork destinationUnitOfWork)
        {
            var sourceList = sourceUnitOfWork.Repository <AgencyAgentDTO>().Query()
                             .Include(h => h.Agency, h => h.Agent)
                             .Filter(a => !a.Synced && a.DateLastModified > LastServerSyncDate)
                             .Get(1)
                             .ToList();

            if (sourceList.Any())
            {
                _updatesFound = true;
                var destAgencyDtos =
                    destinationUnitOfWork.Repository <AgencyDTO>().Query()
                    .Filter(a => a.Id == Singleton.Agency.Id)
                    .Get(1)
                    .ToList();

                var destAgentDtos =
                    destinationUnitOfWork.Repository <AgentDTO>().Query()
                    .Get(1)
                    .ToList();

                var destList =
                    destinationUnitOfWork.Repository <AgencyAgentDTO>().Query()
                    .Filter(a => a.AgencyId == Singleton.Agency.Id)
                    .Include(a => a.Agency, a => a.Agent)
                    .Get(1)
                    .ToList();

                foreach (var source in sourceList)
                {
                    var destination =
                        destList.FirstOrDefault(i => i.RowGuid == source.RowGuid);

                    var clientId = 0;
                    if (destination == null)
                    {
                        destination = new AgencyAgentDTO();
                    }
                    else
                    {
                        clientId = destination.Id;
                    }

                    try
                    {
                        Mapper.Reset();
                        Mapper.CreateMap <AgencyAgentDTO, AgencyAgentDTO>()
                        .ForMember("Agency", option => option.Ignore())
                        .ForMember("Agent", option => option.Ignore())
                        .ForMember("Users", option => option.Ignore())
                        .ForMember("Synced", option => option.Ignore());
                        destination    = Mapper.Map(source, destination);
                        destination.Id = clientId;

                        destination.CreatedByUserId = GetDestCreatedModifiedByUserId(source.CreatedByUserId,
                                                                                     sourceUnitOfWork, destinationUnitOfWork);
                        destination.ModifiedByUserId = GetDestCreatedModifiedByUserId(source.ModifiedByUserId,
                                                                                      sourceUnitOfWork, destinationUnitOfWork);
                    }
                    catch (Exception ex)
                    {
                        LogUtil.LogError(ErrorSeverity.Critical, "SyncAgencyWithAgents Mapping",
                                         ex.Message + Environment.NewLine + ex.InnerException, UserName, Agency);
                    }
                    try
                    {
                        #region Foreign Keys

                        var agencyDTO =
                            destAgencyDtos.FirstOrDefault(
                                c => source.Agency != null && c.RowGuid == source.Agency.RowGuid);
                        {
                            destination.Agency   = agencyDTO;
                            destination.AgencyId = agencyDTO != null ? agencyDTO.Id : (int?)null;
                        }

                        var agentDTO =
                            destAgentDtos.FirstOrDefault(c => source.Agent != null && c.RowGuid == source.Agent.RowGuid);
                        {
                            destination.Agent   = agentDTO;
                            destination.AgentId = agentDTO != null ? agentDTO.Id : 1;
                        }

                        #endregion

                        destination.Synced = true;
                        destinationUnitOfWork.Repository <AgencyAgentDTO>().InsertUpdate(destination);
                    }
                    catch
                    {
                        _errorsFound = true;
                        LogUtil.LogError(ErrorSeverity.Critical, "SyncAgencyWithAgents Crud",
                                         "Problem On SyncAgencyWithAgents Crud Method", UserName, Agency);
                        return(false);
                    }
                }
                var changes = destinationUnitOfWork.Commit();
                if (changes < 0)
                {
                    _errorsFound = true;
                    LogUtil.LogError(ErrorSeverity.Critical, "SyncAgencyWithAgents Commit",
                                     "Problem Commiting SyncAgencyWithAgents Method", UserName, Agency);
                    return(false);
                }
            }
            return(true);
        }