Skip to content

Commit

Permalink
feat: Export PDF-attachments in addition
Browse files Browse the repository at this point in the history
Refs: #289
  • Loading branch information
grigoriev committed Nov 28, 2024
1 parent 7556d2c commit 6b40eb8
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package ch.sbb.polarion.extension.pdf_exporter.util;

import ch.sbb.polarion.extension.generic.regex.RegexMatcher;
import lombok.experimental.UtilityClass;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@UtilityClass
public class WildcardUtils {

public static @NotNull String toRegex(@Nullable String wildcard) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,11 @@ public BulkPdfExportWidgetRenderer(@NotNull RichPageWidgetCommonContext context)
String sort = sortByParameter.asLuceneSortString();
this.dataSet = dataSetParameter.getFor().sort(sort).revision(null);
this.items = this.dataSet.items();
if (!items.isEmpty()) {
switch (items.iterator().next().getOldApi().getPrototype().getName()) {
case "Module": dataType = "Documents"; break;
case "RichPage": dataType = "Pages"; break;
case "TestRun": dataType = "Test Runs"; break;
default: dataType = "Unknown"; break;
}
} else {
dataType = "Unknown";
switch (dataSetParameter.prototype()) {
case Document: dataType = "Documents"; break;
case RichPage: dataType = "Pages"; break;
case TestRun: dataType = "Test Runs"; break;
default: dataType = "Unknown"; break;
}
this.columns = columnsParameter.fields();

Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/webapp/pdf-exporter/html/popupForm.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
Mark referenced Workitems
</label>
</div>
<div class='property-wrapper'>
<div class='property-wrapper hidden visible-for-test-run visible-for-mixed'>
<label for='popup-download-attachments'>
<input id='popup-download-attachments' onchange='document.getElementById("popup-attachments-filter").style.visibility = this.checked ? "visible" : "hidden"' type='checkbox'/>
Download attachments
Expand Down Expand Up @@ -192,7 +192,7 @@
</div>
</div>

<div class='property-wrapper not-mixed'>
<div class='property-wrapper not-visible-for-mixed'>
<label for='popup-filename'>File name:</label>
<input id='popup-filename' type='text' style="width: 594px" />
</div>
Expand Down
51 changes: 2 additions & 49 deletions src/main/resources/webapp/pdf-exporter/js/bulk-pdf-exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ const BulkPdfExporter = {
const documentId = currentItem.dataset["id"];
if (documentType === ExportParams.DocumentType.TEST_RUN) {
this.exportParams["urlQueryParameters"] = { id: documentId };
this.downloadTestRunAttachments(this.exportParams.projectId, documentId, this.exportParams.revision, this.exportParams.attachmentsFilter);
ExportCommon.downloadTestRunAttachments(this.exportParams.projectId, documentId, this.exportParams.revision, this.exportParams.attachmentsFilter);
} else {
this.exportParams["locationPath"] = `${currentItem.dataset["space"]}/${documentId}`;
}
Expand All @@ -224,7 +224,7 @@ const BulkPdfExporter = {
BulkPdfExporter.finishedCount += 1;
BulkPdfExporter.updateState(BULK_EXPORT_IN_PROGRESS);
const downloadFileName = fileName || `${currentItem.dataset["space"] ? currentItem.dataset["space"] + "_" : ""}${documentId}.pdf`; // Fallback if file name wasn't received in response
this.downloadBlob(responseBody, downloadFileName);
ExportCommon.downloadBlob(responseBody, downloadFileName);
this.startNextItemExport();
}, errorResponse => {
this.errors = true;
Expand Down Expand Up @@ -255,53 +255,6 @@ const BulkPdfExporter = {
}
},

downloadTestRunAttachments: function (projectId, testRunId, revision = null, filter = null) {
let url = `/polarion/pdf-exporter/rest/internal/projects/${projectId}/testruns/${testRunId}/attachments?`;
if (revision) url += `&revision=${revision}`;
if (filter) url += `&filter=${filter}`;

SbbCommon.callAsync({
method: "GET",
url: url,
responseType: "json",
onOk: (responseText, request) => {
for (const attachment of request.response) {
this.downloadAttachmentContent(projectId, testRunId, attachment.id, revision);
}
},
onError: (status, errorMessage, request) => {
console.error('Error fetching attachments:', request.response);
}
});
},

downloadAttachmentContent: function (projectId, testRunId, attachmentId, revision = null) {
let url = `/polarion/pdf-exporter/rest/internal/projects/${projectId}/testruns/${testRunId}/attachments/${attachmentId}/content?`;
if (revision) url += `&revision=${revision}`;

SbbCommon.callAsync({
method: "GET",
url: url,
responseType: "blob",
onOk: (responseText, request) => {
this.downloadBlob(request.response, request.getResponseHeader("Filename"));
},
onError: (status, errorMessage, request) => {
console.error(`Error downloading attachment ${attachmentId}:`, request.response);
}
});
},

downloadBlob: function(blob, fileName) {
const objectURL = (window.URL ? window.URL : window.webkitURL).createObjectURL(blob);
const link = document.createElement("a");
link.href = objectURL;
link.download = fileName
link.target = "_blank";
link.click();
link.remove();
setTimeout(() => URL.revokeObjectURL(objectURL), 100);
}
}

BulkPdfExporter.init();
48 changes: 48 additions & 0 deletions src/main/resources/webapp/pdf-exporter/js/export-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,52 @@ const ExportCommon = {
});
},

