Пример #1
0
        public static void CheckRelatedOnDelete(IDbConnection connection, string tableName, Action <SqlQuery> filter)
        {
            var query = new SqlQuery().Select("1").From(tableName, Alias.T0);

            filter(query);
            if (query.Take(1).Exists(connection))
            {
                throw DataValidation.RelatedRecordExist(tableName);
            }
        }
        protected virtual void LoadOldEntity()
        {
            var idField = (Field)(Row.IdField);
            var id      = Row.IdField[Row].Value;

            if (!PrepareQuery().GetFirst(Connection))
            {
                throw DataValidation.EntityNotFoundError(Row, id);
            }
        }
Пример #3
0
        //    public static void ValidateRequestSupport(this ListRequest request, ListRequestSupport support)
        //    {
        //        DataValidation.CheckNotNull(request);

        //        if (request.Take < 0 ||
        //            request.Skip < 0)
        //            throw new ValidationError("Liste isteklerinde Take ve Skip parametreleri 0'dan büyük olmalıdır!");

        //        if ((support & ListRequestSupport.Paging) != ListRequestSupport.Paging &&
        //            (request.Take > 0 || request.Skip > 0))
        //        {
        //            throw new ValidationError("Bu liste servisi sayfalamayı desteklemiyor!");
        //        }

        //        if ((support & ListRequestSupport.Sorting) != ListRequestSupport.Sorting &&
        //            !request.Sort.IsEmptyOrNull())
        //        {
        //            throw new ValidationError("Bu liste servisi sıralamayı desteklemiyor!");
        //        }

        //        if ((support & ListRequestSupport.ContainsText) != ListRequestSupport.ContainsText &&
        //            !request.ContainsText.IsNullOrEmpty())
        //        {
        //            throw new ValidationError("Bu liste servisi metin aramasını desteklemiyor!");
        //        }

        //        if ((support & ListRequestSupport.IncludeDeleted) != ListRequestSupport.IncludeDeleted &&
        //            request.IncludeDeleted)
        //        {
        //            throw new ValidationError("Bu liste servisi silinmiş kayıtları göstermeyi desteklemiyor!");
        //        }

        //        /*if ((support & ListRequestSupport.FilterLines) != ListRequestSupport.FilterLines &&
        //            !request.FilterLines.IsEmptyOrNull())
        //        {
        //            throw new ValidationError("Bu liste servisi filtre satırlarını desteklemiyor!");
        //        }

        //        if ((support & ListRequestSupport.Filter) != ListRequestSupport.Filter &&
        //            request.Filter != null)
        //        {
        //            throw new ValidationError("Bu liste servisi filtre desteklemiyor!");
        //        }*/
        //    }

        //    public static void CheckRelatedOnDelete(IDbConnection connection, string tableName, Action<SqlQuery> filter)
        //    {
        //        var query = new SqlQuery().Select("1").From(tableName, Alias.T0);
        //        filter(query);
        //        if (query.Take(1).Exists(connection))
        //            throw DataValidation.RelatedRecordExist(tableName);
        //    }

        public static void CheckParentNotDeleted(IDbConnection connection, string tableName, string moduleName, Action <SqlQuery> filter)
        {
            var query = new SqlQuery(connection.GetDialect()).Select("1").From(tableName, moduleName, Alias.T0);

            filter(query);
            if (query.Take(1).Exists(connection))
            {
                throw DataValidation.ParentRecordDeleted(tableName);
            }
        }
Пример #4
0
        protected virtual void ValidateIsActive()
        {
            var isActiveRow = Old as IIsActiveRow;

            if (isActiveRow != null &&
                isActiveRow.IsActiveField[Old] < 0)
            {
                throw DataValidation.RecordNotActive(Old);
            }
        }
Пример #5
0
        public TDeleteResponse Process(IUnitOfWork unitOfWork, TDeleteRequest request)
        {
            StateBag.Clear();

            if (unitOfWork == null)
            {
                throw new ArgumentNullException("unitOfWork");
            }

            ValidatePermissions();

            UnitOfWork = unitOfWork;

            Request  = request;
            Response = new TDeleteResponse();

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

            Row = new TRow();

            var idField = (Field)Row.IdField;

            LoadEntity();

            ValidateRequest();

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

            if ((isDeletedRow != null && isDeletedRow.IsDeletedField[Row] == true) ||
                (isActiveDeletedRow != null && isActiveDeletedRow.IsActiveField[Row] < 0) ||
                (deleteLogRow != null && !((Field)deleteLogRow.DeleteUserIdField).IsNull(Row)))
            {
                Response.WasAlreadyDeleted = true;
            }
            else
            {
                OnBeforeDelete();

                ExecuteDelete();

                OnAfterDelete();

                DoAudit();
            }

            OnReturn();

            return(Response);
        }
