public static Dictionary <Guid, IntegrityCheck> GetErrors(Modifiable mod)
        {
            var graph = GraphExplorer.PreSaving(() => GraphExplorer.FromRoot(mod));
            var error = GraphExplorer.FullIntegrityCheck(graph);

            return(error);
        }
Exemple #2
0
        private bool HasErrors()
        {
            GraphExplorer.PreSaving(() => GraphExplorer.FromRoot(Request));

            var errors = Request.FullIntegrityCheck();

            if (errors == null)
            {
                return(false);
            }

            MessageBox.Show(Window.GetWindow(this), "There are errors in the chart settings:\r\n" + errors, "Errors in the chart", MessageBoxButton.OK, MessageBoxImage.Stop);

            return(true);
        }
Exemple #3
0
        public async Task <List <EvalEntityError> > GetEvalErrors([Required, FromBody] QueryEntitiesRequestTS request)
        {
            var allEntities = await QueryLogic.Queries.GetEntitiesLite(request.ToQueryEntitiesRequest()).Select(a => a.Entity).ToListAsync();

            return(allEntities.Select(entity =>
            {
                GraphExplorer.PreSaving(() => GraphExplorer.FromRoot(entity));

                return new EvalEntityError
                {
                    lite = entity.ToLite(),
                    error = entity.FullIntegrityCheck().EmptyIfNull().Select(a => a.Value).SelectMany(a => a.Errors.Values).ToString("\n")
                };
            })
                   .Where(ee => ee.error.HasText())
                   .ToList());
        }
Exemple #4
0
        internal static DirectedGraph <Modifiable> PreSaving(Func <DirectedGraph <Modifiable> > recreate)
        {
            Schema schema = Schema.Current;

            return(GraphExplorer.PreSaving(recreate, (Modifiable m, PreSavingContext ctx) =>
            {
                if (m is ModifiableEntity me)
                {
                    me.SetTemporalErrors(null);
                }

                m.PreSaving(ctx);

                if (m is Entity ident)
                {
                    schema.OnPreSaving(ident, ctx);
                }
            }));
        }
    public async Task <List <EvalEntityError> > GetEvalErrors([Required, FromBody] QueryEntitiesRequestTS request)
    {
        DynamicPanelPermission.ViewDynamicPanel.AssertAuthorized();

        var allEntities = await QueryLogic.Queries.GetEntitiesLite(request.ToQueryEntitiesRequest(SignumServer.JsonSerializerOptions)).Select(a => a.Entity).ToListAsync();

        return(allEntities.Select(entity =>
        {
            GraphExplorer.PreSaving(() => GraphExplorer.FromRoot(entity));

            return new EvalEntityError
            {
                lite = entity.ToLite(),
                error = entity.FullIntegrityCheck().EmptyIfNull().Select(a => a.Value).SelectMany(a => a.Errors.Values).ToString("\n")
            };
        })
               .Where(ee => ee.error.HasText())
               .ToList());
    }
        public static int BulkInsertTable <T>(IEnumerable <T> entities,
                                              SqlBulkCopyOptions copyOptions = SqlBulkCopyOptions.Default,
                                              bool preSaving       = false,
                                              bool validateFirst   = false,
                                              bool disableIdentity = false,
                                              int?timeout          = null,
                                              string message       = null)
            where T : Entity
        {
            if (message != null)
            {
                return(SafeConsole.WaitRows(message == "auto" ? $"BulkInsering {entities.Count()} {typeof(T).TypeName()}" : message,
                                            () => BulkInsertTable(entities, copyOptions, preSaving, validateFirst, disableIdentity, timeout, message: null)));
            }

            if (disableIdentity)
            {
                copyOptions |= SqlBulkCopyOptions.KeepIdentity;
            }

            if (copyOptions.HasFlag(SqlBulkCopyOptions.UseInternalTransaction))
            {
                throw new InvalidOperationException("BulkInsertDisableIdentity not compatible with UseInternalTransaction");
            }

            var list = entities.ToList();

            if (preSaving)
            {
                Schema schema = Schema.Current;
                GraphExplorer.PreSaving(() => GraphExplorer.FromRoots(list), (Modifiable m, ref bool graphModified) =>
                {
                    if (m is ModifiableEntity me)
                    {
                        me.SetTemporalErrors(null);
                    }

                    m.PreSaving(ref graphModified);

                    if (m is Entity ident)
                    {
                        schema.OnPreSaving(ident, ref graphModified);
                    }
                });
            }

            if (validateFirst)
            {
                Validate <T>(list);
            }

            var  t = Schema.Current.Table <T>();
            bool disableIdentityBehaviour = copyOptions.HasFlag(SqlBulkCopyOptions.KeepIdentity);
            bool oldIdentityBehaviour     = t.IdentityBehaviour;

            DataTable dt = new DataTable();

            foreach (var c in disableIdentityBehaviour ? t.Columns.Values : t.Columns.Values.Where(c => !c.IdentityBehaviour))
            {
                dt.Columns.Add(new DataColumn(c.Name, c.Type.UnNullify()));
            }

            if (disableIdentityBehaviour)
            {
                t.IdentityBehaviour = false;
            }
            foreach (var e in entities)
            {
                if (!e.IsNew)
                {
                    throw new InvalidOperationException("Entites should be new");
                }
                t.SetToStrField(e);
                dt.Rows.Add(t.BulkInsertDataRow(e));
            }
            if (disableIdentityBehaviour)
            {
                t.IdentityBehaviour = oldIdentityBehaviour;
            }

            using (Transaction tr = new Transaction())
            {
                Schema.Current.OnPreBulkInsert(typeof(T), inMListTable: false);

                Executor.BulkCopy(dt, t.Name, copyOptions, timeout);

                foreach (var item in list)
                {
                    item.SetNotModified();
                }

                return(tr.Commit(list.Count));
            }
        }
