예제 #1
0
        public void AddFolder(string name, DatabaseDirectory parentDirectory)
        {
            //check if folder name already exists within same parent folder
            string countQuery = string.Format("select count(*) from Folders where name_folder = '{0}' and parent_folder = {1}", name,
                                              parentDirectory.id_folder);


            if ((ConnectivityState)_connectivityStateEnum == ConnectivityState.Offline)
            {
                int a = _sqLiteConnector.ExecuteScalarQuery(countQuery);

                //throw exception or write function to check validity in sqlserver/lite
                if (a != 0)
                {
                    MessageBox.Show("Name already exists, please change it.");
                    return;
                }

                string insertOfflineQuery = String.Format("insert into Folders (name_folder, parent_folder, created_by_client, is_synced) values('{0}', {1}, {2}, {3})",
                                                          name, parentDirectory.id_folder, _client._clientID, 0);

                _sqLiteConnector.ExecuteInsertQuery(insertOfflineQuery);

                SetOfflineTree();
            }//=======================================================================================================================================================================
            else if ((ConnectivityState)_connectivityStateEnum == ConnectivityState.Online)
            {
                //check if sqlite has no synced items

                try
                {
                    int a = _sqlServerConnector.ExecuteScalarQuery(countQuery);

                    if (a != 0)
                    {
                        MessageBox.Show("Name already exists, please change it.");
                        return;
                    }

                    string insertOnlineQuery = String.Format("insert into Folders (id_folder, name_folder, parent_folder, created_by_client) values(next value for id_folder_seq, '{0}', {1}, {2})",
                                                             name, parentDirectory.id_folder, _client._clientID);

                    _sqlServerConnector.ExecuteInsertQuery(insertOnlineQuery);
                    //get last id of inserted record
                    int newId = _sqlServerConnector.ExecuteScalarQuery("SELECT MAX(id_folder) FROM Folders");

                    string insertOfflineQuery = String.Format("insert into Folders (id_folder, name_folder, parent_folder, created_by_client, is_synced) values({0}, '{1}', {2}, {3}, {4})",
                                                              newId, name, parentDirectory.id_folder, _client._clientID, 1);

                    _sqLiteConnector.ExecuteInsertQuery(insertOfflineQuery);

                    SetOnlineTree();
                }
                catch (Exception e)
                {
                    MessageBox.Show("Cannot connect in SQL Server.");

                    _connectivityStateEnum = ConnectivityState.Offline;
                    SetOfflineTree();
                    _formInterface.EnableOnlineTree(isEnabled: false);
                    _formInterface.EnableOfflineTree(isEnabled: true);
                    _formInterface.PassOffline();
                    _timeManager.ContinueScheduler();
                }
            }
            _formInterface.ExpandTrees();
        }
예제 #2
0
        public void InitializeEngine(FormInterface formInterface)
        {
            _formInterface   = formInterface;
            _sqLiteConnector = new SQLiteConnector(_sqLiteConnString);
            _formInterface   = formInterface;



            _timeManager = new SyncTimeManager();
            _timeManager.Init(this);



            SetConnectivityState();
            DatabaseDirectory.SetConnectors(_sqlServerConnector, _sqLiteConnector);


            _formInterface.SetCorrStatus((ConnectivityState)_connectivityStateEnum);

            //_client = new Client(_sqlServerConnector, _sqLiteConnector, _connectivityStateEnum);


            //Debug:
            _client = new Client(_sqlServerConnector, _sqLiteConnector, _connectivityStateEnum);
            _timeManager.RescheduleFromCronExpr(_client._cronExpression);

            if ((ConnectivityState)_connectivityStateEnum == ConnectivityState.Offline)
            {
                if (_client.IsSet)
                {
                    //start last set schedule by client (cronExpr from sqlite)
                    string cronExpr = _client.GetCron();

                    //_timeManager.RescheduleFromCronExpr(cronExpr);
                    _timeManager.StartScheduler(2);

                    //set interface depending on cronExpression
                    _formInterface.SetConfigInterfaceCron(cronExpr);
                }
                else
                {
                    //no sync for him
                }


                //disables interface for changing jobs

                //todo code for tree:
                SetOfflineTree();
                _formInterface.EnableOfflineTree(true);
                _formInterface.EnableOnlineTree(false);

                //throw new Exception("not yet configured");
            }
            else if ((ConnectivityState)_connectivityStateEnum == ConnectivityState.Online)
            {
                //launch job from cronExpr
                string cronExpr = _client.GetCron();
                //_timeManager.RescheduleFromCronExpr(cronExpr);
                _timeManager.StartScheduler(2);

                //Enable formInterface for change of cronEpxr


                //todo code to load tree
                int nonSyncedFiles   = _sqLiteConnector.ExecuteScalarQuery("SELECT COUNT(*) FROM Files WHERE is_synced = 0");
                int nonSyncedFolders = _sqLiteConnector.ExecuteScalarQuery("SELECT COUNT(*) FROM Folders WHERE is_synced = 0");

                if (nonSyncedFolders != 0 || nonSyncedFiles != 0)
                {
                    //formInterface --> disable sqlserver tree and enable sqlite tree
                    _formInterface.EnableOnlineTree(isEnabled: false);
                    _formInterface.EnableOfflineTree(isEnabled: true);

                    SetOnlineTree();
                    SetOfflineTree();

                    //bug prompt to sync and lock database if accepted to sync ??
                }
                else
                {
                    _formInterface.EnableOnlineTree(isEnabled: true);
                    _formInterface.EnableOfflineTree(isEnabled: false);
                    SetOnlineTree();
                }
            }
        }