-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathUtility.cs
More file actions
252 lines (228 loc) · 9.67 KB
/
Copy pathUtility.cs
File metadata and controls
252 lines (228 loc) · 9.67 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* Copyright (c) 2017+ brianpos, Firely and contributors
* See the file CONTRIBUTORS for details.
*
* This file is licensed under the BSD 3-Clause license
* available at https://github.com/ewoutkramer/fhir-net-api/blob/master/LICENSE
*/
using Hl7.Fhir.Model;
using Hl7.Fhir.Rest;
using Hl7.Fhir.Serialization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using Microsoft.AspNetCore.Mvc.Formatters;
namespace Hl7.Fhir.WebApi
{
public static class Utility
{
#region << WebApi Controller Extensions >>
public static string CalculateBaseURI(this Controller me, string resourceName)
{
Uri ri = me.Request.RequestUri();
if (resourceName == "metadata" || resourceName == "${operation}")
{
return String.Format("{0}://{1}{2}{3}{4}",
ri.Scheme,
ri.Host,
ri.IsDefaultPort ? "" : ":" + ri.Port.ToString(),
me.Request.PathBase.Value.TrimEnd('/') + '/',
me.ControllerContext.ActionDescriptor.AttributeRouteInfo.Template.Replace("metadata", "").Replace("/${operation}", "").Replace("${operation}", ""));
}
if (me.ControllerContext.ActionDescriptor.AttributeRouteInfo.Template.Contains("{ResourceName"))
resourceName = "{ResourceName";
string baseUri = String.Format("{0}://{1}{2}{3}{4}",
ri.Scheme,
ri.Host,
ri.IsDefaultPort ? "" : ":" + ri.Port.ToString(),
me.Request.PathBase.Value.TrimEnd('/') + '/',
me.ControllerContext.ActionDescriptor.AttributeRouteInfo.Template.Substring(0, me.ControllerContext.ActionDescriptor.AttributeRouteInfo.Template.LastIndexOf(resourceName)));
return baseUri;
}
#endregion
#region << Div Content Formatting helpers >>
public const string TextDivFieldType = "span";
public const string TextDivFieldStyle = " style=\"color: gray;\"";
/// <summary>
///
/// </summary>
/// <param name="textContent"></param>
/// <param name="fieldname">The fieldname to be displayed as a prefix</param>
/// <param name="format">the format string,
/// {0} is the HTML element type to use for the field - set at span
/// {1} is the style to be used in this HTML field span
/// {2} is the name of the field
/// </param>
/// <param name="args"></param>
static public void AppendFormatFHIRFields(this StringBuilder textContent, string fieldname, string format, params object[] args)
{
if (!string.IsNullOrEmpty(fieldname))
textContent.AppendFormat(" <{0}{1}>{2}:</{0}> ", TextDivFieldType, TextDivFieldStyle, fieldname);
textContent.AppendFormat(format, args);
textContent.AppendFormat("<br/>\r\n");
}
public static Hl7.Fhir.Model.Narrative CreateNarative(string div)
{
var n = new Hl7.Fhir.Model.Narrative();
if (!string.IsNullOrEmpty(div) && !div.StartsWith("<div", StringComparison.CurrentCultureIgnoreCase))
n.Div = String.Format("<div xmlns=\"http://www.w3.org/1999/xhtml\">{0}</div>", div);
else
n.Div = div;
return n;
}
public static Hl7.Fhir.Model.CodeableConcept CreateCodeableConcept(string system, string code)
{
if (string.IsNullOrEmpty(code))
return null;
if (!system.StartsWith("http://"))
system = "http://" + system;
var n = new Hl7.Fhir.Model.CodeableConcept(system, code);
return n;
}
public static string[] CreateStringArray(object value)
{
if (value == null)
return null;
if (value is string)
return (value as string).Split('\r');
return null;
}
#endregion
#region << Static Utility functions for extracting common fhir types >>
public static DateTime? ToDateTime(this FhirDateTime me)
{
if (me == null)
return null;
DateTime result;
if (DateTime.TryParse(me.Value, out result))
return result;
if (!string.IsNullOrEmpty(me.Value))
{
// the date didn't parse, one of the common mistakes
// with dates is not to include the - symbols
// so lets put them in and proceed
if (me.Value.Length == 8 && !me.Value.Contains("-"))
{
string newValue = me.Value.Insert(4, "-").Insert(7, "-");
System.Diagnostics.Debug.WriteLine(String.Format("Invalid Date [{0}] was encountered, processing it as though it was [{1}]", me.Value, newValue));
if (DateTime.TryParse(newValue, out result))
return result;
}
}
return null;
}
public static DateTime? ToDateTime(this Date me)
{
if (me == null)
return null;
DateTime result;
if (DateTime.TryParse(me.Value, out result))
return result;
if (!string.IsNullOrEmpty(me.Value))
{
// the date didn't parse, one of the common mistakes
// with dates is not to include the - symbols
// so lets put them in and proceed
if (me.Value.Length == 8 && !me.Value.Contains("-"))
{
string newValue = me.Value.Insert(4, "-").Insert(7, "-");
System.Diagnostics.Debug.WriteLine(String.Format("Invalid Date [{0}] was encountered, processing it as though it was [{1}]", me.Value, newValue));
if (DateTime.TryParse(newValue, out result))
return result;
}
}
return null;
}
public static string ToFhirId(this System.Guid me)
{
return me.ToString("n");
}
public static string ToFhirId(this System.Guid? me)
{
if (me.HasValue)
return me.Value.ToString("n");
return null;
}
public static string ToFhirDate(this System.DateTime me)
{
return me.ToString("yyyy-MM-dd");
}
public static string ToFhirDate(this System.DateTime? me)
{
if (me.HasValue)
return me.Value.ToString("yyyy-MM-dd");
return null;
}
public static string ToFhirDateTime(this System.DateTime me)
{
return me.ToString("yyyy-MM-ddTHH:mm:ss.sss");
}
public static string ToFhirDateTime(this System.DateTime? me)
{
if (me.HasValue)
return me.Value.ToString("yyyy-MM-ddTHH:mm:ss.sss");
return null;
}
#endregion
#region << Static Helpers for Header Subset comparison that ignores Header parameters >>
public static bool IsSubsetOfIgnoreParameters(this MediaType supported, MediaType set)
{
return supported.MatchesType(set) &&
supported.MatchesSubtype(set);
}
private static bool MatchesType(this MediaType supported, MediaType set)
{
return set.MatchesAllTypes ||
set.Type.Equals(supported.Type, StringComparison.OrdinalIgnoreCase);
}
private static bool MatchesSubtype(this MediaType supported, MediaType set)
{
if (set.MatchesAllSubTypes)
{
return true;
}
if (set.SubTypeSuffix.HasValue)
{
if (supported.SubTypeSuffix.HasValue)
{
// Both the set and the media type being checked have suffixes, so both parts must match.
return supported.MatchesSubtypeWithoutSuffix(set) && supported.MatchesSubtypeSuffix(set);
}
else
{
// The set has a suffix, but the media type being checked doesn't. We never consider this to match.
return false;
}
}
else
{
// If this subtype or suffix matches the subtype of the set,
// it is considered a subtype.
// Ex: application/json > application/val+json
return supported.MatchesEitherSubtypeOrSuffix(set);
}
}
private static bool MatchesSubtypeWithoutSuffix(this MediaType supported, MediaType set)
{
return set.MatchesAllSubTypesWithoutSuffix ||
set.SubTypeWithoutSuffix.Equals(supported.SubTypeWithoutSuffix, StringComparison.OrdinalIgnoreCase);
}
private static bool MatchesSubtypeSuffix(this MediaType supported, MediaType set)
{
// We don't have support for wildcards on suffixes alone (e.g., "application/entity+*")
// because there's no clear use case for it.
return set.SubTypeSuffix.Equals(supported.SubTypeSuffix, StringComparison.OrdinalIgnoreCase);
}
private static bool MatchesEitherSubtypeOrSuffix(this MediaType supported, MediaType set)
{
return set.SubType.Equals(supported.SubType, StringComparison.OrdinalIgnoreCase) ||
set.SubType.Equals(supported.SubTypeSuffix, StringComparison.OrdinalIgnoreCase);
}
#endregion
}
}