CP-33892: Audited the use of First() and replaced with FirstOrDefault() where appropriate.

Signed-off-by: Konstantina Chremmou <konstantina.chremmou@citrix.com>
This commit is contained in:
Konstantina Chremmou 2020-06-20 14:35:25 +01:00 committed by Mihaela Stoica
parent c14184919d
commit f3c5e74dae
11 changed files with 41 additions and 93 deletions

View File

@ -87,7 +87,7 @@ namespace XenAdmin.Actions.OVFActions
m_shouldVerify = shouldVerify;
if (m_vmsToExport.Count == 1)
VM = m_vmsToExport.First();
VM = m_vmsToExport[0];
}
protected override XenOvfTransportBase TransportAction => m_transportAction;

View File

@ -55,16 +55,6 @@ namespace XenAdmin.Commands
{
}
public DestroyHostCommand(IMainWindow mainWindow, Host host)
: base(mainWindow, host)
{
}
public DestroyHostCommand(IMainWindow mainWindow, IEnumerable<Host> hosts)
: base(mainWindow, ConvertToSelection<Host>(hosts))
{
}
protected override void ExecuteCore(SelectedItemCollection selection)
{
List<AsyncAction> actions = new List<AsyncAction>();
@ -81,10 +71,9 @@ namespace XenAdmin.Commands
private static bool CanExecute(Host host)
{
if (host == null || host.Connection == null)
{
if (host?.Connection == null)
return false;
}
Pool pool = Helpers.GetPool(host.Connection);
return pool != null && !Helpers.HostIsMaster(host) && !host.IsLive();
}
@ -92,28 +81,14 @@ namespace XenAdmin.Commands
protected override bool CanExecuteCore(SelectedItemCollection selection)
{
if (!selection.AllItemsAre<Host>() || selection.Count > 1)
{
return false;
}
return CanExecute(selection.AsXenObjects<Host>().First());
return CanExecute(selection.AsXenObjects<Host>().FirstOrDefault());
}
public override string ContextMenuText
{
get
{
return Messages.DESTROY_HOST_CONTEXT_MENU_ITEM_TEXT;
}
}
public override string ContextMenuText => Messages.DESTROY_HOST_CONTEXT_MENU_ITEM_TEXT;
protected override bool ConfirmationRequired
{
get
{
return true;
}
}
protected override bool ConfirmationRequired => true;
protected override string ConfirmationDialogText
{
@ -123,7 +98,6 @@ namespace XenAdmin.Commands
if (selection.Count > 0)
{
Host host = (Host)selection[0].XenObject;
Pool pool = Helpers.GetPool(host.Connection);
return string.Format(Messages.CONFIRM_DESTROY_HOST, host.Name());
}
@ -131,28 +105,10 @@ namespace XenAdmin.Commands
}
}
protected override string ConfirmationDialogTitle
{
get
{
return Messages.CONFIRM_DESTROY_HOST_TITLE;
}
}
protected override string ConfirmationDialogTitle => Messages.CONFIRM_DESTROY_HOST_TITLE;
protected override string ConfirmationDialogYesButtonLabel
{
get
{
return Messages.CONFIRM_DESTROY_HOST_YES_BUTTON_LABEL;
}
}
protected override string ConfirmationDialogYesButtonLabel => Messages.CONFIRM_DESTROY_HOST_YES_BUTTON_LABEL;
protected override bool ConfirmationDialogNoButtonSelected
{
get
{
return true;
}
}
protected override bool ConfirmationDialogNoButtonSelected => true;
}
}

View File

