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); }
private void ProcessFinish() { if (_completionControl.SelectedFolderId == Guid.Parse("00000000-0000-0000-0000-000000000000")) { MessageBox.Show("Please Select a destination folder", "Import Scenario", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } StringBuilder importMessage = new StringBuilder("The Scenario '"); importMessage.Append(_scenarioContract.Name); importMessage.AppendLine("' was successfully imported."); try { using (new BusyCursor()) { if (!_masterCompositeControl.Validate()) { return; } if (_compositeContract != null) { using (AssetInventoryContext context = DbConnect.AssetInventoryContext()) { bool changesMade = false; HashSet <Guid> pQueues = new HashSet <Guid>(); foreach (var printer in _compositeContract.Printers) { if (!context.Assets.Any(x => x.AssetId.Equals(printer.AssetId))) { context.Assets.Add(ContractFactory.Create(printer, context)); changesMade = true; } } var items = _compositeContract.Scenario.ActivityPrintQueueUsage; XmlDocument doc = new XmlDocument(); foreach (var item in items) { doc.LoadXml(item.XmlSelectionData); XmlNode node = doc.DocumentElement.FirstChild.FirstChild.FirstChild; if (!string.IsNullOrEmpty(node.InnerText) && IsGuid(node.InnerText)) { if (!pQueues.Contains(Guid.Parse(node.InnerText)) && node.FirstChild.FirstChild.Name == "_printQueueId") { pQueues.Add(Guid.Parse(node.InnerText)); } } } var containsQueues = context.RemotePrintQueues.Where(x => pQueues.Contains(x.RemotePrintQueueId)).Select(x => x.Name); if (pQueues.Count() != containsQueues.Count() || containsQueues.Count() == 0) { importMessage.AppendLine("Warning: Some Print Queues May Not Have Imported."); importMessage.AppendLine("Please use Bulk Edit Tool to resolve."); } if (changesMade) { context.SaveChanges(); } } } using (EnterpriseTestContext context = new EnterpriseTestContext()) { var importMaps = new List <ContractFactory.ImportMap>(); if (_finalScenarioEntity == null) { _finalScenarioEntity = ContractFactory.Create(_scenarioContract, out importMaps); } var emptyPlatforms = ( from r in _finalScenarioEntity.VirtualResources where (string.IsNullOrEmpty(r.Platform) || r.Platform.Equals("LOCAL")) && !r.ResourceType.Equals("SolutionTester") select r ); if (emptyPlatforms != null && emptyPlatforms.Count() > 0) { using (AssignPlatformDialog dialog = new AssignPlatformDialog(emptyPlatforms)) { dialog.ShowDialog(); return; } } _finalScenarioEntity.Owner = UserManager.CurrentUserName; foreach (var group in _scenarioContract.UserGroups) { var entity = context.UserGroups.FirstOrDefault(x => x.GroupName.Equals(group)); if (entity != null) { _finalScenarioEntity.UserGroups.Add(entity); } } _finalScenarioEntity.FolderId = _completionControl.SelectedFolderId; context.AddToEnterpriseScenarios(_finalScenarioEntity); CreateFolders(_scenarioContract, _finalScenarioEntity, context, importMaps); context.SaveChanges(); ScenarioImported = true; } if (_importMetadataMessages.Length > 0) { importMessage.AppendLine(_importMetadataMessages.ToString()); } MessageBox.Show(importMessage.ToString(), "Import Scenario", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message); TraceFactory.Logger.Error(ex); } }