Example #1
0
        // Update
        public void UpdateUserDetails(User user)
        {
            // Thread safety lock
            lock (this.DashboardDataLock)
            {
                // Get live spreadsheets database reference.
                LiveSpreadsheetsDb db = this._dataRepository.GoogleService.SpreadsheetsDb;

                // Loop through all the rows in the dashboard
                // Loop through each of the rows (apart from the header row) in the LiveSheet containing saturn 5 database.
                for (int i = 1; i < this._dashboardSheet.RowCount; i++)
                {
                    // Obtain dashboard row located on the currently looped through index...
                    LiveRow dashboardRow = this._dashboardSheet[i];

                    // ... get the cell containing last seen Username for the specific saturn unit located on the dashboard row...
                    LiveCell usernameCell = dashboardRow[Saturns5DashboardRepository.Saturns5Dashboard_LastSeenUserUsername];

                    // ... get the username data from that cell...
                    string dashboardRowUsername = usernameCell.GetDataAsString();

                    // ... and compare provided user Username with dashboard row saturn 5 last seen username,
                    // if they are found being equal, update data in other user related cells.
                    if (user.Username == dashboardRowUsername)
                    {
                        // Boolean flag indicating necessity for updating the changes.
                        bool changesRequired = false;

                        LiveCell firstNameCell          = dashboardRow[Saturns5DashboardRepository.Saturns5Dashboard_LastSeenUserFirstName];
                        LiveCell surnameCell            = dashboardRow[Saturns5DashboardRepository.Saturns5Dashboard_LastSeenUserSurname];
                        LiveCell userSpreadsheetURLCell = dashboardRow[Saturns5DashboardRepository.Saturns5Dashboard_LastSeenUserSpreadsheetURL];
                        LiveCell saturn5StatusCell      = dashboardRow[Saturns5DashboardRepository.Saturns5Dashboard_Status];

                        string currentFirstName      = firstNameCell.GetDataAsString();
                        string currentSurname        = surnameCell.GetDataAsString();
                        string currentSpreadsheetURL = userSpreadsheetURLCell.GetDataAsString();

                        // Obtain status from the dashboard cell associated with the saturn 5
                        // with (according dashboard) the same last user as the one provided for update.
                        Saturn5Status currentStatus = Saturn5StatusService.GetStatusFromDashboardString(saturn5StatusCell.GetDataAsString());

                        if (currentStatus.IsWithUser())
                        {
                            // Get saturn5 status according the type of the user the saturn5 is getting allocated to.
                            Saturn5Status status = user.Type.GetWithUserSaturn5Status();

                            if (currentStatus != status)
                            {
                                changesRequired = true;

                                saturn5StatusCell.SetData(Saturn5StatusService.GetDashboardString(status));
                            }
                        }

                        if (currentFirstName != user.FirstName)
                        {
                            changesRequired = true;

                            firstNameCell.SetData(user.FirstName);
                        }

                        if (currentSurname != user.Surname)
                        {
                            changesRequired = true;

                            surnameCell.SetData(user.Surname);
                        }

                        string userSpreadsheetId = this._dataRepository.UserRepository.GetUserLogSpreadsheetId(dashboardRowUsername);
                        string newSpreadsheetURL = this.GetUrlFromSpreadsheetId(userSpreadsheetId);

                        if (currentSpreadsheetURL != newSpreadsheetURL)
                        {
                            changesRequired = true;

                            userSpreadsheetURLCell.SetData(newSpreadsheetURL);
                        }

                        if (changesRequired)
                        {
                            dashboardRow.Upload(db);
                        }
                    }
                }
            }
        }