Esempio n. 1
0
        public bool WriteErrorLog(ErrorLog errorLog)
        {
            try
            {
                Domain.Local.Logs.ErrorLogs newErrorLog = new Domain.Local.Logs.ErrorLogs
                {
                    installation   = session.Get <TUFStatus.Domain.Local.App.Installations>(errorLog.InstallationID),
                    application_id = (int)errorLog.ApplicationID,
                    action_type_id = (int)errorLog.ActionTypeID,
                    gear_code      = errorLog.GearCode,
                    error_time     = errorLog.ErrorTime,
                    error_message  = errorLog.ErrorMessage,
                    error_info     = errorLog.ErrorInfo,
                    is_transferred = 0
                };

                var repository = new TUFStatus.DAL.Repositories.Repository <ISession, Domain.Local.Logs.ErrorLogs>(session);


                using (var xa = session.BeginTransaction())
                {
                    repository.Add(newErrorLog);
                    xa.Commit();
                    //Assert.NotNull(fad);
                    //Assert.AreNotEqual(0, fad.Id);
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.Instance.HandleError(ActionLog.ActionTypes.Application, "", "There was an error writing the local action log:", ex.Message);
                return(false);
            }

            return(true);
        }
Esempio n. 2
0
        public int AppendActionLogs(List <TUFStatus.Domain.Local.Logs.ActionLogs> actionLogs, ISession localSession)
        {
            int transferCount = 0;

            if (actionLogs.Count > 0)
            {
                try
                {
                    var localRepository = new TUFStatus.DAL.Repositories.Repository <ISession, Domain.Local.Logs.ActionLogs>(localSession);
                    var cloudRepository = new TUFStatus.DAL.Repositories.Repository <ISession, Domain.Cloud.Logs.ActionLogs>(session);

                    for (int i = 0; i < actionLogs.Count; i++)
                    {
                        TUFStatus.Domain.Cloud.Logs.ActionLogs newActionLog = new TUFStatus.Domain.Cloud.Logs.ActionLogs();

                        newActionLog.installation    = session.Get <TUFStatus.Domain.Cloud.App.Installations>(actionLogs[i].installation.installation_id);
                        newActionLog.application     = session.Get <TUFStatus.Domain.Cloud.App.Applications>(actionLogs[i].application_id);
                        newActionLog.action_type_id  = actionLogs[i].action_type_id;
                        newActionLog.gear_code       = actionLogs[i].gear_code;
                        newActionLog.action_time     = actionLogs[i].action_time;
                        newActionLog.action_messages = actionLogs[i].action_messages;
                        newActionLog.action_result   = actionLogs[i].action_result;
                        newActionLog.had_error       = actionLogs[i].had_error;

                        using (var xa = session.BeginTransaction())
                        {
                            cloudRepository.Add(newActionLog);
                            xa.Commit();
                            //Assert.NotNull(fad);
                            //Assert.AreNotEqual(0, fad.Id);
                        }

                        using (var xb = localSession.BeginTransaction())
                        {
                            actionLogs[i].is_transferred = 1;
                            localRepository.Update(actionLogs[i]);
                            xb.Commit();
                        }
                        transferCount += 1;
                    }
                }
                catch (Exception ex)
                {
                    ErrorHandler.Instance.HandleError(ActionLog.ActionTypes.LogTransfer, "", "There was an error appending the action logs:", ex.Message);
                    return(0);
                }
            }
            return(transferCount);
        }
Esempio n. 3
0
        public bool SaveInstallationDetails(TUFStatus.Domain.Cloud.App.Installations installation)
        {
            try
            {
                // first delete existing ? no need because we use save or update

                Domain.Local.App.Installations newInstallation = new Domain.Local.App.Installations
                {
                    installation_id    = installation.installation_id,
                    country_code       = installation.country_code,
                    install_no         = installation.install_no,
                    description        = installation.description,
                    tufman_driver      = installation.tufman_driver,
                    tufman_server      = installation.tufman_server,
                    tufman_database    = installation.tufman_database,
                    tufman_userlogin   = installation.tufman_userlogin,
                    tufman_username    = installation.tufman_username,
                    tufman_password    = installation.tufman_password,
                    run_backup         = installation.run_backup,
                    backup_folder      = installation.backup_folder,
                    backup_copy_folder = installation.backup_copy_folder,
                    portal_driver      = installation.portal_driver,
                    portal_server      = installation.portal_server,
                    portal_database    = installation.portal_database,
                    portal_userlogin   = installation.portal_userlogin,
                    portal_username    = installation.portal_username,
                    portal_password    = installation.portal_password
                };

                var repository = new TUFStatus.DAL.Repositories.Repository <ISession, Domain.Local.App.Installations>(session);


                using (var xa = session.BeginTransaction())
                {
                    repository.Add(newInstallation);
                    xa.Commit();
                    //Assert.NotNull(fad);
                    //Assert.AreNotEqual(0, fad.Id);
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.Instance.HandleError(ActionLog.ActionTypes.Application, "", "There was an error writing the local installation copy:", ex.Message);
                return(false);
            }

            return(true);
        }
Esempio n. 4
0
        public List <Domain.Local.Logs.ErrorLogs> GetLocalErrorLogs()
        {
            List <TUFStatus.Domain.Local.Logs.ErrorLogs> errorLogs;

            try
            {
                var repository = new TUFStatus.DAL.Repositories.Repository <ISession, Domain.Local.Logs.ErrorLogs>(session);
                errorLogs = repository.All().ToList();
                return(errorLogs);
            }
            catch (Exception ex)
            {
                ErrorHandler.Instance.HandleError(ActionLog.ActionTypes.Application, "", "There was an error retrieving the local error logs:", ex.Message);
                return(null);
            }
        }
Esempio n. 5
0
        public bool WriteActionLog(ActionLog actionLog)
        {
            try
            {
                short hadError;

                if (actionLog.HadError)
                {
                    hadError = 1;
                }
                else
                {
                    hadError = 0;
                }

                Domain.Local.Logs.ActionLogs newActionLog = new Domain.Local.Logs.ActionLogs
                {
                    installation    = session.Get <TUFStatus.Domain.Local.App.Installations> (actionLog.InstallationID),
                    application_id  = (int)actionLog.ApplicationID,
                    action_type_id  = (int)actionLog.ActionTypeID,
                    gear_code       = actionLog.GearCode,
                    action_time     = actionLog.ActionTime,
                    action_result   = actionLog.ActionResult,
                    action_messages = actionLog.ActionMessage,
                    had_error       = hadError,
                    is_transferred  = 0
                };

                var repository = new TUFStatus.DAL.Repositories.Repository <ISession, Domain.Local.Logs.ActionLogs>(session);


                using (var xa = session.BeginTransaction())
                {
                    repository.Add(newActionLog);
                    xa.Commit();
                    //Assert.NotNull(fad);
                    //Assert.AreNotEqual(0, fad.Id);
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.Instance.HandleError(ActionLog.ActionTypes.Application, "", "There was an error writing the local action log:", ex.Message);
                return(false);
            }

            return(true);
        }
Esempio n. 6
0
        public object GetInstallationDetails()
        {
            Domain.Local.App.Installations installation;

            try
            {
                var repository = new TUFStatus.DAL.Repositories.Repository <ISession, Domain.Local.App.Installations>(session);

                installation = repository.FindById(Program.TufmanInstallationID);
            }
            catch (Exception ex)
            {
                ErrorHandler.Instance.HandleError(ActionLog.ActionTypes.Application, "", "There was an error retrieving the installation details from the local DB:", ex.Message);
                return(null);
            }
            return(installation);
        }
Esempio n. 7
0
        public void test_add_new_connection_log(ISession session)
        {
            var connection_log = new Domain.Cloud.Logs.ConnectionLogs {
                application = session.Get <TUFStatus.Domain.Cloud.App.Applications>(3), installation = session.Get <TUFStatus.Domain.Cloud.App.Installations>(1), connection_time = DateTime.Now
            };
            //if connection_log.disconnection_time.HasValue

            //IConnectionLogRepository repository = new ConnectionLogRepository();
            var repository = new TUFStatus.DAL.Repositories.Repository <ISession, Domain.Cloud.Logs.ConnectionLogs>(session);

            using (var xa = session.BeginTransaction())
            {
                repository.Add(connection_log);
                //frepo.Add(fad);
                xa.Commit();
                //frepo.Reload(fad);
                //Assert.NotNull(fad);
                //Assert.AreNotEqual(0, fad.Id);
            }
        }
Esempio n. 8
0
        public int TransferErrorLogs(IStatusDB destDB)
        {
            // transfers error logs with 'is_transferred' of false to the destination (cloud) db
            int result = 0;
            List <TUFStatus.Domain.Local.Logs.ErrorLogs> errorLogs;

            try
            {
                var repository = new TUFStatus.DAL.Repositories.Repository <ISession, Domain.Local.Logs.ErrorLogs>(session);
                errorLogs = repository.FilterBy(x => x.is_transferred == 0).ToList();

                result = destDB.AppendErrorLogs(errorLogs, session);
            }
            catch (Exception ex)
            {
                ErrorHandler.Instance.HandleError(ActionLog.ActionTypes.LogTransfer, "", "There was an error transferring the local action logs to the cloud:", ex.Message);
                return(-1);
            }

            return(result);
        }
Esempio n. 9
0
        public int WriteConnectionLog()
        {
            try
            {
                //using (ISession session = NHibernateHelper.OpenLocalSession())
                Domain.Cloud.Logs.ConnectionLogs connection_log = new Domain.Cloud.Logs.ConnectionLogs
                {
                    application     = session.Get <TUFStatus.Domain.Cloud.App.Applications>(3),
                    installation    = session.Get <TUFStatus.Domain.Cloud.App.Installations>(Program.TufmanInstallationID),
                    connection_time = DateTime.Now
                };

                var repository = new TUFStatus.DAL.Repositories.Repository <ISession, Domain.Cloud.Logs.ConnectionLogs>(session);


                using (var xa = session.BeginTransaction())
                {
                    repository.Add(connection_log);
                    xa.Commit();
                    //Assert.NotNull(fad);
                    //Assert.AreNotEqual(0, fad.Id);
                }


                if (Program.RunMode == 0)
                {
                    int x = connection_log.connection_id;
                    System.Windows.Forms.MessageBox.Show(x.ToString() + ":" + connection_log.installation.installation_id.ToString());    //.description.ToString());
                }

                connectionID = connection_log.connection_id;
            }
            catch (Exception ex)
            {
                ErrorHandler.Instance.HandleError(ActionLog.ActionTypes.Application, "", "There was an error writing the connection log:", ex.Message);
                return(0);
            }

            return(connectionID);
        }
Esempio n. 10
0
        public int ClearErrorLogs()
        {
            // clear the local log file of transferred data
            int count = 0;

            // error logs
            List <TUFStatus.Domain.Local.Logs.ErrorLogs> errorLogs;
            var repository = new TUFStatus.DAL.Repositories.Repository <ISession, Domain.Local.Logs.ErrorLogs>(session);

            errorLogs = repository.FilterBy(x => x.is_transferred == 1).ToList();

            var xa = session.BeginTransaction();

            for (int i = 0; i < errorLogs.Count; i++)
            {
                repository.Delete(errorLogs[i]);
                count += 1;
            }
            xa.Commit();

            return(count);
        }
Esempio n. 11
0
        public bool WriteDisconnectionTime()
        {
            try
            {
                var repository = new TUFStatus.DAL.Repositories.Repository <ISession, Domain.Cloud.Logs.ConnectionLogs>(session);
                //Domain.Cloud.Logs.Connection_Logs connection_log = session.Get<Domain.Cloud.Logs.Connection_Logs>(connectionID);

                using (var xa = session.BeginTransaction())
                {
                    Domain.Cloud.Logs.ConnectionLogs connection_log = repository.FindById(connectionID);
                    connection_log.disconnection_time = DateTime.Now;
                    repository.Update(connection_log);
                    xa.Commit();
                    //Assert.NotNull(fad);
                    //Assert.AreNotEqual(0, fad.Id);
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.Instance.HandleError(ActionLog.ActionTypes.Application, "", "There was an error writing the disconnection time:", ex.Message);
                return(false);
            }
            return(true);
        }
Esempio n. 12
0
        //public int SynchroniseTable(ISession sourceSession, ISession destinationSession,Synchroniser.SyncDirection syncDirection, Synchroniser.SyncType syncType)   //, List<T> sourceList
        //{
        //    int result = 0;
        //    int batchSize = 50;
        //    int numBatches = 0;

        //    // get the source list -- need to add filtering though
        //    TUFStatus.DAL.Repositories.Repository<ISession, T> sourceRepo = new TUFStatus.DAL.Repositories.Repository<ISession, T>(sourceSession);
        //    List<T> sourceList = sourceRepo.All().ToList();

        //    // destination repo
        //    //TUFStatus.DAL.Repositories.Repository<ISession, T> destRepo = new TUFStatus.DAL.Repositories.Repository<ISession, T>(destinationSession);

        //    // setbatchsize does not work for postgres, batching not supported for that
        //    //destinationSession.SetBatchSize(100);

        //    // split into batch-size chunks

        //    numBatches = sourceList.Count() / batchSize;
        //    if ((sourceList.Count() % batchSize) > 0)
        //        numBatches += 1;

        //    for (int b = 0; b < numBatches; b++)
        //    {
        //        var xa = destinationSession.BeginTransaction();
        //        for (int i = 0; i < batchSize; i++)
        //        {
        //            int item = (b * batchSize) + i;

        //            if (item < sourceList.Count())
        //            {
        //                destinationSession.Merge(sourceList[(b * batchSize) + i]);
        //                result += 1;
        //            }
        //        }
        //        xa.Commit();
        //    }

        //    //var xa = destinationSession.BeginTransaction();
        //    //foreach (T sourceRecord in sourceList)
        //    //{
        //    //    //destinationSession.SaveOrUpdate(sourceRecord);
        //    //    destinationSession.Merge(sourceRecord);
        //    //    //destRepo.Save(sourceRecord);
        //    //}
        //    //xa.Commit();

        //    sourceRepo = null;

        //    return result;
        //}

        public int SynchroniseTable(ISession sourceSession, ISession destinationSession, IStatelessSession destinationStatelessSession, Func <T, bool> predicate, Synchroniser.SyncDirection syncDirection, Synchroniser.SyncType syncType, bool forcePredicate)   //, List<T> sourceList
        {
            int      result     = 0;
            int      batchSize  = 50;
            int      numBatches = 0;
            List <T> sourceList;

            try
            {
                // get the source list -- don't use filtering if in replace mode
                TUFStatus.DAL.Repositories.Repository <ISession, T> sourceRepo = new TUFStatus.DAL.Repositories.Repository <ISession, T>(sourceSession);

                if (syncType == Synchroniser.SyncType.Replace)
                {
                    // delete all records first
                    var xa = destinationSession.BeginTransaction();

                    destinationSession.Delete("from " + typeof(T));

                    xa.Commit();

                    if (forcePredicate)
                    {
                        sourceList = sourceRepo.All().Where(predicate).ToList();
                    }
                    else
                    {
                        sourceList = sourceRepo.All().ToList();
                    }

                    numBatches = sourceList.Count() / batchSize;
                    if ((sourceList.Count() % batchSize) > 0)
                    {
                        numBatches += 1;
                    }

                    for (int b = 0; b < numBatches; b++)
                    {
                        var xb = destinationStatelessSession.BeginTransaction();
                        for (int i = 0; i < batchSize; i++)
                        {
                            int item = (b * batchSize) + i;

                            if (item < sourceList.Count())
                            {
                                destinationStatelessSession.Insert(sourceList[(b * batchSize) + i]);
                                result += 1;
                            }
                        }
                        xb.Commit();
                    }
                }
                else // merge
                {
                    sourceList = sourceRepo.All().Where(predicate).ToList();

                    // split into batch-size chunks

                    numBatches = sourceList.Count() / batchSize;
                    if ((sourceList.Count() % batchSize) > 0)
                    {
                        numBatches += 1;
                    }

                    for (int b = 0; b < numBatches; b++)
                    {
                        var xa = destinationSession.BeginTransaction();
                        for (int i = 0; i < batchSize; i++)
                        {
                            int item = (b * batchSize) + i;

                            if (item < sourceList.Count())
                            {
                                destinationSession.Merge(sourceList[(b * batchSize) + i]); //Merge
                                result += 1;
                            }
                        }
                        xa.Commit();
                    }
                    sourceRepo = null;
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.Instance.HandleError(ActionLog.ActionTypes.Application, "", "There was an error syncronising " + typeof(T).ToString() + ":", ex.Message);
                result = -1;
            }
            return(result);
        }
Esempio n. 13
0
        public int SynchroniseTableDeletes(ISession sourceSession, ISession destinationSession, Func <T, bool> predicate, DateTime?syncDate, Int16?tableID)    //, List<T> sourceList
        {
            int result = 0;
            //int batchSize = 50;
            //int numBatches = 0;
            List <DeletedLog> deleteList;
            string            keyClause = "";

            TUFStatus.DAL.Repositories.Repository <ISession, DeletedLog> sourceRepo = null;

            try
            {
                // get the source delete list
                sourceRepo = new TUFStatus.DAL.Repositories.Repository <ISession, DeletedLog>(sourceSession);

                if (syncDate == null)
                {
                    deleteList = sourceRepo.All().Where(x => x.audited_table.table_id == tableID).ToList();
                }
                else
                {
                    deleteList = sourceRepo.All().Where(x => x.audited_table.table_id == tableID).Where(x => x.deleted_date > syncDate).ToList();
                }

                result = deleteList.Count;

                // build the key clause
                // **********************************************
                // may need to break this into chunks of ~50 records, in case the list is too long

                if (deleteList.Count > 0)
                {
                    for (int i = 0; i < deleteList.Count; i++)
                    {
                        string keyVal;

                        keyVal = deleteList[i].keyvalue;
                        if (deleteList[i].audited_table.key_field_type == "string")
                        {
                            keyVal = "'" + keyVal + "'";
                        }

                        if (i == 0)
                        {
                            keyClause = keyVal;
                        }
                        else
                        {
                            keyClause = keyClause + "," + keyVal;
                        }
                    }

                    var xa = destinationSession.BeginTransaction();

                    destinationSession.Delete("from " + typeof(T) + " where " + deleteList[0].audited_table.key_field + " in (" + keyClause + ")");  // where + key_field in (list)


                    xa.Commit();


                    // split into batch-size chunks

                    //numBatches = sourceList.Count() / batchSize;
                    //if ((sourceList.Count() % batchSize) > 0)
                    //    numBatches += 1;

                    //for (int b = 0; b < numBatches; b++)
                    //{
                    //    var xa = destinationSession.BeginTransaction();
                    //    for (int i = 0; i < batchSize; i++)
                    //    {
                    //        int item = (b * batchSize) + i;

                    //        if (item < sourceList.Count())
                    //        {
                    //            destinationSession.Merge(sourceList[(b * batchSize) + i]);
                    //            result += 1;
                    //        }
                    //    }
                    //    xa.Commit();


                    //}
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.Instance.HandleError(ActionLog.ActionTypes.Application, "", "There was an error syncronising table deletes for " + typeof(T).ToString() + ":", ex.Message);
                result = -1;
            }
            finally
            {
                sourceRepo = null;
            }
            return(result);
        }
Esempio n. 14
0
        public int SynchroniseTableInserts(ISession sourceSession, IStatelessSession destinationSession, Func <T, bool> predicate, Synchroniser.SyncDirection syncDirection, Synchroniser.SyncType syncType)   //, List<T> sourceList
        {
            int      result     = 0;
            int      batchSize  = 50;
            int      numBatches = 0;
            List <T> sourceList;

            try
            {
                // get the source list -- don't use filtering if in replace mode
                TUFStatus.DAL.Repositories.Repository <ISession, T> sourceRepo = new TUFStatus.DAL.Repositories.Repository <ISession, T>(sourceSession);

                if (syncType == Synchroniser.SyncType.Replace)
                {
                    // delete all records first
                    var xa = destinationSession.BeginTransaction();

                    ///var metaData = destinationSession.SessionFactory.GetClassMetadata(typeof(T)) as NHibernate.Persister.Entity.AbstractEntityPersister;
                    ///string table = metaData.TableName;
                    ///string deleteAll = string.Format("DELETE FROM \"{0}\"", table);
                    ///destinationSession.CreateSQLQuery(deleteAll).ExecuteUpdate();
                    //destinationSession.Delete("Select * from " + typeof(T));

                    destinationSession.Delete("from " + typeof(T));


                    //TUFStatus.DAL.Repositories.Repository<ISession, T> destRepo = new TUFStatus.DAL.Repositories.Repository<ISession, T>(destinationSession);

                    //List<T> deleteList = sourceRepo.All().ToList() ;
                    //foreach (T deleteItem in deleteList)
                    //{
                    //    destRepo.Delete(deleteItem);
                    //}

                    xa.Commit();

                    sourceList = sourceRepo.All().ToList();
                }
                else
                {
                    sourceList = sourceRepo.All().Where(predicate).ToList();
                }

                // split into batch-size chunks

                numBatches = sourceList.Count() / batchSize;
                if ((sourceList.Count() % batchSize) > 0)
                {
                    numBatches += 1;
                }

                for (int b = 0; b < numBatches; b++)
                {
                    var xa = destinationSession.BeginTransaction();
                    for (int i = 0; i < batchSize; i++)
                    {
                        int item = (b * batchSize) + i;

                        if (item < sourceList.Count())
                        {
                            destinationSession.Insert(sourceList[(b * batchSize) + i]); //Merge
                            result += 1;
                        }
                    }
                    xa.Commit();

                    sourceRepo = null;
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.Instance.HandleError(ActionLog.ActionTypes.Application, "", "There was an error syncronising inserts for " + typeof(T).ToString() + ":", ex.Message);
                result = -1;
            }
            return(result);
        }
Esempio n. 15
0
        public int SynchroniseWithSPC(CloudStatusDB cloudStatusDB)
        {
            // synchronises from the cloud portal db to tufsync_xx at SPC. The tables to be synchronised are stored in the app.sync folder of the tufsync_control db.
            int               result              = 0;
            ISession          IMSSession          = null;
            IStatelessSession IMSStatelessSession = null;
            ISession          spcSession          = null;
            IStatelessSession spcStatelessSession = null;
            SyncDirection     syncDirection;
            SyncType          syncType;

            // retrieve a list of the synchronisation items
            List <TUFStatus.Domain.Cloud.App.Sync> syncList;

            try
            {
                var repository = new TUFStatus.DAL.Repositories.Repository <ISession, Domain.Cloud.App.Sync>(cloudStatusDB.session);
                //var xa = cloudStatusDB.session.BeginTransaction();
                syncList = repository.FilterBy(x => x.application.application_id == (int)TUFStatus.TUFMANInstallation.ApplicationList.TUFStatusSPC).OrderBy(x => x.installation.installation_id).ThenBy(x => x.sync_order).ToList();

                //xa.Dispose();
                //xa.Rollback();

                foreach (TUFStatus.Domain.Cloud.App.Sync syncItem in syncList)
                {
                    string tablename;
                    int    tableResult = 0;
                    string countryCode;
                    string portalDatabase;
                    string portalServer;
                    string spcSyncDB;

                    // Tufman installation information comes from the 'installation' object of the sync item, i.e. the installation_id field of the sync table
                    portalDatabase = syncItem.installation.portal_database;
                    portalServer   = syncItem.installation.portal_server;
                    spcSyncDB      = "tufsync_" + syncItem.installation.country_code.ToLower();

                    // Cloud IMS session
                    IMSSession          = TUFStatus.DAL.Configuration.IMSNHibernateHelper.CreateSessionFactory(portalServer, portalDatabase).OpenSession();
                    IMSStatelessSession = TUFStatus.DAL.Configuration.IMSNHibernateHelper.CreateSessionFactory(portalServer, portalDatabase).OpenStatelessSession();

                    spcSession          = SPCNHibernateHelper.CreateSessionFactory(spcSyncDB).OpenSession();
                    spcStatelessSession = SPCNHibernateHelper.CreateSessionFactory(spcSyncDB).OpenStatelessSession();

                    tablename = syncItem.schemaname + "." + syncItem.tablename;

                    countryCode = Program.tufmanInstallation.CountryCode();

                    // Check the direction of syncronisation to do
                    switch (syncItem.direction_code)
                    {
                    case "up":     // merge or replace
                        syncDirection = SyncDirection.Up;
                        break;

                    case "dn":     // replace
                        syncDirection = SyncDirection.Down;
                        break;

                    default:     // anything else, only "up" is supported at present though
                        syncDirection = SyncDirection.Up;
                        break;
                    }

                    // Check the type of syncronisation to do
                    switch (syncItem.sync_type_code)
                    {
                    case "mr":     // merge or replace
                        syncType = SyncType.Merge;
                        break;

                    case "rp":     // replace
                        syncType = SyncType.Replace;
                        break;

                    case "dl":     // delete
                        syncType = SyncType.Delete;
                        break;

                    default:     // 'mg' - merge only
                        syncType = SyncType.MergeOrReplace;
                        break;
                    }

                    // set sync mode to replace if mode is 'full' and sync type is merge or replace
                    //if (mode == 1)
                    //{
                    //    if (syncType == SyncType.MergeOrReplace)
                    //        syncType = SyncType.Replace;
                    //}

                    // checked the last sync date, if null then sync becomes 'replace'
                    if (syncItem.sync_date == null & syncType != SyncType.Delete)
                    {
                        syncType = SyncType.Replace;
                    }



                    // ********************* need to add filtering to this **************************** //
                    if (syncType == SyncType.Delete)
                    {
                        if (syncDirection == SyncDirection.Down)
                        {
                            switch (tablename.ToLower())
                            {
                            case "lic.agr_rep_period":
                                TableSynchroniser <TUFMAN.Domain.Lic.AgrRepPeriod> ts11 = new TableSynchroniser <TUFMAN.Domain.Lic.AgrRepPeriod>();
                                tableResult = ts11.SynchroniseTableDeletes(IMSSession, spcSession, x => (x.changed_date >= syncItem.sync_date), syncItem.sync_date, syncItem.table_id);
                                break;

                            default:
                                // report an error here, table is not recognised and won't be synchronised
                                break;
                            }
                        }
                        else           // up is the default
                        {
                            switch (tablename.ToLower())
                            {
                            case "lic.agr_rep_period":
                                TableSynchroniser <TUFMAN.Domain.Lic.AgrRepPeriod> ts11 = new TableSynchroniser <TUFMAN.Domain.Lic.AgrRepPeriod>();
                                tableResult = ts11.SynchroniseTableDeletes(spcSession, IMSSession, x => (x.changed_date >= syncItem.sync_date), syncItem.sync_date, syncItem.table_id);
                                break;

                            default:
                                // report an error here, table is not recognised and won't be synchronised
                                break;
                            }
                        }
                    }
                    else
                    {
                        if (syncDirection == SyncDirection.Down)
                        {
                            switch (tablename.ToLower())
                            {
                            case "ves.vessels":
                                TableSynchroniser <TUFMAN.Domain.Ves.Vessels> ts1 = new TableSynchroniser <TUFMAN.Domain.Ves.Vessels>();

                                tableResult = ts1.SynchroniseTable(IMSSession, spcSession, spcStatelessSession, x => (x.changed_date >= syncItem.sync_date), syncDirection, syncType, false);
                                break;

                            default:
                                // report an error here, table is not recognised and won't be synchronised
                                break;
                            }
                        }
                        else           // up is the default
                        {
                            switch (tablename.ToLower())
                            {
                            case "ves.vessels":
                                TableSynchroniser <TUFMAN.Domain.Ves.Vessels> ts1 = new TableSynchroniser <TUFMAN.Domain.Ves.Vessels>();

                                tableResult = ts1.SynchroniseTable(spcSession, IMSSession, IMSStatelessSession, x => (x.changed_date >= syncItem.sync_date), syncDirection, syncType, false);
                                break;

                            default:
                                // report an error here, table is not recognised and won't be synchronised
                                break;
                            }
                        }
                    }
                    var xa = cloudStatusDB.session.BeginTransaction();
                    syncItem.last_run_date   = DateTime.Now;
                    syncItem.last_run_result = tableResult;
                    if (tableResult >= 0)
                    {
                        result            += tableResult;
                        syncItem.sync_date = DateTime.Now;
                    }
                    xa.Commit();
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.Instance.HandleError(ActionLog.ActionTypes.Application, "", "There was an error running the synchroniser:", ex.Message);
                return(-1);
            }
            finally
            {
                if (IMSSession != null && IMSSession.IsOpen)
                {
                    IMSSession.Close();
                }

                if (IMSStatelessSession != null && IMSStatelessSession.IsOpen)
                {
                    IMSStatelessSession.Close();
                }

                //IMSNHibernateHelper.SessionFactory.Close();

                if (spcSession != null && spcSession.IsOpen)
                {
                    spcSession.Close();
                }
            }

            return(result);
        }
Esempio n. 16
0
        public int Synchronise(int installationID, CloudStatusDB cloudStatusDB, int mode)
        {
            // mode 0 = partial, 1 = full (i.e. mergeorReplace become replace)
            int               result              = 0;
            ISession          IMSSession          = null;
            IStatelessSession IMSStatelessSession = null;
            ISession          tufmanSession       = null;
            SyncDirection     syncDirection;
            SyncType          syncType;

            // retrieve a list of the synchronisation items
            List <TUFStatus.Domain.Cloud.App.Sync> syncList;

            try
            {
                var repository = new TUFStatus.DAL.Repositories.Repository <ISession, Domain.Cloud.App.Sync>(cloudStatusDB.session);
                //var xa = cloudStatusDB.session.BeginTransaction();
                syncList = repository.FilterBy(x => x.installation.installation_id == installationID && x.application.application_id == (int)TUFStatus.TUFMANInstallation.ApplicationList.TUFStatus).OrderBy(x => x.sync_order).ToList();

                //xa.Dispose();
                //xa.Rollback();
                // Cloud IMS session
                IMSSession          = TUFStatus.DAL.Configuration.IMSNHibernateHelper.CreateSessionFactory().OpenSession();
                IMSStatelessSession = TUFStatus.DAL.Configuration.IMSNHibernateHelper.CreateSessionFactory().OpenStatelessSession();


                foreach (TUFStatus.Domain.Cloud.App.Sync syncItem in syncList)
                {
                    string tablename;
                    int    tableResult = 0;
                    string countryCode;

                    //Type open = typeof(TableSynchroniser<>);
                    ////Type closed = open.MakeGenericType(typeof(TUFMAN.Domain.Ves.VesselCategories));

                    //Type closed = open.MakeGenericType(Type.GetType(GetTableClass(tablename)));
                    //// Type closed2 = open.MakeGenericType(x.GetType());
                    //dynamic ts = Activator.CreateInstance(closed);

                    //TUFMAN.Domain.Ves.VesselActAnnual v;


                    // create the TUFMAN session

                    //UnitOfWork unitOfWorkTUFMAN = new UnitOfWork(TufmanNHibernateHelper.CreateSessionFactory("Server=NOUSQL50\\SQLEXPRESS;Database=tufman_ws;Trusted_Connection=True"));
                    tufmanSession = TufmanNHibernateHelper.CreateSessionFactory().OpenSession();

                    tablename = syncItem.schemaname + "." + syncItem.tablename;

                    countryCode = Program.tufmanInstallation.CountryCode();

                    // Check the direction of syncronisation to do
                    switch (syncItem.direction_code)
                    {
                    case "up":     // merge or replace
                        syncDirection = SyncDirection.Up;
                        break;

                    case "dn":     // replace
                        syncDirection = SyncDirection.Down;
                        break;

                    default:     // anything else, only "up" is supported at present though
                        syncDirection = SyncDirection.Up;
                        break;
                    }

                    // Check the type of syncronisation to do
                    switch (syncItem.sync_type_code)
                    {
                    case "mr":     // merge or replace
                        syncType = SyncType.Merge;
                        break;

                    case "rp":     // replace
                        syncType = SyncType.Replace;
                        break;

                    case "dl":     // delete
                        syncType = SyncType.Delete;
                        break;

                    default:     // 'mg' - merge only
                        syncType = SyncType.MergeOrReplace;
                        break;
                    }

                    // set sync mode to replace if mode is 'full' and sync type is merge or replace
                    if (mode == 1)
                    {
                        if (syncType == SyncType.MergeOrReplace)
                        {
                            syncType = SyncType.Replace;
                        }
                    }

                    // checked the last sync date, if null then sync becomes 'replace'
                    if (syncItem.sync_date == null & syncType != SyncType.Delete)
                    {
                        syncType = SyncType.Replace;
                    }



                    // ********************* need to add filtering to this **************************** //
                    if (syncType == SyncType.Delete)
                    {
                        switch (tablename.ToLower())
                        {
                        case "lic.agr_rep_period":
                            TableSynchroniser <TUFMAN.Domain.Lic.AgrRepPeriod> ts11 = new TableSynchroniser <TUFMAN.Domain.Lic.AgrRepPeriod>();
                            tableResult = ts11.SynchroniseTableDeletes(tufmanSession, IMSSession, x => (x.changed_date >= syncItem.sync_date), syncItem.sync_date, syncItem.table_id);
                            break;

                        default:
                            // report an error here, table is not recognised and won't be synchronised
                            break;
                        }
                    }
                    else
                    {
                        switch (tablename.ToLower())
                        {
                        case "ves.vessel_categories":
                            TableSynchroniser <TUFMAN.Domain.Ves.VesselCategories> ts0 = new TableSynchroniser <TUFMAN.Domain.Ves.VesselCategories>();
                            tableResult = ts0.SynchroniseTable(tufmanSession, IMSSession, IMSStatelessSession, x => (x.changed_date >= syncItem.sync_date), syncDirection, syncType, false);
                            break;

                        case "ves.vessels":
                            TableSynchroniser <TUFMAN.Domain.Ves.Vessels> ts1 = new TableSynchroniser <TUFMAN.Domain.Ves.Vessels>();
                            //ts1.SynchroniseTable(tufmanSession, IMSSession);
                            //tableResult = ts1.SynchroniseTableInserts(tufmanSession, IMSStatelessSession, x => (x.changed_date >= syncItem.sync_date), syncDirection, syncType);

                            tableResult = ts1.SynchroniseTable(tufmanSession, IMSSession, IMSStatelessSession, x => (x.changed_date >= syncItem.sync_date), syncDirection, syncType, false);
                            break;

                        case "ref.agreement_ownership":
                            TableSynchroniser <TUFMAN.Domain.Ref.AgreementOwnership> ts2 = new TableSynchroniser <TUFMAN.Domain.Ref.AgreementOwnership>();
                            tableResult = ts2.SynchroniseTable(tufmanSession, IMSSession, IMSStatelessSession, x => (x.changed_date >= syncItem.sync_date), syncDirection, syncType, false);
                            break;

                        case "ref.agreement_types":
                            TableSynchroniser <TUFMAN.Domain.Ref.AgreementTypes> ts3 = new TableSynchroniser <TUFMAN.Domain.Ref.AgreementTypes>();
                            tableResult = ts3.SynchroniseTable(tufmanSession, IMSSession, IMSStatelessSession, x => (x.changed_date >= syncItem.sync_date), syncDirection, syncType, false);
                            break;

                        case "ref.license_status":
                            TableSynchroniser <TUFMAN.Domain.Ref.LicenseStatus> ts4 = new TableSynchroniser <TUFMAN.Domain.Ref.LicenseStatus>();
                            tableResult = ts4.SynchroniseTable(tufmanSession, IMSSession, IMSStatelessSession, x => (x.changed_date >= syncItem.sync_date), syncDirection, syncType, false);
                            break;

                        case "ref.license_types":
                            TableSynchroniser <TUFMAN.Domain.Ref.LicenseTypes> ts5 = new TableSynchroniser <TUFMAN.Domain.Ref.LicenseTypes>();
                            tableResult = ts5.SynchroniseTable(tufmanSession, IMSSession, IMSStatelessSession, x => (x.changed_date >= syncItem.sync_date), syncDirection, syncType, false);
                            break;

                        case "ref.iaf_zones":
                            TableSynchroniser <TUFMAN.Domain.Ref.IafZones> ts6 = new TableSynchroniser <TUFMAN.Domain.Ref.IafZones>();
                            tableResult = ts6.SynchroniseTable(tufmanSession, IMSSession, IMSStatelessSession, x => (x.changed_date >= syncItem.sync_date), syncDirection, syncType, false);
                            break;

                        case "tufman.license_profiles":
                            TableSynchroniser <TUFMAN.Domain.Tufman.LicenseProfiles> ts7 = new TableSynchroniser <TUFMAN.Domain.Tufman.LicenseProfiles>();
                            tableResult = ts7.SynchroniseTable(tufmanSession, IMSSession, IMSStatelessSession, x => (x.changed_date >= syncItem.sync_date), syncDirection, syncType, false);
                            break;

                        case "tufman.payment_types":
                            TableSynchroniser <TUFMAN.Domain.Tufman.PaymentTypes> ts8 = new TableSynchroniser <TUFMAN.Domain.Tufman.PaymentTypes>();
                            tableResult = ts8.SynchroniseTable(tufmanSession, IMSSession, IMSStatelessSession, x => (x.changed_date >= syncItem.sync_date), syncDirection, syncType, false);
                            break;

                        case "tufman.companies":
                            TableSynchroniser <TUFMAN.Domain.Tufman.Companies> ts9 = new TableSynchroniser <TUFMAN.Domain.Tufman.Companies>();
                            tableResult = ts9.SynchroniseTable(tufmanSession, IMSSession, IMSStatelessSession, x => (x.changed_date >= syncItem.sync_date), syncDirection, syncType, false);
                            break;

                        case "lic.agreements":
                            TableSynchroniser <TUFMAN.Domain.Lic.Agreements> ts10 = new TableSynchroniser <TUFMAN.Domain.Lic.Agreements>();
                            tableResult = ts10.SynchroniseTable(tufmanSession, IMSSession, IMSStatelessSession, x => (x.changed_date >= syncItem.sync_date), syncDirection, syncType, false);
                            break;

                        case "lic.agr_rep_period":
                            TableSynchroniser <TUFMAN.Domain.Lic.AgrRepPeriod> ts11 = new TableSynchroniser <TUFMAN.Domain.Lic.AgrRepPeriod>();
                            tableResult = ts11.SynchroniseTable(tufmanSession, IMSSession, IMSStatelessSession, x => (x.changed_date >= syncItem.sync_date), syncDirection, syncType, false);
                            break;

                        case "lic.agr_lic_profile":
                            TableSynchroniser <TUFMAN.Domain.Lic.AgrLicProfile> ts12 = new TableSynchroniser <TUFMAN.Domain.Lic.AgrLicProfile>();
                            tableResult = ts12.SynchroniseTable(tufmanSession, IMSSession, IMSStatelessSession, x => (x.changed_date >= syncItem.sync_date), syncDirection, syncType, false);
                            break;

                        case "lic.license_history":
                            TableSynchroniser <TUFMAN.Domain.Lic.LicenseHistory> ts13 = new TableSynchroniser <TUFMAN.Domain.Lic.LicenseHistory>();
                            tableResult = ts13.SynchroniseTable(tufmanSession, IMSSession, IMSStatelessSession, x => (x.changed_date >= syncItem.sync_date), syncDirection, syncType, false);
                            break;

                        case "lic.licenses":
                            TableSynchroniser <TUFMAN.Domain.Lic.Licenses> ts14 = new TableSynchroniser <TUFMAN.Domain.Lic.Licenses>();
                            tableResult = ts14.SynchroniseTable(tufmanSession, IMSSession, IMSStatelessSession, x => (x.changed_date >= syncItem.sync_date), syncDirection, syncType, false);
                            break;

                        case "lic.nat_fleet_iaf":
                            TableSynchroniser <TUFMAN.Domain.Lic.NatFleetIaf> ts15 = new TableSynchroniser <TUFMAN.Domain.Lic.NatFleetIaf>();
                            tableResult = ts15.SynchroniseTable(tufmanSession, IMSSession, IMSStatelessSession, x => (x.changed_date >= syncItem.sync_date), syncDirection, syncType, false);
                            break;

                        case "lic.nat_fleet_lic":
                            TableSynchroniser <TUFMAN.Domain.Lic.NatFleetLic> ts16 = new TableSynchroniser <TUFMAN.Domain.Lic.NatFleetLic>();
                            tableResult = ts16.SynchroniseTable(tufmanSession, IMSSession, IMSStatelessSession, x => (x.changed_date >= syncItem.sync_date), syncDirection, syncType, false);
                            break;

                        case "lic.nat_fleet_reg":
                            TableSynchroniser <TUFMAN.Domain.Lic.NatFleetReg> ts17 = new TableSynchroniser <TUFMAN.Domain.Lic.NatFleetReg>();
                            tableResult = ts17.SynchroniseTable(tufmanSession, IMSSession, IMSStatelessSession, x => (x.changed_date >= syncItem.sync_date), syncDirection, syncType, false);
                            break;

                        case "lic.national_fleets":
                            TableSynchroniser <TUFMAN.Domain.Lic.NationalFleets> ts18 = new TableSynchroniser <TUFMAN.Domain.Lic.NationalFleets>();
                            tableResult = ts18.SynchroniseTable(tufmanSession, IMSSession, IMSStatelessSession, x => (x.changed_date >= syncItem.sync_date), syncDirection, syncType, false);
                            break;

                        case "recon.view_log_yy_mm_eez_all_id":
                            TableSynchroniser <TUFMAN.Domain.Recon.ViewLogYyMmEezAllID> ts19 = new TableSynchroniser <TUFMAN.Domain.Recon.ViewLogYyMmEezAllID>();
                            tableResult = ts19.SynchroniseTable(tufmanSession, IMSSession, IMSStatelessSession, x => (x.YY >= (DateTime.Now.Year - 4) & x.eez_code == countryCode), syncDirection, syncType, true);
                            break;

                        case "recon.view_unloads_yy_mm_all_id":
                            TableSynchroniser <TUFMAN.Domain.Recon.ViewUnloadsYyMmAllID> ts20 = new TableSynchroniser <TUFMAN.Domain.Recon.ViewUnloadsYyMmAllID>();
                            tableResult = ts20.SynchroniseTable(tufmanSession, IMSSession, IMSStatelessSession, x => (x.YY >= (DateTime.Now.Year - 4)), syncDirection, syncType, true);
                            break;

                        case "recon.view_samples_yy_mm_all_id":
                            TableSynchroniser <TUFMAN.Domain.Recon.ViewSamplesYyMmAllID> ts21 = new TableSynchroniser <TUFMAN.Domain.Recon.ViewSamplesYyMmAllID>();
                            tableResult = ts21.SynchroniseTable(tufmanSession, IMSSession, IMSStatelessSession, x => (x.YY >= (DateTime.Now.Year - 4)), syncDirection, syncType, true);
                            break;

                        case "unload.unloadings_ps":
                            TableSynchroniser <TUFMAN.Domain.Unload.UnloadingsPs> ts22 = new TableSynchroniser <TUFMAN.Domain.Unload.UnloadingsPs>();
                            tableResult = ts22.SynchroniseTable(tufmanSession, IMSSession, IMSStatelessSession, x => (x.changed_date >= syncItem.sync_date), syncDirection, syncType, false);
                            break;

                        default:
                            // report an error here, table is not recognised and won't be synchronised
                            break;
                        }
                    }
                    var xa = cloudStatusDB.session.BeginTransaction();
                    syncItem.last_run_date   = DateTime.Now;
                    syncItem.last_run_result = tableResult;
                    if (tableResult >= 0)
                    {
                        result            += tableResult;
                        syncItem.sync_date = DateTime.Now;
                    }
                    xa.Commit();
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.Instance.HandleError(ActionLog.ActionTypes.Application, "", "There was an error running the synchroniser:", ex.Message);
                return(-1);
            }
            finally
            {
                if (IMSSession != null && IMSSession.IsOpen)
                {
                    IMSSession.Close();
                }

                if (IMSStatelessSession != null && IMSStatelessSession.IsOpen)
                {
                    IMSStatelessSession.Close();
                }

                //IMSNHibernateHelper.SessionFactory.Close();

                if (tufmanSession != null && tufmanSession.IsOpen)
                {
                    tufmanSession.Close();
                }
            }

            return(result);
        }