public override IDataValidationResults <TKey> Deleting(TKey id, IDataValidationResults <TKey> validationResults, params object[] args)
        {
            if (args == null || args.Length == 0)
            {
                throw new ArgumentNullException("args[0] should contain reference to XPO entity");
            }
            IDataValidationResults <TKey> result = new DataValidationResults <TKey>();
            TXPORole role = args[0] as TXPORole;

            if (role != null)
            {
                int userCount = (int)role.Session.Evaluate(typeof(XpoDxUser),
                                                           CriteriaOperator.Parse("Count"),
                                                           CriteriaOperator.Parse("Roles[Id == ?]", role.Id));
                if (userCount > 0)
                {
                    result.Add(new DataValidationResult <TKey>
                    {
                        ResultType = DataValidationResultType.Error,
                        ID         = id,
                        Message    = $"Role '{role.Name}' cannot be deleted because there are users in this Role"
                    });
                }
            }

            if (result == null)
            {
                result = base.Deleting(id, validationResults, args);
            }
            validationResults.AddRange(result);
            return(result);
        }
Exemple #2
0
        public override IDataValidationResults <TKey> Update(IEnumerable <TModel> items)
        {
            if (items == null)
            {
                throw new ArgumentNullException(nameof(items));
            }

            IDataValidationResults <TKey> result = new DataValidationResults <TKey>();

            result = DB.Execute((db, ctx, tran) =>
            {
                var r = new DataValidationResults <TKey>();
                foreach (var item in items)
                {
                    var canUpdate = Validator?.Updating(item, r);
                    if (canUpdate.ResultType == DataValidationResultType.Error)
                    {
                        tran.Rollback();
                        r.Add(canUpdate);
                        break;
                    }

                    var updatedItem = EFQuery(ctx).Where(m => m.ID.Equals(item.ID)).FirstOrDefault();
                    if (updatedItem == null)
                    {
                        r.Add(DataValidationResultType.Error, item.ID, "KeyField", $"Unable to locate {typeof(TEFClass).Name}({item.ID}) in datastore", 0, DataValidationEventType.Updating);
                        break;
                    }

                    Assign(item, updatedItem);
                    ctx.Entry(updatedItem).State = EntityState.Modified;

                    var hasUpdated = Validator?.Updated(item, updatedItem, r);
                    if (hasUpdated.ResultType == DataValidationResultType.Error)
                    {
                        tran.Rollback();
                        r.Add(hasUpdated);
                        break;
                    }
                }
                try
                {
                    ctx.SaveChanges();
                    tran.Commit();
                }
                catch (Exception e)
                {
                    tran.Rollback();
                    r.Add(new DataValidationResult <TKey>
                    {
                        ResultType = DataValidationResultType.Error,
                        Message    = e.GetInnerException().Message
                    });
                }
                return(r);
            }, true, false);

            return(result);
        }
Exemple #3
0
        public override IDataValidationResults <TKey> Deleted(TKey id, TXPOClass dbModel, IDataValidationResults <TKey> validationResults)
        {
            var result = new DataValidationResults <TKey>(
                new DataValidationResult <TKey>(DataValidationResultType.Success, dbModel.ID, string.Empty, string.Empty, 0, DataValidationEventType.Deleted));

            validationResults.AddRange(result.Results);
            return(result);
        }
Exemple #4
0
        public override IDataValidationResults <TKey> Deleting(TKey id, IDataValidationResults <TKey> validationResults, params object[] args)
        {
            var result = new DataValidationResults <TKey>(
                new DataValidationResult <TKey>(DataValidationResultType.Success, id, string.Empty, string.Empty, 0, DataValidationEventType.Inserting));

            validationResults.AddRange(result.Results);
            return(result);
        }
Exemple #5
0
        public override IDataValidationResults <TKey> Inserted(TKey id, TModel model, TEFClass dbModel, IDataValidationResults <TKey> validationResults)
        {
            var result = new DataValidationResults <TKey>(
                new DataValidationResult <TKey>(DataValidationResultType.Success, id, string.Empty, string.Empty, 0, DataValidationEventType.Inserted));

            validationResults.AddRange(result.Results);
            return(result);
        }
Exemple #6
0
        public override IDataValidationResults <TKey> Updating(TModel model, IDataValidationResults <TKey> validationResults)
        {
            var result = new DataValidationResults <TKey>(
                new DataValidationResult <TKey>(DataValidationResultType.Success, model.ID, string.Empty, string.Empty, 0, DataValidationEventType.Updating));

            validationResults.AddRange(result.Results);
            return(result);
        }
