public async Task DeleteNode([FromBody] NodeInstance node) { var transaction = DbContext.Database.BeginTransaction(); try { var existingNode = DbContext.NodeInstances.AsNoTracking().SingleOrDefault(a => a.ObjId == node.ObjId); if (existingNode != null) { DbContext.Entry(existingNode).State = EntityState.Deleted; } await DbContext.SaveChangesAsync(); await transaction.CommitAsync(); try { var rootNode = _nodeInstanceCache.GetDriverNodeInstanceFromChild(node); if (rootNode.ObjId == node.ObjId) { var driver = _driverNodeStore.GetDriver(rootNode.ObjId); await _coreServer.StopDriver(driver); } else { await _notifyDriver.NotifyDeleted(node); } } catch (Exception e) { SystemLogger.Instance.LogError(e, $"Error stopping driver {node.Name}..."); } _nodeInstanceCache.Clear(); } catch (Exception e) { transaction.Rollback(); SystemLogger.Instance.LogError(e, $"Could not {nameof(DeleteNode)} {nameof(NodeInstance)}", e); throw; } }
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()); }