Skip to content

Commit f92fd94

Browse files
authored
Fix copy observations (#1755)
2 parents daeb58a + 04ad415 commit f92fd94

File tree

6 files changed

+248
-104
lines changed

6 files changed

+248
-104
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
- Filtering striae for `not specified` returned wrong results.
4141
- Filtering by `borehole status` did not work.
4242
- When saving with ctrl+s in the borehole sections, the form content was reset.
43+
- When copying a borehole, the nested collections of observations were not copied.
4344
- There was a bug when changing the order, transparency or visibility of custom WMS user layers.
4445
- The borehole status was not translated everywhere in the workflow panel.
4546

src/api/BdmsContextExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ public static void SeedData(this BdmsContext context)
198198
.RuleFor(o => o.PrecisionLocationY, f => f.PickRandom(Enumerable.Range(0, 10)))
199199
.RuleFor(o => o.PrecisionLocationXLV03, f => f.PickRandom(Enumerable.Range(0, 10)))
200200
.RuleFor(o => o.PrecisionLocationYLV03, f => f.PickRandom(Enumerable.Range(0, 10)))
201+
.RuleFor(o => o.Observations, _ => new Collection<Observation>())
201202
.FinishWith((f, o) => { o.AlternateName = o.OriginalName; });
202203

203204
Borehole SeededBoreholes(int seed) => fakeBoreholes.UseSeed(seed).Generate();

src/api/Controllers/BoreholeController.cs

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -117,31 +117,6 @@ public async Task<ActionResult<Borehole>> GetByIdAsync(int id)
117117
return Ok(borehole);
118118
}
119119

120-
private IQueryable<Borehole> GetBoreholesWithIncludes()
121-
{
122-
return Context.Boreholes.Include(b => b.Stratigraphies).ThenInclude(s => s.Layers).ThenInclude(l => l.LayerColorCodes)
123-
.Include(b => b.Stratigraphies).ThenInclude(s => s.Layers).ThenInclude(l => l.LayerDebrisCodes)
124-
.Include(b => b.Stratigraphies).ThenInclude(s => s.Layers).ThenInclude(l => l.LayerGrainAngularityCodes)
125-
.Include(b => b.Stratigraphies).ThenInclude(s => s.Layers).ThenInclude(l => l.LayerGrainShapeCodes)
126-
.Include(b => b.Stratigraphies).ThenInclude(s => s.Layers).ThenInclude(l => l.LayerOrganicComponentCodes)
127-
.Include(b => b.Stratigraphies).ThenInclude(s => s.Layers).ThenInclude(l => l.LayerUscs3Codes)
128-
.Include(b => b.Stratigraphies).ThenInclude(s => s.LithologicalDescriptions)
129-
.Include(b => b.Stratigraphies).ThenInclude(s => s.FaciesDescriptions)
130-
.Include(b => b.Stratigraphies).ThenInclude(s => s.ChronostratigraphyLayers)
131-
.Include(b => b.Stratigraphies).ThenInclude(s => s.LithostratigraphyLayers)
132-
.Include(b => b.Completions).ThenInclude(c => c.Casings).ThenInclude(c => c.CasingElements)
133-
.Include(b => b.Completions).ThenInclude(c => c.Instrumentations)
134-
.Include(b => b.Completions).ThenInclude(c => c.Backfills)
135-
.Include(b => b.Sections).ThenInclude(s => s.SectionElements)
136-
.Include(b => b.Observations)
137-
.Include(b => b.BoreholeCodelists)
138-
.Include(b => b.Workflows)
139-
.Include(b => b.BoreholeFiles)
140-
.Include(b => b.BoreholeGeometry)
141-
.Include(b => b.Workgroup)
142-
.Include(b => b.UpdatedBy);
143-
}
144-
145120
/// <summary>
146121
/// Asynchronously copies a <see cref="Borehole"/>.
147122
/// </summary>
@@ -170,36 +145,7 @@ public async Task<ActionResult<int>> CopyAsync([Required] int id, [Required] int
170145
.SingleOrDefaultAsync(b => b.Id == id)
171146
.ConfigureAwait(false);
172147

173-
if (borehole == null)
174-
{
175-
return NotFound();
176-
}
177-
178-
if (borehole.Observations != null)
179-
{
180-
// Include FieldMeasurementResults and HydrotestResults separately since Entity Framework does not support casting in an Include statement
181-
var fieldMeasurements = borehole.Observations.OfType<FieldMeasurement>().ToList();
182-
#pragma warning disable CS8603
183-
// Cannot include null test for fieldMeasurementResults and hydrotestResults since they are not yet loaded
184-
// if there are no fieldMeasurementResults of hydrotestResults the LoadAsync method will be called but have no effect
185-
foreach (var fieldMeasurement in fieldMeasurements)
186-
{
187-
await Context.Entry(fieldMeasurement)
188-
.Collection(f => f.FieldMeasurementResults)
189-
.LoadAsync()
190-
.ConfigureAwait(false);
191-
}
192-
193-
var hydrotests = borehole.Observations.OfType<Hydrotest>().ToList();
194-
foreach (var hydrotest in hydrotests)
195-
{
196-
await Context.Entry(hydrotest)
197-
.Collection(h => h.HydrotestResults)
198-
.LoadAsync()
199-
.ConfigureAwait(false);
200-
}
201-
#pragma warning restore CS8603
202-
}
148+
if (borehole == null) return NotFound();
203149

204150
// Set ids of copied entities to zero. Entities with an id of zero are added as new entities to the DB.
205151
borehole.Id = 0;
@@ -293,6 +239,30 @@ await Context.Entry(hydrotest)
293239
hydrotestResult.Id = 0;
294240
}
295241
}
242+
243+
if (hydrotest.HydrotestKindCodes != null)
244+
{
245+
foreach (var hydrotestKindCode in hydrotest.HydrotestKindCodes)
246+
{
247+
hydrotestKindCode.HydrotestId = 0;
248+
}
249+
}
250+
251+
if (hydrotest.HydrotestEvaluationMethodCodes != null)
252+
{
253+
foreach (var hydrotestEvaluationMethodCode in hydrotest.HydrotestEvaluationMethodCodes)
254+
{
255+
hydrotestEvaluationMethodCode.HydrotestId = 0;
256+
}
257+
}
258+
259+
if (hydrotest.HydrotestFlowDirectionCodes != null)
260+
{
261+
foreach (var hydrotestFlowDirectionCode in hydrotest.HydrotestFlowDirectionCodes)
262+
{
263+
hydrotestFlowDirectionCode.HydrotestId = 0;
264+
}
265+
}
296266
}
297267
}
298268

