Skip to content

Commit a91e997

Browse files
Merge pull request #504 from bcgov/1.3.1
1.3.1 - 1.3.2
2 parents 5da4f4d + 31f4f93 commit a91e997

File tree

73 files changed

+3932
-116
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+3932
-116
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Hmcr.Api.Controllers.Base;
6+
using Hmcr.Domain.Services;
7+
using Hmcr.Model;
8+
using Hmcr.Model.Dtos.ActivityRule;
9+
using Hmcr.Model.Dtos.User;
10+
using Microsoft.AspNetCore.Http;
11+
using Microsoft.AspNetCore.Mvc;
12+
13+
namespace Hmcr.Api.Controllers
14+
{
15+
[ApiVersion("1.0")]
16+
[Route("api/activityrule")]
17+
[ApiController]
18+
public class ActivityRuleController : HmcrControllerBase
19+
{
20+
private IActivityRuleService _activityRuleSvc;
21+
public ActivityRuleController(IActivityRuleService activityRuleSvc)
22+
{
23+
_activityRuleSvc = activityRuleSvc;
24+
}
25+
26+
[HttpGet("roadlength")]
27+
public async Task<ActionResult<IEnumerable<ActivityCodeRuleDto>>> GetRoadLengthRulesAsync()
28+
{
29+
return Ok(await _activityRuleSvc.GetRoadLengthRulesAsync());
30+
}
31+
32+
[HttpGet("surfacetype")]
33+
public async Task<ActionResult<IEnumerable<ActivityCodeRuleDto>>> GetSurfaceTypeRulesAsync()
34+
{
35+
return Ok(await _activityRuleSvc.GetSurfaceTypeRulesAsync());
36+
}
37+
38+
[HttpGet("roadclass")]
39+
public async Task<ActionResult<IEnumerable<ActivityCodeRuleDto>>> GetRoadClassRulesAsync()
40+
{
41+
return Ok(await _activityRuleSvc.GetRoadClassRulesAsync());
42+
}
43+
}
44+
}

api/Hmcr.Api/Startup.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ namespace Hmcr.Api
1515
{
1616
public class Startup
1717
{
18-
1918
public IConfiguration Configuration { get; }
2019

2120
private IWebHostEnvironment _env;
@@ -45,7 +44,8 @@ public void ConfigureServices(IServiceCollection services)
4544
}
4645

4746
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ISubmissionObjectJobService jobService,
48-
IServiceScopeFactory serviceScopeFactory, IServiceAreaService svcAreaService, ICodeLookupRepository codeLookupRepo, IFieldValidatorService validator)
47+
IServiceScopeFactory serviceScopeFactory, IServiceAreaService svcAreaService, ICodeLookupRepository codeLookupRepo,
48+
IActivityRuleRepository activityRuleRepo, IFieldValidatorService validator)
4949
{
5050
if (env.IsDevelopment())
5151
app.UseDeveloperExceptionPage();
@@ -60,6 +60,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ISubmiss
6060
app.UseHmcrSwagger(env, Configuration.GetSection("Constants:SwaggerApiUrl").Value);
6161

6262
validator.CodeLookup = codeLookupRepo.LoadCodeLookupCache();
63+
validator.ActivityCodeRuleLookup = activityRuleRepo.LoadActivityCodeRuleCache();
6364
}
6465
}
6566
}

api/Hmcr.Chris/ChrisServiceCollectionExtensions.cs

+12
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ public static void AddChrisHttpClient(this IServiceCollection services, IConfigu
4141
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", basicAuth);
4242
});
4343

44+
services.AddHttpClient<IInventoryApi, InventoryApi>(client =>
45+
{
46+
client.BaseAddress = new Uri(config.GetValue<string>("CHRIS:OASUrl"));
47+
client.Timeout = new TimeSpan(0, 0, 45);
48+
client.DefaultRequestHeaders.Clear();
49+
50+
var userId = config.GetValue<string>("ServiceAccount:User");
51+
var password = config.GetValue<string>("ServiceAccount:Password");
52+
var basicAuth = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes($"{userId}:{password}"));
53+
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", basicAuth);
54+
});
55+
4456
services.AddScoped<IApi, Api>();
4557
}
4658
}

