Beispiel #1
0
        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;
                }
            }
        }
Beispiel #2
0
        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();
                }
            }
        }