Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apr 1635 extract submitted application #545

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
196 changes: 196 additions & 0 deletions src/SFA.DAS.ApplyService.ApplicationExtract/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Internal;
using Microsoft.Extensions.Logging.Abstractions;
using Newtonsoft.Json;
using SFA.DAS.ApplyService.Configuration;
using SFA.DAS.ApplyService.Domain.Apply;
using SFA.DAS.ApplyService.Domain.Entities;
using SFA.DAS.ApplyService.Web.Infrastructure;

namespace SFA.DAS.ApplyService.ApplicationExtract
{
class Program
{
private static QnaApiClient _qnaClient;

static async Task Main(string[] args)
{
var configService = new ConfigService();
configService.GetQnaApiConfig();

Console.Clear();
Console.WriteLine("Enter the output file path:");
var outputPath = Console.ReadLine();

Console.Clear();
Console.WriteLine("Enter the application id (GUID):");
var applicationId = Guid.Parse(Console.ReadLine());

outputPath = Path.Combine(outputPath, applicationId.ToString());

_qnaClient = new QnaApiClient(configService, new NullLogger<QnaApiClient>(), new QnaTokenService(configService, new HostingEnvironment { EnvironmentName = EnvironmentName.Staging }));
var result = await _qnaClient.GetSections(applicationId);

foreach (var section in result.Where(x => !x.NotRequired && x.PagesActive > 0))
{
await WriteSectionQuestionsAndAnswers(section, outputPath);
}

Console.Clear();
Console.WriteLine("All QnA data has been saved");
Console.Write("Press any key to exit");
Console.Read();
}

private static async Task WriteSectionQuestionsAndAnswers(ApplicationSection section, string outputPath)
{
var outputSection = new Section();
outputSection.Title = section.Title;

var directoryPath = Path.Combine(outputPath, "Sequence " + section.SequenceId);
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}

foreach (var page in section.QnAData.Pages.Where(x => x.Active && !x.NotRequired))
{
foreach (var question in page.Questions)
{
await OutputQuestion(section, question, page, outputSection, directoryPath);
}
}

var outputString = JsonConvert.SerializeObject(outputSection, Formatting.Indented);
var filePath = Path.Combine(directoryPath, section.SectionId + " - " + section.LinkTitle + ".json");
File.WriteAllText(filePath, outputString);
}

private static async Task OutputQuestion(ApplicationSection section, Domain.Apply.Question question, Page page, Section outputSection, string directoryPath)
{
var questionText = question.Label;
var questionAnswer = page.PageOfAnswers.SingleOrDefault()?.Answers.SingleOrDefault(x => x.QuestionId == question.QuestionId)?.Value;
if (question.Input.Type == "TabularData")
{
if (!string.IsNullOrEmpty(questionAnswer))
{
var tabularData = JsonConvert.DeserializeObject<TabularData>(questionAnswer);
questionText = tabularData.Caption ?? question.Label;
questionAnswer = "";
foreach (var column in tabularData.HeadingTitles)
{
questionText += " - ";
questionText += column;
}

foreach (var row in tabularData.DataRows)
{
questionAnswer += "\n";
foreach (var column in row.Columns)
{
questionAnswer += column;
questionAnswer += " - ";
}
}
}
}

if (!string.IsNullOrEmpty(questionAnswer))
{
if (string.IsNullOrEmpty(questionText))
{
questionText = question.QuestionTag;
}

outputSection.Questions.Add(new Question
{
QuestionText = questionText,
Answer = questionAnswer
});
}

if (question.Input.Type == "FileUpload" && !string.IsNullOrEmpty(questionAnswer))
{
await DownloadFile(section.ApplicationId, section.Id, page.PageId, question.QuestionId, questionAnswer, directoryPath);
}

if (question.Input.Options != null)
{
foreach (var option in question.Input.Options)
{
if (option.FurtherQuestions != null)
{
foreach (var furtherQuestion in option.FurtherQuestions)
{
await OutputQuestion(section, furtherQuestion, page, outputSection, directoryPath);
}
}
}
}
}

private static async Task DownloadFile(Guid applicationId, Guid sectionId, string pageId, string questionId, string fileName, string directoryPath)
{
var fileResponse = await _qnaClient.DownloadFile(applicationId, sectionId, pageId, questionId, fileName);

var outputStream = File.Create(Path.Combine(directoryPath, fileName));
await fileResponse.Content.CopyToAsync(outputStream);
outputStream.Close();
}
}

