Inheritance: ServiceRequest
Ejemplo n.º 1
0
        public TUndeleteResponse Process(IUnitOfWork unitOfWork, UndeleteRequest request)
        {
            if (unitOfWork == null)
            {
                throw new ArgumentNullException("unitOfWork");
            }

            ValidatePermissions();

            UnitOfWork = unitOfWork;

            Request  = request;
            Response = new TUndeleteResponse();

            if (request.EntityId == null)
            {
                throw DataValidation.RequiredError("EntityId");
            }

            Row = new TRow();

            var isDeletedRow = Row as IIsActiveDeletedRow;
            var deleteLogRow = Row as IDeleteLogRow;

            if (isDeletedRow == null && deleteLogRow == null)
            {
                throw new NotImplementedException();
            }

            var idField = (Field)Row.IdField;
            var id      = idField.ConvertValue(Request.EntityId, CultureInfo.InvariantCulture);

            LoadEntity();

            ValidateRequest();

            if ((isDeletedRow != null && isDeletedRow.IsActiveField[Row] > 0) ||
                (deleteLogRow != null && ((Field)deleteLogRow.DeleteUserIdField).IsNull(Row)))
            {
                Response.WasNotDeleted = true;
            }
            else
            {
                OnBeforeUndelete();

                var update = new SqlUpdate(unitOfWork.Connection.GetDialect(), Row.Table, string.Empty)
                             .WhereEqual(idField, id);

                if (isDeletedRow != null)
                {
                    update.Set(isDeletedRow.IsActiveField, 1)
                    .WhereEqual(isDeletedRow.IsActiveField, -1);
                }

                if (deleteLogRow != null)
                {
                    update.Set((Field)deleteLogRow.DeleteUserIdField, null)
                    .Set(deleteLogRow.DeleteDateField, null);

                    if (isDeletedRow == null)
                    {
                        update.Where(((Field)deleteLogRow.DeleteUserIdField).IsNotNull());
                    }
                }

                if (update.Execute(Connection) != 1)
                {
                    throw DataValidation.EntityNotFoundError(Row, id);
                }

                InvalidateCacheOnCommit();

                OnAfterUndelete();

                DoAudit();
            }

            OnReturn();

            return(Response);
        }
Ejemplo n.º 2
0
 public Result<UndeleteResponse> Undelete(UndeleteRequest request)
 {
     return this.InTransaction("Default", (uow) => new MyRepository().Undelete(uow, request));
 }
Ejemplo n.º 3
0
        public TUndeleteResponse Process(IUnitOfWork unitOfWork, UndeleteRequest request)
        {
            if (unitOfWork == null)
            {
                throw new ArgumentNullException("unitOfWork");
            }

            ValidatePermissions();

            UnitOfWork = unitOfWork;

            Request  = request;
            Response = new TUndeleteResponse();

            if (request.EntityId == null)
            {
                throw DataValidation.RequiredError("EntityId");
            }

            Row = new TRow();

            var isActiveRow = Row as IIsActiveRow;

            if (isActiveRow == null)
            {
                throw new NotImplementedException();
            }

            var idField = (Field)Row.IdField;

            LoadEntity();

            ValidateRequest();

            if (isActiveRow.IsActiveField[Row] > 0)
            {
                Response.WasNotDeleted = true;
            }
            else
            {
                OnBeforeUndelete();

                if (new SqlUpdate(Row.Table)
                    .Set(isActiveRow.IsActiveField, 1)
                    .WhereEqual(idField, request.EntityId.Value)
                    .WhereEqual(isActiveRow.IsActiveField, -1)
                    .Execute(Connection) != 1)
                {
                    throw DataValidation.EntityNotFoundError(Row, request.EntityId.Value);
                }

                InvalidateCacheOnCommit();

                OnAfterUndelete();

                DoAudit();
            }

            OnReturn();

            return(Response);
        }