/// <summary>
        /// Creates the folders within a scenario during import
        /// </summary>
        /// <param name="contract">The contract.</param>
        /// <param name="scenario">The scenario.</param>
        /// <param name="context">The context.</param>
        /// <param name="importMaps">The import maps.</param>
        private void CreateFolders(EnterpriseScenarioContract contract, EnterpriseScenario scenario, EnterpriseTestContext context, List <ContractFactory.ImportMap> importMaps)
        {
            try
            {
                foreach (var folder in contract.Folders)
                {
                    // create the folder with parent defaulted to scenario
                    var newFolderEntity = ConfigurationTreeFolder.CreateConfigurationTreeFolder(SequentialGuid.NewGuid(), folder.Name, folder.FolderType);
                    newFolderEntity.ParentId = scenario.EnterpriseScenarioId;
                    context.AddToConfigurationTreeFolders(newFolderEntity);

                    // set children for folder based on import mapping
                    var childMaps = (from c in folder.ChildIds
                                     join i in importMaps on c equals i.OldId
                                     select i);

                    if (childMaps.Any())
                    {
                        newFolderEntity.ParentId = childMaps.First().NewParentId;

                        switch (folder.FolderType)
                        {
                        case "ResourceFolder":
                            var vr = (from e in scenario.VirtualResources
                                      join c in childMaps on e.VirtualResourceId equals c.NewId
                                      select e).ToList();
                            vr.ForEach(x => x.FolderId = newFolderEntity.ConfigurationTreeFolderId);
                            break;

                        case "MetadataFolder":
                            var md = (from e in scenario.VirtualResources.SelectMany(x => x.VirtualResourceMetadataSet)
                                      join c in childMaps on e.VirtualResourceMetadataId equals c.NewId
                                      select e).ToList();
                            md.ForEach(x => x.FolderId = newFolderEntity.ConfigurationTreeFolderId);
                            break;

                        default:
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                TraceFactory.Logger.Error("Error creating folders within scenario", ex);
            }
        }
Example #2
0
        private bool ImportConfigurationFiles()
        {
            try
            {
                SystemTrace.Instance.Debug("Checking for import files");

                string[] importFiles = Directory.GetFiles("Import", "*.stbs");

                if (importFiles.Count() > 0)
                {
                    // Import any exported scenario files that were included in the installation
                    using (EnterpriseTestContext context = new EnterpriseTestContext())
                    {
                        UpdateStatus("Importing Scenarios");
                        ContractFactory.OnStatusChanged += ContractFactory_OnStatusChanged;

                        string folderName = $"V{_ticket.CurrentVersion} Imports";

                        ConfigurationTreeFolder folder = context.ConfigurationTreeFolders.FirstOrDefault(x => x.Name.Equals(folderName));
                        if (folder == null)
                        {
                            folder = new ConfigurationTreeFolder(SequentialGuid.NewGuid(), folderName, ConfigurationObjectType.ScenarioFolder.ToString(), null);
                            context.AddToConfigurationTreeFolders(folder);
                            context.SaveChanges();
                        }

                        //Ensure the path exists for import files in the shared folder location
                        StringBuilder importDestination = new StringBuilder(_fileSharePath);
                        importDestination.Append("\\Import\\V");
                        importDestination.Append(_ticket.CurrentVersion);

                        Directory.CreateDirectory(importDestination.ToString());

                        importDestination.Append("\\");
                        int destinationPathIndex = importDestination.Length;

                        foreach (string fileName in importFiles)
                        {
                            SystemTrace.Instance.Debug($"Importing {fileName}");
                            EnterpriseScenario enterpriseScenario = null;

                            try
                            {
                                XElement fileData = XElement.Parse(File.ReadAllText(fileName));

                                // If this is a composite contract file it may contain printer and document
                                // information in addition to the base scenario data.
                                if (fileData.Name.LocalName == "Composite")
                                {
                                    var compositeContract = Serializer.Deserialize <EnterpriseScenarioCompositeContract>(fileData);

                                    if (!ImportExportUtil.ProcessCompositeContractFile(compositeContract))
                                    {
                                        SystemTrace.Instance.Error($"Failed to process composite contract: {fileName}.");
                                    }

                                    enterpriseScenario = ContractFactory.Create(compositeContract.Scenario);
                                }
                                else
                                {
                                    var scenarioContract = Serializer.Deserialize <EnterpriseScenarioContract>(fileData);
                                    enterpriseScenario = ContractFactory.Create(scenarioContract);
                                }

                                enterpriseScenario.FolderId = folder.ConfigurationTreeFolderId;
                                SystemTrace.Instance.Debug($"Adding Scenario '{enterpriseScenario.Name}'");
                                context.AddToEnterpriseScenarios(enterpriseScenario);

                                // Copy the import file to the shared folder location
                                importDestination.Append(Path.GetFileName(fileName));
                                try
                                {
                                    File.Copy(fileName, importDestination.ToString(), true);
                                }
                                catch (Exception ex)
                                {
                                    SystemTrace.Instance.Error($"Failed to copy '{fileName}' to '{importDestination.ToString()}'.", ex);
                                }
                                importDestination.Remove(destinationPathIndex, importDestination.Length - destinationPathIndex);
                            }
                            catch (Exception ex)
                            {
                                // Log an error for the current file, but keep going
                                SystemTrace.Instance.Error($"Failed to import: {fileName}", ex);
                            }
                        }

                        context.SaveChanges();
                        ContractFactory.OnStatusChanged -= ContractFactory_OnStatusChanged;

                        UpdateStatus("Scenario Import Complete");
                    }
                }
            }
            catch (Exception ex)
            {
                SendError(ex);
                return(false);
            }

            return(true);
        }