Exemple #7
0
        public virtual DataValidationResult <TKey> Deleting(TKey id,
                                                            DataValidationResults <TKey> validationResults,
                                                            params object[] args)
        {
            var result = CreateDefaultResult(id, DataValidationEventType.Deleting);

            validationResults.Add(result);
            return(result);
        }
Exemple #8
0
        public override IDataValidationResults <TKey> Update(IEnumerable <TModel> items)
        {
            if (items == null)
            {
                throw new ArgumentNullException("items");
            }

            IDataValidationResults <TKey> result = new DataValidationResults <TKey>();

            result = DB.Execute((db, w) =>
            {
                var r = new DataValidationResults <TKey>();
                foreach (var item in items)
                {
                    var canUpdate = Validator?.Updating(item, r);
                    if (canUpdate.ResultType == DataValidationResultType.Error)
                    {
                        w.RollbackTransaction();
                        r.Add(canUpdate);
                        break;
                    }

                    var updatedItem = w.GetObjectByKey <TXPOClass>(item.ID);
                    if (updatedItem == null)
                    {
                        r.Add(DataValidationResultType.Error, item.ID, "KeyField", String.Format("Unable to locate {0}({1}) in datastore", typeof(TXPOClass).Name, item.ID), 0);
                        break;
                    }

                    Assign(item, updatedItem);

                    var hasUpdated = Validator?.Updated(item, updatedItem, r);
                    if (hasUpdated.ResultType == DataValidationResultType.Error)
                    {
                        w.RollbackTransaction();
                        r.Add(hasUpdated);
                        break;
                    }
                }
                try
                {
                    w.CommitTransaction();
                }
                catch (Exception e)
                {
                    w.RollbackTransaction();
                    r.Add(new DataValidationResult <TKey>
                    {
                        ResultType = DataValidationResultType.Error,
                        Message    = e.InnerException != null ? e.InnerException.Message : e.Message
                    });
                }
                return(r);
            });

            return(result);
        }
Exemple #9
0
        public override IDataValidationResults <TKey> Update(IEnumerable <TModel> items)
        {
            if (items == null)
            {
                throw new ArgumentNullException("items");
            }

            IDataValidationResults <TKey> result = new DataValidationResults <TKey>();

            try
            {
                result = xpo.Execute((db, w) =>
                {
                    var r = new DataValidationResults <TKey>();
                    foreach (var item in items)
                    {
                        val?.Updating(item, r);

                        var updatedItem = w.GetObjectByKey <TXPOClass>(item.ID);
                        if (updatedItem == null)
                        {
                            r.Add(DataValidationResultType.Error, item.ID, "KeyField", String.Format("Unable to locate {0}({1}) in datastore", typeof(TXPOClass).Name, item.ID), 0);
                        }

                        Assign(item, updatedItem);
                        // Move to Validator Updated
                        //updatedItem.ModStampUTC = DateTime.UtcNow;

                        try
                        {
                            w.CommitTransaction();
                            //item.ID = updatedItem.ID;
                            val?.Updated(item, updatedItem, r);
                        }
                        catch (Exception e)
                        {
                            r.Add(new DataValidationResult <TKey>
                            {
                                ResultType = DataValidationResultType.Error,
                                Message    = e.InnerException != null ? e.InnerException.Message : e.Message
                            });
                        }
                    }
                    return(r);
                });
            }
            catch (Exception ex)
            {
                result.Add(new DataValidationResult <TKey>
                {
                    ResultType = DataValidationResultType.Error,
                    Message    = ex.InnerException != null ? ex.InnerException.Message : ex.Message
                });
            }
            return(result);
        }
