/// <summary> /// Creates a new folder under the specified parent id. /// </summary> /// <param name="parentId">The parent id.</param> /// <returns>The ID of the created folder.</returns> public Guid CreateFolder(Guid?parentId) { // Create a default folder, which is valid if the parent is null var folder = new ConfigurationTreeFolder(SequentialGuid.NewGuid(), "New Folder", "ScenarioFolder", parentId); // Determine the real folder type and parent if (parentId != null) { EnterpriseTestMapNode node = _databaseMap[(Guid)parentId]; switch (node.NodeType) { case ConfigurationObjectType.EnterpriseScenario: case ConfigurationObjectType.ResourceFolder: folder.FolderType = "ResourceFolder"; break; case ConfigurationObjectType.VirtualResource: case ConfigurationObjectType.MetadataFolder: folder.FolderType = "MetadataFolder"; break; } } using (EnterpriseTestContext context = new EnterpriseTestContext()) { context.ConfigurationTreeFolders.AddObject(folder); HandleObjectChange(context, folder); context.SaveChanges(); } return(folder.ConfigurationTreeFolderId); }
/// <summary> /// Gets the entity object. /// </summary> /// <param name="id">The id.</param> /// <returns></returns> public EntityObject GetEntityObject(Guid id) { // Discard our old entity context and create a new one. This seems wasteful, // but if we maintain the same context it will cache all the data that is ever // loaded from it. This can cause huge memory growth over time, as well as // concurrency issues. Creating a new context is cheap and avoids these issues. if (_context != null) { _context.Dispose(); } _context = new EnterpriseTestContext(); EnterpriseTestMapNode node = _databaseMap[id]; switch (node.NodeType) { case ConfigurationObjectType.EnterpriseScenario: return(EnterpriseScenario.Select(_context, id)); case ConfigurationObjectType.VirtualResource: return(VirtualResource.Select(_context, id)); case ConfigurationObjectType.ResourceMetadata: return(VirtualResourceMetadata.Select(_context, id)); case ConfigurationObjectType.ScenarioFolder: case ConfigurationObjectType.ResourceFolder: case ConfigurationObjectType.MetadataFolder: return(ConfigurationTreeFolder.Select(_context, id)); default: return(null); } }
/// <summary> /// Creates a copy of a Resource. /// </summary> /// <param name="source">The Source Resource to copy from</param> /// <param name="srcParent"></param> /// <param name="newParent"></param> /// <param name="Mappings"></param> /// <returns>Guid of the Resource node that was created</returns> public Guid CopyResource(Guid source, Guid srcParent, Guid newParent, Dictionary <Guid?, Guid?> Mappings) { using (EnterpriseTestContext ctx = new EnterpriseTestContext()) { VirtualResource vrSource = VirtualResource.Select(ctx, source); VirtualResource vrCopy = vrSource.ShallowCopy(); HandleObjectChange(ctx, vrCopy); ctx.SaveChanges(); EnterpriseScenario dstScenario; if (vrSource.EnterpriseScenarioId == srcParent) { dstScenario = EnterpriseScenario.Select(ctx, newParent); } else { try { vrCopy.FolderId = (Guid)Mappings[srcParent]; dstScenario = EnterpriseScenario.Select(ctx, (Guid)Mappings[vrSource.EnterpriseScenarioId]); } catch (Exception) { throw new Exception("Cannot find New Target Scenario /Resource Folder for " + source.ToString()); } } dstScenario.VirtualResources.Add(vrCopy); HandleObjectChange(ctx, vrCopy); HandleObjectChange(ctx, dstScenario); ctx.SaveChanges(); return(vrCopy.VirtualResourceId); } }
/// <summary> /// Creates a copy of an activity (Virtual Resource metadata) /// </summary> /// <param name="source">The source activity to copy from</param> /// <param name="srcParent"></param> /// <param name="newParent"></param> /// <param name="Mappings"></param> /// <param name="isPartOfResourceCopy"></param> /// <returns>Guid of the Activity (metadata) node that was created</returns> public Guid CopyActivity(Guid source, Guid srcParent, Guid newParent, Dictionary <Guid?, Guid?> Mappings, bool isPartOfResourceCopy) { using (EnterpriseTestContext ctx = new EnterpriseTestContext()) { VirtualResourceMetadata vrmdSource = VirtualResourceMetadata.Select(ctx, source); VirtualResourceMetadata vrmdCopy = vrmdSource.Copy(isPartOfResourceCopy); vrmdCopy.Name = vrmdSource.Name; VirtualResource dstVR; HandleObjectChange(ctx, vrmdCopy); ctx.SaveChanges(); if (vrmdSource.VirtualResourceId == srcParent) { dstVR = VirtualResource.Select(ctx, newParent); } else { try { vrmdCopy.FolderId = (Guid)Mappings[srcParent]; dstVR = VirtualResource.Select(ctx, (Guid)Mappings[vrmdSource.VirtualResourceId]); } catch (Exception) { throw new Exception("Cannot find New Target VR / MetaData Folder for " + source.ToString()); } } dstVR.VirtualResourceMetadataSet.Add(vrmdCopy); HandleObjectChange(ctx, vrmdCopy); HandleObjectChange(ctx, dstVR); ctx.SaveChanges(); return(vrmdCopy.VirtualResourceMetadataId); } }
/// <summary> /// Discards all changes. /// </summary> public void DiscardChanges() { if (_context != null) { _context.Dispose(); _context = null; } }
/// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> public void Dispose() { if (_context != null) { _context.Dispose(); _context = null; } }
/// <summary> /// Creates a new scenario under the specified parent id. /// </summary> /// <param name="folderId">The folder id.</param> /// <returns>The ID of the created scenario.</returns> public Guid CreateScenario(Guid?folderId) { // Create a new scenario EnterpriseScenario scenario = new EnterpriseScenario(); scenario.FolderId = folderId; scenario.Owner = UserManager.CurrentUserName; using (EnterpriseTestContext context = new EnterpriseTestContext()) { scenario.AddGroups(context, scenario.Owner); context.EnterpriseScenarios.AddObject(scenario); HandleObjectChange(context, scenario); context.SaveChanges(); } return(scenario.EnterpriseScenarioId); }
/// <summary> /// Creates a Copy of a Folder node. Can make an exact copy or one with a date/time stamp appended. /// </summary> /// <param name="source">Fodler to copy</param> /// <param name="parent">Parent node for the copy</param> /// <returns>Guid of the folder node that was created</returns> public Guid CopyFolder(Guid source, Guid?parent) { using (EnterpriseTestContext ctx = new EnterpriseTestContext()) { ConfigurationTreeFolder srcFolder = ConfigurationTreeFolder.Select(ctx, source); Guid dst = CreateFolder(parent); ConfigurationTreeFolder dstFolder = ConfigurationTreeFolder.Select(ctx, dst); Rename(dst, srcFolder.Name); HandleObjectChange(ctx, dstFolder); if (parent != null) { HandleObjectChange(ctx, GetEntityObject((Guid)parent)); } ctx.SaveChanges(); return(dst); } }
/// <summary> /// Creates a copy of a scenario. Can make an exact copy, or one that has the name appendeed with a time/date stamp /// </summary> /// <param name="source">The Source scenario Id</param> /// <param name="parent">Parent if available</param> /// <returns>Guid of the Scenario node that was created</returns> public Guid CopyScenario(Guid source, Guid?parent) { using (EnterpriseTestContext ctx = new EnterpriseTestContext()) { var srcScenario = EnterpriseScenario.Select(ctx, source); Guid dst = CreateScenario(parent); EnterpriseScenario dstScenario = EnterpriseScenario.Select(ctx, dst); dstScenario.Name = srcScenario.Name; dstScenario.Description = srcScenario.Description; dstScenario.Vertical = srcScenario.Vertical; dstScenario.Company = srcScenario.Company; HandleObjectChange(ctx, dstScenario); if (parent != null) { HandleObjectChange(ctx, GetEntityObject((Guid)parent)); } ctx.SaveChanges(); return(dst); } }
/// <summary> /// Enables/disable the specified object. /// </summary> /// <param name="id">The id.</param> /// <param name="enabled">Whether the object should be enabled.</param> public void EnableDisable(Guid id, bool enabled) { using (EnterpriseTestContext context = new EnterpriseTestContext()) { EnterpriseTestMapNode node = _databaseMap[id]; switch (node.NodeType) { case ConfigurationObjectType.VirtualResource: var resource = VirtualResource.Select(context, id); resource.Enabled = enabled; HandleObjectChange(context, resource); break; case ConfigurationObjectType.ResourceMetadata: var metadata = VirtualResourceMetadata.Select(context, id); metadata.Enabled = enabled; HandleObjectChange(context, metadata); break; } context.SaveChanges(); } }
private void MoveHandler(EnterpriseTestContext context, IEnumerable <Guid> affected) { foreach (Guid affectedId in affected) { EnterpriseTestMapNode moved = _databaseMap[affectedId]; switch (moved.NodeType) { case ConfigurationObjectType.EnterpriseScenario: var scenario = EnterpriseScenario.Select(context, affectedId); scenario.FolderId = moved.FolderId; HandleObjectChange(context, scenario); break; case ConfigurationObjectType.VirtualResource: var resource = VirtualResource.Select(context, affectedId); resource.EnterpriseScenarioId = (Guid)moved.ParentId; resource.FolderId = moved.FolderId; HandleObjectChange(context, resource); break; case ConfigurationObjectType.ResourceMetadata: var metadata = VirtualResourceMetadata.Select(context, affectedId); metadata.VirtualResourceId = (Guid)moved.ParentId; metadata.FolderId = moved.FolderId; HandleObjectChange(context, metadata); break; case ConfigurationObjectType.ScenarioFolder: case ConfigurationObjectType.ResourceFolder: case ConfigurationObjectType.MetadataFolder: var folder = ConfigurationTreeFolder.Select(context, affectedId); folder.ParentId = moved.ParentId; HandleObjectChange(context, folder); break; } } context.SaveChanges(); }
private static void LoadFolders(EnterpriseTestContext context, List <EnterpriseTestMapNode> nodes) { List <ConfigurationTreeFolder> remainingFolders = ConfigurationTreeFolder.Select(context).ToList(); List <ConfigurationTreeFolder> foldersToAdd = new List <ConfigurationTreeFolder>(); int lastCount = -1; // This process must be iterative, since folders can contain other folders. // Keep looping over the folders until none of them got processed - then we're done while (remainingFolders.Count != lastCount) { lastCount = remainingFolders.Count; // For all of the remaining folders, add any that we know we can add now foreach (ConfigurationTreeFolder folder in remainingFolders) { // If something needs to go in this folder, add it if (nodes.Any(n => n.ParentId == folder.ConfigurationTreeFolderId || n.FolderId == folder.ConfigurationTreeFolderId)) { foldersToAdd.Add(folder); } // If this is not a scenario folder, but its parent exists, add it else if (folder.FolderType != "ScenarioFolder" && nodes.Any(n => n.Id == folder.ParentId)) { foldersToAdd.Add(folder); } } // Add nodes for all of the folders to add, then remove them from the remaining folder list foreach (ConfigurationTreeFolder folder in foldersToAdd) { nodes.Add(new EnterpriseTestMapNode(folder)); remainingFolders.Remove(folder); } foldersToAdd.Clear(); } }
/// <summary> /// Moves the object with the specified id to the specified parent. /// </summary> /// <param name="nodeId">The id.</param> /// <param name="targetId">The target parent id.</param> public void Move(Guid nodeId, Guid?targetId) { // Get the map node that this id corresponds to and update it EnterpriseTestMapNode node = _databaseMap[nodeId]; var affected = _databaseMap.GetMoveAffectedNodes(node); _databaseMap.Move(node, targetId); // Update all database objects that were affected // Use the local context if it is available - this fixes an issue where the parent id changes // but the cached copy in the local context does not get updated, which causes a crash when the object is committed if (_context != null) { MoveHandler(_context, affected); } else { using (EnterpriseTestContext context = new EnterpriseTestContext()) { MoveHandler(context, affected); } } }
/// <summary> /// Selects all the print queues currently being used in activities for the given server. /// </summary> /// <param name="server">The server.</param> /// <returns></returns> public SortableBindingList <PrintQueueInUse> SelectQueuesInUse(FrameworkServer server) { if (server == null) { throw new ArgumentNullException("server"); } SortableBindingList <PrintQueueInUse> queuesInUse = new SortableBindingList <PrintQueueInUse>(); using (EnterpriseTestContext context = new EnterpriseTestContext()) { foreach (RemotePrintQueue printQueue in _context.RemotePrintQueues.Where(n => n.PrintServerId == server.FrameworkServerId)) { foreach (PrintQueueInUse printQueueInUse in GetQueueUsages(context, printQueue.RemotePrintQueueId.ToString())) { printQueueInUse.ServerName = server.HostName; printQueueInUse.QueueName = printQueue.Name; queuesInUse.Add(printQueueInUse); } } } return(queuesInUse); }
/// <summary> /// Renames the object with the specified id. /// </summary> /// <param name="id">The id.</param> /// <param name="name">The name.</param> public void Rename(Guid id, string name) { using (EnterpriseTestContext context = new EnterpriseTestContext()) { EnterpriseTestMapNode node = _databaseMap[id]; switch (node.NodeType) { case ConfigurationObjectType.EnterpriseScenario: var scenario = EnterpriseScenario.Select(context, id); scenario.Name = name; HandleObjectChange(context, scenario); break; case ConfigurationObjectType.VirtualResource: var resource = VirtualResource.Select(context, id); resource.Name = name; HandleObjectChange(context, resource); break; case ConfigurationObjectType.ResourceMetadata: var metadata = VirtualResourceMetadata.Select(context, id); metadata.Name = name; HandleObjectChange(context, metadata); break; case ConfigurationObjectType.ScenarioFolder: case ConfigurationObjectType.ResourceFolder: case ConfigurationObjectType.MetadataFolder: var folder = ConfigurationTreeFolder.Select(context, id); folder.Name = name; HandleObjectChange(context, folder); break; } context.SaveChanges(); } }
private static List <EnterpriseTestMapNode> LoadNodesFromDatabase(UserCredential user) { List <EnterpriseTestMapNode> nodes = new List <EnterpriseTestMapNode>(); bool needFilter = (user != null && !user.HasPrivilege(UserRole.Manager)); string sqlText = needFilter ? Resource.SelectTreeViewData.FormatWith(user.UserName) : Resource.SelectTreeViewDataNoFilter; using (EnterpriseTestContext context = new EnterpriseTestContext()) { Collection <ScenarioData> scenarioData = new Collection <ScenarioData>(); using (SqlAdapter adapter = new SqlAdapter(EnterpriseTestSqlConnection.ConnectionString)) { DbDataReader reader = adapter.ExecuteReader(sqlText); if (reader != null) { while (reader.Read()) { scenarioData.Add(new ScenarioData() { MetadataEnabled = !string.IsNullOrEmpty(reader["MDE"].ToString()) && (bool)reader["MDE"], MetadataFolderId = !string.IsNullOrEmpty(reader["MDFID"].ToString()) ? (Guid?)reader["MDFID"] : (Guid?)null, MetadataId = !string.IsNullOrEmpty(reader["MDID"].ToString()) ? (Guid)reader["MDID"] : Guid.Empty, MetadataName = !string.IsNullOrEmpty(reader["MDN"].ToString()) ? (string)reader["MDN"]: string.Empty, MetadataType = !string.IsNullOrEmpty(reader["MDT"].ToString()) ? (string)reader["MDT"] : string.Empty, ResourceEnabled = !string.IsNullOrEmpty(reader["VRE"].ToString()) && (bool)reader["VRE"], ResourceFolderId = !string.IsNullOrEmpty(reader["VRFID"].ToString()) ? (Guid?)reader["VRFID"] : (Guid?)null, ResourceId = !string.IsNullOrEmpty(reader["VRID"].ToString()) ? (Guid)reader["VRID"] : Guid.Empty, ResourceName = !string.IsNullOrEmpty(reader["VRN"].ToString()) ? (string)reader["VRN"] : string.Empty, ResourceType = !string.IsNullOrEmpty(reader["VRT"].ToString()) ? (string)reader["VRT"] : string.Empty, ScenarioFolderId = !string.IsNullOrEmpty(reader["ESFID"].ToString()) ? (Guid?)reader["ESFID"] : (Guid?)null, ScenarioId = (Guid)reader["ESID"], ScenarioName = (string)reader["ESN"], }); } } } nodes.AddRange (scenarioData .Where(x => x.ScenarioId != (Guid?)null && x.ScenarioId != Guid.Empty) .GroupBy(x => x.ScenarioId) .Select(x => new EnterpriseTestMapNode(x.First().ScenarioId) { NodeType = ConfigurationObjectType.EnterpriseScenario, Name = x.First().ScenarioName, FolderId = x.First().ScenarioFolderId }) ); nodes.AddRange (scenarioData .Where(x => x.ResourceId != (Guid?)null && x.ResourceId != Guid.Empty) .GroupBy(x => x.ResourceId) .Select(x => new EnterpriseTestMapNode(x.First().ResourceId) { NodeType = ConfigurationObjectType.VirtualResource, Name = x.First().ResourceName, FolderId = x.First().ResourceFolderId, ParentId = x.First().ScenarioId, Enabled = x.First().ResourceEnabled, ResourceType = x.First().ResourceType }) ); nodes.AddRange (scenarioData .Where(x => x.MetadataId != (Guid?)null && x.MetadataId != Guid.Empty) .GroupBy(x => x.MetadataId) .Select(x => new EnterpriseTestMapNode(x.First().MetadataId) { NodeType = ConfigurationObjectType.ResourceMetadata, Name = x.First().MetadataName, FolderId = x.First().MetadataFolderId, ParentId = x.First().ResourceId, Enabled = x.First().MetadataEnabled, ResourceType = x.First().ResourceType, MetadataType = x.First().MetadataType }) ); // Load the folders based on the test objects that have already been loaded if (needFilter) { LoadFolders(context, nodes); } else { foreach (ConfigurationTreeFolder folder in ConfigurationTreeFolder.Select(context)) { nodes.Add(new EnterpriseTestMapNode(folder)); } } } return(nodes); }
private static void DatabaseDelete(IEnumerable <EnterpriseTestMapNode> nodes) { //-------------------------------------------------------------------------------------------------- // ADD: CR #1894. Cascading deletes were throwing an unhandled exception because of a DB Foreign // Key Constraint on the folderId field. What that means is that the ORDER in which things are // deleted, now becomes very important. If we attempt to remove a 'container' before we remove // the contents, the exception will be thrown when the contents are removed. So - in order to make // this work, we group the objects to be deleted - and then we have to order those groups (since the // order of the GroupBy could be non-deterministic) and make sure that we delete things in the // correct sequence. There is probably a more elegant way of doing this - but there was no time. using (EnterpriseTestContext context = new EnterpriseTestContext()) { int[] ordered = new int[6] { -1, -1, -1, -1, -1, -1 }; var colls = nodes.GroupBy(n => n.NodeType, n => n.Id); int ndx = 0; foreach (var grp in colls) { switch (grp.Key) { case ConfigurationObjectType.ResourceMetadata: ordered[0] = ndx++; break; case ConfigurationObjectType.MetadataFolder: ordered[1] = ndx++; break; case ConfigurationObjectType.VirtualResource: ordered[2] = ndx++; break; case ConfigurationObjectType.ResourceFolder: ordered[3] = ndx++; break; case ConfigurationObjectType.EnterpriseScenario: ordered[4] = ndx++; break; case ConfigurationObjectType.ScenarioFolder: ordered[5] = ndx++; break; } } for (ndx = 0; ndx < 6; ndx++) { if (ordered[ndx] >= 0) { switch (colls.ElementAt(ordered[ndx]).Key) { case ConfigurationObjectType.ResourceMetadata: foreach (var vrmd in VirtualResourceMetadata.Select(context, colls.ElementAt(ordered[ndx]))) { context.DeleteObject(vrmd); } break; case ConfigurationObjectType.MetadataFolder: foreach (var mdf in ConfigurationTreeFolder.Select(context, colls.ElementAt(ordered[ndx]))) { context.DeleteObject(mdf); } break; case ConfigurationObjectType.VirtualResource: foreach (var vr in VirtualResource.Select(context, colls.ElementAt(ordered[ndx]))) { context.DeleteObject(vr); } break; case ConfigurationObjectType.ResourceFolder: foreach (var rf in ConfigurationTreeFolder.Select(context, colls.ElementAt(ordered[ndx]))) { context.DeleteObject(rf); } break; case ConfigurationObjectType.EnterpriseScenario: foreach (var scenario in EnterpriseScenario.Select(context, colls.ElementAt(ordered[ndx]))) { context.DeleteObject(scenario); } break; case ConfigurationObjectType.ScenarioFolder: foreach (var sf in ConfigurationTreeFolder.Select(context, colls.ElementAt(ordered[ndx]))) { var deletingScenarioIds = nodes.Where(n => n.NodeType == ConfigurationObjectType.EnterpriseScenario).Select(n => n.Id); foreach (var folder in ConfigurationTreeFolder.Select(context, colls.ElementAt(ordered[ndx]))) { var containedScenarios = ConfigurationTreeFolder.ContainedScenarioIds(context, folder.ConfigurationTreeFolderId); if (containedScenarios.All(n => deletingScenarioIds.Contains(n))) { // All of the sceanrios under this folder are in the list of scenarios to delete, // so we can delete this folder as well. context.DeleteObject(folder); } } } break; } context.SaveChanges(); } } } }