public void SetUserMigrationState(string clientName, string projectName, string userPrincipalName, StateType desiredState)
        {
            var clientId        = GetClientId(clientName);
            var projectId       = GetProjectId(projectName, clientId);
            var userMigrationId = GetUserMigrationId(userPrincipalName, projectId);

            int migrationStateId = 3;

            switch (desiredState)
            {
            case StateType.Stopping:
            case StateType.Preparing:
            case StateType.Syncing:
            case StateType.RollbackInProgress:
            case StateType.RollbackError:
                migrationStateId = 18;
                break;

            case StateType.Matched:
                migrationStateId = 3;
                break;

            case StateType.Prepared:
            case StateType.Synced:
            case StateType.RollbackCompleted:
            case StateType.Complete:
                migrationStateId = 20;
                break;

            case StateType.Moved:
                migrationStateId = 10;
                break;

            default:
                throw new Exception(string.Format("MigrationStateId was not be found for state '{0}' ", desiredState));
            }

            T2T.ExecuteNonQuery(string.Format("UPDATE UserMigration SET MigrationStateID = {0} WHERE UserMigrationID = {1}", migrationStateId, userMigrationId));
        }
 public void RemoveCutoverJobs(int userMigrationId, SqlTransaction transaction = null)
 {
     T2T.ExecuteNonQuery(string.Format("DELETE FROM CutoverJob WHERE UserMigrationID = {0}", userMigrationId), transaction);
 }
        public void ResetUser(string clientName, string projectName, string sourceUserPrincipalName, StateType desiredState)
        {
            var clientId        = GetClientId(clientName);
            var projectId       = GetProjectId(projectName, clientId);
            var userMigrationId = GetUserMigrationId(sourceUserPrincipalName, projectId);

            bool removePrepareJobs = false;
            bool removeSyncJobs    = false;
            bool removeCutoverJobs = false;

            bool removeTargetUserId = false;
            bool resetIsLocked      = false;
            bool resetIsArchived    = false;

            int migrationStateId = StateTypeToMigrationStateId(desiredState);

            switch (desiredState)
            {
            case StateType.Ready:
            case StateType.Matched:
            case StateType.NoMatch:
                removePrepareJobs  = true;
                removeSyncJobs     = true;
                removeCutoverJobs  = true;
                removeTargetUserId = true;
                resetIsLocked      = true;
                resetIsArchived    = true;
                break;

            case StateType.Prepared:
                removeSyncJobs    = true;
                removeCutoverJobs = true;
                break;

            case StateType.Synced:
                removeCutoverJobs = true;
                break;

            default:
                break;
            }

            using (var transaction = T2T.BeginTransaction())
            {
                try
                {
                    if (removePrepareJobs)
                    {
                        RemovePrepareJobs(userMigrationId, transaction);
                    }
                    if (removeSyncJobs)
                    {
                        RemoveSyncJobs(userMigrationId, transaction);
                    }
                    if (removeCutoverJobs)
                    {
                        RemoveCutoverJobs(userMigrationId, transaction);
                    }

                    var setValues = string.Format("MigrationStateId = {0}", migrationStateId);

                    if (removeTargetUserId)
                    {
                        setValues = setValues + string.Format(", TargetUserId = NULL, NewPrimarySMTPAddress = NULL, NewUserPrincipalName = NULL");
                    }

                    if (resetIsLocked)
                    {
                        setValues = setValues + string.Format(", IsLocked = 0");
                    }

                    if (resetIsArchived)
                    {
                        setValues = setValues + string.Format(", IsArchived = 0");
                    }

                    T2T.ExecuteNonQuery(string.Format("UPDATE UserMigration SET {0} WHERE UserMigrationID = {1}", setValues, userMigrationId), transaction);

                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }