Example #1
0
        /// <summary>
        /// Generates list of sync actions that will synchronize current PC
        /// and other PC based on the metadata.
        /// </summary>
        /// <returns>List of sync actions</returns>
        public IList<SyncAction> Generate(string sourceId, IList<FileMetaDataItem> leftOnly, 
            IList<FileMetaDataItem> rightOnly, IList<FileMetaDataItem> both)
        {
            IList<SyncAction> actions = new List<SyncAction>();

            /* Keep track of create and delete actions to detect rename */
            var createActions = new List<SyncAction>();
            var deleteActions = new List<SyncAction>();

            //Get newly created items by comparing relative paths
            foreach (FileMetaDataItem item in leftOnly)
            {
                var createAction = new CreateAction(0, item.SourceId, item.RelativePath, item.HashCode);
                actions.Add(createAction);
                createActions.Add(createAction);
            }

            foreach (FileMetaDataItem item in rightOnly)
            {
                //the source id of this action must be source id of the folder where the item is deleted
                var deleteAction = new DeleteAction(0, sourceId, item.RelativePath, item.HashCode);
                actions.Add(deleteAction);
                deleteActions.Add(deleteAction);
            }

            foreach (FileMetaDataItem item in both)
            {
                var createAction = new CreateAction(0, item.SourceId, item.RelativePath, item.HashCode);
                actions.Add(createAction);
            }

            DetectRenameActions(actions, createActions, deleteActions);

            return actions;
        }
        private static bool InsertCreateAction(CreateAction createAction, SqliteConnection con)
        {
            using (SqliteCommand cmd = con.CreateCommand ())
            {
                cmd.CommandText = "INSERT INTO " + Configuration.TBL_ACTION +
                                 " ( " + Configuration.COL_CHANGE_IN + "," +
                                 Configuration.COL_ACTION_TYPE + "," +
                                 Configuration.COL_NEW_RELATIVE_PATH + "," +
                                 Configuration.COL_NEW_HASH + ") VALUES (@changeIn, @action, @newPath, @newHash)";

                SqliteParameterCollection paramList = new SqliteParameterCollection();
                cmd.Parameters.Add( new SqliteParameter("@changeIn", System.Data.DbType.String) { Value = createAction.SourceID });
                cmd.Parameters.Add(new SqliteParameter("@action", System.Data.DbType.Int32) { Value = createAction.ChangeType });
                cmd.Parameters.Add(new SqliteParameter("@newPath", System.Data.DbType.String) { Value = createAction.RelativeFilePath });
                cmd.Parameters.Add(new SqliteParameter("@newHash", System.Data.DbType.String) { Value = createAction.FileHash });

                cmd.ExecuteNonQuery();
                return true;
            }
        }
        private bool InsertCreateAction(CreateAction createAction)
        {
            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)));

                string cmdText = "INSERT INTO " + Configuration.TBL_ACTION +
                                 " ( " + Configuration.COL_CHANGE_IN + "," +
                                 Configuration.COL_ACTION_TYPE + "," +
                                 Configuration.COL_NEW_RELATIVE_PATH + "," +
                                 Configuration.COL_NEW_HASH + ") VALUES (@changeIn, @action, @newPath, @newHash)";

                SqliteParameterCollection paramList = new SqliteParameterCollection();
                paramList.Add(new SqliteParameter("@changeIn", System.Data.DbType.String) { Value = createAction.SourceID });
                paramList.Add(new SqliteParameter("@action", System.Data.DbType.Int32) { Value = createAction.ChangeType });
                paramList.Add(new SqliteParameter("@newPath", System.Data.DbType.String) { Value = createAction.RelativeFilePath });
                paramList.Add(new SqliteParameter("@newHash", System.Data.DbType.String) { Value = createAction.FileHash });

                db.ExecuteNonQuery(cmdText, paramList);
            }

            return true;
        }
        public override IList<SyncAction> Load(string sourceID, SourceOption option)
        {
            string opt = (option == SourceOption.SOURCE_ID_NOT_EQUALS) ? " <> " : " = ";
            IList<SyncAction> actions = new List<SyncAction>();

            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)));

                string cmdText = "SELECT * FROM " + Configuration.TBL_ACTION +
                                 " WHERE " + Configuration.COL_CHANGE_IN + opt + " @sourceId";

                SqliteParameterCollection paramList = new SqliteParameterCollection();
                paramList.Add(new SqliteParameter("@sourceId", System.Data.DbType.String) { Value = sourceID });

                db.ExecuteReader(cmdText, paramList, reader =>
                    {
                        ChangeType actionType = (ChangeType)reader[Configuration.COL_ACTION_TYPE];

                        if (actionType == ChangeType.DELETED)
                        {
                            DeleteAction delAction = new DeleteAction(
                                    (int)reader[Configuration.COL_ACTION_ID],
                                    (string)reader[Configuration.COL_CHANGE_IN],
                                    (string)reader[Configuration.COL_OLD_RELATIVE_PATH], (string)reader[Configuration.COL_OLD_HASH]);
                            actions.Add(delAction);
                        }
                        else if (actionType == ChangeType.NEWLY_CREATED)
                        {
                            CreateAction createAction = new CreateAction(
                                (int)reader[Configuration.COL_ACTION_ID],
                                (string)reader[Configuration.COL_CHANGE_IN],
                                (string)reader[Configuration.COL_NEW_RELATIVE_PATH], (string)reader[Configuration.COL_NEW_HASH]);
                            actions.Add(createAction);
                        }
                        else if (actionType == ChangeType.RENAMED)
                        {
                            RenameAction renameAction = new RenameAction(
                                (int)reader[Configuration.COL_ACTION_ID],
                                (string)reader[Configuration.COL_CHANGE_IN],
                                (string)reader[Configuration.COL_NEW_RELATIVE_PATH],
                                (string)reader[Configuration.COL_OLD_RELATIVE_PATH],
                                (string)reader[Configuration.COL_OLD_HASH]);
                            actions.Add(renameAction);
                        }
                    }
                );

            }
            return actions;
        }