Exemple #7
0
        public static void Save(Entity[] entities)
        {
            if (entities == null || entities.Any(e => e == null))
            {
                throw new ArgumentNullException("entity");
            }

            using (var log = HeavyProfiler.LogNoStackTrace("PreSaving"))
            {
                Schema schema = Schema.Current;
                DirectedGraph <Modifiable> modifiables = GraphExplorer.PreSaving(() => GraphExplorer.FromRoots(entities), (Modifiable m, ref bool graphModified) =>
                {
                    ModifiableEntity me = m as ModifiableEntity;

                    if (me != null)
                    {
                        me.SetTemporalErrors(null);
                    }

                    m.PreSaving(ref graphModified);

                    Entity ident = m as Entity;

                    if (ident != null)
                    {
                        schema.OnPreSaving(ident, ref graphModified);
                    }
                });

                HashSet <Entity> wasNew          = modifiables.OfType <Entity>().Where(a => a.IsNew).ToHashSet();
                HashSet <Entity> wasSelfModified = modifiables.OfType <Entity>().Where(a => a.Modified == ModifiedState.SelfModified).ToHashSet();

                log.Switch("Integrity");

                var error = GraphExplorer.FullIntegrityCheck(modifiables);
                if (error != null)
                {
                    throw new IntegrityCheckException(error);
                }

                log.Switch("Graph");

                GraphExplorer.PropagateModifications(modifiables.Inverse());

                //colapsa modifiables (collections and embeddeds) keeping indentifiables only
                DirectedGraph <Entity> identifiables = GraphExplorer.ColapseIdentifiables(modifiables);

                foreach (var node in identifiables)
                {
                    schema.OnSaving(node);
                }

                //Remove all the edges that doesn't mean a dependency
                identifiables.RemoveEdges(identifiables.Edges.Where(e => !e.To.IsNew).ToList());

                //Remove all the nodes that are not modified
                List <Entity> notModified = identifiables.Where(node => !node.IsGraphModified).ToList();

                notModified.ForEach(node => identifiables.RemoveFullNode(node, None));

                log.Switch("SaveGroups");

                SaveGraph(schema, identifiables);

                foreach (var node in identifiables)
                {
                    schema.OnSaved(node, new SavedEventArgs
                    {
                        IsRoot          = entities.Contains(node),
                        WasNew          = wasNew.Contains(node),
                        WasSelfModified = wasSelfModified.Contains(node),
                    });
                }

                EntityCache.Add(identifiables);
                EntityCache.Add(notModified);

                GraphExplorer.CleanModifications(modifiables);
            }
        }