Пример #6
0
        protected virtual void LoadOldEntity()
        {
            if (!PrepareQuery().GetFirst(Connection))
            {
                var idField = (Field)(Row.IdField);
                var id      = Request.EntityId != null?
                              idField.ConvertValue(Request.EntityId, CultureInfo.InvariantCulture)
                                  : idField.AsObject(Row);

                throw DataValidation.EntityNotFoundError(Row, id);
            }
        }
Пример #7
0
        protected virtual void LoadEntity()
        {
            var idField = (Field)Row.IdField;

            var query = new SqlQuery().From(Row)
                        .WhereEqual(idField, Request.EntityId.Value);

            PrepareQuery(query);

            if (!query.GetFirst(Connection))
            {
                throw DataValidation.EntityNotFoundError(Row, Request.EntityId.Value);
            }
        }
Пример #8
0
        public TDeleteResponse Process(IUnitOfWork unitOfWork, TDeleteRequest request)
        {
            if (unitOfWork == null)
            {
                throw new ArgumentNullException("unitOfWork");
            }

            ValidatePermissions();

            UnitOfWork = unitOfWork;

            Request  = request;
            Response = new TDeleteResponse();

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

            Row = new TRow();

            var idField = (Field)Row.IdField;

            LoadEntity();

            ValidateRequest();

            var isActiveRow = Row as IIsActiveRow;

            if (isActiveRow != null &&
                !ShouldActuallyDelete &&
                isActiveRow.IsActiveField[Row] < 0)
            {
                Response.WasAlreadyDeleted = true;
            }
            else
            {
                OnBeforeDelete();

                ExecuteDelete();

                OnAfterDelete();

                DoAudit();
            }

            OnReturn();

            return(Response);
        }
Пример #9
0
        protected virtual void ValidateIsActive()
        {
            if (Old is IIsActiveRow isActiveRow &&
                isActiveRow.IsActiveField[Old] < 0)
            {
                throw DataValidation.RecordNotActive(Old, Localizer);
            }

            if (Old is IIsDeletedRow isDeletedRow &&
                isDeletedRow.IsDeletedField[Old] == true)
            {
                throw DataValidation.RecordNotActive(Old, Localizer);
            }
        }
Пример #10
0
        protected virtual void LoadEntity()
        {
            var idField = Row.IdField;
            var id = idField.ConvertValue(Request.EntityId, CultureInfo.InvariantCulture);

            var query = new SqlQuery()
                .Dialect(Connection.GetDialect())
                .From(Row)
                .WhereEqual(idField, id);

            PrepareQuery(query);

            if (!query.GetFirst(Connection))
                throw DataValidation.EntityNotFoundError(Row, Request.EntityId, Localizer);
        }
Пример #11
0
        protected virtual void HandleNonEditable(Field field)
        {
            if (IsUpdate && field.IndexCompare(Row, Old) == 0)
            {
                field.CopyNoAssignment(Old, Row);
                Row.ClearAssignment(field);
                return;
            }

            bool isNonTableField = ((field.Flags & FieldFlags.Foreign) == FieldFlags.Foreign) ||
                                   ((field.Flags & FieldFlags.Calculated) == FieldFlags.Calculated) ||
                                   ((field.Flags & FieldFlags.NotMapped) == FieldFlags.NotMapped);

            if (IsUpdate)
            {
                if ((field.Flags & FieldFlags.Reflective) != FieldFlags.Reflective)
                {
                    if (!isNonTableField)
                    {
                        throw DataValidation.ReadOnlyError(Row, field);
                    }

                    field.CopyNoAssignment(Old, Row);
                    Row.ClearAssignment(field);
                }
            }
            else if (IsCreate)
            {
                if (!field.IsNull(Row) &&
                    (field.Flags & FieldFlags.Reflective) != FieldFlags.Reflective)
                {
                    if (!isNonTableField)
                    {
                        throw DataValidation.ReadOnlyError(Row, field);
                    }

                    field.AsObject(Row, null);
                    Row.ClearAssignment(field);
                }
            }

            if (Row.IsAssigned(field))
            {
                Row.ClearAssignment(field);
            }
        }
