public override bool Update(SyncJob job) { if (this.SyncJobExists(job.Name, job.ID)) { throw new SyncJobNameExistException(String.Format(m_ResourceManager.GetString("err_syncjobCreated"), job.Name)); } SQLiteSyncSourceProvider provider = (SQLiteSyncSourceProvider)SyncClient.GetSyncSourceProvider(job.IntermediaryStorage.Path); if (provider.GetSyncSourceCount() > 2) { throw new SyncSourcesNumberExceededException(m_ResourceManager.GetString("err_onlyTwoSyncSourceFolders")); } // Update a profile requires update 2 tables at the same time, // If one update on a table fails, the total update action must fail too. string updateProfileText = "UPDATE " + SYNCJOB_TABLE + " SET " + COL_METADATA_SOURCE_LOCATION + " = @mdSource, " + COL_SYNCJOB_NAME + " = @name WHERE " + COL_SYNCJOB_ID + " = @id;"; SqliteParameterCollection paramList = new SqliteParameterCollection(); // Add parameters for 1st Update statement paramList.Add(new SqliteParameter("@mdSource", System.Data.DbType.String) { Value = job.IntermediaryStorage.Path }); paramList.Add(new SqliteParameter("@name", System.Data.DbType.String) { Value = job.Name }); paramList.Add(new SqliteParameter("@id", System.Data.DbType.String) { Value = job.ID }); SQLiteAccess db = new SQLiteAccess(Path.Combine(this.StoragePath, DATABASE_NAME), false); using (SqliteConnection con = db.NewSQLiteConnection()) { if (con == null) { throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(this.StoragePath, DATABASE_NAME))); } SqliteTransaction transaction = (SqliteTransaction)con.BeginTransaction(); try { SQLiteSyncSourceProvider.Update(job.SyncSource, con); db.ExecuteNonQuery(updateProfileText, paramList); transaction.Commit(); return(true); } catch (Exception) { transaction.Rollback(); throw; } } }
public override bool Delete(SyncJob job) { SQLiteAccess db = new SQLiteAccess(Path.Combine(this.StoragePath, DATABASE_NAME), false); using (SqliteConnection con = db.NewSQLiteConnection()) { if (con == null) { throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(this.StoragePath, DATABASE_NAME))); } SqliteParameterCollection paramList = new SqliteParameterCollection(); string cmdText = "DELETE FROM " + SyncSource.DATASOURCE_INFO_TABLE + " WHERE " + SyncSource.SOURCE_ID + " = @sid;"; paramList.Add(new SqliteParameter("@sid", System.Data.DbType.String) { Value = job.SyncSource.ID }); cmdText += "DELETE FROM " + SYNCJOB_TABLE + " WHERE " + COL_SYNCJOB_ID + " = @pid"; paramList.Add(new SqliteParameter("@pid", System.Data.DbType.String) { Value = job.ID }); db.ExecuteNonQuery(cmdText, paramList); } return(true); }
// Constructor public FileSyncAgent(SyncJob job) { this._job = job; // Instantiates providers actProvider = SyncClient.GetSyncActionsProvider(this._job.IntermediaryStorage.Path); mdProvider = SyncClient.GetMetaDataProvider(_job.IntermediaryStorage.Path, _job.SyncSource.ID); }
/// <summary> /// Copy a file from sync folder and update action table /// </summary> /// <param name="action"></param> /// <param name="profile"></param> public static void CopyToSyncFolderAndUpdateActionTable(SyncAction action, SyncJob job) { if (!Directory.Exists(job.SyncSource.Path)) { throw new SyncSourceException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), job.SyncSource.Path)); } if (!Directory.Exists(job.IntermediaryStorage.Path)) { throw new SyncSourceException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), job.IntermediaryStorage.Path)); } // TODO: atomic.... string absolutePathInIntermediateStorage = job.IntermediaryStorage.DirtyFolderPath + action.RelativeFilePath; string absolutePathInSyncSource = job.SyncSource.Path + action.RelativeFilePath; SQLiteAccess dbAccess = new SQLiteAccess(Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME), true); SqliteConnection con = dbAccess.NewSQLiteConnection(); if (con == null) { throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME))); } SqliteTransaction trasaction = (SqliteTransaction)con.BeginTransaction(); try { SQLiteSyncActionsProvider actProvider = (SQLiteSyncActionsProvider)SyncClient.GetSyncActionsProvider(job.IntermediaryStorage.Path); actProvider.Delete(action, con); if (!Files.FileUtils.Copy(absolutePathInIntermediateStorage, absolutePathInSyncSource, true)) { throw new Exception(String.Format(m_ResourceManager.GetString("err_cannotCopyFile"), absolutePathInIntermediateStorage)); } trasaction.Commit(); Files.FileUtils.DeleteFileAndFolderIfEmpty(job.IntermediaryStorage.DirtyFolderPath, absolutePathInIntermediateStorage, true); } catch (OutOfDiskSpaceException) { trasaction.Rollback(); throw; } catch (Exception) { trasaction.Rollback(); throw; } finally { if (con != null) { con.Dispose(); } } }
public static void CopyToDirtyFolderAndUpdateActionTable(SyncAction action, SyncJob job) { if (!Directory.Exists(job.SyncSource.Path)) throw new SyncSourceException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), job.SyncSource.Path)); if ( !Directory.Exists(job.IntermediaryStorage.Path)) throw new SyncSourceException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), job.IntermediaryStorage.Path)); if (!Directory.Exists(job.IntermediaryStorage.DirtyFolderPath)) Directory.CreateDirectory(job.IntermediaryStorage.DirtyFolderPath); // TODO: make it atomic?? string absolutePathInSyncSource = job.SyncSource.Path + action.RelativeFilePath; string absolutePathInImediateStorage = job.IntermediaryStorage.DirtyFolderPath + action.RelativeFilePath; var db = new SQLiteAccess(Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME),true); var con = db.NewSQLiteConnection(); if (con == null) throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME))); var transaction = (SqliteTransaction)con.BeginTransaction(); try { var actProvider = (SQLiteSyncActionsProvider)SyncClient.GetSyncActionsProvider(job.IntermediaryStorage.Path); actProvider.Add(action, con); if (!FileUtils.Copy(absolutePathInSyncSource, absolutePathInImediateStorage, true)) throw new Exception(String.Format(m_ResourceManager.GetString("err_cannotCopyFile"), absolutePathInSyncSource)); transaction.Commit(); } catch (OutOfDiskSpaceException) { transaction.Rollback(); throw; } catch (Exception) { transaction.Rollback(); throw; } finally { if (con != null) con.Dispose(); } }
public override IList <SyncJob> LoadAllJobs() { IList <SyncJob> jobs = new List <SyncJob>(); // Note: The SQL depends on other tables as well which might not be created. // So empty SyncJob list should be returned if there is an exception try { SQLiteAccess db = new SQLiteAccess(Path.Combine(this.StoragePath, Configuration.DATABASE_NAME), false); using (SqliteConnection con = db.NewSQLiteConnection()) { if (con == null) { throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(this.StoragePath, Configuration.DATABASE_NAME))); } using (SqliteCommand cmd = con.CreateCommand()) { cmd.CommandText = "SELECT * FROM " + SYNCJOB_TABLE + " p, " + DATASOURCE_INFO_TABLE + " d WHERE p" + "." + COL_SYNC_SOURCE_ID + " = d" + "." + COL_SOURCE_ID; using (SqliteDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { SyncSource source = new SyncSource((string)reader[COL_SYNC_SOURCE_ID], (string)reader[COL_SOURCE_ABS_PATH]); IntermediaryStorage mdSource = new IntermediaryStorage((string)reader[COL_METADATA_SOURCE_LOCATION]); SyncJob p = new SyncJob((string)reader[COL_SYNCJOB_ID], (string)reader[COL_SYNCJOB_NAME], source, mdSource); jobs.Add(p); } } } } } catch (Exception ex) { Console.WriteLine(ex.Message); // Log error? } return(jobs); }
/// <summary> /// Renames conflicted file so that RenameAction can be executed /// </summary> /// <param name="renameAction">Rename action that cannot be executed due to conflict.</param> public static void ConflictRenameAndUpdateActionTable(RenameAction action, SyncJob job, bool keepConflictedFile) { if (!Directory.Exists(job.SyncSource.Path)) throw new SyncSourceException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), job.SyncSource.Path)); if (!Directory.Exists(job.IntermediaryStorage.Path)) throw new SyncSourceException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), job.IntermediaryStorage.Path)); string absPathInIStorage = job.IntermediaryStorage.DirtyFolderPath + action.RelativeFilePath; string absPathInSyncSource = job.SyncSource.Path + action.RelativeFilePath; string absOldPathInSyncSource = job.SyncSource.Path + action.PreviousRelativeFilePath; SQLiteAccess access = new SQLiteAccess(Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME), true); SqliteConnection con = access.NewSQLiteConnection(); if (con == null) throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME))); SqliteTransaction trasaction = (SqliteTransaction)con.BeginTransaction(); try { SQLiteSyncActionsProvider actProvider = (SQLiteSyncActionsProvider)SyncClient.GetSyncActionsProvider(job.IntermediaryStorage.Path); actProvider.Delete(action, con); if (!keepConflictedFile) Files.FileUtils.Delete(absPathInSyncSource, true); else Files.FileUtils.DuplicateRename(absPathInSyncSource, absPathInSyncSource); if (!Files.FileUtils.Move(absOldPathInSyncSource, absPathInSyncSource, true)) throw new Exception(String.Format(m_ResourceManager.GetString("err_cannotRenameFile"), absPathInIStorage)); trasaction.Commit(); Files.FileUtils.DeleteFileAndFolderIfEmpty(job.IntermediaryStorage.DirtyFolderPath, absPathInIStorage, true); } catch (Exception) { trasaction.Rollback(); throw; } finally { if (con != null) con.Dispose(); } }
/// <summary> /// /// </summary> /// <param name="jobName"></param> /// <param name="absoluteSyncPath"></param> /// <param name="absoluteIntermediatePath"></param> /// <returns></returns> /// <exception cref="SyncJobNameExistException">if syncjob name already exists.</exception> public override SyncJob CreateSyncJob(string jobName, string absoluteSyncPath, string absoluteIntermediatePath) { SyncSource syncSource = new SyncSource(System.Guid.NewGuid().ToString(), absoluteSyncPath); IntermediaryStorage iStorage = new IntermediaryStorage(absoluteIntermediatePath); SyncJob job = new SyncJob(System.Guid.NewGuid().ToString(), jobName, syncSource, iStorage); CreateDataStore(this.StoragePath, syncSource, iStorage); // Returns job if it is successfully added. if (Add(job)) { return(job); } else { return(null); } }
/// <summary> /// Instantiate a sync preview window job to be previewed. /// </summary> /// <param name="syncActions">All sync actions to be dispayed for preview.</param> public WinSyncPreview(SyncJob job) { this.InitializeComponent(); this._job = job; // Initialize previewWorker BackgroundWorker previewWorker = new BackgroundWorker(); previewWorker.DoWork += new DoWorkEventHandler(previewWorker_DoWork); previewWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(previewWorker_RunWorkerCompleted); ; if (tbManager != null) tbManager.SetProgressState(TaskbarProgressBarState.Indeterminate); // Check job parameters if (jobValid(_job)) { previewUIUpdate(false); previewWorker.RunWorkerAsync(); } language(); }
public override SyncJob Load(string jobName) { SyncJob p = null; SQLiteAccess db = new SQLiteAccess(Path.Combine(this.StoragePath, DATABASE_NAME), false); using (SqliteConnection con = db.NewSQLiteConnection()) { if (con == null) { throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(this.StoragePath, Configuration.DATABASE_NAME))); } string cmdText = "SELECT * FROM " + SYNCJOB_TABLE + " p, " + DATASOURCE_INFO_TABLE + " d WHERE p" + "." + COL_SYNC_SOURCE_ID + " = d" + "." + COL_SOURCE_ID + " AND " + COL_SYNCJOB_NAME + " = @pname"; SqliteParameterCollection paramList = new SqliteParameterCollection(); paramList.Add(new SqliteParameter("@pname", System.Data.DbType.String) { Value = jobName }); db.ExecuteReader(cmdText, paramList, reader => { // TODO: constructor of Profile takes in more arguments to remove dependency on IntermediaryStorage and SyncSource class. SyncSource source = new SyncSource((string)reader[COL_SYNC_SOURCE_ID], (string)reader[COL_SOURCE_ABS_PATH]); IntermediaryStorage mdSource = new IntermediaryStorage((string)reader[COL_METADATA_SOURCE_LOCATION]); p = new SyncJob((string)reader[COL_SYNCJOB_ID], (string)reader[COL_SYNCJOB_NAME], source, mdSource); return; } ); } return(p); }
public bool Add(SyncJob job, SqliteConnection con) { if (this.SyncJobExists(job.Name, job.ID)) { throw new SyncJobNameExistException(String.Format(m_ResourceManager.GetString("err_syncjobCreated"), job.Name)); } using (SqliteCommand cmd = con.CreateCommand()) { cmd.CommandText = "INSERT INTO " + SYNCJOB_TABLE + " (" + COL_SYNCJOB_ID + ", " + COL_SYNCJOB_NAME + " ," + COL_METADATA_SOURCE_LOCATION + ", " + COL_SYNC_SOURCE_ID + ") VALUES (@id, @name, @meta, @source)"; cmd.Parameters.Add(new SqliteParameter("@id", System.Data.DbType.String) { Value = job.ID }); cmd.Parameters.Add(new SqliteParameter("@name", System.Data.DbType.String) { Value = job.Name }); cmd.Parameters.Add(new SqliteParameter("@meta", System.Data.DbType.String) { Value = job.IntermediaryStorage.Path }); cmd.Parameters.Add(new SqliteParameter("@source", System.Data.DbType.String) { Value = job.SyncSource.ID }); cmd.ExecuteNonQuery(); } SQLiteSyncSourceProvider.Add(job.SyncSource, con); return(true); }
/// <summary> /// /// </summary> /// <param name="jobName"></param> /// <param name="absoluteSyncPath"></param> /// <param name="absoluteIntermediatePath"></param> /// <returns></returns> /// <exception cref="SyncJobNameExistException">if syncjob name already exists.</exception> public override SyncJob CreateSyncJob(string jobName, string absoluteSyncPath, string absoluteIntermediatePath) { SyncSource syncSource = new SyncSource(System.Guid.NewGuid().ToString(), absoluteSyncPath); IntermediaryStorage iStorage = new IntermediaryStorage(absoluteIntermediatePath); SyncJob job = new SyncJob(System.Guid.NewGuid().ToString(), jobName, syncSource, iStorage); CreateDataStore(this.StoragePath, syncSource, iStorage); // Returns job if it is successfully added. if (Add(job)) return job; else return null; }
public bool Add(SyncJob job, SqliteConnection con) { if (this.SyncJobExists(job.Name, job.ID)) throw new SyncJobNameExistException(String.Format(m_ResourceManager.GetString("err_syncjobCreated"), job.Name)); using (SqliteCommand cmd = con.CreateCommand ()) { cmd.CommandText = "INSERT INTO " + SYNCJOB_TABLE + " (" + COL_SYNCJOB_ID + ", " + COL_SYNCJOB_NAME + " ," + COL_METADATA_SOURCE_LOCATION + ", " + COL_SYNC_SOURCE_ID + ") VALUES (@id, @name, @meta, @source)"; cmd.Parameters.Add ( new SqliteParameter("@id", System.Data.DbType.String) { Value = job.ID }); cmd.Parameters.Add(new SqliteParameter("@name", System.Data.DbType.String) { Value = job.Name }); cmd.Parameters.Add(new SqliteParameter("@meta", System.Data.DbType.String) { Value = job.IntermediaryStorage.Path }); cmd.Parameters.Add(new SqliteParameter("@source", System.Data.DbType.String) { Value = job.SyncSource.ID }); cmd.ExecuteNonQuery (); } SQLiteSyncSourceProvider.Add(job.SyncSource, con); return true; }
public static void DeleteFromActionTable(SyncAction action, SyncJob job) { SyncActionsProvider actProvider = SyncClient.GetSyncActionsProvider(job.IntermediaryStorage.Path); actProvider.Delete(action); }
private bool jobValid(SyncJob job) { if (!Directory.Exists(job.IntermediaryStorage.Path)) { showErrorMsg(String.Format(m_ResourceManager.GetString("err_IntermediateStorageNoutFound"), job.IntermediaryStorage.Path)); return false; } return true; }
/// <summary> /// Delete a file in sync source folder and update action table /// </summary> /// <param name="action"></param> /// <param name="profile"></param> public static void DeleteInSyncFolderAndUpdateActionTable(SyncAction action, SyncJob job) { string absolutePathInSyncSource = job.SyncSource.Path + action.RelativeFilePath; SQLiteAccess access = new SQLiteAccess(Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME),true); SqliteConnection con = access.NewSQLiteConnection(); if (con == null) throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME))); SqliteTransaction transaction = (SqliteTransaction)con.BeginTransaction(); try { SQLiteSyncActionsProvider actProvider = (SQLiteSyncActionsProvider)SyncClient.GetSyncActionsProvider(job.IntermediaryStorage.Path); actProvider.Delete(action); if (!Files.FileUtils.Delete(absolutePathInSyncSource, true)) throw new Exception(String.Format(m_ResourceManager.GetString("err_cannotDeleteFile"), absolutePathInSyncSource)); transaction.Commit(); /* Files.FileUtils.DeleteFileAndFolderIfEmpty(job.IntermediaryStorage.DirtyFolderPath, job.IntermediaryStorage.DirtyFolderPath + action.RelativeFilePath , true); */ } catch (Exception) { transaction.Rollback(); throw; } finally { if (con != null) con.Dispose(); } }
private void UpdateSyncInfoUI(SyncJob p) { // Update Sync UI info lblSyncJobName.Content = p.Name; lblSyncJobName.ToolTip = p.Name; }
public override IList<SyncJob> LoadAllJobs() { IList<SyncJob> jobs = new List<SyncJob>(); // Note: The SQL depends on other tables as well which might not be created. // So empty SyncJob list should be returned if there is an exception try { SQLiteAccess db = new SQLiteAccess(Path.Combine (this.StoragePath, Configuration.DATABASE_NAME ),false); using (SqliteConnection con = db.NewSQLiteConnection ()) { if (con == null) throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(this.StoragePath, Configuration.DATABASE_NAME))); using (SqliteCommand cmd = con.CreateCommand()) { cmd.CommandText = "SELECT * FROM " + SYNCJOB_TABLE + " p, " + DATASOURCE_INFO_TABLE + " d WHERE p" + "." + COL_SYNC_SOURCE_ID + " = d" + "." + COL_SOURCE_ID; using (SqliteDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { SyncSource source = new SyncSource((string)reader[COL_SYNC_SOURCE_ID], (string)reader[COL_SOURCE_ABS_PATH]); IntermediaryStorage mdSource = new IntermediaryStorage((string)reader[COL_METADATA_SOURCE_LOCATION]); SyncJob p = new SyncJob((string)reader[COL_SYNCJOB_ID], (string)reader[COL_SYNCJOB_NAME], source, mdSource); jobs.Add(p); } } } } } catch (Exception ex) { Console.WriteLine(ex.Message); // Log error? } return jobs; }
public override bool Add(SyncJob job) { if (this.SyncJobExists(job.Name, job.ID)) { throw new SyncJobNameExistException(String.Format(m_ResourceManager.GetString("err_syncjobCreated"), job.Name)); } SQLiteAccess dbAccess1 = new SQLiteAccess(Path.Combine(this.StoragePath, Configuration.DATABASE_NAME), false); SQLiteAccess dbAccess2 = new SQLiteAccess(Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME), false); SqliteConnection con1 = dbAccess1.NewSQLiteConnection(); SqliteConnection con2 = dbAccess2.NewSQLiteConnection(); if (con1 == null) { throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(this.StoragePath, DATABASE_NAME))); } if (con2 == null) { throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME))); } SqliteTransaction transaction1 = (SqliteTransaction)con1.BeginTransaction(); SqliteTransaction transaction2 = (SqliteTransaction)con2.BeginTransaction(); try { string insertJobText = "INSERT INTO " + SYNCJOB_TABLE + " (" + COL_SYNCJOB_ID + ", " + COL_SYNCJOB_NAME + " ," + COL_METADATA_SOURCE_LOCATION + ", " + COL_SYNC_SOURCE_ID + ") VALUES (@id, @name, @meta, @source)"; SqliteParameterCollection paramList = new SqliteParameterCollection(); paramList.Add(new SqliteParameter("@id", System.Data.DbType.String) { Value = job.ID }); paramList.Add(new SqliteParameter("@name", System.Data.DbType.String) { Value = job.Name }); paramList.Add(new SqliteParameter("@meta", System.Data.DbType.String) { Value = job.IntermediaryStorage.Path }); paramList.Add(new SqliteParameter("@source", System.Data.DbType.String) { Value = job.SyncSource.ID }); dbAccess1.ExecuteNonQuery(insertJobText, paramList); SQLiteSyncSourceProvider.Add(job.SyncSource, con1); SQLiteSyncSourceProvider provider = (SQLiteSyncSourceProvider)SyncClient.GetSyncSourceProvider(job.IntermediaryStorage.Path); if (provider.GetSyncSourceCount() == 2) { throw new SyncSourcesNumberExceededException(m_ResourceManager.GetString("err_onlyTwoSyncSourceFolders")); } SQLiteSyncSourceProvider.Add(job.SyncSource, con2); transaction1.Commit(); transaction2.Commit(); return(true); } catch (Exception) { transaction1.Rollback(); transaction2.Rollback(); throw; } finally { if (con1 != null) { con1.Dispose(); } if (con2 != null) { con2.Dispose(); } } }
/// <summary> /// Delete a SyncJob requires delete data from 2 tables SYNCSOURCE_INFO and SYNCJOB /// If deletion action on one table fails, the total action must fail too. /// </summary> /// <param name="job"></param> /// <returns>true if successful.</returns> public abstract bool Delete(SyncJob job);
/// <summary> /// Add new SyncJob /// </summary> /// <param name="job">SyncJob to be added</param> /// <returns>true if SyncJob is added successfully.</returns> /// <exception cref="SyncJobNameExistException">SyncJob with same name already exists.</exception> public abstract bool Add(SyncJob job);
public override bool Delete(SyncJob job) { SQLiteAccess db = new SQLiteAccess(Path.Combine(this.StoragePath, DATABASE_NAME),false); using (SqliteConnection con = db.NewSQLiteConnection ()) { if (con == null) throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(this.StoragePath, DATABASE_NAME))); SqliteParameterCollection paramList = new SqliteParameterCollection(); string cmdText = "DELETE FROM " + SyncSource.DATASOURCE_INFO_TABLE + " WHERE " + SyncSource.SOURCE_ID + " = @sid;"; paramList.Add(new SqliteParameter("@sid", System.Data.DbType.String) { Value = job.SyncSource.ID }); cmdText += "DELETE FROM " + SYNCJOB_TABLE + " WHERE " + COL_SYNCJOB_ID + " = @pid"; paramList.Add(new SqliteParameter("@pid", System.Data.DbType.String) { Value = job.ID }); db.ExecuteNonQuery(cmdText, paramList); } return true; }
public override SyncJob Load(string jobName) { SyncJob p = null; SQLiteAccess db = new SQLiteAccess(Path.Combine(this.StoragePath, DATABASE_NAME),false); using (SqliteConnection con = db.NewSQLiteConnection()) { if (con == null) throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(this.StoragePath, Configuration.DATABASE_NAME))); string cmdText = "SELECT * FROM " + SYNCJOB_TABLE + " p, " + DATASOURCE_INFO_TABLE + " d WHERE p" + "." + COL_SYNC_SOURCE_ID + " = d" + "." + COL_SOURCE_ID + " AND " + COL_SYNCJOB_NAME + " = @pname"; SqliteParameterCollection paramList = new SqliteParameterCollection(); paramList.Add(new SqliteParameter("@pname", System.Data.DbType.String) { Value = jobName }); db.ExecuteReader(cmdText, paramList, reader => { // TODO: constructor of Profile takes in more arguments to remove dependency on IntermediaryStorage and SyncSource class. SyncSource source = new SyncSource((string)reader[COL_SYNC_SOURCE_ID], (string)reader[COL_SOURCE_ABS_PATH]); IntermediaryStorage mdSource = new IntermediaryStorage((string)reader[COL_METADATA_SOURCE_LOCATION]); p = new SyncJob((string)reader[COL_SYNCJOB_ID], (string)reader[COL_SYNCJOB_NAME], source, mdSource); return; } ); } return p; }
public static void UpdateTableAction(SyncAction action, SyncJob job) { var actProvider = (SQLiteSyncActionsProvider)SyncClient.GetSyncActionsProvider(job.IntermediaryStorage.Path); actProvider.Add(action); }
public override bool Update(SyncJob job) { if (this.SyncJobExists(job.Name, job.ID)) throw new SyncJobNameExistException(String.Format(m_ResourceManager.GetString("err_syncjobCreated"), job.Name)); SQLiteSyncSourceProvider provider = (SQLiteSyncSourceProvider)SyncClient.GetSyncSourceProvider(job.IntermediaryStorage.Path); if (provider.GetSyncSourceCount() > 2) throw new SyncSourcesNumberExceededException(m_ResourceManager.GetString("err_onlyTwoSyncSourceFolders")); // Update a profile requires update 2 tables at the same time, // If one update on a table fails, the total update action must fail too. string updateProfileText = "UPDATE " + SYNCJOB_TABLE + " SET " + COL_METADATA_SOURCE_LOCATION + " = @mdSource, " + COL_SYNCJOB_NAME + " = @name WHERE " + COL_SYNCJOB_ID + " = @id;"; SqliteParameterCollection paramList = new SqliteParameterCollection(); // Add parameters for 1st Update statement paramList.Add(new SqliteParameter("@mdSource", System.Data.DbType.String) { Value = job.IntermediaryStorage.Path }); paramList.Add(new SqliteParameter("@name", System.Data.DbType.String) { Value = job.Name }); paramList.Add(new SqliteParameter("@id", System.Data.DbType.String) { Value = job.ID }); SQLiteAccess db = new SQLiteAccess(Path.Combine(this.StoragePath, DATABASE_NAME),false); using (SqliteConnection con = db.NewSQLiteConnection ()) { if (con == null) throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(this.StoragePath, DATABASE_NAME))); SqliteTransaction transaction = (SqliteTransaction)con.BeginTransaction(); try { SQLiteSyncSourceProvider.Update(job.SyncSource,con ); db.ExecuteNonQuery(updateProfileText, paramList); transaction.Commit(); return true; } catch (Exception) { transaction.Rollback(); throw; } } }
/// <summary> /// Rename a file in sync source folder and update action table /// </summary> /// <param name="action"></param> /// <param name="profile"></param> /// <exception cref="System.ComponentModel.Win32Exception"></exception> public static void RenameInSyncFolderAndUpdateActionTable(RenameAction action, SyncJob job) { if (!Directory.Exists(job.SyncSource.Path)) { throw new SyncSourceException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), job.SyncSource.Path)); } if (!Directory.Exists(job.IntermediaryStorage.Path)) { throw new SyncSourceException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), job.IntermediaryStorage.Path)); } string oldAbsolutePathInSyncSource = job.SyncSource.Path + action.PreviousRelativeFilePath; string newAbsolutePathInSyncSource = job.SyncSource.Path + action.RelativeFilePath; SQLiteAccess access = new SQLiteAccess(Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME), true); SqliteConnection con = access.NewSQLiteConnection(); if (con == null) { throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME))); } SqliteTransaction transaction = (SqliteTransaction)con.BeginTransaction(); try { SyncActionsProvider actProvider = SyncClient.GetSyncActionsProvider(job.IntermediaryStorage.Path); actProvider.Delete(action); if (File.Exists(oldAbsolutePathInSyncSource) && !Files.FileUtils.Move(oldAbsolutePathInSyncSource, newAbsolutePathInSyncSource, true)) { throw new Exception(String.Format(m_ResourceManager.GetString("err_cannotRenameFile"), oldAbsolutePathInSyncSource)); } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } finally { if (con != null) { con.Dispose(); } } }
/// <summary> /// Rename a file in sync source folder and update action table /// </summary> /// <param name="action"></param> /// <param name="profile"></param> /// <exception cref="System.ComponentModel.Win32Exception"></exception> public static void RenameInSyncFolderAndUpdateActionTable(RenameAction action, SyncJob job) { if (!Directory.Exists(job.SyncSource.Path)) throw new SyncSourceException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), job.SyncSource.Path)); if (!Directory.Exists(job.IntermediaryStorage.Path)) throw new SyncSourceException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), job.IntermediaryStorage.Path)); string oldAbsolutePathInSyncSource = job.SyncSource.Path + action.PreviousRelativeFilePath; string newAbsolutePathInSyncSource = job.SyncSource.Path + action.RelativeFilePath; SQLiteAccess access = new SQLiteAccess(Path.Combine (job.IntermediaryStorage.Path, Configuration.DATABASE_NAME),true); SqliteConnection con = access.NewSQLiteConnection(); if (con == null) throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME))); SqliteTransaction transaction = (SqliteTransaction)con.BeginTransaction(); try { SyncActionsProvider actProvider = SyncClient.GetSyncActionsProvider(job.IntermediaryStorage.Path); actProvider.Delete(action); if (File.Exists(oldAbsolutePathInSyncSource) && !Files.FileUtils.Move(oldAbsolutePathInSyncSource, newAbsolutePathInSyncSource, true)) throw new Exception(String.Format(m_ResourceManager.GetString("err_cannotRenameFile"), oldAbsolutePathInSyncSource)); transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } finally { if (con != null) con.Dispose(); } }
/// <summary> /// Update a SyncJob requires update 2 tables at the same time, /// If one update on a table fails, the total update action must fail too. /// </summary> /// <param name="job"></param> public abstract bool Update(SyncJob job);
public UISyncJobEntry(SyncJob job) { this._syncJob = job; this._agent = new FileSyncAgent(job); }
public override bool Add(SyncJob job) { if (this.SyncJobExists(job.Name, job.ID)) throw new SyncJobNameExistException(String.Format(m_ResourceManager.GetString("err_syncjobCreated"), job.Name)); SQLiteAccess dbAccess1 = new SQLiteAccess(Path.Combine (this.StoragePath, Configuration.DATABASE_NAME),false); SQLiteAccess dbAccess2 = new SQLiteAccess(Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME),false); SqliteConnection con1 = dbAccess1.NewSQLiteConnection(); SqliteConnection con2 = dbAccess2.NewSQLiteConnection(); if (con1 == null) throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(this.StoragePath, DATABASE_NAME))); if (con2 == null) throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME))); SqliteTransaction transaction1 = (SqliteTransaction)con1.BeginTransaction(); SqliteTransaction transaction2 = (SqliteTransaction)con2.BeginTransaction(); try { string insertJobText = "INSERT INTO " + SYNCJOB_TABLE + " (" + COL_SYNCJOB_ID + ", " + COL_SYNCJOB_NAME + " ," + COL_METADATA_SOURCE_LOCATION + ", " + COL_SYNC_SOURCE_ID + ") VALUES (@id, @name, @meta, @source)"; SqliteParameterCollection paramList = new SqliteParameterCollection(); paramList.Add(new SqliteParameter("@id", System.Data.DbType.String) { Value = job.ID }); paramList.Add(new SqliteParameter("@name", System.Data.DbType.String) { Value = job.Name }); paramList.Add(new SqliteParameter("@meta", System.Data.DbType.String) { Value = job.IntermediaryStorage.Path }); paramList.Add(new SqliteParameter("@source", System.Data.DbType.String) { Value = job.SyncSource.ID }); dbAccess1.ExecuteNonQuery(insertJobText, paramList); SQLiteSyncSourceProvider.Add(job.SyncSource, con1); SQLiteSyncSourceProvider provider = (SQLiteSyncSourceProvider)SyncClient.GetSyncSourceProvider(job.IntermediaryStorage.Path); if (provider.GetSyncSourceCount() == 2) throw new SyncSourcesNumberExceededException(m_ResourceManager.GetString("err_onlyTwoSyncSourceFolders")); SQLiteSyncSourceProvider.Add(job.SyncSource, con2); transaction1.Commit(); transaction2.Commit(); return true; } catch (Exception) { transaction1.Rollback(); transaction2.Rollback(); throw; } finally { if (con1 != null) con1.Dispose(); if (con2 != null) con2.Dispose(); } }
/// <summary> /// Renames conflicted file so that RenameAction can be executed /// </summary> /// <param name="renameAction">Rename action that cannot be executed due to conflict.</param> public static void ConflictRenameAndUpdateActionTable(RenameAction action, SyncJob job, bool keepConflictedFile) { if (!Directory.Exists(job.SyncSource.Path)) { throw new SyncSourceException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), job.SyncSource.Path)); } if (!Directory.Exists(job.IntermediaryStorage.Path)) { throw new SyncSourceException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), job.IntermediaryStorage.Path)); } string absPathInIStorage = job.IntermediaryStorage.DirtyFolderPath + action.RelativeFilePath; string absPathInSyncSource = job.SyncSource.Path + action.RelativeFilePath; string absOldPathInSyncSource = job.SyncSource.Path + action.PreviousRelativeFilePath; SQLiteAccess access = new SQLiteAccess(Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME), true); SqliteConnection con = access.NewSQLiteConnection(); if (con == null) { throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME))); } SqliteTransaction trasaction = (SqliteTransaction)con.BeginTransaction(); try { SQLiteSyncActionsProvider actProvider = (SQLiteSyncActionsProvider)SyncClient.GetSyncActionsProvider(job.IntermediaryStorage.Path); actProvider.Delete(action, con); if (!keepConflictedFile) { Files.FileUtils.Delete(absPathInSyncSource, true); } else { Files.FileUtils.DuplicateRename(absPathInSyncSource, absPathInSyncSource); } if (!Files.FileUtils.Move(absOldPathInSyncSource, absPathInSyncSource, true)) { throw new Exception(String.Format(m_ResourceManager.GetString("err_cannotRenameFile"), absPathInIStorage)); } trasaction.Commit(); Files.FileUtils.DeleteFileAndFolderIfEmpty(job.IntermediaryStorage.DirtyFolderPath, absPathInIStorage, true); } catch (Exception) { trasaction.Rollback(); throw; } finally { if (con != null) { con.Dispose(); } } }