Skip to content

Commit

Permalink
Added documents dynamic input
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalii-bezuhlyi committed Mar 22, 2024
1 parent 26da9ed commit a4b1345
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 12 deletions.
18 changes: 10 additions & 8 deletions Apps.Memoq/Actions/FileActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,11 @@ public async Task<UploadFileResponse> UploadAndImportFileToProject(
DocumentGuid = result.DocumentGuids.Select(x => x.ToString()).First()
};
}

[Action("Re-import document", Description = "Uploads and re-imports a document to a project")]
public async Task<UploadFileResponse> UploadAndReimportFileToProject(
[ActionParameter] UploadDocumentToProjectRequest request, [ActionParameter] ReimportDocumentsRequest reimportDocumentsRequest)
[ActionParameter] UploadDocumentToProjectRequest request,
[ActionParameter] ReimportDocumentsRequest reimportDocumentsRequest)
{
using var fileService = new MemoqServiceFactory<IFileManagerService>(
SoapConstants.FileServiceUrl, Creds);
Expand Down Expand Up @@ -254,13 +255,13 @@ public async Task<UploadFileResponse> UploadAndReimportFileToProject(
throw new InvalidOperationException(
$"Error while importing file, result status: {result.First(x => x.ResultStatus == ResultStatus.Error).ResultStatus}, message: {result.First(x => x.ResultStatus == ResultStatus.Error).DetailedMessage}");
}

var first = result.FirstOrDefault(x => x.ResultStatus == ResultStatus.Success);
if (first is null)
{
throw new InvalidOperationException("No successful reimport found");
}

return new()
{
// Right now we have 1 target language, so 1 document GUID. If we have multiple target files, this should be changed as well or we need an extra action
Expand Down Expand Up @@ -363,6 +364,7 @@ public async Task<DownloadFileResponse> DownloadFileByGuid(
[Action("Export document as XLIFF",
Description = "Exports and downloads the translation document as XLIFF (MQXLIFF) bilingual")]
public async Task<DownloadFileResponse> DownloadFileAsXliff(
[ActionParameter] GetDocumentRequest documentRequest,
[ActionParameter] DownloadXliffRequest request)
{
using var fileService = new MemoqServiceFactory<IFileManagerService>(
Expand All @@ -374,8 +376,8 @@ public async Task<DownloadFileResponse> DownloadFileAsXliff(
var includeSkeleton = request.IncludeSkeleton ?? false;

var exportResult = await projectService.Service
.ExportTranslationDocumentAsXliffBilingualAsync(Guid.Parse(request.ProjectGuid),
Guid.Parse(request.DocumentGuid), new XliffBilingualExportOptions
.ExportTranslationDocumentAsXliffBilingualAsync(Guid.Parse(documentRequest.ProjectGuid),
Guid.Parse(documentRequest.DocumentGuid), new XliffBilingualExportOptions
{
FullVersionHistory = fullVersion,
IncludeSkeleton = includeSkeleton,
Expand All @@ -385,8 +387,8 @@ public async Task<DownloadFileResponse> DownloadFileAsXliff(
var data = DownloadFile(fileService.Service, exportResult.FileGuid, out var filename);
var document = GetFile(new()
{
ProjectGuid = request.ProjectGuid
}, request.DocumentGuid);
ProjectGuid = documentRequest.ProjectGuid
}, documentRequest.DocumentGuid);

using var stream = new MemoryStream(data);

Expand Down
50 changes: 50 additions & 0 deletions Apps.Memoq/DataSourceHandlers/DocumentDataHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Apps.Memoq.Contracts;
using Apps.Memoq.Models;
using Apps.Memoq.Models.Files.Requests;
using Blackbird.Applications.Sdk.Common;
using Blackbird.Applications.Sdk.Common.Authentication;
using Blackbird.Applications.Sdk.Common.Dynamic;
using Blackbird.Applications.Sdk.Common.Invocation;
using MQS.ServerProject;

namespace Apps.Memoq.DataSourceHandlers;

public class DocumentDataHandler : BaseInvocable, IDataSourceHandler
{
private IEnumerable<AuthenticationCredentialsProvider> Creds =>
InvocationContext.AuthenticationCredentialsProviders;

private readonly string _projectGuid;

public DocumentDataHandler(InvocationContext invocationContext, [ActionParameter] GetDocumentRequest request) :
base(invocationContext)
{
_projectGuid = request.ProjectGuid;
}

public Dictionary<string, string> GetData(DataSourceContext context)
{
if (string.IsNullOrEmpty(_projectGuid))
{
throw new InvalidOperationException("You should input a project guid first");
}

using var projectService = new MemoqServiceFactory<IServerProjectService>(
SoapConstants.ProjectServiceUrl, Creds);

var response = projectService.Service.ListProjectTranslationDocumentsGroupedBySourceFile(
Guid.Parse(_projectGuid));

var files = response
.SelectMany(x => x.Groups)
.SelectMany(x => x.Documents)
.ToList();

return files
.Where(x => context.SearchString is null ||
x.Name.Contains(context.SearchString, StringComparison.OrdinalIgnoreCase))
.OrderByDescending(x => x.Name)
.Take(20)
.ToDictionary(x => x.DocumentGuid.ToString(), x => x.Name);
}
}
14 changes: 14 additions & 0 deletions Apps.Memoq/Models/Files/Requests/GetDocumentRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Apps.Memoq.DataSourceHandlers;
using Blackbird.Applications.Sdk.Common;
using Blackbird.Applications.Sdk.Common.Dynamic;

namespace Apps.Memoq.Models.Files.Requests;

public class GetDocumentRequest
{
[Display("Project"), DataSource(typeof(ProjectDataHandler))]
public string ProjectGuid { get; set; }

[Display("Document GUID"), DataSource(typeof(DocumentDataHandler))]
public string DocumentGuid { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

namespace Apps.Memoq.Models.ServerProjects.Requests
{
public class DownloadXliffRequest : ProjectRequest
public class DownloadXliffRequest
{
[Display("Document GUID")]
public string DocumentGuid { get; set; }

[Display("Include full version history?")]
public bool? FullVersionHistory { get; set; }

Expand Down

0 comments on commit a4b1345

Please sign in to comment.