Skip to content

Commit

Permalink
chore!: refactoring (#200)
Browse files Browse the repository at this point in the history
DocumentData refactored to use generics
DocumentType enum values changed
  • Loading branch information
grigoriev authored Sep 11, 2024
1 parent 101af70 commit ec21fcd
Show file tree
Hide file tree
Showing 35 changed files with 374 additions and 252 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ All good so far.
### Upgrade from version 6.x.x to 7.0.0

In version 7.0.0 `/export-filename` REST API endpoint changed. As a result, if the endpoint has been used, it's required to adjust the calls accordingly.
`DocumentType` enum in `ExportParams` has been changed. As a result, if enum values have been used, it's required to adjust the calls accordingly.
Main package has been renamed from `ch.sbb.polarion.extension.pdf.exporter` to `ch.sbb.polarion.extension.pdf_exporter`. As a result, if the extension has been used in another OSGi bundles, it's required to adjust the package imports accordingly.


### Upgrade from version 5.x.x to 6.0.0

Expand Down
10 changes: 5 additions & 5 deletions docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1366,12 +1366,12 @@
"documentType": {
"description": "Type of the document",
"enum": [
"DOCUMENT",
"REPORT",
"TESTRUN",
"WIKI"
"LIVE_DOC",
"LIVE_REPORT",
"TEST_RUN",
"WIKI_PAGE"
],
"example": "DOCUMENT",
"example": "LIVE_DOC",
"type": "string"
},
"enableCommentsRendering": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import ch.sbb.polarion.extension.pdf_exporter.settings.CoverPageSettings;
import ch.sbb.polarion.extension.pdf_exporter.settings.CssSettings;
import ch.sbb.polarion.extension.pdf_exporter.settings.HeaderFooterSettings;
import ch.sbb.polarion.extension.pdf_exporter.settings.WebhooksSettings;
import ch.sbb.polarion.extension.pdf_exporter.settings.LocalizationSettings;
import ch.sbb.polarion.extension.pdf_exporter.settings.StylePackageSettings;
import ch.sbb.polarion.extension.pdf_exporter.settings.WebhooksSettings;
import ch.sbb.polarion.extension.pdf_exporter.util.DocumentFileNameHelper;
import ch.sbb.polarion.extension.pdf_exporter.util.EnumValuesProvider;
import ch.sbb.polarion.extension.pdf_exporter.util.ExceptionHandler;
Expand Down Expand Up @@ -328,7 +328,7 @@ private String getFilename(@NotNull IModule module) {
.projectId(module.getProject().getId())
.locationPath(module.getModuleLocation().getLocationPath())
.revision(module.getRevision())
.documentType(DocumentType.DOCUMENT)
.documentType(DocumentType.LIVE_DOC)
.build();

return documentFileNameHelper.getDocumentFileName(exportParams);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package ch.sbb.polarion.extension.pdf_exporter.converter;

import ch.sbb.polarion.extension.generic.settings.SettingId;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.DocumentData;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.conversion.ExportParams;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.settings.coverpage.CoverPageModel;
import ch.sbb.polarion.extension.pdf_exporter.settings.CoverPageSettings;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.DocumentData;
import ch.sbb.polarion.extension.pdf_exporter.util.MediaUtils;
import ch.sbb.polarion.extension.pdf_exporter.util.PdfGenerationLog;
import ch.sbb.polarion.extension.pdf_exporter.util.PdfTemplateProcessor;
import ch.sbb.polarion.extension.pdf_exporter.util.placeholder.PlaceholderProcessor;
import ch.sbb.polarion.extension.pdf_exporter.util.velocity.VelocityEvaluator;
import ch.sbb.polarion.extension.pdf_exporter.weasyprint.WeasyPrintOptions;
import ch.sbb.polarion.extension.pdf_exporter.weasyprint.service.WeasyPrintServiceConnector;
import com.polarion.alm.projects.model.IUniqueObject;
import lombok.SneakyThrows;
import org.jetbrains.annotations.VisibleForTesting;

Expand Down Expand Up @@ -46,7 +47,7 @@ public CoverPageProcessor(PlaceholderProcessor placeholderProcessor,
}

@SneakyThrows
public byte[] generatePdfWithTitle(DocumentData documentData, ExportParams exportParams,
public byte[] generatePdfWithTitle(DocumentData<? extends IUniqueObject> documentData, ExportParams exportParams,
String contentHtml, PdfGenerationLog generationLog) {
String titleHtml = composeTitleHtml(documentData, exportParams);
generationLog.log("Starting concurrent generation for cover page and content");
Expand All @@ -62,12 +63,12 @@ public byte[] generatePdfWithTitle(DocumentData documentData, ExportParams expor
}

@VisibleForTesting
String composeTitleHtml(DocumentData documentData, ExportParams exportParams) {
String composeTitleHtml(DocumentData<? extends IUniqueObject> documentData, ExportParams exportParams) {
CoverPageModel settings = coverPageSettings.load(exportParams.getProjectId(), SettingId.fromName(exportParams.getCoverPage()));
String templateHtml = settings.getTemplateHtml();
String content = placeholderProcessor.replacePlaceholders(documentData, exportParams, templateHtml);
String evaluatedContent = velocityEvaluator.evaluateVelocityExpressions(documentData, content);
return pdfTemplateProcessor.processUsing(exportParams, documentData.getDocumentTitle(), coverPageSettings.processImagePlaceholders(settings.getTemplateCss()), evaluatedContent);
return pdfTemplateProcessor.processUsing(exportParams, documentData.getTitle(), coverPageSettings.processImagePlaceholders(settings.getTemplateCss()), evaluatedContent);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.sbb.polarion.extension.pdf_exporter.converter;

import ch.sbb.polarion.extension.generic.regex.RegexMatcher;
import ch.sbb.polarion.extension.pdf_exporter.properties.PdfExporterExtensionConfiguration;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.conversion.Orientation;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.conversion.PaperSize;
Expand All @@ -9,7 +10,6 @@
import ch.sbb.polarion.extension.pdf_exporter.util.PdfExporterFileResourceProvider;
import ch.sbb.polarion.extension.pdf_exporter.util.PdfTemplateProcessor;
import ch.sbb.polarion.extension.pdf_exporter.util.html.HtmlLinksHelper;
import ch.sbb.polarion.extension.generic.regex.RegexMatcher;
import ch.sbb.polarion.extension.pdf_exporter.weasyprint.WeasyPrintOptions;
import ch.sbb.polarion.extension.pdf_exporter.weasyprint.service.WeasyPrintServiceConnector;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ch.sbb.polarion.extension.generic.settings.SettingId;
import ch.sbb.polarion.extension.generic.util.ScopeUtils;
import ch.sbb.polarion.extension.pdf_exporter.properties.PdfExporterExtensionConfiguration;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.DocumentData;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.ExportMetaInfoCallback;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.WorkItemRefData;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.conversion.DocumentType;
Expand All @@ -17,11 +18,10 @@
import ch.sbb.polarion.extension.pdf_exporter.settings.HeaderFooterSettings;
import ch.sbb.polarion.extension.pdf_exporter.settings.LocalizationSettings;
import ch.sbb.polarion.extension.pdf_exporter.settings.WebhooksSettings;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.DocumentData;
import ch.sbb.polarion.extension.pdf_exporter.util.DocumentDataHelper;
import ch.sbb.polarion.extension.pdf_exporter.util.EnumValuesProvider;
import ch.sbb.polarion.extension.pdf_exporter.util.HtmlLogger;
import ch.sbb.polarion.extension.pdf_exporter.util.HtmlProcessor;
import ch.sbb.polarion.extension.pdf_exporter.util.DocumentDataHelper;
import ch.sbb.polarion.extension.pdf_exporter.util.PdfExporterFileResourceProvider;
import ch.sbb.polarion.extension.pdf_exporter.util.PdfExporterListStyleProvider;
import ch.sbb.polarion.extension.pdf_exporter.util.PdfGenerationLog;
Expand All @@ -32,6 +32,7 @@
import ch.sbb.polarion.extension.pdf_exporter.weasyprint.WeasyPrintOptions;
import ch.sbb.polarion.extension.pdf_exporter.weasyprint.service.WeasyPrintServiceConnector;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.polarion.alm.projects.model.IUniqueObject;
import com.polarion.alm.tracker.model.ITrackerProject;
import com.polarion.core.util.StringUtils;
import com.polarion.core.util.logging.Logger;
Expand Down Expand Up @@ -98,17 +99,17 @@ public byte[] convertToPdf(@NotNull ExportParams exportParams, @Nullable ExportM
generationLog.log("Starting html generation");

@Nullable ITrackerProject project = getTrackerProject(exportParams);
@NotNull final DocumentData documentData = getDocumentData(exportParams, project);
@NotNull final DocumentData<? extends IUniqueObject> documentData = getDocumentData(exportParams, project);
@NotNull String htmlContent = prepareHtmlContent(exportParams, project, documentData, metaInfoCallback);

generationLog.log("Html is ready, starting pdf generation");
if (PdfExporterExtensionConfiguration.getInstance().isDebug()) {
new HtmlLogger().log(documentData.getDocumentContent(), htmlContent, generationLog.getLog());
new HtmlLogger().log(documentData.getContent(), htmlContent, generationLog.getLog());
}
byte[] bytes = generatePdf(documentData, exportParams, metaInfoCallback, htmlContent, generationLog);

if (exportParams.getInternalContent() == null) { //do not log time for internal parts processing
String finalMessage = "PDF document '" + documentData.getDocumentTitle() + "' has been generated within " + (System.currentTimeMillis() - startTime) + " milliseconds";
String finalMessage = "PDF document '" + documentData.getTitle() + "' has been generated within " + (System.currentTimeMillis() - startTime) + " milliseconds";
logger.info(finalMessage);
generationLog.log(finalMessage);
}
Expand All @@ -117,7 +118,7 @@ public byte[] convertToPdf(@NotNull ExportParams exportParams, @Nullable ExportM

public @NotNull String prepareHtmlContent(@NotNull ExportParams exportParams, @Nullable ExportMetaInfoCallback metaInfoCallback) {
@Nullable ITrackerProject project = getTrackerProject(exportParams);
@NotNull final DocumentData documentData = getDocumentData(exportParams, project);
@NotNull final DocumentData<? extends IUniqueObject> documentData = getDocumentData(exportParams, project);
return prepareHtmlContent(exportParams, project, documentData, metaInfoCallback);
}

Expand All @@ -129,21 +130,21 @@ public byte[] convertToPdf(@NotNull ExportParams exportParams, @Nullable ExportM
return project;
}

private @NotNull DocumentData getDocumentData(@NotNull ExportParams exportParams, @Nullable ITrackerProject project) {
private @NotNull DocumentData<? extends IUniqueObject> getDocumentData(@NotNull ExportParams exportParams, @Nullable ITrackerProject project) {
return switch (exportParams.getDocumentType()) {
case DOCUMENT -> documentDataHelper.getLiveDocument(Objects.requireNonNull(project), exportParams);
case REPORT -> documentDataHelper.getLiveReport(project, exportParams);
case TESTRUN -> documentDataHelper.getTestRun(project, exportParams);
case WIKI -> documentDataHelper.getWikiDocument(project, exportParams);
case LIVE_DOC -> documentDataHelper.getLiveDoc(Objects.requireNonNull(project), exportParams);
case LIVE_REPORT -> documentDataHelper.getLiveReport(project, exportParams);
case TEST_RUN -> documentDataHelper.getTestRun(project, exportParams);
case WIKI_PAGE -> documentDataHelper.getWikiPage(project, exportParams);
};
}

private @NotNull String prepareHtmlContent(@NotNull ExportParams exportParams, @Nullable ITrackerProject project, @NotNull DocumentData documentData, @Nullable ExportMetaInfoCallback metaInfoCallback) {
private @NotNull String prepareHtmlContent(@NotNull ExportParams exportParams, @Nullable ITrackerProject project, @NotNull DocumentData<? extends IUniqueObject> documentData, @Nullable ExportMetaInfoCallback metaInfoCallback) {
String cssContent = getCssContent(documentData, exportParams);
String preparedDocumentContent = postProcessDocumentContent(exportParams, project, documentData.getDocumentContent());
String preparedDocumentContent = postProcessDocumentContent(exportParams, project, documentData.getContent());
String headerFooterContent = getHeaderFooterContent(documentData, exportParams);
HtmlData htmlData = new HtmlData(cssContent, preparedDocumentContent, headerFooterContent);
String htmlContent = composeHtml(documentData.getDocumentTitle(), htmlData, exportParams);
String htmlContent = composeHtml(documentData.getTitle(), htmlData, exportParams);
if (metaInfoCallback != null) {
metaInfoCallback.setLinkedWorkItems(WorkItemRefData.extractListFromHtml(htmlContent, exportParams.getProjectId()));
}
Expand Down Expand Up @@ -230,7 +231,7 @@ private static void addAuthHeader(@NotNull WebhookConfig webhookConfig, @NotNull

@VisibleForTesting
byte[] generatePdf(
DocumentData documentData,
DocumentData<? extends IUniqueObject> documentData,
ExportParams exportParams,
ExportMetaInfoCallback metaInfoCallback,
String htmlPage,
Expand Down Expand Up @@ -266,7 +267,7 @@ String composeHtml(@NotNull String documentName,
@NotNull
@VisibleForTesting
String getCssContent(
@NotNull DocumentData documentData,
@NotNull DocumentData<? extends IUniqueObject> documentData,
@NotNull ExportParams exportParams) {
String cssSettingsName = exportParams.getCss() != null ? exportParams.getCss() : NamedSettings.DEFAULT_NAME;
String pdfStyles = cssSettings.load(exportParams.getProjectId(), SettingId.fromName(cssSettingsName)).getCss();
Expand All @@ -282,13 +283,13 @@ String getCssContent(
String content = placeholderProcessor.replacePlaceholders(documentData, exportParams, css);
String processed = velocityEvaluator.evaluateVelocityExpressions(documentData, content);

String cssContent = (exportParams.getDocumentType() != DocumentType.DOCUMENT) ? appendWikiCss(processed) : processed;
String cssContent = (exportParams.getDocumentType() != DocumentType.LIVE_DOC) ? appendWikiCss(processed) : processed;
return htmlProcessor.replaceImagesAsBase64Encoded(cssContent);
}

@VisibleForTesting
String getHeaderFooterContent(
@NotNull DocumentData documentData,
@NotNull DocumentData<? extends IUniqueObject> documentData,
@NotNull ExportParams exportParams) {
String headerFooterSettingsName = exportParams.getHeaderFooter() != null ? exportParams.getHeaderFooter() : NamedSettings.DEFAULT_NAME;
HeaderFooterModel headerFooter = headerFooterSettings.load(exportParams.getProjectId(), SettingId.fromName(headerFooterSettingsName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import ch.sbb.polarion.extension.pdf_exporter.settings.CssSettings;
import ch.sbb.polarion.extension.pdf_exporter.settings.FileNameTemplateSettings;
import ch.sbb.polarion.extension.pdf_exporter.settings.HeaderFooterSettings;
import ch.sbb.polarion.extension.pdf_exporter.settings.WebhooksSettings;
import ch.sbb.polarion.extension.pdf_exporter.settings.LocalizationSettings;
import ch.sbb.polarion.extension.pdf_exporter.settings.StylePackageSettings;
import ch.sbb.polarion.extension.pdf_exporter.settings.WebhooksSettings;
import com.polarion.core.util.logging.Logger;
import org.jetbrains.annotations.NotNull;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,15 @@ public WidthValidationResult validatePdfWidth(
)
@SuppressWarnings("java:S1166")
public NestedListsCheck checkNestedLists(ExportParams exportParams) {
boolean containsNestedLists = documentDataHelper.documentContainsNestedNumberedLists(exportParams);
boolean containsNestedLists = documentDataHelper.hasLiveDocNestedNumberedLists(exportParams);
return NestedListsCheck.builder().containsNestedLists(containsNestedLists).build();
}

private void validateExportParameters(ExportParams exportParams) {
if (exportParams == null) {
throw new BadRequestException("Missing export parameters");
}
if (exportParams.getDocumentType() == DocumentType.DOCUMENT && exportParams.getProjectId() == null) {
if (exportParams.getDocumentType() == DocumentType.LIVE_DOC && exportParams.getProjectId() == null) {
throw new BadRequestException("Parameter 'projectId' should be provided");
}
if (exportParams.getLocationPath() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ public void deleteImages(@PathParam("name") String coverPageName, @QueryParam("s
}
)
public Collection<SettingName> getSuitableStylePackageNames(@QueryParam("projectId") String projectId, @QueryParam("spaceId") String spaceId, @QueryParam("documentName") String documentName) {
if (projectId == null || spaceId == null || documentName == null) {
throw new BadRequestException("Parameters 'projectId', 'spaceId', 'documentName' are required'");
if (spaceId == null || documentName == null) {
throw new BadRequestException("Parameters 'spaceId' and 'documentName' are required'");
}
return pdfExporterPolarionService.getSuitableStylePackages(projectId, spaceId, documentName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
package ch.sbb.polarion.extension.pdf_exporter.rest.model;

import com.polarion.alm.tracker.model.IModule;
import com.polarion.alm.tracker.model.IRichPage;
import com.polarion.alm.tracker.model.ITestRun;
import com.polarion.alm.tracker.model.IWikiPage;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.conversion.DocumentType;
import com.polarion.alm.projects.model.IUniqueObject;
import lombok.Builder;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@Builder
@Builder(builderMethodName = "create")
@Getter
public class DocumentData {
private String projectName;
private IModule document;
private IWikiPage wikiPage;
private IRichPage richPage;
private ITestRun testRun;
private String lastRevision;
private String baselineName;
private String documentId;
private String documentTitle;
private String documentContent;
public class DocumentData<T extends IUniqueObject> {
private final @NotNull DocumentType documentType;
private final @NotNull T documentObject;

private @NotNull String id;
private @NotNull String title;
private @Nullable String content;

private @Nullable String projectName;
private @Nullable String lastRevision;
private @Nullable String baselineName;

public static <T extends IUniqueObject> DocumentDataBuilder<T> builder(@NotNull DocumentType documentType, @NotNull T documentObject) {
return DocumentData.<T>create()
.documentType(documentType)
.documentObject(documentObject);
}

// making javadoc maven plugin happy
@SuppressWarnings("unused")
public static class DocumentDataBuilder<T extends IUniqueObject> {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
@Schema(description = "Type of the document")
public enum DocumentType {
@Schema(description = "Live document")
DOCUMENT,
LIVE_DOC,

@Schema(description = "Live report")
REPORT,
LIVE_REPORT,

@Schema(description = "Test run")
TESTRUN,
TEST_RUN,

@Schema(description = "Wiki page")
WIKI;
WIKI_PAGE;

@SuppressWarnings("unused")
@JsonCreator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ExportParams {
@Schema(description = "The specific revision of the document to export")
private String revision;

@Schema(description = "The type of document to export", example = "DOCUMENT")
@Schema(description = "The type of document to export", example = "LIVE_DOC")
private DocumentType documentType;

@Schema(description = "Cover page settings name")
Expand Down Expand Up @@ -95,7 +95,7 @@ public class ExportParams {
public DocumentType getDocumentType() {
// Default to DOCUMENT
if (documentType == null) {
return DocumentType.DOCUMENT;
return DocumentType.LIVE_DOC;
}
return documentType;
}
Expand Down
Loading

0 comments on commit ec21fcd

Please sign in to comment.