Example #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);
        }
        private void DoWork(object sender, DoWorkEventArgs e)
        {
            ProductActivation.ProductKey = ProductKey;
            var key = _dbContext.ActivationKeys
                      .FirstOrDefault(a => a.ProductKey == ProductActivation.ProductKey &&
                                      a.KeyStatus == 0 && a.ProductType == 0);

            //O represents Active and O represents PinnaFace

            if (key != null)
            {
                try
                {
                    if (string.IsNullOrEmpty(key.BIOS_SN))
                    {
                        key.BIOS_SN = ProductActivation.BiosSn;
                        //key.FirstActivatedDate = DateTime.Now;
                        //    //GETUTCDATETIME StoredPr. //the time will be better if it is the server timer
                        //key.ExpirationDate = key.FirstActivatedDate.Value.AddDays(key.ExpiryDuration);
                    }
                    else
                    {
                        if (!key.BIOS_SN.Contains(ProductActivation.BiosSn))
                        {
                            if (key.NoOfAllowedPcs == 1)
                            {
                                MessageBox.Show(
                                    "Can't Activate the product, " +
                                    "Check your product key and try again, " +
                                    "or contact pinnaface office!", "More than Allowed Pcs");
                                ProductKey        = "";
                                CommandsEnability = true;

                                return;
                            }

                            key.BIOS_SN = key.BIOS_SN + "," + ProductActivation.BiosSn;
                            if (key.BIOS_SN.Split(',').Count() > key.NoOfAllowedPcs)
                            {
                                MessageBox.Show(
                                    "Can't Activate the product, " +
                                    "Check your product key and try again, " +
                                    "or contact pinnaface office!", "More than Allowed Pcs");
                                ProductKey        = "";
                                CommandsEnability = true;
                                return;
                            }
                        }
                    }

                    key.NoOfActivations = key.NoOfActivations + 1;
                    if (key.NoOfActivations > key.NoOfAllowedActivations)
                    {
                        MessageBox.Show(
                            "Can't Activate the product, " +
                            "Check your product key and try again, " +
                            "or contact pinnaface office!", "More than Allowed Activations");
                        ProductKey        = "";
                        CommandsEnability = true;
                        return;
                    }

                    _dbContext.Add(key);
                    _dbContext.SaveChanges();

                    ProductActivation.RegisteredBiosSn = key.BIOS_SN;
                    ProductActivation.DateLastModified = DbCommandUtil.GetCurrentSqlDate(true);//  DateTime.Now; //GETUTCDATETIME StoredPr
                    ProductActivation.Synced           = false;

                    if (ProductActivation.Id == 0)
                    {
                        ProductActivation.LicensedTo           = key.CustomerName;
                        ProductActivation.DatabaseVersionDate  = Singleton.SystemVersionDate;
                        ProductActivation.MaximumSystemVersion = key.MaximumAllowedSystemVersion;


                        ProductActivation.ModifiedByUserId  = 1;
                        ProductActivation.CreatedByUserId   = 1;
                        ProductActivation.DateRecordCreated = DbCommandUtil.GetCurrentSqlDate(true);//DateTime.Now; //GETUTCDATETIME StoredPr

                        #region Set UserAccounts

                        ProductActivation.SuperName = key.SuperName; //.Email;
                        ProductActivation.SuperPass = key.SuperPass;
                        ProductActivation.AdminName = key.AdminName;
                        ProductActivation.AdminPass = key.AdminPass;
                        ProductActivation.User1Name = key.User1Name;
                        ProductActivation.User1Pass = key.User1Pass;

                        #endregion

                        #region Agency

                        var localAgency = new AgencyDTO
                        {
                            AgencyName         = key.CustomerName ?? (key.CustomerName = ""),
                            AgencyNameAmharic  = "-",
                            ManagerName        = "-",
                            ManagerNameAmharic = "-",
                            Address            = new AddressDTO
                            {
                                AddressType  = AddressTypes.Local,
                                Country      = CountryList.Ethiopia,
                                City         = EnumUtil.GetEnumDesc(CityList.AddisAbeba),
                                Region       = "14",
                                Telephone    = key.Telephone ?? (key.Telephone = ""),
                                PrimaryEmail = key.Email ?? (key.Email = "")
                            },
                            Header           = new AttachmentDTO(),
                            Footer           = new AttachmentDTO(),
                            LicenceNumber    = "-",
                            SaudiOperation   = key.SaudiOperation,
                            DubaiOperation   = key.DubaiOperation,
                            KuwaitOperation  = key.KuwaitOperation,
                            QatarOperation   = key.QatarOperation,
                            JordanOperation  = key.JordanOperation,
                            LebanonOperation = key.LebanonOperation,
                            BahrainOperation = key.BahrainOperation,
                            DepositAmount    = "100,000 USD",
                            Managertype      = "ዋና ስራ አስኪያጅ"
                        };

                        #endregion

                        #region Foreign Agents

                        var foreignAgent = new AgentDTO
                        {
                            AgentName        = "-",
                            AgentNameAmharic = "-",
                            Address          = new AddressDTO
                            {
                                AddressType = AddressTypes.Foreign,
                                Country     = CountryList.SaudiArabia,
                                City        = EnumUtil.GetEnumDesc(CityList.Riyadh)
                            },
                            LicenseNumber = "-",
                            Header        = new AttachmentDTO(),
                            Footer        = new AttachmentDTO(),
                        };

                        #endregion

                        #region Setting

                        var setting = new SettingDTO
                        {
                            AwajNumber             = "923/2008",
                            EmbassyApplicationType = EmbassyApplicationTypes.SponsorNameOnTop,
                            SyncDuration           = 1,
                            StartSync = true,
                        };

                        #endregion

                        ProductActivation.FirstActivatedDate = key.FirstActivatedDate; // DateTime.Now; //GETUTCDATETIME StoredPr
                        ProductActivation.ExpiryDate         = key.ExpiryDate;         // DateTime.Now.AddDays(key.ExpiryDuration);

                        ProductActivation.Agency = localAgency;
                        setting.Agency           = localAgency;

                        ////Since we don't update Header and Footer from Server we didnt need the following 6 lines
                        //localAgency.Address.Agency = localAgency;
                        //localAgency.Header.Agency = localAgency;
                        //localAgency.Footer.Agency = localAgency;

                        //foreignAgent.Address.Agency = localAgency;
                        //foreignAgent.Header.Agency = localAgency;
                        //foreignAgent.Footer.Agency = localAgency;

                        _unitOfWork.Repository <AgencyDTO>().Insert(localAgency);
                        _unitOfWork.Repository <AgentDTO>().Insert(foreignAgent);

                        _unitOfWork.Repository <SettingDTO>().Insert(setting);
                        _unitOfWork.Repository <ProductActivationDTO>().Insert(ProductActivation);
                    }
                    else
                    {
                        //localAgency.Synced = false;
                        //foreignAgent.Synced = false;

                        //_unitOfWork.Repository<AgencyDTO>().Update(localAgency);
                        //_unitOfWork.Repository<AgentDTO>().Update(foreignAgent);

                        _unitOfWork.Repository <ProductActivationDTO>().Update(ProductActivation);
                    }

                    int changes = _unitOfWork.Commit();
                    if (changes > 0)
                    {
                        Singleton.ProductActivation = ProductActivation;
                        _login = true;
                    }
                    else
                    {
                        MessageBox.Show(
                            "Can't Activate the product, check your product key and try again, or contact pinnasofts!");
                        ProductKey        = "";
                        CommandsEnability = true;
                    }
                }
                catch
                {
                    MessageBox.Show("Error:" + Environment.NewLine + " There may be no Internet connection." +
                                    Environment.NewLine + "Check your connection and try again.");
                    CommandsEnability = true;
                }
            }
            else
            {
                MessageBox.Show(
                    "Can't Activate the product, check your product key and try again, or contact pinnasofts!");
                ProductKey        = "";
                CommandsEnability = true;
            }
        }