downloadTestRunAttachments: function (projectId, testRunId, revision = null, filter = null) {
let url = `/polarion/pdf-exporter/rest/internal/projects/${projectId}/testruns/${testRunId}/attachments?`;
if (revision) url += `&revision=${revision}`;
if (filter) url += `&filter=${filter}`;

SbbCommon.callAsync({
method: "GET",
url: url,
responseType: "json",
onOk: (responseText, request) => {
for (const attachment of request.response) {
this.downloadAttachmentContent(projectId, testRunId, attachment.id, revision);
}
},
onError: (status, errorMessage, request) => {
console.error('Error fetching attachments:', request.response);
}
});
},

downloadAttachmentContent: function (projectId, testRunId, attachmentId, revision = null) {
let url = `/polarion/pdf-exporter/rest/internal/projects/${projectId}/testruns/${testRunId}/attachments/${attachmentId}/content?`;
if (revision) url += `&revision=${revision}`;

SbbCommon.callAsync({
method: "GET",
url: url,
responseType: "blob",
onOk: (responseText, request) => {
this.downloadBlob(request.response, request.getResponseHeader("Filename"));
},
onError: (status, errorMessage, request) => {
console.error(`Error downloading attachment ${attachmentId}:`, request.response);
}
});
},

downloadBlob: function(blob, fileName) {
const objectURL = (window.URL ? window.URL : window.webkitURL).createObjectURL(blob);
const link = document.createElement("a");
link.href = objectURL;
link.download = fileName
link.target = "_blank";
link.click();
link.remove();
setTimeout(() => URL.revokeObjectURL(objectURL), 100);
},

}
10 changes: 2 additions & 8 deletions src/main/resources/webapp/pdf-exporter/js/export-pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,8 @@ const ExportPdf = {

ExportCommon.asyncConvertPdf(request, successResponse => {
this.actionInProgress(false);
const objectURL = (window.URL ? window.URL : window.webkitURL).createObjectURL(successResponse);
const anchorElement = document.createElement("a");
anchorElement.href = objectURL;
anchorElement.download = fileName;
anchorElement.target = "_blank";
anchorElement.click();
anchorElement.remove();
setTimeout(() => URL.revokeObjectURL(objectURL), 100);

ExportCommon.downloadBlob(successResponse, fileName);
}, errorResponse => {
this.actionInProgress(false);
errorResponse.text().then(errorJson => {
Expand Down
18 changes: 9 additions & 9 deletions src/main/resources/webapp/pdf-exporter/js/pdf-exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ const PdfExporter = {
.forEach(propertyBlock => propertyBlock.style.display = "flex");
break;
case ExportParams.DocumentType.MIXED:
document.querySelectorAll(".modal__container.pdf-exporter .property-wrapper.not-mixed")
document.querySelectorAll(".modal__container.pdf-exporter .property-wrapper.not-visible-for-mixed")
.forEach(propertyBlock => propertyBlock.style.display = "none");
document.querySelectorAll(".modal__container.pdf-exporter .property-wrapper.visible-for-mixed")
.forEach(propertyBlock => propertyBlock.style.display = "flex");
break;
}
MicroModal.show(POPUP_ID);
Expand Down Expand Up @@ -442,6 +444,11 @@ const PdfExporter = {
return;
}

if (this.exportContext.getDocumentType() === ExportParams.DocumentType.TEST_RUN) {
const testRunId = new URLSearchParams(this.exportContext.getUrlQueryParameters()).get("id")
ExportCommon.downloadTestRunAttachments(exportParams.projectId, testRunId, exportParams.revision, exportParams.attachmentsFilter);
}

this.actionInProgress({inProgress: true, message: "Generating PDF"})

const requestBody = exportParams.toJSON();
Expand All @@ -450,14 +457,7 @@ const PdfExporter = {
}

ExportCommon.asyncConvertPdf(requestBody, responseBody => {
const objectURL = (window.URL ? window.URL : window.webkitURL).createObjectURL(responseBody);
const anchorElement = document.createElement("a");
anchorElement.href = objectURL;
anchorElement.download = fileName;
anchorElement.target = "_blank";
anchorElement.click();
anchorElement.remove();
setTimeout(() => URL.revokeObjectURL(objectURL), 100);
ExportCommon.downloadBlob(responseBody, fileName);

this.showNotification({alertType: "success", message: "PDF was successfully generated"});
this.actionInProgress({inProgress: false});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class WildcardUtilsTest {

static Stream<Arguments> provideToRegexArguments() {
return Stream.of(
Arguments.of(null, ".*"),
Arguments.of("", ".*"),
Arguments.of("*.txt", "^.*\\.txt$"),
Arguments.of("file?.doc", "^file.\\.doc$"),
Arguments.of("doc*", "^doc.*$"),
Expand All @@ -29,6 +31,8 @@ void testToRegex(String wildcard, String expectedRegex) {

static Stream<Arguments> provideMatchesArguments() {
return Stream.of(
Arguments.of(null, "*.txt", false),
Arguments.of("", "*.txt", false),
Arguments.of("file.txt", "*.txt", true),
Arguments.of("file.doc", "*.txt", false),
Arguments.of("doc1.doc", "doc?.doc", true),
Expand Down

0 comments on commit 6b40eb8

Please sign in to comment.