Exemplo n.º 1
0
        public async Task <SaveAllLogicEditor> SaveAll([FromBody] SaveAllLogicEditor data)
        {
            await using var dbContextNodeInstances = new AutomaticaContext(_config);

            var nodeInstanceController = new NodeInstanceController(dbContextNodeInstances, _notifyDriver,
                                                                    _nodeInstanceCache, _coreServer);

            var pages = await Save(data.LogicPages);

            await _coreServer.ReInit();

            return(new SaveAllLogicEditor
            {
                LogicPages = pages.ToList(),
                NodeInstances = nodeInstanceController.Get().ToList()
            });
        }
Exemplo n.º 2
0
        public async Task <SaveAllLogicEditor> SaveAll([FromBody] SaveAllLogicEditor data)
        {
            IEnumerable <NodeInstance> nodeInstancesSaved = null;

            await using (var dbContextNodeInstances = new AutomaticaContext(_config))
            {
                var nodeInstanceController = new NodeInstanceController(dbContextNodeInstances, _notifyDriver, _nodeInstanceCache, _coreServer);
                nodeInstancesSaved = await nodeInstanceController.Save(data.NodeInstances, false);
            }

            var pages = await Save(data.LogicPages);

            await _coreServer.ReInit();

            return(new SaveAllLogicEditor
            {
                LogicPages = pages.ToList(),
                NodeInstances = nodeInstancesSaved.ToList()
            });
        }
        public async Task <IEnumerable <NodeInstance> > Save([FromBody] List <NodeInstance> nodeInstances, bool reInit = true)
        {
            SystemLogger.Instance.LogDebug($"Begin NodeInstance save...");
            var transaction = DbContext.Database.BeginTransaction();

            try
            {
                SystemLogger.Instance.LogDebug("Start checking node instances...");
                var dbEntries = DbContext.NodeInstances.AsNoTracking();
                foreach (var node in nodeInstances)
                {
                    node.This2ParentNodeInstance = null; //set root parent always to null -> if we are a slave server the parentid could be set
                    var state = await SetNodeInstanceState(node, null, dbEntries.ToDictionary(a => a.ObjId, a => a));

                    SystemLogger.Instance.LogDebug($"NodeInstance State Added Nodes {state.AddedNodeInstances.Count}, UpdatedNodes {state.UpdatedNodeInstances.Count}. AddedProperties: {state.AddedPropertyInstances.Count}, UpdatedProperties: {state.UpdatedPropertyInstances.Count}");
                    SystemLogger.Instance.LogDebug("Start add/updating entities...done");
                    DbContext.NodeInstances.AddRange(state.AddedNodeInstances);
                    DbContext.NodeInstances.UpdateRange(state.UpdatedNodeInstances);
                    DbContext.PropertyInstances.AddRange(state.AddedPropertyInstances);
                    DbContext.PropertyInstances.UpdateRange(state.UpdatedPropertyInstances);

                    SystemLogger.Instance.LogDebug("Start add/updating entities...done");
                }
                SystemLogger.Instance.LogDebug("Start checking node instances...done");
                SystemLogger.Instance.LogDebug("Start checking deleted items...");

                var flatList = nodeInstances.Flatten(a => a.InverseThis2ParentNodeInstanceNavigation).ToList();

                var removedNodes = (from c in DbContext.NodeInstances
                                    where !(from o in flatList select o.ObjId).Contains(c.ObjId)
                                    select c).ToList();

                if (_notifyDriver != null)
                {
                    foreach (var removed in removedNodes)
                    {
                        try
                        {
                            await _notifyDriver.NotifyDeleted(removed);
                        }
                        catch (Exception e)
                        {
                            SystemLogger.Instance.LogError(e, "Could not call notify save");
                        }
                    }
                }

                SystemLogger.Instance.LogDebug($"Found {removedNodes.Count} items to delete...");
                DbContext.RemoveRange(removedNodes);
                SystemLogger.Instance.LogDebug("Start checking deleted items...done");

                var settings = DbContext.Settings.SingleOrDefault(a => a.ValueKey == ServerInfo.DbConfigVersionKey);
                if (settings != null)
                {
                    settings.ValueInt++;
                    ServerInfo.DbConfigVersion = settings.ValueInt.GetValueOrDefault();

                    DbContext.Settings.Update(settings);
                }
                SystemLogger.Instance.LogDebug("Save and Commit changes");
                DbContext.SaveChanges();
                transaction.Commit();
                _nodeInstanceCache.Clear();
                SystemLogger.Instance.LogDebug("Save and Commit changes...done");
            }
            catch (Exception e)
            {
                transaction.Rollback();
                SystemLogger.Instance.LogError(e, "Could not save nodeInstances", e);
                throw;
            }

            SystemLogger.Instance.LogDebug($"Begin NodeInstance save...done");


            if (reInit)
            {
                SystemLogger.Instance.LogDebug($"Begin NodeInstance re-init...");
                await _server.ReInit();

                SystemLogger.Instance.LogDebug($"Begin NodeInstance re-init...done");
            }

            return(Get());
        }