Skip to content

Commit 6b40eb8

Browse files
committed
feat: Export PDF-attachments in addition
Refs: #289
1 parent 7556d2c commit 6b40eb8

File tree

8 files changed

+74
-77
lines changed

8 files changed

+74
-77
lines changed

src/main/java/ch/sbb/polarion/extension/pdf_exporter/util/WildcardUtils.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package ch.sbb.polarion.extension.pdf_exporter.util;
22

33
import ch.sbb.polarion.extension.generic.regex.RegexMatcher;
4+
import lombok.experimental.UtilityClass;
45
import org.jetbrains.annotations.NotNull;
56
import org.jetbrains.annotations.Nullable;
67

8+
@UtilityClass
79
public class WildcardUtils {
810

911
public static @NotNull String toRegex(@Nullable String wildcard) {

src/main/java/ch/sbb/polarion/extension/pdf_exporter/widgets/BulkPdfExportWidgetRenderer.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,11 @@ public BulkPdfExportWidgetRenderer(@NotNull RichPageWidgetCommonContext context)
4040
String sort = sortByParameter.asLuceneSortString();
4141
this.dataSet = dataSetParameter.getFor().sort(sort).revision(null);
4242
this.items = this.dataSet.items();
43-
if (!items.isEmpty()) {
44-
switch (items.iterator().next().getOldApi().getPrototype().getName()) {
45-
case "Module": dataType = "Documents"; break;
46-
case "RichPage": dataType = "Pages"; break;
47-
case "TestRun": dataType = "Test Runs"; break;
48-
default: dataType = "Unknown"; break;
49-
}
50-
} else {
51-
dataType = "Unknown";
43+
switch (dataSetParameter.prototype()) {
44+
case Document: dataType = "Documents"; break;
45+
case RichPage: dataType = "Pages"; break;
46+
case TestRun: dataType = "Test Runs"; break;
47+
default: dataType = "Unknown"; break;
5248
}
5349
this.columns = columnsParameter.fields();
5450

src/main/resources/webapp/pdf-exporter/html/popupForm.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
Mark referenced Workitems
143143
</label>
144144
</div>
145-
<div class='property-wrapper'>
145+
<div class='property-wrapper hidden visible-for-test-run visible-for-mixed'>
146146
<label for='popup-download-attachments'>
147147
<input id='popup-download-attachments' onchange='document.getElementById("popup-attachments-filter").style.visibility = this.checked ? "visible" : "hidden"' type='checkbox'/>
148148
Download attachments
@@ -192,7 +192,7 @@
192192
</div>
193193
</div>
194194

195-
<div class='property-wrapper not-mixed'>
195+
<div class='property-wrapper not-visible-for-mixed'>
196196
<label for='popup-filename'>File name:</label>
197197
<input id='popup-filename' type='text' style="width: 594px" />
198198
</div>

src/main/resources/webapp/pdf-exporter/js/bulk-pdf-exporter.js

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ const BulkPdfExporter = {
212212
const documentId = currentItem.dataset["id"];
213213
if (documentType === ExportParams.DocumentType.TEST_RUN) {
214214
this.exportParams["urlQueryParameters"] = { id: documentId };
215-
this.downloadTestRunAttachments(this.exportParams.projectId, documentId, this.exportParams.revision, this.exportParams.attachmentsFilter);
215+
ExportCommon.downloadTestRunAttachments(this.exportParams.projectId, documentId, this.exportParams.revision, this.exportParams.attachmentsFilter);
216216
} else {
217217
this.exportParams["locationPath"] = `${currentItem.dataset["space"]}/${documentId}`;
218218
}
@@ -224,7 +224,7 @@ const BulkPdfExporter = {
224224
BulkPdfExporter.finishedCount += 1;
225225
BulkPdfExporter.updateState(BULK_EXPORT_IN_PROGRESS);
226226
const downloadFileName = fileName || `${currentItem.dataset["space"] ? currentItem.dataset["space"] + "_" : ""}${documentId}.pdf`; // Fallback if file name wasn't received in response
227-
this.downloadBlob(responseBody, downloadFileName);
227+
ExportCommon.downloadBlob(responseBody, downloadFileName);
228228
this.startNextItemExport();
229229
}, errorResponse => {
230230
this.errors = true;
@@ -255,53 +255,6 @@ const BulkPdfExporter = {
255255
}
256256
},
257257

258-
downloadTestRunAttachments: function (projectId, testRunId, revision = null, filter = null) {
259-
let url = `/polarion/pdf-exporter/rest/internal/projects/${projectId}/testruns/${testRunId}/attachments?`;
260-
if (revision) url += `&revision=${revision}`;
261-
if (filter) url += `&filter=${filter}`;
262-
263-
SbbCommon.callAsync({
264-
method: "GET",
265-
url: url,
266-
responseType: "json",
267-
onOk: (responseText, request) => {
268-
for (const attachment of request.response) {
269-
this.downloadAttachmentContent(projectId, testRunId, attachment.id, revision);
270-
}
271-
},
272-
onError: (status, errorMessage, request) => {
273-
console.error('Error fetching attachments:', request.response);
274-
}
275-
});
276-
},
277-
278-
downloadAttachmentContent: function (projectId, testRunId, attachmentId, revision = null) {
279-
let url = `/polarion/pdf-exporter/rest/internal/projects/${projectId}/testruns/${testRunId}/attachments/${attachmentId}/content?`;
280-
if (revision) url += `&revision=${revision}`;
281-
282-
SbbCommon.callAsync({
283-
method: "GET",
284-
url: url,
285-
responseType: "blob",
286-
onOk: (responseText, request) => {
287-
this.downloadBlob(request.response, request.getResponseHeader("Filename"));
288-
},
289-
onError: (status, errorMessage, request) => {
290-
console.error(`Error downloading attachment ${attachmentId}:`, request.response);
291-
}
292-
});
293-
},
294-
295-
downloadBlob: function(blob, fileName) {
296-
const objectURL = (window.URL ? window.URL : window.webkitURL).createObjectURL(blob);
297-
const link = document.createElement("a");
298-
link.href = objectURL;
299-
link.download = fileName
300-
link.target = "_blank";
301-
link.click();
302-
link.remove();
303-
setTimeout(() => URL.revokeObjectURL(objectURL), 100);
304-
}
305258
}
306259

307260
BulkPdfExporter.init();

src/main/resources/webapp/pdf-exporter/js/export-common.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,52 @@ const ExportCommon = {
6565
});
6666
},
6767

68+
downloadTestRunAttachments: function (projectId, testRunId, revision = null, filter = null) {
69+
let url = `/polarion/pdf-exporter/rest/internal/projects/${projectId}/testruns/${testRunId}/attachments?`;
70+
if (revision) url += `&revision=${revision}`;
71+
if (filter) url += `&filter=${filter}`;
72+
73+
SbbCommon.callAsync({
74+
method: "GET",
75+
url: url,
76+
responseType: "json",
77+
onOk: (responseText, request) => {
78+
for (const attachment of request.response) {
79+
this.downloadAttachmentContent(projectId, testRunId, attachment.id, revision);
80+
}
81+
},
82+
onError: (status, errorMessage, request) => {
83+
console.error('Error fetching attachments:', request.response);
84+
}
85+
});
86+
},
87+
88+
downloadAttachmentContent: function (projectId, testRunId, attachmentId, revision = null) {
89+
let url = `/polarion/pdf-exporter/rest/internal/projects/${projectId}/testruns/${testRunId}/attachments/${attachmentId}/content?`;
90+
if (revision) url += `&revision=${revision}`;
91+
92+
SbbCommon.callAsync({
93+
method: "GET",
94+
url: url,
95+
responseType: "blob",
96+
onOk: (responseText, request) => {
97+
this.downloadBlob(request.response, request.getResponseHeader("Filename"));
98+
},
99+
onError: (status, errorMessage, request) => {
100+
console.error(`Error downloading attachment ${attachmentId}:`, request.response);
101+
}
102+
});
103+
},
104+
105+
downloadBlob: function(blob, fileName) {
106+
const objectURL = (window.URL ? window.URL : window.webkitURL).createObjectURL(blob);
107+
const link = document.createElement("a");
108+
link.href = objectURL;
109+
link.download = fileName
110+
link.target = "_blank";
111+
link.click();
112+
link.remove();
113+
setTimeout(() => URL.revokeObjectURL(objectURL), 100);
114+
},
115+
68116
}

