-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathHlaMetadataDictionaryController.cs
95 lines (83 loc) · 3.85 KB
/
HlaMetadataDictionaryController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Collections.Generic;
using System.Reflection;
using Atlas.HlaMetadataDictionary.ExternalInterface;
using Atlas.HlaMetadataDictionary.ExternalInterface.Models;
using Atlas.MatchingAlgorithm.Services.ConfigurationProviders;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Atlas.HlaMetadataDictionary.ExternalInterface.Models.Metadata;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Atlas.MatchingAlgorithm.Api.Controllers
{
[Route("hla-metadata-dictionary")]
public class HlaMetadataDictionaryController : ControllerBase
{
private readonly IHlaMetadataDictionary hlaMetadataDictionary;
public HlaMetadataDictionaryController(
IHlaMetadataDictionaryFactory factory,
IActiveHlaNomenclatureVersionAccessor hlaNomenclatureVersionAccessor)
{
// TODO: ATLAS-355: Remove the need for a hardcoded default value
var hlaVersionOrDefault = hlaNomenclatureVersionAccessor.DoesActiveHlaNomenclatureVersionExist()
? hlaNomenclatureVersionAccessor.GetActiveHlaNomenclatureVersion()
: HlaMetadataDictionaryConstants.NoActiveVersionValue;
hlaMetadataDictionary = factory.BuildDictionary(hlaVersionOrDefault);
}
[HttpPost]
[Route("create-latest-version")]
public async Task CreateLatestHlaMetadataDictionary()
{
await hlaMetadataDictionary.RecreateHlaMetadataDictionary(CreationBehaviour.Latest);
}
[HttpPost]
[Route("create-specific-version")]
public async Task CreateSpecificHlaMetadataDictionary(string version)
{
await hlaMetadataDictionary.RecreateHlaMetadataDictionary(CreationBehaviour.Specific(version));
}
[HttpPost]
[Route("recreate-active-version")]
public async Task RecreateActiveHlaMetadataDictionary()
{
await hlaMetadataDictionary.RecreateHlaMetadataDictionary(CreationBehaviour.Active);
}
/// <summary>
/// Gets all pre-calculated HLA metadata to the specified version.
/// Note: none of the returned data is persisted.
/// Used when manually refreshing the contents of the file-backed HMD.
/// </summary>
[HttpPost]
[Route("regenerate-metadata-file")]
public string RegenerateNewMetadataFile(string version, string outputPath = null)
{
var metadata = hlaMetadataDictionary.GenerateAllHlaMetadata(version);
var stringRepresentation = SerialiseToJsonString(metadata);
if (!string.IsNullOrWhiteSpace(outputPath))
{
System.IO.File.WriteAllText(outputPath, stringRepresentation);
}
return stringRepresentation;
}
private static string SerialiseToJsonString(HlaMetadataCollection metadata)
{
var jsonSerializerSettings = new JsonSerializerSettings {ContractResolver = new IgnoreHlaInfoToSerialise()};
return JsonConvert.SerializeObject(metadata, Formatting.Indented, jsonSerializerSettings);
}
public class IgnoreHlaInfoToSerialise : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
//It seems to check both the interface AND the concrete class and writes if EITHER are non-Ignored, and we can't [JsonIgnore] the prop on the interface.
if (property.PropertyName == nameof(ISerialisableHlaMetadata.HlaInfoToSerialise))
{
property.ShouldSerialize = _ => false;
property.Ignored = true;
}
return property;
}
}
}
}