/// <summary> /// Deprecated Method for adding a new object to the GoogleDrivePracticeInfoes EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToGoogleDrivePracticeInfoes(GoogleDrivePracticeInfo googleDrivePracticeInfo) { base.AddObject("GoogleDrivePracticeInfoes", googleDrivePracticeInfo); }
/// <summary> /// Create a new GoogleDrivePracticeInfo object. /// </summary> /// <param name="int">Initial value of the int property.</param> /// <param name="practiceId">Initial value of the PracticeId property.</param> /// <param name="cerebelloFolderId">Initial value of the CerebelloFolderId property.</param> public static GoogleDrivePracticeInfo CreateGoogleDrivePracticeInfo(global::System.Int32 @int, global::System.Int32 practiceId, global::System.String cerebelloFolderId) { GoogleDrivePracticeInfo googleDrivePracticeInfo = new GoogleDrivePracticeInfo(); googleDrivePracticeInfo.@int = @int; googleDrivePracticeInfo.PracticeId = practiceId; googleDrivePracticeInfo.CerebelloFolderId = cerebelloFolderId; return googleDrivePracticeInfo; }
public static void BackupEverything([NotNull] CerebelloEntities db, List<string> errors = null) { if (db == null) throw new ArgumentNullException("db"); if(errors == null) errors =new List<string>(); using (db) { var dbWrapper = new CerebelloEntitiesAccessFilterWrapper(db); var patientsToBackup = db.Patients.Where(p => p.Doctor.Users.Any(u => u.Person.GoogleUserAccoutInfoes.Any()) && !p.IsBackedUp).GroupBy(p => p.DoctorId); foreach (var patientGroup in patientsToBackup.ToList()) { try { var doctor = db.Doctors.First(d => d.Id == patientGroup.Key); dbWrapper.SetCurrentUserById(doctor.Users.First().Id); var doctorGoogleAccountInfo = doctor.Users.First().Person.GoogleUserAccoutInfoes.FirstOrDefault(); if (doctorGoogleAccountInfo != null) { // in this case the doctor for these patients have a Google Account associated var requestAccessResult = GoogleApiHelper.RequestAccessToken(doctorGoogleAccountInfo.RefreshToken); var authenticator = GoogleApiHelper.GetAuthenticator( doctorGoogleAccountInfo.RefreshToken, requestAccessResult.access_token); var driveService = new DriveService(authenticator); // create Cerebello folder if it does not exist var practiceGoogleDriveInfo = doctor.Practice.GoogleDrivePracticeInfoes.FirstOrDefault(); File cerebelloFolder = null; if (practiceGoogleDriveInfo != null) { try { cerebelloFolder = GoogleApiHelper.GetFile(driveService, practiceGoogleDriveInfo.CerebelloFolderId); if (cerebelloFolder.Labels.Trashed.HasValue && cerebelloFolder.Labels.Trashed.Value) cerebelloFolder = null; } catch (Exception ex) { var errorMessage = "Error downloading file from Google Drive. Exception message: " + ex.Message; // the f*****g user deleted the f*****g folder OR something went wrong downloading the file Trace.TraceError("BackupHelper.BackupEverything(db, errors): " + errorMessage); errors.Add(errorMessage); } } if (cerebelloFolder == null) { cerebelloFolder = GoogleApiHelper.CreateFolder(driveService, "Cerebello", "Pasta do Cerebello"); if (practiceGoogleDriveInfo != null) practiceGoogleDriveInfo.CerebelloFolderId = cerebelloFolder.Id; else { practiceGoogleDriveInfo = new GoogleDrivePracticeInfo() { CerebelloFolderId = cerebelloFolder.Id, PracticeId = doctor.PracticeId }; doctor.Practice.GoogleDrivePracticeInfoes.Add(practiceGoogleDriveInfo); } db.SaveChanges(); } foreach (var patient in patientGroup) { try { var patientBackup = GeneratePatientBackup(dbWrapper, patient); var fileName = string.Format("{0} (id:{1})", patient.Person.FullName, patient.Id) + ".zip"; var fileDescription = string.Format( "Arquivo de backup do(a) paciente {0} (id:{1})", patient.Person.FullName, patient.Id); // check if the file exists already var patientGoogleDriveFile = patient.GoogleDrivePatientInfoes.FirstOrDefault(); File googleDrivePatientFile = null; if (patientGoogleDriveFile != null) { try { // get reference to existing file to make sure it exists var existingFile = GoogleApiHelper.GetFile( driveService, patientGoogleDriveFile.PatientBackupFileId); if (!existingFile.Labels.Trashed.HasValue || !existingFile.Labels.Trashed.Value) { googleDrivePatientFile = GoogleApiHelper.UpdateFile( driveService, patientGoogleDriveFile.PatientBackupFileId, fileName, fileDescription, MimeTypesHelper.GetContentType(".zip"), patientBackup); if (googleDrivePatientFile.Labels.Trashed.HasValue && googleDrivePatientFile.Labels.Trashed.Value) googleDrivePatientFile = null; } } catch (Exception ex) { var errorMessage = "Error updating file from Google Drive. Exception message: " + ex.Message; // the f*****g user deleted the f*****g folder OR something went wrong downloading the file Trace.TraceError("BackupHelper.BackupEverything(db, errors): " + errorMessage); errors.Add(errorMessage); } } if (googleDrivePatientFile == null) { googleDrivePatientFile = GoogleApiHelper.CreateFile( driveService, fileName, fileDescription, MimeTypesHelper.GetContentType(".zip"), patientBackup, new List<ParentReference>() { new ParentReference() { Id = cerebelloFolder.Id } }); if (patientGoogleDriveFile != null) patientGoogleDriveFile.PatientBackupFileId = googleDrivePatientFile.Id; else { patient.GoogleDrivePatientInfoes.Add( new GoogleDrivePatientInfo() { PatientBackupFileId = googleDrivePatientFile.Id, PracticeId = doctor.PracticeId }); } } patient.IsBackedUp = true; db.SaveChanges(); } catch (Exception ex) { var errorMessage = "Error synchronizing files for a specific doctor. Exception message" + ex.Message; // the f*****g user deleted the f*****g folder OR something went wrong downloading the file Trace.TraceError("BackupHelper.BackupEverything(db, errors): " + errorMessage); errors.Add(errorMessage); } } } } catch (Exception ex) { var errorMessage = "Error synchronizing files. Exception message" + ex.Message; // the f*****g user deleted the f*****g folder OR something went wrong downloading the file Trace.TraceError("BackupHelper.BackupEverything(db, errors): " + errorMessage); errors.Add(errorMessage); } } } }