public void Update(Saturn5 saturn5) { if (saturn5 is null) { throw new ArgumentNullException(nameof(saturn5)); } if (!this.HasSerialNumberAssociatedEntry(saturn5.SerialNumber)) { throw new ArgumentException("Saturns5DashboardRepository doesn't contain dashboard entry associated with the provided Saturn5.", nameof(saturn5)); } // Thread safety lock lock (this.DashboardDataLock) { // Get live spreadsheets database reference. LiveSpreadsheetsDb db = this._dataRepository.GoogleService.SpreadsheetsDb; IList <string> updatedDashboardEntryRowData = this.GetUpdatedDashboardEntryRowData(saturn5); int serialNumberDashboardRowIndex = this._dashboardRowIndexesBySerialNumbers[saturn5.SerialNumber]; LiveRow saturns5DashboardEntry = this._dashboardSheet[serialNumberDashboardRowIndex]; saturns5DashboardEntry.SetDataFromStringsData(0, updatedDashboardEntryRowData); saturns5DashboardEntry.Upload(db); } }
public IList <Saturns5DashboardEntry> ReadAll() { // Thread safety lock lock (this.DashboardDataLock) { IList <Saturns5DashboardEntry> dashboardEntries = new List <Saturns5DashboardEntry>(); for (int i = 1; i < this._dashboardSheet.RowCount; i++) { LiveRow saturn5DashbordEntryRow = this._dashboardSheet[i]; dashboardEntries.Add(new Saturns5DashboardEntry( // Serial number saturn5DashbordEntryRow[Saturns5DashboardRepository.Saturns5Dashboard_SerialNumber].GetDataAsString(), // ShortId saturn5DashbordEntryRow[Saturns5DashboardRepository.Saturns5Dashboard_ShortId].GetDataAsString(), // Status saturn5DashbordEntryRow[Saturns5DashboardRepository.Saturns5Dashboard_Status].GetDataAsString().ValueToSaturn5Status(), // Phone number saturn5DashbordEntryRow[Saturns5DashboardRepository.Saturns5Dashboard_PhoneNumber].GetDataAsString(), // Last seen date saturn5DashbordEntryRow[Saturns5DashboardRepository.Saturns5Dashboard_LastSeenDate].GetDataAsString(), // Last seen time saturn5DashbordEntryRow[Saturns5DashboardRepository.Saturns5Dashboard_LastSeenTime].GetDataAsString(), // Last seen username saturn5DashbordEntryRow[Saturns5DashboardRepository.Saturns5Dashboard_LastSeenUserUsername].GetDataAsString(), // Last seen first username saturn5DashbordEntryRow[Saturns5DashboardRepository.Saturns5Dashboard_LastSeenUserFirstName].GetDataAsString(), // Last seen surname saturn5DashbordEntryRow[Saturns5DashboardRepository.Saturns5Dashboard_LastSeenUserSurname].GetDataAsString())); } return(dashboardEntries); } }
// TO BE EXECUTED ONLY AFTER ALL saturn5 individual spreadsheets gonna get loaded internal async Task AssureSaturnsDataConsistencyAsync(CancellationToken?token = null) { await Task.Run(() => { // Thread safety lock lock (this.DashboardDataLock) { token.GetValueOrDefault().ThrowIfCancellationRequested(); foreach (string dbSerialNumber in this._dataRepository.Saturns5DB.RowIndexesBySerialNumbers.Keys) { if (!this.HasSerialNumberAssociatedEntry(dbSerialNumber)) { throw new InvalidOperationException($"All individual Saturn5 spreadsheets has to be fully(partially metadata and issues sheets) prior to executing this method: {nameof(AssureSaturnsDataConsistencyAsync)}"); } } IList <Saturn5> saturns5 = this._dataRepository.Saturn5Repository.ReadAll(); foreach (Saturn5 saturn5 in saturns5) { token.GetValueOrDefault().ThrowIfCancellationRequested(); // Get dashboard row index of the dashboard row associated with currently looped through saturn 5... int dashboardRowIndex = this._dbRowIndexesBySerialNumbers[saturn5.SerialNumber] + 1; // ... and get the dashboard row containing associate dashboard entry. LiveRow dashboardRow = this._dashboardSheet[dashboardRowIndex]; string saturn5SpreadsheetId = this._dataRepository.Saturn5Repository.GetSaturn5LogSpreadsheetId(saturn5.SerialNumber); string saturn5SpreadsheetURL = this.GetUrlFromSpreadsheetId(saturn5SpreadsheetId); // if any of the properties located on the dashboard doesn't match these ones obtained from the Saturn5Repository... // ShortId if (saturn5.ShortId != dashboardRow[Saturns5DashboardRepository.Saturns5Dashboard_ShortId].GetDataAsString() // Saturn5 Spreadsheet URL || saturn5SpreadsheetURL != dashboardRow[Saturns5DashboardRepository.Saturns5Dashboard_SaturnSpreadsheetURL].GetDataAsString() // Saturn5Status || Saturn5StatusService.GetDashboardString(saturn5.Status) != dashboardRow[Saturns5DashboardRepository.Saturns5Dashboard_Status].GetDataAsString() // PhoneNumber || saturn5.PhoneNumber != dashboardRow[Saturns5DashboardRepository.Saturns5Dashboard_PhoneNumber].GetDataAsString() // LastSeenDate || saturn5.LastSeenDate != dashboardRow[Saturns5DashboardRepository.Saturns5Dashboard_LastSeenDate].GetDataAsString() // LastSeenTime || saturn5.LastSeenTime != dashboardRow[Saturns5DashboardRepository.Saturns5Dashboard_LastSeenTime].GetDataAsString() // LastSeenUsername || saturn5.LastSeenUsername != dashboardRow[Saturns5DashboardRepository.Saturns5Dashboard_LastSeenUserUsername].GetDataAsString()) { // Update dashboard sheet accordingly this.Update(saturn5); } } } }); await AssureUsersDataConsistencyAsync(token); }
public Saturns5DashboardEntry Read(string serialNumber) { if (serialNumber is null) { throw new ArgumentNullException(nameof(serialNumber)); } if (!this.HasSerialNumberAssociatedEntry(serialNumber)) { throw new ArgumentException("Saturns5DashboardRepository doesn't contain dashboard entry associated with the provided serial number.", nameof(serialNumber)); } // Thread safety lock lock (this.DashboardDataLock) { int serialNumberAssociatedRowIndex = this._dbRowIndexesBySerialNumbers[serialNumber] - 1; LiveRow saturn5DashbordEntryRow = this._dashboardSheet[serialNumberAssociatedRowIndex]; return(new Saturns5DashboardEntry( // Serial number serialNumber, // ShortId saturn5DashbordEntryRow[Saturns5DashboardRepository.Saturns5Dashboard_ShortId].GetDataAsString(), // Status saturn5DashbordEntryRow[Saturns5DashboardRepository.Saturns5Dashboard_Status].GetDataAsString().ValueToSaturn5Status(), // Phone number saturn5DashbordEntryRow[Saturns5DashboardRepository.Saturns5Dashboard_PhoneNumber].GetDataAsString(), // Last seen date saturn5DashbordEntryRow[Saturns5DashboardRepository.Saturns5Dashboard_LastSeenDate].GetDataAsString(), // Last seen time saturn5DashbordEntryRow[Saturns5DashboardRepository.Saturns5Dashboard_LastSeenTime].GetDataAsString(), // Last seen username saturn5DashbordEntryRow[Saturns5DashboardRepository.Saturns5Dashboard_LastSeenUserUsername].GetDataAsString(), // Last seen first username saturn5DashbordEntryRow[Saturns5DashboardRepository.Saturns5Dashboard_LastSeenUserFirstName].GetDataAsString(), // Last seen surname saturn5DashbordEntryRow[Saturns5DashboardRepository.Saturns5Dashboard_LastSeenUserSurname].GetDataAsString())); } }
// Clears and builds from scratch dictionary of serialNumber-rowIndex association. private void ReBuildDashboardSerialNumberIndex() { // Clear existing relativity table. this._dashboardRowIndexesBySerialNumbers.Clear(); // 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++) { // Get saturn 5 dashboard entry row appropriate for the currently looped row through index. LiveRow saturns5DashboardEntryRow = this._dashboardSheet[i]; // Obtain saturn 5 serial number. string serialNumber = saturns5DashboardEntryRow[Saturns5DashboardRepository.Saturns5Dashboard_SerialNumber].GetDataAsString(); // Serial number might be null if method is called during dashboard recreation process if (serialNumber is null) { break; } // Add serial number association with currently lopped through row index. this._dashboardRowIndexesBySerialNumbers.Add(serialNumber, i); } }
// 3) private void AssureRowIndexLocationSaturns5DashboardEntries(LiveSpreadsheetsDb db) { // ASSURE ROW INDEXES INTEGRITY BETWEEN ALL ENTRIES EXISTING ENTRIES IN THE Saturns5DashboardsRepository and Saturn5Repository bool modified = false; do { modified = false; foreach (KeyValuePair <string, int> rowIndexBySerialNumber in this._dataRepository.Saturns5DB.RowIndexesBySerialNumbers.ToList()) { // Get currently looped through saturns 5 database serial number string dbSerialNumber = rowIndexBySerialNumber.Key; // Get currently saturns 5 database row index. int dbRowIndex = rowIndexBySerialNumber.Value; // Get row dashboard row currently located on the dbRow saturn 5 serial number associated row LiveRow dashboardRow = this._dashboardSheet.SheetRows.ElementAtOrDefault(dbRowIndex + 1); // Get saturn 5 serial number currently stored in the dashboard row entry located at the same rowIndex // as the currently looped through saturn 5 DB row. string dashboardRowSerialNumber = dashboardRow[Saturns5DashboardRepository.Saturns5Dashboard_SerialNumber].GetDataAsString(); // if dbRow index is in the scope of this._dashboardSheet // and dashbordRow contains serial number matching serial number // located on the row with matching index in the saturn 5 database if (dbSerialNumber == dashboardRowSerialNumber) { // (TO BRAKE OUT OF DO WHILE LOOP - each of looped through entries in // the foreach loop should terminate here). continue; } #region WARRNING - int dashboardRowIndex variable // WARRNING - DO NOT REPLACE with indexer from this._dashboardRowIndexesBySerialNumbers[] // at this point this quick-access serialNumber-dashboardRowIndex dictionary is in INVALID state... // (as content of this._dashboardSheet has been potentially already partially modified, // but quick-access serialNumber -dashboardRowIndex dictionary index hasn't been modified accordingly yet // - and it cannot be modified before all of the content of this._dashboardSheet will be corrected), // ...and dashboardRowIndex has to be found using LINQ instead, to obtain valid data. #endregion // Get row index of the entry in this_dashboardSheet having equal serial number then the one in // the currently looped through Saturns5DB row. int dashboardRowIndex = this._dashboardSheet.SheetRows.First((dashboardSheetRow) => { return(dashboardSheetRow[Saturns5DashboardRepository.Saturns5Dashboard_SerialNumber].GetDataAsString() == dbSerialNumber); } ).RowIndex; // Else if serial number located on the currently looped through saturns5 database row, // can be found one of this._dashbordSheet rows, but it is located ABOVE its intended location. if (dashboardRowIndex != dbRowIndex) { // Get row data from its current position IList <string> newDashboardEntryRowData = this._dashboardSheet[dashboardRowIndex].GetDataAsStrings(); // Remove it from its current position this._dashboardSheet.RemoveRows(db, dashboardRowIndex, Saturns5DashboardRepository.Saturns5Dashboard_EntryRowsCount); // Insert it into its designated position this._dashboardSheet.InsertRows(db, dbRowIndex + 1, Saturns5DashboardRepository.Saturns5Dashboard_EntryRowsCount, new IList <string>[Saturns5DashboardRepository.Saturns5Dashboard_EntryRowsCount] { newDashboardEntryRowData }); // Set flag to indicate that content of this._dashboardSheet has been modified modified = true; } else { throw new InvalidOperationException($"THIS SHOULD NOT HAPPENED - CLEARLY INVALID LOGIC IMPLEMENTATION - {nameof(Saturns5DashboardRepository)}"); } } } while (modified); }
// 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); } } } } }