Skip to content

Commit

Permalink
Merge pull request #504 from bcgov/1.3.1
Browse files Browse the repository at this point in the history
1.3.1 - 1.3.2
  • Loading branch information
AdvSol-Darrel authored Feb 11, 2021
2 parents 5da4f4d + 31f4f93 commit a91e997
Show file tree
Hide file tree
Showing 73 changed files with 3,932 additions and 116 deletions.
44 changes: 44 additions & 0 deletions api/Hmcr.Api/Controllers/ActivityRuleController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Hmcr.Api.Controllers.Base;
using Hmcr.Domain.Services;
using Hmcr.Model;
using Hmcr.Model.Dtos.ActivityRule;
using Hmcr.Model.Dtos.User;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Hmcr.Api.Controllers
{
[ApiVersion("1.0")]
[Route("api/activityrule")]
[ApiController]
public class ActivityRuleController : HmcrControllerBase
{
private IActivityRuleService _activityRuleSvc;
public ActivityRuleController(IActivityRuleService activityRuleSvc)
{
_activityRuleSvc = activityRuleSvc;
}

[HttpGet("roadlength")]
public async Task<ActionResult<IEnumerable<ActivityCodeRuleDto>>> GetRoadLengthRulesAsync()
{
return Ok(await _activityRuleSvc.GetRoadLengthRulesAsync());
}

[HttpGet("surfacetype")]
public async Task<ActionResult<IEnumerable<ActivityCodeRuleDto>>> GetSurfaceTypeRulesAsync()
{
return Ok(await _activityRuleSvc.GetSurfaceTypeRulesAsync());
}

[HttpGet("roadclass")]
public async Task<ActionResult<IEnumerable<ActivityCodeRuleDto>>> GetRoadClassRulesAsync()
{
return Ok(await _activityRuleSvc.GetRoadClassRulesAsync());
}
}
}
5 changes: 3 additions & 2 deletions api/Hmcr.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace Hmcr.Api
{
public class Startup
{

public IConfiguration Configuration { get; }

private IWebHostEnvironment _env;
Expand Down Expand Up @@ -45,7 +44,8 @@ public void ConfigureServices(IServiceCollection services)
}

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

validator.CodeLookup = codeLookupRepo.LoadCodeLookupCache();
validator.ActivityCodeRuleLookup = activityRuleRepo.LoadActivityCodeRuleCache();
}
}
}
12 changes: 12 additions & 0 deletions api/Hmcr.Chris/ChrisServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ public static void AddChrisHttpClient(this IServiceCollection services, IConfigu
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", basicAuth);
});

services.AddHttpClient<IInventoryApi, InventoryApi>(client =>
{
client.BaseAddress = new Uri(config.GetValue<string>("CHRIS:OASUrl"));
client.Timeout = new TimeSpan(0, 0, 45);
client.DefaultRequestHeaders.Clear();

var userId = config.GetValue<string>("ServiceAccount:User");
var password = config.GetValue<string>("ServiceAccount:Password");
var basicAuth = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes($"{userId}:{password}"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", basicAuth);
});

services.AddScoped<IApi, Api>();
}
}
Expand Down
4 changes: 4 additions & 0 deletions api/Hmcr.Chris/Hmcr.Chris.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<ItemGroup>
<None Remove="XmlTemplates\IsPointOnRfiSegment.xml" />
<None Remove="XmlTemplates\IsPointWithinServiceArea.xml" />
<None Remove="XmlTemplates\GetInventoryAssocWithWorkActivity.xml" />
</ItemGroup>

<ItemGroup>
Expand All @@ -16,6 +17,9 @@
<Content Include="XmlTemplates\IsPointWithinServiceArea.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="XmlTemplates\GetInventoryAssocWithWorkActivity.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
Expand Down
85 changes: 85 additions & 0 deletions api/Hmcr.Chris/InventoryApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using Hmcr.Chris.Models;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;