Exemple #10
0
        public override IDataValidationResults <TKey> Delete(IEnumerable <TKey> ids)
        {
            var result = DB.Execute((db, ctx, tran) =>
            {
                var r = new DataValidationResults <TKey>();
                foreach (var id in ids)
                {
                    var item = EFQuery(ctx).Where(m => m.ID.Equals(id)).FirstOrDefault();
                    if (item == null)
                    {
                        r.Add(DataValidationResultType.Error, item.ID, "KeyField", $"Unable to locate {typeof(TEFClass).Name}({item.ID}) in datastore", 0, DataValidationEventType.Deleting);
                        break;
                    }
                    var canDelete = Validator?.Deleting(id, r, item);
                    if (canDelete.ResultType == DataValidationResultType.Error)
                    {
                        tran.Rollback();
                        r.Add(canDelete);
                        break;
                    }

                    ctx.Entry(item).State = EntityState.Deleted;

                    //val.Deleted(id, item, r);
                    var hasDeleted = Validator?.Deleted(id, item, r);
                    if (hasDeleted.ResultType == DataValidationResultType.Error)
                    {
                        tran.Rollback();
                        r.Add(hasDeleted);
                        break;                         // throw new Exception(hasInserted.Message);
                    }
                }
                try
                {
                    ctx.SaveChanges();
                    tran.Commit();
                }
                catch (Exception e)
                {
                    r.Add(new DataValidationResult <TKey>
                    {
                        ResultType = DataValidationResultType.Error,
                        Message    = e.InnerException != null ? e.InnerException.Message : e.Message
                    });
                }
                return(r);
            }, true, false);

            return(result);
        }
Exemple #11
0
        public override IDataValidationResults <TKey> Delete(IEnumerable <TKey> ids)
        {
            var result = DB.Execute((db, w) =>
            {
                var r = new DataValidationResults <TKey>();
                foreach (var id in ids)
                {
                    var item = w.GetObjectByKey <TXPOClass>(id);
                    if (item == null)
                    {
                        r.Add(DataValidationResultType.Error, item.ID, "KeyField", String.Format("Unable to locate {0}({1}) in datastore", typeof(TXPOClass).Name, item.ID), 0);
                        break;
                    }
                    var canDelete = Validator?.Deleting(id, item, r);
                    if (canDelete.ResultType == DataValidationResultType.Error)
                    {
                        w.RollbackTransaction();
                        r.Add(canDelete);
                        break;
                    }

                    item.Delete();
                    //val.Deleted(id, item, r);
                    var hasDeleted = Validator?.Deleted(id, item, r);
                    if (hasDeleted.ResultType == DataValidationResultType.Error)
                    {
                        w.RollbackTransaction();
                        r.Add(hasDeleted);
                        break;                         // throw new Exception(hasInserted.Message);
                    }
                }
                try
                {
                    w.CommitTransaction();
                }
                catch (Exception e)
                {
                    r.Add(new DataValidationResult <TKey>
                    {
                        ResultType = DataValidationResultType.Error,
                        Message    = e.InnerException != null ? e.InnerException.Message : e.Message
                    });
                }
                return(r);
            });

            return(result);
        }
Exemple #12
0
        public override IDataValidationResults <TKey> Delete(IEnumerable <TKey> ids)
        {
            var result = xpo.Execute((db, w) =>
            {
                var r = new DataValidationResults <TKey>();
                foreach (var id in ids)
                {
                    var item = w.GetObjectByKey <TXPOClass>(id);
                    if (item == null)
                    {
                        r.Add(DataValidationResultType.Error, item.ID, "KeyField", String.Format("Unable to locate {0}({1}) in datastore", typeof(TXPOClass).Name, item.ID), 0);
                    }

                    val.Deleting(id, item, r);
                    item.Delete();
                    val.Deleted(id, item, r);
                }
                return(r);
            });

            return(result);
        }
