Пример #1
0
        public void Sync() //(object sender, DoWorkEventArgs e)
        {
            Singleton.SystemVersionDate = DbCommandUtil.GetCurrentDatabaseVersion();

            var productionDbVersion = Convert.ToInt32(DbCommandUtil.GetCurrentDbVersion());
            var systemVersionDate   = Convert.ToInt32(Singleton.SystemVersionDate);

            if (systemVersionDate < productionDbVersion)
            {
                LogUtil.LogError(ErrorSeverity.Critical, "ValidateProduct",
                                 "Higher Database Version", UserName, Agency);
                return;
            }

            LogUtil.LogError(ErrorSeverity.Critical, "Sync", "Started", UserName, Agency);

            AgencyWithAgentsUtility.InsertAgencyNamesonAddressesandAttachments();
            if (!AgencyWithAgentsUtility.InsertAgencyWithAgents(UserName, Agency))
            {
                return;
            }
            if (!AgencyWithAgentsUtility.InsertUserWithAgencyWithAgents(UserName, Agency))
            {
                return;
            }


            IUnitOfWork sourceUnitOfWork = new UnitOfWork(
                new DbContextFactory().Create());
            IUnitOfWork destinationUnitOfWork = new UnitOfWorkServer(
                new ServerDbContextFactory().Create());

            var agency = sourceUnitOfWork.Repository <AgencyDTO>()
                         .Query()
                         .Get()
                         .FirstOrDefault();

            //Setting = sourceUnitOfWork.Repository<SettingDTO>()
            //    .Query()
            //    .Get(1)
            //    .FirstOrDefault();

            try
            {
                //if (Setting != null)
                //{
                //    LastFromServerSyncDate = Setting.LastFromServerSyncDate != null
                //        ? (DateTime) Setting.LastFromServerSyncDate
                //        : DbCommandUtil.GetCurrentSqlDate(false).AddYears(-1);//If it is for first time collect all last one year data
                //    LastToServerSyncDate = Setting.LastToServerSyncDate != null
                //        ? (DateTime) Setting.LastToServerSyncDate
                //        : DbCommandUtil.GetCurrentSqlDate(false).AddYears(-1);
                //}
                //else
                //{
                LastFromServerSyncDate = DbCommandUtil.GetCurrentSqlDate(false).AddYears(-10);
                LastToServerSyncDate   = DbCommandUtil.GetCurrentSqlDate(false).AddYears(-10);
                //}
            }
            catch (Exception ex)
            {
                LogUtil.LogError(ErrorSeverity.Critical, "Sync DbCommandUtil.GetCurrentSqlDate(true)",
                                 ex.Message + Environment.NewLine + ex.InnerException, UserName, Agency);
            }

            LogUtil.LogError(ErrorSeverity.Info, "Sync To Server",
                             "Started", UserName, Agency);

            #region Sync To Server

            ToServerSyncing    = true;
            LastServerSyncDate = LastToServerSyncDate; //.AddMinutes(-10);//-10 Minutes should be syncronized with MonitorTimerElapsed

            try
            {
                if (!SyncUsers2(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);

                if (!SyncAgencies2(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);

                Singleton.Agency = destinationUnitOfWork.Repository <AgencyDTO>()
                                   .Query().Filter(a => a.RowGuid == agency.RowGuid)
                                   .Get()
                                   .FirstOrDefault();

                if (!SyncAddresses(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);

                if (!SyncAttachments(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);
                if (!SyncRequiredDocuments(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);

                if (!SyncVisaSponsors(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);
                if (!SyncVisaConditions(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);

                if (!SyncEducation(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);
                if (!SyncExperiences(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);
                if (!SyncHawala(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);

                if (!SyncInsurance(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);
                if (!SyncLabour(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);
                if (!SyncEmbassy(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);
                if (!SyncFlight(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);

                if (!SyncAgencies(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);
                if (!SyncForeignAgents(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);
                if (!SyncAgencyWithAgents(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);

                if (!SyncSettings(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);
                if (!SyncProductActivations(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);

                if (!SyncVisas(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);

                if (!SyncEmployees(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);
                if (!SyncRelatives(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);

                if (!SyncComplains(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);
                if (!SyncComplainRemarks(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);
                if (!SyncEmployees2(sourceUnitOfWork, destinationUnitOfWork, false))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);

                if (!SyncUsers(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);
                if (!SyncMemberships(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);
                if (!SyncRoles(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);
                if (!SyncUsersInRoles(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow(destinationUnitOfWork);
                if (!SyncUserWithAgencyWithAgentDTO(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }

                //Sync Setting
                try
                {
                    if (!_errorsFound && _updatesFound)
                    {
                        var        setUnitOfWork = new UnitOfWork(new DbContextFactory().Create());
                        SettingDTO set           = setUnitOfWork.Repository <SettingDTO>().Query().Get(1).FirstOrDefault();

                        if (set != null)
                        {
                            set.LastToServerSyncDate = DbCommandUtil.GetCurrentSqlDate(false); //.AddHours(-1);
                        }
                        setUnitOfWork.Repository <SettingDTO>().SimpleUpdate(set);
                        setUnitOfWork.Commit();
                        setUnitOfWork.Dispose();
                    }
                    else
                    {
                        LogUtil.LogError(ErrorSeverity.Critical, "Sync To Server",
                                         "No Updates Found", UserName, Agency);
                    }
                }
                catch (Exception ex)
                {
                    LogUtil.LogError(ErrorSeverity.Critical, "Update Setting.LastToServerSyncDate",
                                     ex.Message + Environment.NewLine + ex.InnerException, UserName, Agency);
                }
            }
            catch (Exception ex)
            {
                LogUtil.LogError(ErrorSeverity.Critical, "Sync General Method",
                                 ex.Message + Environment.NewLine + ex.InnerException, UserName, Agency);
            }
            finally
            {
                try
                {
                    sourceUnitOfWork.Dispose();
                }
                catch (Exception ex)
                {
                    LogUtil.LogError(ErrorSeverity.Critical, "Sync General sourceUnitOfWork.Dispose",
                                     ex.Message + Environment.NewLine + ex.InnerException, UserName, Agency);
                }
            }

            #endregion

            LogUtil.LogError(ErrorSeverity.Info, "Sync To Server", "Completed", UserName, Agency);

            #region Dispose UoW

            try
            {
                sourceUnitOfWork.Dispose();
                destinationUnitOfWork.Dispose();
            }
            catch (Exception ex)
            {
                LogUtil.LogError(ErrorSeverity.Critical, "Dispose Unit of Work Method",
                                 ex.Message + Environment.NewLine + ex.InnerException, UserName, Agency);
            }

            #endregion

            LogUtil.LogError(ErrorSeverity.Info, "Sync From Server", "Started", UserName, Agency);

            #region Sync From Server

            ToServerSyncing  = false;
            Singleton.Agency = agency;
            try
            {
                LastServerSyncDate = LastFromServerSyncDate; //.AddMinutes(-10);
                //-10 Minutes should be syncronized with MonitorTimerElapsed


                sourceUnitOfWork      = new UnitOfWorkServer(new ServerDbContextFactory().Create());
                destinationUnitOfWork = new UnitOfWork(new DbContextFactory().Create());

                if (!SyncSettings(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow2(destinationUnitOfWork);
                if (!SyncProductActivations(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow2(destinationUnitOfWork);

                if (!SyncAddresses(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow2(destinationUnitOfWork);

                if (!SyncVisaSponsors(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow2(destinationUnitOfWork);
                if (!SyncVisaConditions(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow2(destinationUnitOfWork);

                if (!SyncVisas(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow2(destinationUnitOfWork);

                if (!SyncEmployees(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow2(destinationUnitOfWork);

                if (!SyncComplains(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow2(destinationUnitOfWork);
                if (!SyncComplainRemarks(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow2(destinationUnitOfWork);
                if (!SyncEmployees2(sourceUnitOfWork, destinationUnitOfWork, true))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow2(destinationUnitOfWork);

                if (!SyncRoles(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                destinationUnitOfWork = GetNewUow2(destinationUnitOfWork);
                if (!SyncUsersInRoles(sourceUnitOfWork, destinationUnitOfWork))
                {
                    return;
                }
                ////destinationUnitOfWork = GetNewUow(destinationUnitOfWork);

                //Sync Setting
                try
                {
                    if (!_errorsFound && _updatesFound)
                    {
                        var        setUnitOfWork = new UnitOfWork(new DbContextFactory().Create());
                        SettingDTO set           = setUnitOfWork.Repository <SettingDTO>().Query().Get(1).FirstOrDefault();

                        if (set != null)
                        {
                            set.LastFromServerSyncDate = DbCommandUtil.GetCurrentSqlDate(false); //.AddHours(-1);
                        }
                        setUnitOfWork.Repository <SettingDTO>().SimpleUpdate(set);
                        setUnitOfWork.Commit();
                        setUnitOfWork.Dispose();
                    }
                    else
                    {
                        LogUtil.LogError(ErrorSeverity.Critical, "Sync From Server",
                                         "No Updates Found", UserName, Agency);
                    }
                }
                catch (Exception ex)
                {
                    LogUtil.LogError(ErrorSeverity.Critical, "Update Setting.LastFromServerSyncDate",
                                     ex.Message + Environment.NewLine + ex.InnerException, UserName, Agency);
                }
            }
            catch (Exception ex)
            {
                LogUtil.LogError(ErrorSeverity.Critical, "Sync From Server General Method",
                                 ex.Message + Environment.NewLine + ex.InnerException, UserName, Agency);
            }
            finally
            {
                try
                {
                    sourceUnitOfWork.Dispose();
                }
                catch (Exception ex)
                {
                    LogUtil.LogError(ErrorSeverity.Critical, "Sync From Server General sourceUnitOfWork.Dispose()",
                                     ex.Message + Environment.NewLine + ex.InnerException, UserName, Agency);
                }
            }

            #endregion

            LogUtil.LogError(ErrorSeverity.Info, "Sync From Server",
                             "Completed", UserName, Agency);
        }