src/main/resources/webapp/pdf-exporter/js/export-pdf.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,8 @@ const ExportPdf = {
220220

221221
ExportCommon.asyncConvertPdf(request, successResponse => {
222222
this.actionInProgress(false);
223-
const objectURL = (window.URL ? window.URL : window.webkitURL).createObjectURL(successResponse);
224-
const anchorElement = document.createElement("a");
225-
anchorElement.href = objectURL;
226-
anchorElement.download = fileName;
227-
anchorElement.target = "_blank";
228-
anchorElement.click();
229-
anchorElement.remove();
230-
setTimeout(() => URL.revokeObjectURL(objectURL), 100);
223+
224+
ExportCommon.downloadBlob(successResponse, fileName);
231225
}, errorResponse => {
232226
this.actionInProgress(false);
233227
errorResponse.text().then(errorJson => {

src/main/resources/webapp/pdf-exporter/js/pdf-exporter.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ const PdfExporter = {
5959
.forEach(propertyBlock => propertyBlock.style.display = "flex");
6060
break;
6161
case ExportParams.DocumentType.MIXED:
62-
document.querySelectorAll(".modal__container.pdf-exporter .property-wrapper.not-mixed")
62+
document.querySelectorAll(".modal__container.pdf-exporter .property-wrapper.not-visible-for-mixed")
6363
.forEach(propertyBlock => propertyBlock.style.display = "none");
64+
document.querySelectorAll(".modal__container.pdf-exporter .property-wrapper.visible-for-mixed")
65+
.forEach(propertyBlock => propertyBlock.style.display = "flex");
6466
break;
6567
}
6668
MicroModal.show(POPUP_ID);
@@ -442,6 +444,11 @@ const PdfExporter = {
442444
return;
443445
}
444446

447+
if (this.exportContext.getDocumentType() === ExportParams.DocumentType.TEST_RUN) {
448+
const testRunId = new URLSearchParams(this.exportContext.getUrlQueryParameters()).get("id")
449+
ExportCommon.downloadTestRunAttachments(exportParams.projectId, testRunId, exportParams.revision, exportParams.attachmentsFilter);
450+
}
451+
445452
this.actionInProgress({inProgress: true, message: "Generating PDF"})
446453

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

452459
ExportCommon.asyncConvertPdf(requestBody, responseBody => {
453-
const objectURL = (window.URL ? window.URL : window.webkitURL).createObjectURL(responseBody);
454-
const anchorElement = document.createElement("a");
455-
anchorElement.href = objectURL;
456-
anchorElement.download = fileName;
457-
anchorElement.target = "_blank";
458-
anchorElement.click();
459-
anchorElement.remove();
460-
setTimeout(() => URL.revokeObjectURL(objectURL), 100);
460+
ExportCommon.downloadBlob(responseBody, fileName);
461461

462462
this.showNotification({alertType: "success", message: "PDF was successfully generated"});
463463
this.actionInProgress({inProgress: false});

src/test/java/ch/sbb/polarion/extension/pdf_exporter/util/WildcardUtilsTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class WildcardUtilsTest {
1212

1313
static Stream<Arguments> provideToRegexArguments() {
1414
return Stream.of(
15+
Arguments.of(null, ".*"),
16+
Arguments.of("", ".*"),
1517
Arguments.of("*.txt", "^.*\\.txt$"),
1618
Arguments.of("file?.doc", "^file.\\.doc$"),
1719
Arguments.of("doc*", "^doc.*$"),
@@ -29,6 +31,8 @@ void testToRegex(String wildcard, String expectedRegex) {
2931

3032
static Stream<Arguments> provideMatchesArguments() {
3133
return Stream.of(
34+
Arguments.of(null, "*.txt", false),
35+
Arguments.of("", "*.txt", false),
3236
Arguments.of("file.txt", "*.txt", true),
3337
Arguments.of("file.doc", "*.txt", false),
3438
Arguments.of("doc1.doc", "doc?.doc", true),

0 commit comments

Comments
 (0)