Exemple #13
0
        protected virtual IDataValidationResults <TKey> InternalStore(IEnumerable <TModel> items, StoreMode mode, bool continueOnError)
        {
            if (items == null)
            {
                throw new ArgumentNullException(nameof(items));
            }

            IDataValidationResults <TKey> result = new DataValidationResults <TKey>();

            result = DB.Execute((db, w) =>
            {
                // need to keep the xpo entities together with the model items so we can update
                // the id's of the models afterwards.
                Dictionary <TXPOClass, InsertHelper> batchPairs = new Dictionary <TXPOClass, InsertHelper>();
                var r = new DataValidationResults <TKey>();
                foreach (var item in items)
                {
                    if (item.ID == null || item.ID.Equals(EmptyKeyValue) || mode == StoreMode.Create)
                    {
                        var canInsert = Validator?.Inserting(item, r);
                        if (canInsert.ResultType == DataValidationResultType.Error)
                        {
                            r.Add(canInsert);
                            if (!continueOnError)
                            {
                                w.RollbackTransaction();
                                break;
                            }
                        }

                        TXPOClass newItem = Assign(item,
                                                   Activator.CreateInstance(typeof(TXPOClass), new object[] { w }) as TXPOClass);

                        var hasInserted = Validator?.Inserted(item, newItem, r);
                        if (hasInserted.ResultType == DataValidationResultType.Error)
                        {
                            r.Add(hasInserted);
                            if (!continueOnError)
                            {
                                w.RollbackTransaction();
                                break;
                            }
                        }
                        batchPairs.Add(newItem, new InsertHelper(item, canInsert, hasInserted));
                    }
                    else if (!item.ID.Equals(EmptyKeyValue) && (mode != StoreMode.Create))
                    {
                        var canUpdate = Validator?.Updating(item, r);
                        if (canUpdate.ResultType == DataValidationResultType.Error)
                        {
                            r.Add(canUpdate);
                            if (!continueOnError)
                            {
                                w.RollbackTransaction();
                                break;
                            }
                        }

                        var updatedItem = w.GetObjectByKey <TXPOClass>(item.ID);
                        if (updatedItem == null)
                        {
                            r.Add(DataValidationResultType.Error, item.ID, "KeyField", $"Unable to locate {typeof(TXPOClass).Name}({item.ID}) in datastore", 0, DataValidationEventType.Updating);
                            break;
                        }

                        Assign(item, updatedItem);

                        var hasUpdated = Validator?.Updated(item, updatedItem, r);
                        if (hasUpdated.ResultType == DataValidationResultType.Error)
                        {
                            r.Add(hasUpdated);
                            if (!continueOnError)
                            {
                                w.RollbackTransaction();
                                break;
                            }
                        }
                    }
                }

                try
                {
                    w.ObjectSaved += (s, e) =>
                    {
                        // sync the model ids with the newly generated xpo id's
                        var xpoItem = e.Object as TXPOClass;
                        if (xpoItem != null && batchPairs.ContainsKey(xpoItem))
                        {
                            batchPairs[xpoItem].Model.ID           = xpoItem.ID;
                            batchPairs[xpoItem].InsertingResult.ID = xpoItem.ID;
                            batchPairs[xpoItem].InsertedResult.ID  = xpoItem.ID;
                        }
                    };
                    w.FailedCommitTransaction += (s, e) =>
                    {
                        r.Add(new DataValidationResult <TKey>
                        {
                            ResultType = DataValidationResultType.Error,
                            Message    = e.Exception.InnerException != null ? e.Exception.InnerException.Message : e.Exception.Message
                        });

                        e.Handled = true;
                    };
                    w.CommitTransaction();
                }
                catch
                {
                    w.RollbackTransaction();
                }
                return(r);
            });

            return(result);
        }
