Пример #1
0
        public Client(SQLServerConnector sqlServerConnector, SQLiteConnector sqLiteConnector, Enum connectivityState)
        {
            if ((ConnectivityState)connectivityState == ConnectivityState.Offline)
            {
                _isOnline = false;
            }
            else
            {
                _isOnline = true;
            }

            //bool? firstTimeClient = sqLiteConnector.IsNotEmpty(tableName: "Client");

            bool firstTimeClient = false;

            if (_isOnline)
            {
                if (firstTimeClient == true)
                {
                    _clientID   = sqlServerConnector.GetNewClientId();
                    _clientName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

                    //BUG _clientPublicIP = "publicIP"; _clientPrivateIP = "privateIP";

                    //write to sqlite all data singleton
                    _cronExpression = sqlServerConnector.GetValidCronExpr();

                    sqlServerConnector.ExecuteInsertQuery("INSERT INTO ....");
                    sqLiteConnector.ExecuteInsertQuery("INSERT INTO ....");
                }
                else if (firstTimeClient == false)
                {
                    //laaater : update IPs if they're different write them to sqlite and server?

                    SetClientFromSqlite(sqLiteConnector);
                }

                IsSet = true;
            }
            else
            {
                if (firstTimeClient == true)
                {
                    IsSet     = false;
                    _clientID = -1;

                    //BUG _clientPrivateIP = _clientPublicIP = "Not Connected";


                    //...
                    //
                }
                else
                {
                    //instantiante from sqlite
                    SetClientFromSqlite(sqLiteConnector);
                    IsSet = true;
                }
            }
        }
Пример #2
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();
        }