public class ConfigService : IConfigurationService
{
private string _qnaUrl;
private string _qnaTenantId;
private string _qnaClientId;
private string _qnaClientSecret;
private string _qnaResourceId;

public void GetQnaApiConfig()
{
Console.WriteLine("Enter the QnA API base url:");
_qnaUrl = Console.ReadLine();

Console.WriteLine();
Console.WriteLine("Enter the QnA API tenant id:");
_qnaTenantId = Console.ReadLine();

Console.WriteLine();
Console.WriteLine("Enter the QnA API client id:");
_qnaClientId = Console.ReadLine();

Console.WriteLine();
Console.WriteLine("Enter the QnA API client secret:");
_qnaClientSecret = Console.ReadLine();

Console.WriteLine();
Console.WriteLine("Enter the QnA API resource id:");
_qnaResourceId = Console.ReadLine();
}

public Task<IApplyConfig> GetConfig()
{
return Task.FromResult(new ApplyConfig
{
QnaApiAuthentication = new QnaApiAuthentication
{
ApiBaseAddress = _qnaUrl,
ClientId = _qnaClientId,
ClientSecret = _qnaClientSecret,
ResourceId = _qnaResourceId,
TenantId = _qnaTenantId
}
} as IApplyConfig);
}

public string GetEnvironmentName()
{
return "PP";
}
}
}
8 changes: 8 additions & 0 deletions src/SFA.DAS.ApplyService.ApplicationExtract/Question.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SFA.DAS.ApplyService.ApplicationExtract
{
public class Question
{
public string QuestionText { get; set; }
public string Answer { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\SFA.DAS.ApplyService.Web\SFA.DAS.ApplyService.Web.csproj" />
</ItemGroup>

</Project>
18 changes: 18 additions & 0 deletions src/SFA.DAS.ApplyService.ApplicationExtract/Section.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace SFA.DAS.ApplyService.ApplicationExtract
{
public class Section
{
public Section()
{
Questions = new List<Question>();
}

public string Title { get; set; }

public List<Question> Questions { get; set; }
}
}
11 changes: 10 additions & 1 deletion src/SFA.DAS.ApplyService.sln
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SFA.DAS.ApplyService.Web.Un
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SFA.DAS.ApplyService.Web.IntegrationTests", "SFA.DAS.ApplyService.Web.IntegrationTests\SFA.DAS.ApplyService.Web.IntegrationTests.csproj", "{1F7E8D8A-E6AE-40EB-AB77-52176FD6FFC4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SFA.DAS.ApplyService.Infrastructure", "SFA.DAS.ApplyService.Infrastructure\SFA.DAS.ApplyService.Infrastructure.csproj", "{7FFA630A-F08B-458D-8483-D6681DEC46D4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SFA.DAS.ApplyService.Infrastructure", "SFA.DAS.ApplyService.Infrastructure\SFA.DAS.ApplyService.Infrastructure.csproj", "{7FFA630A-F08B-458D-8483-D6681DEC46D4}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Utilities", "Utilities", "{FC032185-A409-4A21-AB2E-BA43A4F4EC22}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SFA.DAS.ApplyService.ApplicationExtract", "SFA.DAS.ApplyService.ApplicationExtract\SFA.DAS.ApplyService.ApplicationExtract.csproj", "{0B475A79-34E3-412E-AF47-0B655054639C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -124,6 +128,10 @@ Global
{7FFA630A-F08B-458D-8483-D6681DEC46D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7FFA630A-F08B-458D-8483-D6681DEC46D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7FFA630A-F08B-458D-8483-D6681DEC46D4}.Release|Any CPU.Build.0 = Release|Any CPU
{0B475A79-34E3-412E-AF47-0B655054639C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0B475A79-34E3-412E-AF47-0B655054639C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0B475A79-34E3-412E-AF47-0B655054639C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0B475A79-34E3-412E-AF47-0B655054639C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -144,6 +152,7 @@ Global
{8A3BE608-B568-4D87-9F4B-59559CF573E9} = {CB25C537-3812-4629-A142-6B84EF294A9E}
{EAD0B0D6-40F4-4560-BA63-A9E7427E3CCF} = {F48066D2-4DB1-4EA4-B41D-01E99498632F}
{1F7E8D8A-E6AE-40EB-AB77-52176FD6FFC4} = {F48066D2-4DB1-4EA4-B41D-01E99498632F}
{0B475A79-34E3-412E-AF47-0B655054639C} = {FC032185-A409-4A21-AB2E-BA43A4F4EC22}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F9606DF2-1B9C-412A-AA3D-F741341EC9E2}
Expand Down