namespace Hmcr.Chris
{
public interface IInventoryApi
{
Task<List<SurfaceType>> GetSurfaceTypeAssociatedWithLine(string lineStringCoordinates);
Task<SurfaceType> GetSurfaceTypeAssociatedWithPoint(string lineStringCoordinates);

//TODO: implement MC calls to CHRIS

}

public class InventoryApi : IInventoryApi
{
private HttpClient _client;
private InventoryQueries _queries;
private IApi _api;
private string _path;

/// <summary>
/// Chris Query typeName values, used to call the Get Inventory
/// Assoc with Work Activity Query.
/// </summary>
public static class InventoryQueryTypeName
{
public const string SURF_ASSOC_WITH_LINE = "SURF_ASSOCIATED_WITH_LINE";
public const string SURF_ASSOC_WITH_POINT = "SURF_ASSOCIATED_WITH_POINT";
public const string MC_ASSOC_WITH_LINE = "MC_ASSOCIATED_WITH_LINE";
public const string MC_ASSOC_WITH_POINT = "MC_ASSOCIATED_WITH_POINT";
}

public InventoryApi(HttpClient client, IApi api, IConfiguration config)
{
_client = client;
_queries = new InventoryQueries();
_api = api;
_path = config.GetValue<string>("CHRIS:OASPath");
}

public async Task<SurfaceType> GetSurfaceTypeAssociatedWithPoint(string lineStringCoordinates)
{
var body = string.Format(_queries.SurfaceTypeAssocWithPointQuery, lineStringCoordinates, InventoryQueryTypeName.SURF_ASSOC_WITH_POINT);

var contents = await (await _api.PostWithRetry(_client, _path, body)).Content.ReadAsStringAsync();

var results = JsonSerializer.Deserialize<FeatureCollection<object>>(contents);

SurfaceType surfaceType = new SurfaceType();
if (results.features.Length > 0)
{
surfaceType.Type = results.features[0].properties.SURFACE_TYPE;
}

return surfaceType;
}

public async Task<List<SurfaceType>> GetSurfaceTypeAssociatedWithLine(string lineStringCoordinates)
{
var body = string.Format(_queries.SurfaceTypeAssocWithLineQuery, lineStringCoordinates, InventoryQueryTypeName.SURF_ASSOC_WITH_LINE);

var contents = await (await _api.PostWithRetry(_client, _path, body)).Content.ReadAsStringAsync();

var results = JsonSerializer.Deserialize<FeatureCollection<object>>(contents);

var surfaceTypes = new List<SurfaceType>();

foreach (var feature in results.features)
{
SurfaceType surfaceType = new SurfaceType();
surfaceType.Length = feature.properties.CLIPPED_LENGTH_KM;
surfaceType.Type = feature.properties.SURFACE_TYPE;

surfaceTypes.Add(surfaceType);
}

return surfaceTypes;
}
}
}
30 changes: 30 additions & 0 deletions api/Hmcr.Chris/InventoryQueries.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.IO;
using System.Reflection;

namespace Hmcr.Chris
{
public class InventoryQueries
{
private string _surfaceTypeAssocWithLineQuery;
private string _surfaceTypeAssocWithPointQuery;

public string SurfaceTypeAssocWithLineQuery
{
get
{
var folder = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "XmlTemplates");
return _surfaceTypeAssocWithLineQuery ?? (_surfaceTypeAssocWithLineQuery = File.ReadAllText(Path.Combine(folder, "GetInventoryAssocWithWorkActivity.xml")));
}
}

public string SurfaceTypeAssocWithPointQuery
{
get
{
var folder = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "XmlTemplates");
return _surfaceTypeAssocWithPointQuery ?? (_surfaceTypeAssocWithPointQuery = File.ReadAllText(Path.Combine(folder, "GetInventoryAssocWithWorkActivity.xml")));
}
}

}
}
2 changes: 2 additions & 0 deletions api/Hmcr.Chris/Models/Property.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ public class Property
public string NE_DESCR { get; set; }
public float MEASURE { get; set; }
public double POINT_VARIANCE { get; set; }
public string SURFACE_TYPE { get; set; }
public double CLIPPED_LENGTH_KM { get; set; }
}
}
31 changes: 31 additions & 0 deletions api/Hmcr.Chris/Models/SurfaceType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Hmcr.Chris.Models
{
public class SurfaceType
{

public Geometry geometry { get; set; }
/// <summary>
/// Length in Meters
/// </summary>
public double Length { get; set; }

public string Type { get; set; }

}
public class SurfaceType<T>
{

public Geometry geometry { get; set; }
/// <summary>
/// Length in Meters
/// </summary>
public double Length { get; set; }

public string Type { get; set; }

}
}
14 changes: 14 additions & 0 deletions api/Hmcr.Chris/XmlTemplates/GetInventoryAssocWithWorkActivity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<wfs:GetFeature service="WFS"
version="1.1.0"
outputFormat="json"
maxFeatures="50"
viewParams="epsg:4326;coordinates:{0}"
xmlns:topp="http://www.openplans.org/topp"
xmlns:wfs="http://www.opengis.net/wfs"
xmlns="http://www.opengis.net/ogc"
xmlns:gml="http://www.opengis.net/gml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/wfs
http://schemas.opengis.net/wfs/1.1.0/WFS-basic.xsd">
<wfs:Query typeName="cwr:{1}" srsName="EPSG:3005"/>
</wfs:GetFeature>
Loading

0 comments on commit a91e997

Please sign in to comment.