Exemple #14
0
 public virtual DataValidationResult <TKey> Inserting(TModel model, DataValidationResults <TKey> validationResults)
 {
     var result = CreateDefaultResult(default, DataValidationEventType.Inserting);
Exemple #15
0
        public override IDataValidationResults <TKey> Create(IEnumerable <TModel> items)
        {
            if (items == null)
            {
                throw new ArgumentNullException("items");
            }

            IDataValidationResults <TKey> result = new DataValidationResults <TKey>();

            result = DB.Execute((db, w) =>
            {
                // need to keep the xpo entities together with the model items so we can update
                // the id's of the models afterwards.
                Dictionary <TXPOClass, TModel> batchPairs = new Dictionary <TXPOClass, TModel>();

                var r = new DataValidationResults <TKey>();
                foreach (var item in items)
                {
                    var canInsert = Validator?.Inserting(item, r);
                    if (canInsert.ResultType == DataValidationResultType.Error)
                    {
                        w.RollbackTransaction();
                        r.Add(canInsert);
                        break;
                    }

                    TXPOClass newItem = Assign(item, Activator.CreateInstance(typeof(TXPOClass), new object[] { w }) as TXPOClass);
                    batchPairs.Add(newItem, item);

                    var hasInserted = Validator?.Inserted(item, newItem, r);
                    if (hasInserted.ResultType == DataValidationResultType.Error)
                    {
                        w.RollbackTransaction();
                        r.Add(hasInserted);
                        break;
                    }
                }

                try
                {
                    w.ObjectSaved += (s, e) => {
                        // sync the model ids with the newly generated xpo id's
                        var xpoItem = e.Object as TXPOClass;
                        if (xpoItem != null && batchPairs.ContainsKey(xpoItem))
                        {
                            //var model = batchPairs[xpoItem];
                            batchPairs[xpoItem].ID = xpoItem.ID;
                        }
                    };
                    w.CommitTransaction();
                }
                catch (Exception e)
                {
                    w.RollbackTransaction();
                    r.Add(new DataValidationResult <TKey>
                    {
                        ResultType = DataValidationResultType.Error,
                        Message    = e.InnerException != null ? e.InnerException.Message : e.Message
                    });
                }
                return(r);
            });

            return(result);
        }
Exemple #16
0
        public override IDataValidationResults <TKey> Create(IEnumerable <TModel> items)
        {
            if (items == null)
            {
                throw new ArgumentNullException("items");
            }
            IDataValidationResults <TKey> result = new DataValidationResults <TKey>();

            try
            {
                result = xpo.Execute((db, w) =>
                {
                    var r = new DataValidationResults <TKey>();
                    foreach (var item in items)
                    {
                        val?.Inserting(item, r);

                        //bool ok = val.Inserting(item);
                        //if (!ok)
                        //	throw new Exception(String.Format("Validator failed on Inserting on '{0}'", XpoType.Name));

                        var newItem = Assign(item, Activator.CreateInstance(typeof(TXPOClass), new object[] { w }) as TXPOClass);
                        //TODO: Move to Validator Inserted
                        //newItem.AddStampUTC = DateTime.UtcNow;
                        //newItem.ModStampUTC = DateTime.UtcNow;

                        //ok = val.Inserted(item, newItem);
                        //if (!ok)
                        //	throw new Exception(String.Format("Validator failed on Inserted on '{0}'", XpoType.Name));
                        //w.FailedCommitTransaction += (s, e) =>
                        //{

                        //	e.Handled = true;
                        //};
                        try
                        {
                            w.CommitTransaction();
                            item.ID = newItem.ID;
                            val?.Inserted(item, newItem, r);
                        }
                        catch (Exception e)
                        {
                            r.Add(new DataValidationResult <TKey>
                            {
                                ResultType = DataValidationResultType.Error,
                                Message    = e.InnerException != null ? e.InnerException.Message : e.Message
                            });
                        }
                    }
                    return(r);
                });
            }
            catch (Exception ex)
            {
                result.Add(new DataValidationResult <TKey>
                {
                    ResultType = DataValidationResultType.Error,
                    Message    = ex.InnerException != null ? ex.InnerException.Message : ex.Message
                });
            }
            return(result);
        }
Exemple #17
0
        public override IDataValidationResults <TKey> Create(IEnumerable <TModel> items)
        {
            if (items == null)
            {
                throw new ArgumentNullException(nameof(items));
            }

            IDataValidationResults <TKey> result = new DataValidationResults <TKey>();

            result = DB.Execute((db, ctx, tran) =>
            {
                // need to keep the xpo entities together with the model items so we can update
                // the id's of the models afterwards.
                Dictionary <TEFClass, TModel> batchPairs = new Dictionary <TEFClass, TModel>();

                var r = new DataValidationResults <TKey>();
                foreach (var item in items)
                {
                    var canInsert = Validator?.Inserting(item, r);
                    if (canInsert.ResultType == DataValidationResultType.Error)
                    {
                        tran.Rollback();
                        r.Add(canInsert);
                        break;
                    }

                    TEFClass newItem = Assign(item, Activator.CreateInstance(typeof(TEFClass)) as TEFClass);
                    batchPairs.Add(newItem, item);
                    ctx.Entry(newItem).State = EntityState.Added;

                    var hasInserted = Validator?.Inserted(item, newItem, r);
                    if (hasInserted.ResultType == DataValidationResultType.Error)
                    {
                        tran.Rollback();
                        r.Add(hasInserted);
                        break;
                    }
                }

                try
                {
                    //ctx.ObjectSaved += (s, e) => {
                    //	// sync the model ids with the newly generated xpo id's
                    //	var xpoItem = e.Object as TEFClass;
                    //	if (xpoItem != null && batchPairs.ContainsKey(xpoItem))
                    //	{
                    //		//var model = batchPairs[xpoItem];
                    //		batchPairs[xpoItem].ID = xpoItem.ID;
                    //	}
                    //};
                    ctx.SaveChanges();
                    tran.Commit();
                }
                catch (Exception e)
                {
                    tran.Rollback();
                    r.Add(new DataValidationResult <TKey>
                    {
                        ResultType = DataValidationResultType.Error,
                        Message    = e.GetInnerException().Message
                    });
                }
                return(r);
            }, true, false);

            return(result);
        }