Пример #12
0
        public static void ValidateRequestSupport(this ListRequest request, ListRequestSupport support)
        {
            DataValidation.CheckNotNull(request);

            if (request.Take < 0 ||
                request.Skip < 0)
            {
                throw new ValidationError("Liste isteklerinde Take ve Skip parametreleri 0'dan büyük olmalıdır!");
            }

            if ((support & ListRequestSupport.Paging) != ListRequestSupport.Paging &&
                (request.Take > 0 || request.Skip > 0))
            {
                throw new ValidationError("Bu liste servisi sayfalamayı desteklemiyor!");
            }

            if ((support & ListRequestSupport.Sorting) != ListRequestSupport.Sorting &&
                !request.Sort.IsEmptyOrNull())
            {
                throw new ValidationError("Bu liste servisi sıralamayı desteklemiyor!");
            }

            if ((support & ListRequestSupport.ContainsText) != ListRequestSupport.ContainsText &&
                !request.ContainsText.IsNullOrEmpty())
            {
                throw new ValidationError("Bu liste servisi metin aramasını desteklemiyor!");
            }

            if ((support & ListRequestSupport.IncludeDeleted) != ListRequestSupport.IncludeDeleted &&
                request.IncludeDeleted)
            {
                throw new ValidationError("Bu liste servisi silinmiş kayıtları göstermeyi desteklemiyor!");
            }

            if ((support & ListRequestSupport.FilterLines) != ListRequestSupport.FilterLines &&
                !request.FilterLines.IsEmptyOrNull())
            {
                throw new ValidationError("Bu liste servisi filtre satırlarını desteklemiyor!");
            }

            if ((support & ListRequestSupport.Filter) != ListRequestSupport.Filter &&
                request.Filter != null)
            {
                throw new ValidationError("Bu liste servisi filtre desteklemiyor!");
            }
        }
Пример #13
0
        protected virtual void ExecuteDelete()
        {
            var isDeletedRow = Row as IIsActiveDeletedRow;
            var deleteLogRow = Row as IDeleteLogRow;
            var idField      = (Field)Row.IdField;
            var id           = Request.EntityId.Value;

            if (isDeletedRow == null && deleteLogRow == null)
            {
                if (new SqlDelete(Row.Table)
                    .WhereEqual(idField, id)
                    .Execute(Connection) != 1)
                {
                    throw DataValidation.EntityNotFoundError(Row, id);
                }
            }
            else
            {
                if (isDeletedRow != null)
                {
                    if (new SqlUpdate(Row.Table)
                        .Set(isDeletedRow.IsActiveField, -1)
                        .WhereEqual(idField, id)
                        .Where(new Criteria(isDeletedRow.IsActiveField) >= 0)
                        .Execute(Connection) != 1)
                    {
                        throw DataValidation.EntityNotFoundError(Row, id);
                    }
                }
                else //if (deleteLogRow != null)
                {
                    if (new SqlUpdate(Row.Table)
                        .Set(deleteLogRow.DeleteDateField, DateTimeField.ToDateTimeKind(DateTime.Now, deleteLogRow.DeleteDateField.DateTimeKind))
                        .Set((Field)deleteLogRow.DeleteUserIdField, Authorization.UserId.TryParseID())
                        .WhereEqual(idField, id)
                        .Where(new Criteria((Field)deleteLogRow.DeleteUserIdField).IsNull())
                        .Execute(Connection) != 1)
                    {
                        throw DataValidation.EntityNotFoundError(Row, id);
                    }
                }
            }

            InvalidateCacheOnCommit();
        }
Пример #14
0
        public TRetrieveResponse Process(IDbConnection connection, TRetrieveRequest request)
        {
            StateBag.Clear();

            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

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

            Connection = connection;
            Request    = request;
            ValidateRequest();

            Response = new TRetrieveResponse();
            Row      = new TRow();

            this.Query = CreateQuery();

            PrepareQuery(Query);

            OnBeforeExecuteQuery();

            if (Query.GetFirst(Connection))
            {
                Response.Entity = Row;
            }
            else
            {
                throw DataValidation.EntityNotFoundError(Row, request.EntityId);
            }

            OnAfterExecuteQuery();

            OnReturn();
            return(Response);
        }
