public void LoadFolders()
        {
            saveToRadTreeView.Nodes.Clear();

            using (EnterpriseTestContext context = new EnterpriseTestContext())
            {
                var folders = ConfigurationTreeFolder.Select(context, "ScenarioFolder");
                foreach (ConfigurationTreeFolder folder in ConfigurationTreeFolder.SortHierarchical(folders))
                {
                    RadTreeNode node = new RadTreeNode(folder.Name);
                    node.Tag      = folder.ConfigurationTreeFolderId;
                    node.ImageKey = "Folder";

                    if (folder.ParentId == null)
                    {
                        saveToRadTreeView.Nodes.Add(node);
                    }
                    else
                    {
                        var temp = FindNode(folder.ParentId);
                        if (temp != null)
                        {
                            temp.Nodes.Add(node);
                        }
                        else
                        {
                            saveToRadTreeView.Nodes.Add(node);
                        }

                        //FindNode(folder.ParentId).Nodes.Add(node);
                    }
                }
            }
            FindSelectedNode();
        }
Esempio n. 2
0
        private void ScenarioSelectionForm_Load(object sender, EventArgs e)
        {
            bool noOrphans = true;

            using (EnterpriseTestContext context = new EnterpriseTestContext())
            {
                var folders = ConfigurationTreeFolder.Select(context, "ScenarioFolder");
                foreach (ConfigurationTreeFolder folder in ConfigurationTreeFolder.SortHierarchical(folders))
                {
                    RadTreeNode node = new RadTreeNode(folder.Name);
                    node.Tag      = folder.ConfigurationTreeFolderId;
                    node.ImageKey = "Folder";

                    noOrphans &= AddNode(node, folder.ParentId);
                }

                foreach (EnterpriseScenario scenario in context.EnterpriseScenarios)
                {
                    RadTreeNode node = new RadTreeNode(scenario.Name);
                    node.Tag      = scenario.EnterpriseScenarioId;
                    node.ImageKey = "Scenario";

                    noOrphans &= AddNode(node, scenario.FolderId);
                }
            }

            if (!noOrphans)
            {
                MessageBox.Show("One or more scenarios or folders did not load into the tree successfully.\n" +
                                "Please contact an administrator to determine which database items have incorrect configuration.",
                                "Load Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        /// <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);
            }
        }
        private void ScenarioSelection_Loaded(object sender, RoutedEventArgs e)
        {
            EnterpriseTestContext context = new EnterpriseTestContext();
            {
                var           folders = ConfigurationTreeFolder.Select(context, "ScenarioFolder").Where(x => x.ParentId == null);
                ScenarioModel model   = new ScenarioModel(folders.ToArray());

                ScenarioTree.DataContext = model;
            }
        }
        /// <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);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Adds the folder data to the specified scenario contract.
        /// </summary>
        /// <param name="contract">The scenario contract.</param>
        /// <param name="scenario">The scenario object.</param>
        private static void AddFolderDataToContract(EnterpriseScenarioContract contract, EnterpriseScenario scenario)
        {
            try
            {
                using (EnterpriseTestContext context = new EnterpriseTestContext())
                {
                    // get resource and metadata objects
                    IEnumerable <VirtualResource>         resources = scenario.VirtualResources.Where(v => v.FolderId.HasValue && v.FolderId.Value != Guid.Empty);
                    IEnumerable <VirtualResourceMetadata> metadatas = resources.SelectMany(v => v.VirtualResourceMetadataSet).Where(m => m.FolderId.HasValue && m.FolderId.Value != Guid.Empty);

                    // group each by the folder id
                    var groupResources = (from r in resources
                                          group r by r.FolderId into g
                                          select new { FolderId = g.Key.Value, ChildIds = g.Select(x => x.VirtualResourceId) });

                    var groupMetadata = (from m in metadatas
                                         group m by m.FolderId into g
                                         select new { FolderId = g.Key.Value, ChildIds = g.Select(z => z.VirtualResourceMetadataId) });

                    // get referenced folders
                    var allGroups = groupResources.Concat(groupMetadata);
                    var folderIds = allGroups.Select(x => x.FolderId);
                    IQueryable <ConfigurationTreeFolder> folders = ConfigurationTreeFolder.Select(context, folderIds);

                    // add contract data for each referenced folder
                    foreach (var group in allGroups)
                    {
                        ConfigurationTreeFolder folder = folders.FirstOrDefault(x => x.ConfigurationTreeFolderId.Equals(group.FolderId));
                        if (folder != null)
                        {
                            FolderContract folderContract = new FolderContract()
                            {
                                Name = folder.Name, FolderType = folder.FolderType, ChildIds = new Collection <Guid>(group.ChildIds.ToList())
                            };
                            contract.Folders.Add(folderContract);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                TraceFactory.Logger.Error("Error adding folder data.", ex);
            }
        }
Esempio n. 7
0
        private void ScenarioSelectionForm_Load(object sender, EventArgs e)
        {
            using (EnterpriseTestContext context = new EnterpriseTestContext())
            {
                var folders = ConfigurationTreeFolder.Select(context, "ScenarioFolder");
                foreach (ConfigurationTreeFolder folder in ConfigurationTreeFolder.SortHierarchical(folders))
                {
                    RadTreeNode node = new RadTreeNode(folder.Name);
                    node.Tag      = folder.ConfigurationTreeFolderId;
                    node.ImageKey = "Folder";

                    if (folder.ParentId == null)
                    {
                        scenarioTreeView.Nodes.Add(node);
                    }
                    else
                    {
                        FindNode(folder.ParentId).Nodes.Add(node);
                    }
                }

                foreach (EnterpriseScenario scenario in context.EnterpriseScenarios)
                {
                    RadTreeNode node = new RadTreeNode(scenario.Name);
                    node.Tag      = scenario.EnterpriseScenarioId;
                    node.ImageKey = "Scenario";

                    if (scenario.FolderId == null)
                    {
                        scenarioTreeView.Nodes.Add(node);
                    }
                    else
                    {
                        FindNode(scenario.FolderId).Nodes.Add(node);
                    }
                }
            }
        }
        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();
        }
Esempio n. 9
0
        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>
        /// 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 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();
                    }
                }
            }
        }
Esempio n. 12
0
        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);
        }