api/Hmcr.Chris/Hmcr.Chris.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<ItemGroup>
88
<None Remove="XmlTemplates\IsPointOnRfiSegment.xml" />
99
<None Remove="XmlTemplates\IsPointWithinServiceArea.xml" />
10+
<None Remove="XmlTemplates\GetInventoryAssocWithWorkActivity.xml" />
1011
</ItemGroup>
1112

1213
<ItemGroup>
@@ -16,6 +17,9 @@
1617
<Content Include="XmlTemplates\IsPointWithinServiceArea.xml">
1718
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
1819
</Content>
20+
<Content Include="XmlTemplates\GetInventoryAssocWithWorkActivity.xml">
21+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
22+
</Content>
1923
</ItemGroup>
2024

2125
<ItemGroup>

api/Hmcr.Chris/InventoryApi.cs

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using Hmcr.Chris.Models;
2+
using Microsoft.Extensions.Configuration;
3+
using System.Collections.Generic;
4+
using System.Net.Http;
5+
using System.Text.Json;
6+
using System.Threading.Tasks;
7+
8+
namespace Hmcr.Chris
9+
{
10+
public interface IInventoryApi
11+
{
12+
Task<List<SurfaceType>> GetSurfaceTypeAssociatedWithLine(string lineStringCoordinates);
13+
Task<SurfaceType> GetSurfaceTypeAssociatedWithPoint(string lineStringCoordinates);
14+
15+
//TODO: implement MC calls to CHRIS
16+
17+
}
18+
19+
public class InventoryApi : IInventoryApi
20+
{
21+
private HttpClient _client;
22+
private InventoryQueries _queries;
23+
private IApi _api;
24+
private string _path;
25+
26+
/// <summary>
27+
/// Chris Query typeName values, used to call the Get Inventory
28+
/// Assoc with Work Activity Query.
29+
/// </summary>
30+
public static class InventoryQueryTypeName
31+
{
32+
public const string SURF_ASSOC_WITH_LINE = "SURF_ASSOCIATED_WITH_LINE";
33+
public const string SURF_ASSOC_WITH_POINT = "SURF_ASSOCIATED_WITH_POINT";
34+
public const string MC_ASSOC_WITH_LINE = "MC_ASSOCIATED_WITH_LINE";
35+
public const string MC_ASSOC_WITH_POINT = "MC_ASSOCIATED_WITH_POINT";
36+
}
37+
38+
public InventoryApi(HttpClient client, IApi api, IConfiguration config)
39+
{
40+
_client = client;
41+
_queries = new InventoryQueries();
42+
_api = api;
43+
_path = config.GetValue<string>("CHRIS:OASPath");
44+
}
45+
46+
public async Task<SurfaceType> GetSurfaceTypeAssociatedWithPoint(string lineStringCoordinates)
47+
{
48+
var body = string.Format(_queries.SurfaceTypeAssocWithPointQuery, lineStringCoordinates, InventoryQueryTypeName.SURF_ASSOC_WITH_POINT);
49+
50+
var contents = await (await _api.PostWithRetry(_client, _path, body)).Content.ReadAsStringAsync();
51+
52+
var results = JsonSerializer.Deserialize<FeatureCollection<object>>(contents);
53+
54+
SurfaceType surfaceType = new SurfaceType();
55+
if (results.features.Length > 0)
56+
{
57+
surfaceType.Type = results.features[0].properties.SURFACE_TYPE;
58+
}
59+
60+
return surfaceType;
61+
}
62+
63+
public async Task<List<SurfaceType>> GetSurfaceTypeAssociatedWithLine(string lineStringCoordinates)
64+
{
65+
var body = string.Format(_queries.SurfaceTypeAssocWithLineQuery, lineStringCoordinates, InventoryQueryTypeName.SURF_ASSOC_WITH_LINE);
66+
67+
var contents = await (await _api.PostWithRetry(_client, _path, body)).Content.ReadAsStringAsync();
68+
69+
var results = JsonSerializer.Deserialize<FeatureCollection<object>>(contents);
70+
71+
var surfaceTypes = new List<SurfaceType>();
72+
73+
foreach (var feature in results.features)
74+
{
75+
SurfaceType surfaceType = new SurfaceType();
76+
surfaceType.Length = feature.properties.CLIPPED_LENGTH_KM;
77+
surfaceType.Type = feature.properties.SURFACE_TYPE;
78+
79+
surfaceTypes.Add(surfaceType);
80+
}
81+
82+
return surfaceTypes;
83+
}
84+
}
85+
}