Пример #15
0
        protected virtual void ValidateEditableFields(HashSet <Field> editable)
        {
            foreach (Field field in Row.GetFields())
            {
                if (IsUpdate && !Row.IsAssigned(field))
                {
                    field.CopyNoAssignment(Old, Row);
                    Row.ClearAssignment(field);
                    continue;
                }

                if (field is StringField stringField && Row.IsAssigned(field))
                {
                    DataValidation.AutoTrim(Row, stringField);
                }

                if (!editable.Contains(field))
                {
                    HandleNonEditable(field);
                }
            }
        }
Пример #16
0
        public TRetrieveResponse Process(IDbConnection connection, TRetrieveRequest request)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

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

            Connection = connection;
            Request    = request;
            ValidatePermissions();

            Response = new TRetrieveResponse();
            Row      = new TRow();

            var query = CreateQuery();

            PrepareQuery(query);

            if (query.GetFirst(Connection))
            {
                Response.Entity = Row;
            }
            else
            {
                throw DataValidation.EntityNotFoundError(Row, request.EntityId.Value);
            }

            OnReturn();
            return(Response);
        }
Пример #17
0
        protected virtual void ExecuteDelete()
        {
            var isActiveDeletedRow = Row as IIsActiveDeletedRow;
            var isDeletedRow       = Row as IIsDeletedRow;
            var deleteLogRow       = Row as IDeleteLogRow;
            var idField            = (Field)Row.IdField;
            var id = idField.ConvertValue(Request.EntityId, CultureInfo.InvariantCulture);

            if (isActiveDeletedRow == null && isDeletedRow == null && deleteLogRow == null)
            {
                if (new SqlDelete(Row.Table)
                    .WhereEqual(idField, id)
                    .Execute(Connection) != 1)
                {
                    throw DataValidation.EntityNotFoundError(Row, id);
                }
            }
            else
            {
                if (isDeletedRow != null || isActiveDeletedRow != null)
                {
                    var updateLogRow = Row as IUpdateLogRow;
                    var update       = new SqlUpdate(Row.Table)
                                       .WhereEqual(idField, id)
                                       .Where(ServiceQueryHelper.GetNotDeletedCriteria(Row));

                    if (isActiveDeletedRow != null)
                    {
                        update.Set(isActiveDeletedRow.IsActiveField, -1);
                    }
                    else if (isDeletedRow != null)
                    {
                        update.Set(isDeletedRow.IsDeletedField, true);
                    }

                    if (deleteLogRow != null)
                    {
                        update.Set(deleteLogRow.DeleteDateField, DateTimeField.ToDateTimeKind(DateTime.Now,
                                                                                              deleteLogRow.DeleteDateField.DateTimeKind))
                        .Set((Field)deleteLogRow.DeleteUserIdField, Authorization.UserId.TryParseID());
                    }
                    else if (updateLogRow != null)
                    {
                        update.Set(updateLogRow.UpdateDateField, DateTimeField.ToDateTimeKind(DateTime.Now,
                                                                                              updateLogRow.UpdateDateField.DateTimeKind))
                        .Set((Field)updateLogRow.UpdateUserIdField, Authorization.UserId.TryParseID());
                    }

                    if (update.Execute(Connection) != 1)
                    {
                        throw DataValidation.EntityNotFoundError(Row, id);
                    }
                }
                else //if (deleteLogRow != null)
                {
                    if (new SqlUpdate(Row.Table)
                        .Set(deleteLogRow.DeleteDateField, DateTimeField.ToDateTimeKind(DateTime.Now,
                                                                                        deleteLogRow.DeleteDateField.DateTimeKind))
                        .Set((Field)deleteLogRow.DeleteUserIdField, Authorization.UserId.TryParseID())
                        .WhereEqual(idField, id)
                        .Where(new Criteria((Field)deleteLogRow.DeleteUserIdField).IsNull())
                        .Execute(Connection) != 1)
                    {
                        throw DataValidation.EntityNotFoundError(Row, id);
                    }
                }
            }

            InvalidateCacheOnCommit();
        }
Пример #18
0
        public TUndeleteResponse Process(IUnitOfWork unitOfWork, UndeleteRequest request)
        {
            UnitOfWork = unitOfWork ?? throw new ArgumentNullException("unitOfWork");

            ValidatePermissions();

            Request  = request;
            Response = new TUndeleteResponse();

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

            Row = new TRow();

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

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

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

            LoadEntity();

            ValidateRequest();

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

                var update = new SqlUpdate(Row.Table)
                             .WhereEqual(idField, id);

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

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

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

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

                InvalidateCacheOnCommit();

                OnAfterUndelete();

                DoAudit();
            }

            OnReturn();

            return(Response);
        }
Пример #19
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);
        }