@ -31,7 +31,6 @@
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using XenAdmin.Core;
using XenAdmin.Dialogs;
@ -57,9 +56,9 @@ namespace XenAdmin.Commands
protected override void ExecuteCore(SelectedItemCollection selection)
{
var host = selection.AsXenObjects<Host>().First();
var host = selection.AsXenObjects<Host>().FirstOrDefault();
if (!host.Connection.Session.IsLocalSuperuser && !Registry.DontSudo &&
if (host != null && !host.Connection.Session.IsLocalSuperuser && !Registry.DontSudo &&
host.Connection.Session.Roles.All(r => r.name_label != Role.MR_ROLE_POOL_ADMIN))
{
var currentRoles = host.Connection.Session.Roles;
@ -82,7 +81,7 @@ namespace XenAdmin.Commands
if (!selection.AllItemsAre<Host>() || selection.Count > 1)
return false;
var host = selection.AsXenObjects<Host>().First();
var host = selection.AsXenObjects<Host>().FirstOrDefault();
if (host == null)
return false;

View File

@ -84,12 +84,7 @@ namespace XenAdmin.Commands
{
}
/// <summary>
/// Attempts to install tools on the vm
/// </summary>
/// <param name="vm"></param>
/// <returns>null if user cancels or an AsyncAction. This is either the InstallPVToolsAction or the CreateCdDriveAction if the VM needed a DVD drive.</returns>
private void SingleVMExecute(VM vm)
private void InstallToolsOnOneVm(VM vm)
{
if (vm.FindVMCDROM() == null)
{
@ -135,12 +130,7 @@ namespace XenAdmin.Commands
}
}
/// <summary>
/// Attempts to install tools on several VMs
/// </summary>
/// <param name="vms"></param>
/// <returns>Whether the action was launched (i.e., the user didn't Cancel)</returns>
private void MultipleVMExecute(List<VM> vms)
private void InstallToolsOnManyVms(List<VM> vms)
{
bool newDvdDrivesRequired = false;
foreach (VM vm in vms)
@ -276,9 +266,9 @@ namespace XenAdmin.Commands
List<VM> vms = selection.AsXenObjects<VM>(CanExecute);
if (vms.Count == 1)
SingleVMExecute(vms[0]);
InstallToolsOnOneVm(vms[0]);
else
MultipleVMExecute(vms);
InstallToolsOnManyVms(vms);
}
public static bool CanExecute(VM vm)

View File

@ -49,7 +49,6 @@ namespace XenAdmin.Dialogs
/// </summary>
public AssignLicenseDialog(IEnumerable<IXenObject> xos, String firstHost, String firstPort, Host.Edition firstEdition)
{
Util.ThrowIfEnumerableParameterNullOrEmpty(xos, "XenObjects");
this.xos = new List<IXenObject>(xos);
this.currentEdition = firstEdition;
InitializeComponent();

View File

@ -150,14 +150,13 @@ namespace XenAdmin.Dialogs
private void SummariseDisconnectedRows(List<CheckableDataGridViewRow> rowsChecked)
{
//Refresh current row's details if the pool/host is no longer connected
CheckableDataGridViewRow row = rowsChecked.Find(r => r.Highlighted && !r.XenObject.Connection.IsConnected);
CheckableDataGridViewRow row = rowsChecked.FirstOrDefault(r => r.Highlighted && !r.XenObject.Connection.IsConnected);
if (row != null)
SummariseSelectedRow(row);
}
public void AssignLicense(List<CheckableDataGridViewRow> rowsChecked)
{
if (rowsChecked.Any(r => !r.XenObject.Connection.IsConnected))
{
ShowPoolHostNotConnectedError();
@ -166,12 +165,14 @@ namespace XenAdmin.Dialogs
return;
}
List<LicenseDataGridViewRow> licenseRows = rowsChecked.ConvertAll(r => r as LicenseDataGridViewRow);
AssignLicenseDialog ald = new AssignLicenseDialog(licenseRows.ConvertAll(r=>r.XenObject),
licenseRows.First().LicenseServerAddress,
licenseRows.First().LicenseServerPort,
licenseRows.First().LicenseEdition);
ald.ShowDialog(View.Parent);
var licenseRows = rowsChecked.ConvertAll(r => r as LicenseDataGridViewRow);
var row = licenseRows.FirstOrDefault();
var xenObjects = licenseRows.ConvertAll(r => r.XenObject);
if (row != null && xenObjects.Count > 0)
using (var ald = new AssignLicenseDialog(xenObjects,
row.LicenseServerAddress, row.LicenseServerPort, row.LicenseEdition))
ald.ShowDialog(View.Parent);
SummariseDisconnectedRows(rowsChecked);
ResetButtonEnablement();

View File

@ -1891,12 +1891,12 @@ namespace XenAdmin
else
{
//If multiple items have been selected we count the number of the grouping tags in the selection
var selectedGroups = SelectionManager.Selection.Where(s => s.GroupingTag != null);
var selectedGroups = SelectionManager.Selection.Where(s => s.GroupingTag != null).ToList();
//if exactly one grouping tag has been selected we show the search view for that one tag, but only if all the other items in the selection belong to this group/tag
if (selectedGroups.Count() == 1)
if (selectedGroups.Count == 1)
{
var groupingTag = selectedGroups.First().GroupingTag;
var groupingTag = selectedGroups[0].GroupingTag;
if (SelectionManager.Selection.Where(s => s.GroupingTag == null).All(s => s.GroupAncestor == groupingTag))
gt = groupingTag;

View File

@ -95,9 +95,10 @@ namespace XenAdmin.Wizards.ExportWizard
if (!filename.EndsWith(".xva"))
filename += ".xva";
var vm = m_pageExportSelectVMs.VMsToExport.First();
var vm = m_pageExportSelectVMs.VMsToExport.FirstOrDefault();
new ExportVmAction(xenConnection, vm.Home(), vm, filename, m_pageFinish.VerifyExport).RunAsync();
if (vm != null)
new ExportVmAction(xenConnection, vm.Home(), vm, filename, m_pageFinish.VerifyExport).RunAsync();
}
else
{

View File

@ -266,8 +266,8 @@ namespace XenAdmin.Wizards.ImportWizard
private int FindSrItem(SR sr)
{
var existing = m_comboBoxISOLibraries.Items.OfType<ToStringWrapper<SR>>().Where(wrapper => sr.Equals(wrapper.item)).ToList();
return existing.Count > 0 ? m_comboBoxISOLibraries.Items.IndexOf(existing.First()) : -1;
var existing = m_comboBoxISOLibraries.Items.OfType<ToStringWrapper<SR>>().FirstOrDefault(wrapper => sr.Equals(wrapper.item));
return existing == null ? -1 : m_comboBoxISOLibraries.Items.IndexOf(existing);
}
private int AddReplaceSrItem(SR sr)

View File

@ -181,7 +181,9 @@ namespace XenAdmin.Actions
if (xo is Pool)
{
Pool.apply_edition(xo.Connection.Session, pool.opaque_ref, xo.Connection.Cache.Hosts.First().GetEditionText(_edition));
var firstHost = xo.Connection.Cache.Hosts.FirstOrDefault();
if (firstHost != null)
Pool.apply_edition(xo.Connection.Session, pool.opaque_ref, firstHost.GetEditionText(_edition));
}
Description = Messages.APPLYLICENSE_UPDATED;

View File

@ -44,11 +44,11 @@ namespace XenAdmin.Actions
private Dictionary<Host, VDI> suppPackVdis;
public InstallSupplementalPackAction(Dictionary<Host, VDI> suppPackVdis, bool suppressHistory)
: base(null,
suppPackVdis.Count > 1
? string.Format(Messages.UPDATES_WIZARD_APPLYING_UPDATE_MULTIPLE_HOSTS, suppPackVdis.Count)
: string.Format(Messages.UPDATES_WIZARD_APPLYING_UPDATE, suppPackVdis.First().Value, suppPackVdis.First().Key),
suppressHistory)
: base(null,
suppPackVdis.Count == 1
? string.Format(Messages.UPDATES_WIZARD_APPLYING_UPDATE, suppPackVdis.First().Value, suppPackVdis.First().Key)
: string.Format(Messages.UPDATES_WIZARD_APPLYING_UPDATE_MULTIPLE_HOSTS, suppPackVdis.Count),
suppressHistory)
{
this.suppPackVdis = suppPackVdis;
}