diff --git a/Src/Witsml/Extensions/WitsmlLogExtensions.cs b/Src/Witsml/Extensions/WitsmlLogExtensions.cs index 2cc601149..b5a8dd54f 100644 --- a/Src/Witsml/Extensions/WitsmlLogExtensions.cs +++ b/Src/Witsml/Extensions/WitsmlLogExtensions.cs @@ -8,14 +8,14 @@ public static string GetStartIndexAsString(this WitsmlLog witsmlLog) { return witsmlLog.StartIndex == null && string.IsNullOrEmpty(witsmlLog.StartDateTimeIndex) ? null - : witsmlLog.IndexType.Equals(WitsmlLog.WITSML_INDEX_TYPE_MD, System.StringComparison.Ordinal) ? witsmlLog.StartIndex != null ? witsmlLog.StartIndex.ToString() : "" : witsmlLog.StartDateTimeIndex; + : string.Equals(witsmlLog.IndexType, WitsmlLog.WITSML_INDEX_TYPE_MD, System.StringComparison.Ordinal) ? witsmlLog.StartIndex != null ? witsmlLog.StartIndex.ToString() : "" : witsmlLog.StartDateTimeIndex; } public static string GetEndIndexAsString(this WitsmlLog witsmlLog) { return witsmlLog.EndIndex == null && string.IsNullOrEmpty(witsmlLog.EndDateTimeIndex) ? null - : witsmlLog.IndexType.Equals(WitsmlLog.WITSML_INDEX_TYPE_MD, System.StringComparison.Ordinal) ? witsmlLog.EndIndex != null ? witsmlLog.EndIndex.ToString() : "" : witsmlLog.EndDateTimeIndex; + : string.Equals(witsmlLog.IndexType, WitsmlLog.WITSML_INDEX_TYPE_MD, System.StringComparison.Ordinal) ? witsmlLog.EndIndex != null ? witsmlLog.EndIndex.ToString() : "" : witsmlLog.EndDateTimeIndex; } diff --git a/Src/WitsmlExplorer.Api/HttpHandlers/ObjectHandler.cs b/Src/WitsmlExplorer.Api/HttpHandlers/ObjectHandler.cs index 9240c8eb3..8b0695f96 100644 --- a/Src/WitsmlExplorer.Api/HttpHandlers/ObjectHandler.cs +++ b/Src/WitsmlExplorer.Api/HttpHandlers/ObjectHandler.cs @@ -34,7 +34,7 @@ public static async Task GetObjectsIdOnly(string wellUid, string wellbo public static async Task GetObjectIdOnly(string wellUid, string wellboreUid, string objectUid, EntityType objectType, IObjectService objectService) { IEnumerable result = await objectService.GetObjectIdOnly(wellUid, wellboreUid, objectUid, objectType); - return TypedResults.Ok(result?.First()); + return TypedResults.Ok(result?.FirstOrDefault()); } [Produces(typeof(Dictionary))] diff --git a/Src/WitsmlExplorer.Api/Query/TrajectoryQueries.cs b/Src/WitsmlExplorer.Api/Query/TrajectoryQueries.cs index b78ced85f..3c7f05e6e 100644 --- a/Src/WitsmlExplorer.Api/Query/TrajectoryQueries.cs +++ b/Src/WitsmlExplorer.Api/Query/TrajectoryQueries.cs @@ -127,17 +127,17 @@ public static WitsmlTrajectories UpdateTrajectoryStation(TrajectoryStation traje TypeTrajStation = trajectoryStation.TypeTrajStation }; - if (!trajectoryStation.Tvd.Equals(null)) + if (trajectoryStation.Tvd != null) { ts.Tvd = new WitsmlWellVerticalDepthCoord { Uom = trajectoryStation.Tvd.Uom, Value = trajectoryStation.Tvd.Value.ToString(CultureInfo.InvariantCulture) }; } - if (!trajectoryStation.Incl.Equals(null)) + if (trajectoryStation.Incl != null) { ts.Incl = new WitsmlPlaneAngleMeasure { Uom = trajectoryStation.Incl.Uom, Value = trajectoryStation.Incl.Value.ToString(CultureInfo.InvariantCulture) }; } - if (!trajectoryStation.Azi.Equals(null)) + if (trajectoryStation.Azi != null) { ts.Azi = new WitsmlPlaneAngleMeasure { Uom = trajectoryStation.Azi.Uom, Value = trajectoryStation.Azi.Value.ToString(CultureInfo.InvariantCulture) }; } diff --git a/Src/WitsmlExplorer.Api/Services/CapService.cs b/Src/WitsmlExplorer.Api/Services/CapService.cs index 9f02e1a8f..7a9970fce 100644 --- a/Src/WitsmlExplorer.Api/Services/CapService.cs +++ b/Src/WitsmlExplorer.Api/Services/CapService.cs @@ -17,7 +17,7 @@ public CapService(IWitsmlClientProvider witsmlClientProvider) : base(witsmlClien public async Task GetCap() { - return (await _witsmlClient.GetCap()).ServerCapabilities.FirstOrDefault(); + return (await _witsmlClient.GetCap()).ServerCapabilities?.FirstOrDefault(); } } } diff --git a/Src/WitsmlExplorer.Api/Services/CredentialsService.cs b/Src/WitsmlExplorer.Api/Services/CredentialsService.cs index 40acd225a..fa0ea0c16 100644 --- a/Src/WitsmlExplorer.Api/Services/CredentialsService.cs +++ b/Src/WitsmlExplorer.Api/Services/CredentialsService.cs @@ -207,7 +207,7 @@ public ServerCredentials GetCredentials(IEssentialHeaders eh, string server, str if (creds == null && _useOAuth2) { creds = GetSystemCredentialsByToken(eh.GetBearerToken(), new Uri(server)).Result; - if (creds.IsCredsNullOrEmpty() || !creds.UserId.Equals(username, StringComparison.Ordinal)) + if (creds.IsCredsNullOrEmpty() || !string.Equals(creds.UserId, username, StringComparison.Ordinal)) { return null; } diff --git a/Src/WitsmlExplorer.Api/Services/ObjectService.cs b/Src/WitsmlExplorer.Api/Services/ObjectService.cs index ced824bb9..19e258a51 100644 --- a/Src/WitsmlExplorer.Api/Services/ObjectService.cs +++ b/Src/WitsmlExplorer.Api/Services/ObjectService.cs @@ -79,8 +79,8 @@ public async Task> GetObjectsWithParamByType(Ent IWitsmlObjectList capabilityQuery = (IWitsmlObjectList)EntityTypeHelper.ToObjectOnWellbore(objectType).AsSingletonWitsmlList(); IWitsmlObjectList capabilityResult = await _witsmlClient.GetFromStoreNullableAsync(capabilityQuery, new OptionsIn(RequestObjectSelectionCapability: true)); - WitsmlObjectOnWellbore capabilities = capabilityResult.Objects.First(); - bool isCapable = capabilities.GetType().GetProperty(objectProperty.CapitalizeFirstLetter())?.GetValue(capabilities, null) != null; + WitsmlObjectOnWellbore capabilities = capabilityResult?.Objects?.FirstOrDefault(); + bool isCapable = capabilities?.GetType().GetProperty(objectProperty.CapitalizeFirstLetter())?.GetValue(capabilities, null) != null; if (!isCapable) { throw new Middleware.WitsmlUnsupportedCapabilityException($"The server does not support to select {objectProperty} for a {objectType}."); diff --git a/Src/WitsmlExplorer.Api/Services/TubularService.cs b/Src/WitsmlExplorer.Api/Services/TubularService.cs index d24491ba5..3a282a88e 100644 --- a/Src/WitsmlExplorer.Api/Services/TubularService.cs +++ b/Src/WitsmlExplorer.Api/Services/TubularService.cs @@ -48,7 +48,7 @@ public async Task> GetTubularComponents(string wel WitsmlTubular witsmlTubular = result.Tubulars.FirstOrDefault(); return witsmlTubular == null ? null - : (IEnumerable)witsmlTubular.TubularComponents.Select(tComponent => new TubularComponent + : (IEnumerable)witsmlTubular.TubularComponents?.Select(tComponent => new TubularComponent { Uid = tComponent.Uid, Sequence = tComponent.Sequence, diff --git a/Src/WitsmlExplorer.Api/Workers/AnalyzeGapWorker.cs b/Src/WitsmlExplorer.Api/Workers/AnalyzeGapWorker.cs index 42a88a355..1bf8d3a80 100644 --- a/Src/WitsmlExplorer.Api/Workers/AnalyzeGapWorker.cs +++ b/Src/WitsmlExplorer.Api/Workers/AnalyzeGapWorker.cs @@ -146,7 +146,7 @@ private AnalyzeGapReport GetGapReport(IList selectedMnemonics, IList GetAnalyzeGapReportItem(string mnemonic, IList inputIndexList, Index requestedGapSize, bool isLogIncreasing) { List gapValues = new(); - Index lastValueIndex = inputIndexList.FirstOrDefault(); + Index lastValueIndex = inputIndexList?.FirstOrDefault(); if (lastValueIndex == null) return gapValues; diff --git a/Src/WitsmlExplorer.Api/Workers/CheckLogHeaderWorker.cs b/Src/WitsmlExplorer.Api/Workers/CheckLogHeaderWorker.cs index 0b8865f7a..38ee84437 100644 --- a/Src/WitsmlExplorer.Api/Workers/CheckLogHeaderWorker.cs +++ b/Src/WitsmlExplorer.Api/Workers/CheckLogHeaderWorker.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using Microsoft.Extensions.Logging; +using Microsoft.IdentityModel.Tokens; using Witsml.Data; using Witsml.ServiceReference; @@ -74,7 +75,7 @@ public CheckLogHeaderWorker(ILogger logger, IWitsmlClientProv { WitsmlLogs headerQuery = LogQueries.GetLogHeaderIndexes(wellUid, wellboreUid, logUid); WitsmlLogs headerResult = await GetTargetWitsmlClientOrThrow().GetFromStoreNullableAsync(headerQuery, new OptionsIn(ReturnElements.Requested)); - if (headerResult == null) + if (headerResult == null || headerResult.Objects.IsNullOrEmpty()) { return null; } @@ -91,18 +92,30 @@ public CheckLogHeaderWorker(ILogger logger, IWitsmlClientProv WitsmlLogs dataQuery = LogQueries.GetLogContent(wellUid, wellboreUid, logUid, indexType, Enumerable.Empty(), null, null); WitsmlLogs dataStartResult = await GetTargetWitsmlClientOrThrow().GetFromStoreNullableAsync(dataQuery, new OptionsIn(ReturnElements.DataOnly, MaxReturnNodes: 1)); WitsmlLogs dataEndResult = await GetTargetWitsmlClientOrThrow().GetFromStoreNullableAsync(dataQuery, new OptionsIn(ReturnElements.DataOnly, RequestLatestValues: 1)); + if (dataStartResult.Objects.IsNullOrEmpty() || dataEndResult.Objects.IsNullOrEmpty()) + { + return null; + } WitsmlLog dataStartResultLog = (WitsmlLog)dataStartResult.Objects.First(); WitsmlLog dataEndResultLog = (WitsmlLog)dataEndResult.Objects.First(); if (dataStartResultLog.LogData == null || dataEndResultLog.LogData == null) { return null; } - IEnumerable> endResultLogData = dataEndResultLog.LogData.Data.Select(data => data.Data.Split(",")); - string[] startResultLogData = dataStartResultLog.LogData.Data.First().Data.Split(","); + IEnumerable> endResultLogData = dataEndResultLog.LogData.Data?.Select(data => data.Data.Split(",")); + string[] startResultLogData = dataStartResultLog.LogData.Data?.FirstOrDefault()?.Data.Split(","); + if (startResultLogData.IsNullOrEmpty() || endResultLogData.IsNullOrEmpty()) + { + return null; + } IEnumerable dataStartIndexes = startResultLogData.Select(data => data == "" ? "" : startResultLogData[0]); IEnumerable dataEndIndexes = ExtractColumnIndexes(endResultLogData); - string[] startMnemonics = dataStartResultLog.LogData.MnemonicList.Split(","); - string[] endMnemonics = dataEndResultLog.LogData.MnemonicList.Split(","); + string[] startMnemonics = dataStartResultLog.LogData.MnemonicList?.Split(","); + string[] endMnemonics = dataEndResultLog.LogData.MnemonicList?.Split(","); + if (startMnemonics == null || endMnemonics == null) + { + return null; + } Dictionary dataStartValues = dataStartIndexes.Select((value, index) => new { mnemonic = startMnemonics[index], value }).ToDictionary(d => d.mnemonic, d => d.value); Dictionary dataEndValues = dataEndIndexes.Where(value => !string.IsNullOrEmpty(value)).Select((value, index) => new { mnemonic = endMnemonics[index], value }).ToDictionary(d => d.mnemonic, d => d.value); @@ -123,7 +136,7 @@ private async Task> AddStartIndexForMissingMnemonics( List> missingDataResults = missingIndexQueries.Select(query => GetTargetWitsmlClientOrThrow().GetFromStoreNullableAsync(query, new OptionsIn(ReturnElements.DataOnly, MaxReturnNodes: 1))).ToList(); await Task.WhenAll(missingDataResults); IEnumerable missingLogs = missingDataResults.Select(r => (WitsmlLog)r.Result.Objects.First()); - IEnumerable missingDataIndexes = missingLogs.Select(l => l.LogData.Data?.FirstOrDefault()?.Data?.Split(",")?[0] ?? ""); + IEnumerable missingDataIndexes = missingLogs.Select(l => l.LogData?.Data?.FirstOrDefault()?.Data?.Split(",")?[0] ?? ""); // Insert the indexes from the missing mnemonics to the original dict. missingDataIndexes .Select((value, index) => new { mnemonic = missingMnemonics[index], value }) diff --git a/Src/WitsmlExplorer.Api/Workers/Copy/CopyComponentsWorker.cs b/Src/WitsmlExplorer.Api/Workers/Copy/CopyComponentsWorker.cs index ab0852321..5a72aec73 100644 --- a/Src/WitsmlExplorer.Api/Workers/Copy/CopyComponentsWorker.cs +++ b/Src/WitsmlExplorer.Api/Workers/Copy/CopyComponentsWorker.cs @@ -62,15 +62,15 @@ public CopyComponentsWorker(ILogger logger, IWitsmlClientProv string[] toCopyUids = job.Source.ComponentUids; IWitsmlObjectList sourceQuery = ObjectQueries.GetWitsmlObjectByReference(job.Source.Parent, _componentType.ToParentType()); - ObjectQueries.SetComponents(sourceQuery.Objects.First(), _componentType, toCopyUids); + ObjectQueries.SetComponents(sourceQuery.Objects?.FirstOrDefault(), _componentType, toCopyUids); IWitsmlObjectList source = await GetSourceWitsmlClientOrThrow().GetFromStoreNullableAsync(sourceQuery, new OptionsIn(ReturnElements.All)); if (source == null) { - string reason = $"Unable to fetch {_componentType.ToParentType()} with uid {sourceQuery.Objects.First().Uid}."; + string reason = $"Unable to fetch {_componentType.ToParentType()} with uid {sourceQuery.Objects?.FirstOrDefault()?.Uid}."; return LogErrorAndReturnResult(reason); } - IEnumerable sourceComponentUids = ObjectQueries.GetComponentUids(source.Objects.First(), _componentType); + IEnumerable sourceComponentUids = ObjectQueries.GetComponentUids(source.Objects?.FirstOrDefault(), _componentType); IEnumerable missingUids = toCopyUids.Except(sourceComponentUids); if (missingUids.Any()) { @@ -78,7 +78,7 @@ public CopyComponentsWorker(ILogger logger, IWitsmlClientProv return LogErrorAndReturnResult(reason); } - WitsmlObjectOnWellbore updateTargetQuery = ObjectQueries.CopyComponents(source.Objects.First(), _componentType, job.Target, toCopyUids); + WitsmlObjectOnWellbore updateTargetQuery = ObjectQueries.CopyComponents(source.Objects?.FirstOrDefault(), _componentType, job.Target, toCopyUids); QueryResult copyResult = await targetClient.UpdateInStoreAsync(updateTargetQuery.AsSingletonWitsmlList()); if (!copyResult.IsSuccessful) { @@ -95,17 +95,17 @@ public CopyComponentsWorker(ILogger logger, IWitsmlClientProv private async Task VerifyTarget() { IWitsmlObjectList targetQuery = ObjectQueries.GetWitsmlObjectByReference(_job.Target, _componentType.ToParentType()); - ObjectQueries.SetComponents(targetQuery.Objects.First(), _componentType, _job.Source.ComponentUids); + ObjectQueries.SetComponents(targetQuery?.Objects?.FirstOrDefault(), _componentType, _job.Source.ComponentUids); IWitsmlObjectList target = await GetTargetWitsmlClientOrThrow().GetFromStoreNullableAsync(targetQuery, new OptionsIn(ReturnElements.Requested)); if (target == null) { - return $"Target {_componentType.ToParentType()} with uid {targetQuery.Objects.First().Uid} could not be fetched."; + return $"Target {_componentType.ToParentType()} with uid {targetQuery?.Objects?.FirstOrDefault()?.Uid} could not be fetched."; } if (!target.Objects.Any()) //if no uids to copy are present in the target then no object is returned { return null; } - IEnumerable targetComponentUids = ObjectQueries.GetComponentUids(target.Objects.First(), _componentType); + IEnumerable targetComponentUids = ObjectQueries.GetComponentUids(target.Objects.FirstOrDefault(), _componentType); IEnumerable conflictingUids = targetComponentUids.Intersect(_job.Source.ComponentUids); if (conflictingUids.Any()) { diff --git a/Src/WitsmlExplorer.Api/Workers/Copy/CopyUtils.cs b/Src/WitsmlExplorer.Api/Workers/Copy/CopyUtils.cs index 9d7d41521..efcc45f8b 100644 --- a/Src/WitsmlExplorer.Api/Workers/Copy/CopyUtils.cs +++ b/Src/WitsmlExplorer.Api/Workers/Copy/CopyUtils.cs @@ -75,10 +75,10 @@ public CopyUtils(ILogger logger) } }).ToList()); - string successString = successUids.Count > 0 ? $"Copied {queries.First().GetType().Name}s: {string.Join(", ", successUids)}." : ""; + string successString = successUids.Count > 0 ? $"Copied {queries.FirstOrDefault()?.GetType().Name}s: {string.Join(", ", successUids)}." : ""; return !error ? (new WorkerResult(witsmlClient.GetServerHostname(), true, successString), refreshAction) - : (new WorkerResult(witsmlClient.GetServerHostname(), false, $"{successString} Failed to copy some {queries.First().GetType().Name}s", errorReason, errorEntity), successUids.Count > 0 ? refreshAction : null); + : (new WorkerResult(witsmlClient.GetServerHostname(), false, $"{successString} Failed to copy some {queries.First()?.GetType().Name}s", errorReason, errorEntity), successUids.Count > 0 ? refreshAction : null); } } } diff --git a/Src/WitsmlExplorer.Api/Workers/Delete/DeleteCurveValuesWorker.cs b/Src/WitsmlExplorer.Api/Workers/Delete/DeleteCurveValuesWorker.cs index 75e280fbd..dcdf06db9 100644 --- a/Src/WitsmlExplorer.Api/Workers/Delete/DeleteCurveValuesWorker.cs +++ b/Src/WitsmlExplorer.Api/Workers/Delete/DeleteCurveValuesWorker.cs @@ -46,7 +46,7 @@ public DeleteCurveValuesWorker(ILogger logger, IWitsmlClie wellUid, wellboreUid, logUid, - query.Logs.First().LogCurveInfo.First().Mnemonic); + query.Logs.FirstOrDefault()?.LogCurveInfo?.FirstOrDefault()?.Mnemonic); } else { diff --git a/Src/WitsmlExplorer.Api/Workers/Delete/DeleteObjectsWorker.cs b/Src/WitsmlExplorer.Api/Workers/Delete/DeleteObjectsWorker.cs index 50b5de670..228512759 100644 --- a/Src/WitsmlExplorer.Api/Workers/Delete/DeleteObjectsWorker.cs +++ b/Src/WitsmlExplorer.Api/Workers/Delete/DeleteObjectsWorker.cs @@ -38,8 +38,8 @@ public DeleteObjectsWorker(ILogger logger, IWitsmlClientProvid private async Task<(WorkerResult, RefreshAction)> DeleteObjectsOnWellbore(IEnumerable queries, RefreshAction refreshAction) { IWitsmlClient witsmlClient = GetTargetWitsmlClientOrThrow(); - string uidWell = queries.First().UidWell; - string uidWellbore = queries.First().UidWellbore; + string uidWell = queries.FirstOrDefault()?.UidWell; + string uidWellbore = queries.FirstOrDefault()?.UidWellbore; bool error = false; List successUids = new(); @@ -79,10 +79,10 @@ await Task.WhenAll(queries.Select(async (query) => } }).ToList()); - string successString = successUids.Count > 0 ? $"Deleted {queries.First().GetType().Name}s: {string.Join(", ", successUids)}." : ""; + string successString = successUids.Count > 0 ? $"Deleted {queries.FirstOrDefault()?.GetType().Name}s: {string.Join(", ", successUids)}." : ""; return !error ? (new WorkerResult(witsmlClient.GetServerHostname(), true, successString), refreshAction) - : (new WorkerResult(witsmlClient.GetServerHostname(), false, $"{successString} Failed to delete some {queries.First().GetType().Name}s", errorReason, null), successUids.Count > 0 ? refreshAction : null); + : (new WorkerResult(witsmlClient.GetServerHostname(), false, $"{successString} Failed to delete some {queries.FirstOrDefault()?.GetType().Name}s", errorReason, null), successUids.Count > 0 ? refreshAction : null); } } } diff --git a/Src/WitsmlExplorer.Api/Workers/ImportLogDataWorker.cs b/Src/WitsmlExplorer.Api/Workers/ImportLogDataWorker.cs index a3c94472e..d5ce28357 100644 --- a/Src/WitsmlExplorer.Api/Workers/ImportLogDataWorker.cs +++ b/Src/WitsmlExplorer.Api/Workers/ImportLogDataWorker.cs @@ -39,7 +39,7 @@ public ImportLogDataWorker(ILogger logger, IWitsmlClientProvid } WitsmlLogs addMnemonicsQuery = CreateAddMnemonicsQuery(job, witsmlLog); - if (addMnemonicsQuery.Logs.FirstOrDefault().LogCurveInfo.Count > 0) + if (addMnemonicsQuery.Logs.FirstOrDefault().LogCurveInfo?.Count > 0) { QueryResult addMnemonicsResult = await GetTargetWitsmlClientOrThrow().UpdateInStoreAsync(addMnemonicsQuery); if (addMnemonicsResult.IsSuccessful) diff --git a/Src/WitsmlExplorer.Api/Workers/RenameMnemonicWorker.cs b/Src/WitsmlExplorer.Api/Workers/RenameMnemonicWorker.cs index da651426a..ca3cc8705 100644 --- a/Src/WitsmlExplorer.Api/Workers/RenameMnemonicWorker.cs +++ b/Src/WitsmlExplorer.Api/Workers/RenameMnemonicWorker.cs @@ -90,7 +90,7 @@ private static void Verify(RenameMnemonicJob job) { throw new InvalidOperationException("Empty name given when trying to rename a mnemonic. Make sure valid names are given"); } - else if (job.Mnemonic.Equals(job.NewMnemonic, StringComparison.OrdinalIgnoreCase)) + else if (string.Equals(job.Mnemonic, job.NewMnemonic, StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException("Cannot rename a mnemonic to the same name it already has. Make sure new mnemonic is a unique name"); } diff --git a/Src/WitsmlExplorer.Console/ListCommands/ListWellboresCommand.cs b/Src/WitsmlExplorer.Console/ListCommands/ListWellboresCommand.cs index 19c67d116..4b4f876c7 100644 --- a/Src/WitsmlExplorer.Console/ListCommands/ListWellboresCommand.cs +++ b/Src/WitsmlExplorer.Console/ListCommands/ListWellboresCommand.cs @@ -91,8 +91,8 @@ private async IAsyncEnumerable GetActiveWellbores() { UidWell = groupedResult.UidWell, Uid = groupedResult.UidWellbore, - NameWell = groupedResult.Logs.First().NameWell, - Name = groupedResult.Logs.First().NameWellbore + NameWell = groupedResult.Logs.FirstOrDefault()?.NameWell, + Name = groupedResult.Logs.FirstOrDefault()?.NameWellbore }; } }