api/Hmcr.Chris/InventoryQueries.cs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.IO;
2+
using System.Reflection;
3+
4+
namespace Hmcr.Chris
5+
{
6+
public class InventoryQueries
7+
{
8+
private string _surfaceTypeAssocWithLineQuery;
9+
private string _surfaceTypeAssocWithPointQuery;
10+
11+
public string SurfaceTypeAssocWithLineQuery
12+
{
13+
get
14+
{
15+
var folder = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "XmlTemplates");
16+
return _surfaceTypeAssocWithLineQuery ?? (_surfaceTypeAssocWithLineQuery = File.ReadAllText(Path.Combine(folder, "GetInventoryAssocWithWorkActivity.xml")));
17+
}
18+
}
19+
20+
public string SurfaceTypeAssocWithPointQuery
21+
{
22+
get
23+
{
24+
var folder = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "XmlTemplates");
25+
return _surfaceTypeAssocWithPointQuery ?? (_surfaceTypeAssocWithPointQuery = File.ReadAllText(Path.Combine(folder, "GetInventoryAssocWithWorkActivity.xml")));
26+
}
27+
}
28+
29+
}
30+
}

api/Hmcr.Chris/Models/Property.cs

+2
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ public class Property
77
public string NE_DESCR { get; set; }
88
public float MEASURE { get; set; }
99
public double POINT_VARIANCE { get; set; }
10+
public string SURFACE_TYPE { get; set; }
11+
public double CLIPPED_LENGTH_KM { get; set; }
1012
}
1113
}

api/Hmcr.Chris/Models/SurfaceType.cs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Hmcr.Chris.Models
6+
{
7+
public class SurfaceType
8+
{
9+
10+
public Geometry geometry { get; set; }
11+
/// <summary>
12+
/// Length in Meters
13+
/// </summary>
14+
public double Length { get; set; }
15+
16+
public string Type { get; set; }
17+
18+
}
19+
public class SurfaceType<T>
20+
{
21+
22+
public Geometry geometry { get; set; }
23+
/// <summary>
24+
/// Length in Meters
25+
/// </summary>
26+
public double Length { get; set; }
27+
28+
public string Type { get; set; }
29+
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<wfs:GetFeature service="WFS"
2+
version="1.1.0"
3+
outputFormat="json"
4+
maxFeatures="50"
5+
viewParams="epsg:4326;coordinates:{0}"
6+
xmlns:topp="http://www.openplans.org/topp"
7+
xmlns:wfs="http://www.opengis.net/wfs"
8+
xmlns="http://www.opengis.net/ogc"
9+
xmlns:gml="http://www.opengis.net/gml"
10+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
11+
xsi:schemaLocation="http://www.opengis.net/wfs
12+
http://schemas.opengis.net/wfs/1.1.0/WFS-basic.xsd">
13+
<wfs:Query typeName="cwr:{1}" srsName="EPSG:3005"/>
14+
</wfs:GetFeature>

0 commit comments

Comments
 (0)