@@ -327,4 +297,33 @@ await Context.Entry(hydrotest)
327297
if (entity == null) return default;
328298
return await Task.FromResult<int?>(entity.Id).ConfigureAwait(false);
329299
}
300+
301+
private IQueryable<Borehole> GetBoreholesWithIncludes()
302+
{
303+
return Context.Boreholes.Include(b => b.Stratigraphies).ThenInclude(s => s.Layers).ThenInclude(l => l.LayerColorCodes)
304+
.Include(b => b.Stratigraphies).ThenInclude(s => s.Layers).ThenInclude(l => l.LayerDebrisCodes)
305+
.Include(b => b.Stratigraphies).ThenInclude(s => s.Layers).ThenInclude(l => l.LayerGrainAngularityCodes)
306+
.Include(b => b.Stratigraphies).ThenInclude(s => s.Layers).ThenInclude(l => l.LayerGrainShapeCodes)
307+
.Include(b => b.Stratigraphies).ThenInclude(s => s.Layers).ThenInclude(l => l.LayerOrganicComponentCodes)
308+
.Include(b => b.Stratigraphies).ThenInclude(s => s.Layers).ThenInclude(l => l.LayerUscs3Codes)
309+
.Include(b => b.Stratigraphies).ThenInclude(s => s.LithologicalDescriptions)
310+
.Include(b => b.Stratigraphies).ThenInclude(s => s.FaciesDescriptions)
311+
.Include(b => b.Stratigraphies).ThenInclude(s => s.ChronostratigraphyLayers)
312+
.Include(b => b.Stratigraphies).ThenInclude(s => s.LithostratigraphyLayers)
313+
.Include(b => b.Completions).ThenInclude(c => c.Casings).ThenInclude(c => c.CasingElements)
314+
.Include(b => b.Completions).ThenInclude(c => c.Instrumentations)
315+
.Include(b => b.Completions).ThenInclude(c => c.Backfills)
316+
.Include(b => b.Sections).ThenInclude(s => s.SectionElements)
317+
.Include(b => b.Observations).ThenInclude(o => (o as FieldMeasurement)!.FieldMeasurementResults)
318+
.Include(b => b.Observations).ThenInclude(o => (o as Hydrotest)!.HydrotestResults)
319+
.Include(b => b.Observations).ThenInclude(o => (o as Hydrotest)!.HydrotestEvaluationMethodCodes)
320+
.Include(b => b.Observations).ThenInclude(o => (o as Hydrotest)!.HydrotestFlowDirectionCodes)
321+
.Include(b => b.Observations).ThenInclude(o => (o as Hydrotest)!.HydrotestKindCodes)
322+
.Include(b => b.BoreholeCodelists)
323+
.Include(b => b.Workflows)
324+
.Include(b => b.BoreholeFiles)
325+
.Include(b => b.BoreholeGeometry)
326+
.Include(b => b.Workgroup)
327+
.Include(b => b.UpdatedBy);
328+
}
330329
}

src/api/Models/Borehole.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ public class Borehole : IChangeTracking, IIdentifyable
379379
/// <summary>
380380
/// Gets the <see cref="Borehole"/>'s observations.
381381
/// </summary>
382-
public ICollection<Observation>? Observations { get; }
382+
public ICollection<Observation>? Observations { get; set; }
383383

384384
/// <summary>
385385
/// Gets the <see cref="Borehole"/>'s workflows.

0 commit comments

Comments
 (0)