From 3bf64f23b48df6a4ebe7138130332c42a1dfea45 Mon Sep 17 00:00:00 2001 From: "d.svitak" Date: Mon, 8 Dec 2025 11:22:24 +0100 Subject: [PATCH 1/5] WIP - refactor builders - deprecate inconsistent methods and update them with common pattern, add missing methods in various builders. --- .../builder/ParagraphStyleBuilder.kt | 4 +- .../builder/TextStyleBuilder.kt | 4 +- .../builder/documentcontent/AreaBuilder.kt | 27 ++++++ .../documentcontent/FirstMatchBuilder.kt | 97 ++++++++++++++++++- .../documentcontent/ParagraphBuilder.kt | 80 ++++++++++++++- .../SelectByLanguageBuilder.kt | 85 ++++++++++++++-- .../builder/documentcontent/TableBuilder.kt | 96 +++++++++++++++--- 7 files changed, 360 insertions(+), 33 deletions(-) diff --git a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/ParagraphStyleBuilder.kt b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/ParagraphStyleBuilder.kt index 8070f6a..cdd028d 100644 --- a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/ParagraphStyleBuilder.kt +++ b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/ParagraphStyleBuilder.kt @@ -12,8 +12,8 @@ import com.quadient.migration.shared.Size class ParagraphStyleBuilder(id: String) : DtoBuilderBase(id) { var definition: ParagraphStyleDefOrRef? = null - fun definition(builder: ParagraphStyleDefinitionBuilder.() -> ParagraphStyleDefinitionBuilder) = apply { - this.definition = builder(ParagraphStyleDefinitionBuilder()).build() + fun definition(builder: ParagraphStyleDefinitionBuilder.() -> Unit) = apply { + this.definition = ParagraphStyleDefinitionBuilder().apply(builder).build() } fun definition(definition: ParagraphStyleDefinition) = apply { this.definition = definition } diff --git a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/TextStyleBuilder.kt b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/TextStyleBuilder.kt index 0c32ac6..f7a9523 100644 --- a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/TextStyleBuilder.kt +++ b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/TextStyleBuilder.kt @@ -11,8 +11,8 @@ import com.quadient.migration.shared.SuperOrSubscript class TextStyleBuilder(id: String) : DtoBuilderBase(id) { var definition: TextStyleDefOrRef? = null - fun definition(builder: TextStyleDefinitionBuilder.() -> TextStyleDefinitionBuilder) = apply { - this.definition = builder(TextStyleDefinitionBuilder()).build() + fun definition(builder: TextStyleDefinitionBuilder.() -> Unit) = apply { + this.definition = TextStyleDefinitionBuilder().apply(builder).build() } fun definition(definition: TextStyleDefinition) = apply { this.definition = definition } diff --git a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/AreaBuilder.kt b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/AreaBuilder.kt index 22f5e0d..81858d2 100644 --- a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/AreaBuilder.kt +++ b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/AreaBuilder.kt @@ -90,6 +90,33 @@ class AreaBuilder { content.add(ImageRef(imageRefId)) } + /** + * Adds a table to the flow area using a builder function. + * @param builder A builder function to build the table. + * @return The [AreaBuilder] instance for method chaining. + */ + fun table(builder: com.quadient.migration.api.dto.migrationmodel.builder.TableBuilder.() -> Unit) = apply { + content.add(com.quadient.migration.api.dto.migrationmodel.builder.TableBuilder().apply(builder).build()) + } + + /** + * Adds a first match block to the flow area using a builder function. + * @param builder A builder function to build the first match block. + * @return The [AreaBuilder] instance for method chaining. + */ + fun firstMatch(builder: com.quadient.migration.api.dto.migrationmodel.builder.FirstMatchBuilder.() -> Unit) = apply { + content.add(com.quadient.migration.api.dto.migrationmodel.builder.FirstMatchBuilder().apply(builder).build()) + } + + /** + * Adds a select by language block to the flow area using a builder function. + * @param builder A builder function to build the select by language block. + * @return The [AreaBuilder] instance for method chaining. + */ + fun selectByLanguage(builder: SelectByLanguageBuilder.() -> Unit) = apply { + content.add(SelectByLanguageBuilder().apply(builder).build()) + } + /** * Builds the [Area] instance. * @return The constructed [Area] instance. diff --git a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/FirstMatchBuilder.kt b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/FirstMatchBuilder.kt index 691e499..8d8c601 100644 --- a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/FirstMatchBuilder.kt +++ b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/FirstMatchBuilder.kt @@ -2,7 +2,9 @@ package com.quadient.migration.api.dto.migrationmodel.builder import com.quadient.migration.api.dto.migrationmodel.DisplayRuleRef import com.quadient.migration.api.dto.migrationmodel.DocumentContent +import com.quadient.migration.api.dto.migrationmodel.DocumentObjectRef import com.quadient.migration.api.dto.migrationmodel.FirstMatch +import com.quadient.migration.api.dto.migrationmodel.builder.documentcontent.SelectByLanguageBuilder class FirstMatchBuilder { private var default: MutableList = mutableListOf() @@ -32,7 +34,7 @@ class FirstMatchBuilder { fun addCase() = CaseBuilder().apply { cases.add(this) } /** - * Sets the default content for the FirstMatch instance. + * Replaces the default content for the FirstMatch instance. * @param default The default DocumentContent to be used. * @return The FirstMatchBuilder instance for method chaining. */ @@ -55,13 +57,40 @@ class FirstMatchBuilder { cases.add(caseBuilder) } + /** + * Sets the default content as a paragraph using a builder function. + * @param builder A builder function to configure the paragraph. + * @return The FirstMatchBuilder instance for method chaining. + */ + fun defaultParagraph(builder: ParagraphBuilder.() -> Unit) = apply { + default.add(ParagraphBuilder().apply(builder).build()) + } + + /** + * Sets the default content as a table using a builder function. + * @param builder A builder function to configure the table. + * @return The FirstMatchBuilder instance for method chaining. + */ + fun defaultTable(builder: TableBuilder.() -> Unit) = apply { + default.add(TableBuilder().apply(builder).build()) + } + + /** + * Adds default content as a paragraph with the given string. + * @param text The string to be wrapped in a paragraph. + * @return The FirstMatchBuilder instance for method chaining. + */ + fun defaultString(text: String) = apply { + default.add(ParagraphBuilder().string(text).build()) + } + class CaseBuilder { var content: MutableList = mutableListOf() var displayRuleRef: DisplayRuleRef? = null var name: String? = null /** - * Sets the content for the case. + * Replaces the content for the case. * @param content The [DocumentContent] to be used in the case. * @return A CaseBuilder instance for method chaining. */ @@ -94,5 +123,69 @@ class FirstMatchBuilder { * @return A CaseBuilder instance for method chaining. */ fun name(name: String) = apply { this.name = name } + + /** + * Adds a paragraph to the case using a builder function. + * @param builder A builder function to build the paragraph. + * @return A CaseBuilder instance for method chaining. + */ + fun paragraph(builder: ParagraphBuilder.() -> Unit) = apply { + content.add(ParagraphBuilder().apply(builder).build()) + } + + /** + * Adds a table to the case using a builder function. + * @param builder A builder function to build the table. + * @return A CaseBuilder instance for method chaining. + */ + fun table(builder: TableBuilder.() -> Unit) = apply { + content.add(TableBuilder().apply(builder).build()) + } + + /** + * Adds an image reference to the case. + * @param imageId The ID of the image to reference. + * @return A CaseBuilder instance for method chaining. + */ + fun imageRef(imageId: String) = apply { + content.add(com.quadient.migration.api.dto.migrationmodel.ImageRef(imageId)) + } + + /** + * Adds a document object reference to the case. + * @param documentObjectId The ID of the document object to reference. + * @return A CaseBuilder instance for method chaining. + */ + fun documentObjectRef(documentObjectId: String) = apply { + content.add(DocumentObjectRef(documentObjectId, null)) + } + + /** + * Adds a conditional document object reference to the case. + * @param documentObjectId The ID of the document object to reference. + * @param displayRuleId The ID of the display rule. + * @return A CaseBuilder instance for method chaining. + */ + fun documentObjectRef(documentObjectId: String, displayRuleId: String) = apply { + content.add(DocumentObjectRef(documentObjectId, DisplayRuleRef(displayRuleId))) + } + + /** + * Adds a nested first match block to the case. + * @param builder A builder function to build the first match block. + * @return A CaseBuilder instance for method chaining. + */ + fun firstMatch(builder: FirstMatchBuilder.() -> Unit) = apply { + content.add(FirstMatchBuilder().apply(builder).build()) + } + + /** + * Adds a select by language block to the case. + * @param builder A builder function to build the select by language block. + * @return A CaseBuilder instance for method chaining. + */ + fun selectByLanguage(builder: SelectByLanguageBuilder.() -> Unit) = apply { + content.add(SelectByLanguageBuilder().apply(builder).build()) + } } } diff --git a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/ParagraphBuilder.kt b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/ParagraphBuilder.kt index d69b526..d5b6138 100644 --- a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/ParagraphBuilder.kt +++ b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/ParagraphBuilder.kt @@ -2,6 +2,7 @@ package com.quadient.migration.api.dto.migrationmodel.builder import com.quadient.migration.api.dto.migrationmodel.DisplayRuleRef import com.quadient.migration.api.dto.migrationmodel.DocumentObjectRef +import com.quadient.migration.api.dto.migrationmodel.ImageRef import com.quadient.migration.api.dto.migrationmodel.Paragraph import com.quadient.migration.api.dto.migrationmodel.ParagraphStyleRef import com.quadient.migration.api.dto.migrationmodel.StringValue @@ -76,13 +77,26 @@ class ParagraphBuilder { * Adds a string content to the paragraph. * @param content The string content to add. * @return The current instance of [ParagraphBuilder] for method chaining. + * @deprecated Use [string] instead for consistency. This method uses confusing naming. */ + @Deprecated("Use string() instead", ReplaceWith("string(content)")) fun content(content: String) = apply { val textBuilder = TextBuilder() textBuilder.content(StringValue(content)) this.content.add(textBuilder) } + /** + * Adds a string to the paragraph content (creates a text block with StringValue). + * @param text The string to add. + * @return The current instance of [ParagraphBuilder] for method chaining. + */ + fun string(text: String) = apply { + val textBuilder = TextBuilder() + textBuilder.content(StringValue(text)) + this.content.add(textBuilder) + } + /** * Adds a variable reference to the paragraph content. * @param variableId The ID of the variable to reference. @@ -149,38 +163,49 @@ class ParagraphBuilder { fun displayRuleRef(displayRuleRefId: String) = apply { this.displayRuleRef = DisplayRuleRef(displayRuleRefId) } /** - * Sets the content of the text. + * Replaces all content with a single [TextContent] item. * @param content A [TextContent] instance to set as the content. * @return The current instance of [TextBuilder] for method chaining. */ fun content(content: TextContent) = apply { this.content = mutableListOf(content) } /** - * Sets the content of the text using a string. + * Replaces all content with a string. * @param content The string content to set. * @return The current instance of [TextBuilder] for method chaining. + * @deprecated Use [string] for appending strings. This method replaces content which is inconsistent with other specific methods. */ + @Deprecated("Use string() to append string content", ReplaceWith("string(content)")) fun content(content: String) = apply { this.content = mutableListOf(StringValue(content)) } /** - * Appends a [TextContent] to the existing content of the text. + * Appends a [TextContent] to the existing content. * @param content The [TextContent] to append. * @return The current instance of [TextBuilder] for method chaining. */ fun appendContent(content: TextContent) = apply { this.content.add(content) } /** - * Sets the content of the text using a list of [TextContent]. + * Replaces all content with a list of [TextContent]. * @param content A list of [TextContent] to set as the content. * @return The current instance of [TextBuilder] for method chaining. */ fun content(content: List) = apply { this.content = content.toMutableList() } /** - * Appends a string content to the existing content of the text. + * Appends a string to the existing content (creates a StringValue). + * @param text The string to append. + * @return The current instance of [TextBuilder] for method chaining. + */ + fun string(text: String) = apply { this.content.add(StringValue(text)) } + + /** + * Appends a string to the existing content. * @param content The string content to append. * @return The current instance of [TextBuilder] for method chaining. + * @deprecated Use [string] instead for consistency. */ + @Deprecated("Use string() instead", ReplaceWith("string(content)")) fun appendContent(content: String) = apply { this.content.add(StringValue(content)) } /** @@ -211,5 +236,50 @@ class ParagraphBuilder { val firstMatchBuilder = FirstMatchBuilder().apply(builder) content.add(firstMatchBuilder.build()) } + + /** + * Adds a variable reference to the text content. + * @param variableId The ID of the variable to reference. + * @return The current instance of [TextBuilder] for method chaining. + */ + fun variableRef(variableId: String) = apply { + content.add(VariableRef(variableId)) + } + + /** + * Adds a variable reference to the text content. + * @param ref The variable reference to add. + * @return The current instance of [TextBuilder] for method chaining. + */ + fun variableRef(ref: VariableRef) = apply { + content.add(ref) + } + + /** + * Adds an image reference to the text content. + * @param imageId The ID of the image to reference. + * @return The current instance of [TextBuilder] for method chaining. + */ + fun imageRef(imageId: String) = apply { + content.add(ImageRef(imageId)) + } + + /** + * Adds an image reference to the text content. + * @param ref The image reference to add. + * @return The current instance of [TextBuilder] for method chaining. + */ + fun imageRef(ref: ImageRef) = apply { + content.add(ref) + } + + /** + * Adds a table to the text content. + * @param builder A builder function to configure the [TableBuilder]. + * @return The current instance of [TextBuilder] for method chaining. + */ + fun table(builder: TableBuilder.() -> Unit) = apply { + content.add(TableBuilder().apply(builder).build()) + } } } diff --git a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/SelectByLanguageBuilder.kt b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/SelectByLanguageBuilder.kt index 59ee045..4ea23e4 100644 --- a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/SelectByLanguageBuilder.kt +++ b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/SelectByLanguageBuilder.kt @@ -1,9 +1,15 @@ package com.quadient.migration.api.dto.migrationmodel.builder.documentcontent +import com.quadient.migration.api.dto.migrationmodel.DisplayRuleRef import com.quadient.migration.api.dto.migrationmodel.DocumentContent +import com.quadient.migration.api.dto.migrationmodel.DocumentObjectRef +import com.quadient.migration.api.dto.migrationmodel.ImageRef import com.quadient.migration.api.dto.migrationmodel.SelectByLanguage +import com.quadient.migration.api.dto.migrationmodel.builder.FirstMatchBuilder +import com.quadient.migration.api.dto.migrationmodel.builder.ParagraphBuilder +import com.quadient.migration.api.dto.migrationmodel.builder.TableBuilder -class SelectByLanguageBuilder { +class SelectByLanguageBuilder { private var cases: MutableList = mutableListOf() /** @@ -11,14 +17,11 @@ class SelectByLanguageBuilder { * @return A SelectByLanguage instance containing the cases and default content. */ fun build(): SelectByLanguage { - return SelectByLanguage ( + return SelectByLanguage( cases.map { SelectByLanguage.Case( - it.content, - requireNotNull(it.language) { "language must be provided" } - ) - } - ) + it.content, requireNotNull(it.language) { "language must be provided" }) + }) } /** @@ -42,9 +45,8 @@ class SelectByLanguageBuilder { var language: String? = null /** - * Sets the content for the case. + * Replaces the content for the case. * @param content The [DocumentContent] to be used in the case. - * @param language The language to be used for the case. * @return A CaseBuilder instance for method chaining. */ fun content(content: DocumentContent) = apply { this.content = mutableListOf(content) } @@ -52,7 +54,6 @@ class SelectByLanguageBuilder { /** * Appends additional content to the case. * @param content The [DocumentContent] to be added to the case. - * @param language The language to be used for the case. * @return A CaseBuilder instance for method chaining. */ fun appendContent(content: DocumentContent) = apply { this.content.add(content) } @@ -63,5 +64,69 @@ class SelectByLanguageBuilder { * @return A CaseBuilder instance for method chaining. */ fun language(language: String) = apply { this.language = language } + + /** + * Adds a paragraph to the case using a builder function. + * @param builder A builder function to build the paragraph. + * @return A CaseBuilder instance for method chaining. + */ + fun paragraph(builder: ParagraphBuilder.() -> Unit) = apply { + content.add(ParagraphBuilder().apply(builder).build()) + } + + /** + * Adds a table to the case using a builder function. + * @param builder A builder function to build the table. + * @return A CaseBuilder instance for method chaining. + */ + fun table(builder: TableBuilder.() -> Unit) = apply { + content.add(TableBuilder().apply(builder).build()) + } + + /** + * Adds an image reference to the case. + * @param imageId The ID of the image to reference. + * @return A CaseBuilder instance for method chaining. + */ + fun imageRef(imageId: String) = apply { + content.add(ImageRef(imageId)) + } + + /** + * Adds a document object reference to the case. + * @param documentObjectId The ID of the document object to reference. + * @return A CaseBuilder instance for method chaining. + */ + fun documentObjectRef(documentObjectId: String) = apply { + content.add(DocumentObjectRef(documentObjectId, null)) + } + + /** + * Adds a conditional document object reference to the case. + * @param documentObjectId The ID of the document object to reference. + * @param displayRuleId The ID of the display rule. + * @return A CaseBuilder instance for method chaining. + */ + fun documentObjectRef(documentObjectId: String, displayRuleId: String) = apply { + content.add(DocumentObjectRef(documentObjectId, DisplayRuleRef(displayRuleId))) + } + + /** + * Adds a first match block to the case. + * @param builder A builder function to build the first match block. + * @return A CaseBuilder instance for method chaining. + */ + fun firstMatch(builder: FirstMatchBuilder.() -> Unit) = apply { + content.add(FirstMatchBuilder().apply(builder).build()) + } + + /** + * Adds a nested select by language block to the case. + * @param builder A builder function to build the select by language block. + * @return A CaseBuilder instance for method chaining. + */ + fun selectByLanguage(builder: SelectByLanguageBuilder.() -> Unit) = apply { + content.add(SelectByLanguageBuilder().apply(builder).build()) + } } } diff --git a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/TableBuilder.kt b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/TableBuilder.kt index 3653945..9ce6586 100644 --- a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/TableBuilder.kt +++ b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/TableBuilder.kt @@ -2,7 +2,9 @@ package com.quadient.migration.api.dto.migrationmodel.builder import com.quadient.migration.api.dto.migrationmodel.DisplayRuleRef import com.quadient.migration.api.dto.migrationmodel.DocumentContent +import com.quadient.migration.api.dto.migrationmodel.DocumentObjectRef import com.quadient.migration.api.dto.migrationmodel.Table +import com.quadient.migration.api.dto.migrationmodel.builder.documentcontent.SelectByLanguageBuilder import com.quadient.migration.shared.Size class TableBuilder { @@ -67,28 +69,98 @@ class TableBuilder { fun mergeUp(value: Boolean) = apply { mergeUp = value } /** - * Append content to the cell. - * @param content The content to append to the cell. Must be either - * a [String], [com.quadient.migration.api.dto.migrationmodel.Text] - * or [com.quadient.migration.api.dto.migrationmodel.Ref]. + * Appends content to the cell. + * @param content The content to append to the cell. + * @return The [Cell] instance for method chaining. */ fun appendContent(content: DocumentContent) = apply { this.content.add(content) } /** - * Replace content of the cell with single object. - * @param content The content to append to the cell. Must be either - * a [String], [com.quadient.migration.api.dto.migrationmodel.Text] - * or [com.quadient.migration.api.dto.migrationmodel.Ref]. + * Replaces all content in the cell with a single item. + * @param content The content to set as the cell content. + * @return The [Cell] instance for method chaining. */ fun content(content: DocumentContent) = apply { this.content.apply { clear() }.add(content) } /** - * Replace content of the cell with the provided list of objects. - * @param content The content to append to the cell. Must be either - * a [String], [com.quadient.migration.api.dto.migrationmodel.Text] - * or [com.quadient.migration.api.dto.migrationmodel.Ref]. + * Replaces all content in the cell with multiple items. + * @param content The list of content to set as the cell content. + * @return The [Cell] instance for method chaining. */ fun content(content: List) = apply { this@Cell.content.apply { clear() }.addAll(content) } + + /** + * Adds a paragraph with the given string to the cell. + * @param text The string to add in a paragraph. + * @return The [Cell] instance for method chaining. + */ + fun string(text: String) = apply { + this.content.add(ParagraphBuilder().string(text).build()) + } + + /** + * Adds a paragraph to the cell using a builder function. + * @param builder A builder function to build the paragraph. + * @return The [Cell] instance for method chaining. + */ + fun paragraph(builder: ParagraphBuilder.() -> Unit) = apply { + content.add(ParagraphBuilder().apply(builder).build()) + } + + /** + * Adds a table to the cell using a builder function. + * @param builder A builder function to build the table. + * @return The [Cell] instance for method chaining. + */ + fun table(builder: TableBuilder.() -> Unit) = apply { + content.add(TableBuilder().apply(builder).build()) + } + + /** + * Adds an image reference to the cell. + * @param imageId The ID of the image to reference. + * @return The [Cell] instance for method chaining. + */ + fun imageRef(imageId: String) = apply { + content.add(com.quadient.migration.api.dto.migrationmodel.ImageRef(imageId)) + } + + /** + * Adds a document object reference to the cell. + * @param documentObjectId The ID of the document object to reference. + * @return The [Cell] instance for method chaining. + */ + fun documentObjectRef(documentObjectId: String) = apply { + content.add(DocumentObjectRef(documentObjectId, null)) + } + + /** + * Adds a conditional document object reference to the cell. + * @param documentObjectId The ID of the document object to reference. + * @param displayRuleId The ID of the display rule. + * @return The [Cell] instance for method chaining. + */ + fun documentObjectRef(documentObjectId: String, displayRuleId: String) = apply { + content.add(DocumentObjectRef(documentObjectId, DisplayRuleRef(displayRuleId))) + } + + /** + * Adds a first match block to the cell using a builder function. + * @param builder A builder function to build the first match block. + * @return The [Cell] instance for method chaining. + */ + fun firstMatch(builder: FirstMatchBuilder.() -> Unit) = apply { + content.add(FirstMatchBuilder().apply(builder).build()) + } + + /** + * Adds a select by language block to the cell using a builder function. + * @param builder A builder function to build the select by language block. + * @return The [Cell] instance for method chaining. + */ + fun selectByLanguage(builder: SelectByLanguageBuilder.() -> Unit) = apply { + content.add(SelectByLanguageBuilder().apply(builder).build()) + } } data class ColumnWidth(val minWidth: Size, val percentWidth: Double) From 3f4c7056a15110bbb911040223323137d8ee4bbd Mon Sep 17 00:00:00 2001 From: "d.svitak" Date: Mon, 8 Dec 2025 12:40:45 +0100 Subject: [PATCH 2/5] further refactor builders by introducing DocumentContentBuilderBase which is common behavior standard for all objects working with content (i.e. List { + /** + * The mutable list of document content. + * Implementing classes must provide this property. + */ + val content: MutableList + + /** + * Replaces all content with a single DocumentContent item. + * @param content The content to set. + * @return This builder instance for method chaining. + */ + fun content(content: DocumentContent): T = apply { + this.content.clear() + this.content.add(content) + } as T + + /** + * Replaces all content with multiple DocumentContent items. + * @param content The list of content to set. + * @return This builder instance for method chaining. + */ + fun content(content: List): T = apply { + this.content.clear() + this.content.addAll(content) + } as T + + /** + * Appends a DocumentContent item to the existing content. + * @param content The content to append. + * @return This builder instance for method chaining. + */ + fun appendContent(content: DocumentContent): T = apply { + this.content.add(content) + } as T + + /** + * Adds a paragraph to the content using a builder function. + * @param builder A builder function to build the paragraph. + * @return This builder instance for method chaining. + */ + fun paragraph(builder: ParagraphBuilder.() -> Unit): T = apply { + this.content.add(ParagraphBuilder().apply(builder).build()) + } as T + + /** + * Adds a table to the content using a builder function. + * @param builder A builder function to build the table. + * @return This builder instance for method chaining. + */ + fun table(builder: TableBuilder.() -> Unit): T = apply { + this.content.add(TableBuilder().apply(builder).build()) + } as T + + /** + * Adds an image reference to the content. + * @param imageId The ID of the image to reference. + * @return This builder instance for method chaining. + */ + fun imageRef(imageId: String): T = apply { + this.content.add(ImageRef(imageId)) + } as T + + /** + * Adds a document object reference to the content. + * @param documentObjectId The ID of the document object to reference. + * @return This builder instance for method chaining. + */ + fun documentObjectRef(documentObjectId: String): T = apply { + this.content.add(DocumentObjectRef(documentObjectId, null)) + } as T + + /** + * Adds a conditional document object reference to the content. + * @param documentObjectId The ID of the document object to reference. + * @param displayRuleId The ID of the display rule. + * @return This builder instance for method chaining. + */ + fun documentObjectRef(documentObjectId: String, displayRuleId: String): T = apply { + this.content.add(DocumentObjectRef(documentObjectId, DisplayRuleRef(displayRuleId))) + } as T + + /** + * Adds a first match block to the content using a builder function. + * @param builder A builder function to build the first match block. + * @return This builder instance for method chaining. + */ + fun firstMatch(builder: FirstMatchBuilder.() -> Unit): T = apply { + this.content.add(FirstMatchBuilder().apply(builder).build()) + } as T + + /** + * Adds a select by language block to the content using a builder function. + * @param builder A builder function to build the select by language block. + * @return This builder instance for method chaining. + */ + fun selectByLanguage(builder: SelectByLanguageBuilder.() -> Unit): T = apply { + this.content.add(SelectByLanguageBuilder().apply(builder).build()) + } as T +} \ No newline at end of file diff --git a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/DocumentObjectBuilder.kt b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/DocumentObjectBuilder.kt index 582b923..dd9827a 100644 --- a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/DocumentObjectBuilder.kt +++ b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/DocumentObjectBuilder.kt @@ -3,19 +3,16 @@ package com.quadient.migration.api.dto.migrationmodel.builder import com.quadient.migration.api.dto.migrationmodel.DisplayRuleRef import com.quadient.migration.api.dto.migrationmodel.DocumentContent import com.quadient.migration.api.dto.migrationmodel.DocumentObject -import com.quadient.migration.api.dto.migrationmodel.DocumentObjectRef -import com.quadient.migration.api.dto.migrationmodel.ImageRef import com.quadient.migration.api.dto.migrationmodel.VariableStructureRef import com.quadient.migration.api.dto.migrationmodel.builder.documentcontent.AreaBuilder -import com.quadient.migration.api.dto.migrationmodel.builder.documentcontent.SelectByLanguageBuilder import com.quadient.migration.shared.DocumentObjectOptions import com.quadient.migration.shared.DocumentObjectType import com.quadient.migration.shared.MetadataPrimitive import com.quadient.migration.shared.SkipOptions class DocumentObjectBuilder(id: String, private val type: DocumentObjectType) : - DtoBuilderBase(id) { - private var content: List = mutableListOf() + DtoBuilderBase(id), DocumentContentBuilderBase { + override val content: MutableList = mutableListOf() private var internal: Boolean = false private var targetFolder: String? = null private var displayRuleRef: DisplayRuleRef? = null @@ -27,13 +24,6 @@ class DocumentObjectBuilder(id: String, private val type: DocumentObjectType) : private var placeholder: String? = null private var reason: String? = null - /** - * Replace content of the document object. - * @param content List of [DocumentContent] to set as the content of the document object. - * @return This builder instance for method chaining. - */ - fun content(content: List) = apply { this.content = content } - /** * Set whether the document object is internal. Internal objects do not create a separate * file in the target system. @@ -48,24 +38,28 @@ class DocumentObjectBuilder(id: String, private val type: DocumentObjectType) : * @return This builder instance for method chaining. */ fun targetFolder(targetFolder: String) = apply { this.targetFolder = targetFolder } + /** * Add display rule to this document object. * @param id ID of the display rule to reference. * @return This builder instance for method chaining. */ fun displayRuleRef(id: String) = apply { this.displayRuleRef = DisplayRuleRef(id) } + /** * Add display rule to this document object. * @param ref Reference to the display rule. * @return This builder instance for method chaining. */ fun displayRuleRef(ref: DisplayRuleRef) = apply { this.displayRuleRef = ref } + /** * Add a reference to a variable structure to this document object. * @param id ID of the variable structure to reference. * @return This builder instance for method chaining. */ fun variableStructureRef(id: String) = apply { this.variableStructureRef = VariableStructureRef(id) } + /** * Override the default base template for this document object. * @param baseTemplate Path to the base template to use for this document object. @@ -81,99 +75,12 @@ class DocumentObjectBuilder(id: String, private val type: DocumentObjectType) : fun options(options: DocumentObjectOptions) = apply { this.options = options } /** - * Add an area to the document object. + * Add an area to the document object (only for Page-type documents). * @param builder Builder function where receiver is a [AreaBuilder]. * @return This builder instance for method chaining. */ fun area(builder: AreaBuilder.() -> Unit) = apply { - val areaBuilder = AreaBuilder().apply(builder) - content = content + areaBuilder.build() - } - - /** - * Add a paragraph to the document object. - * @param builder Builder function where receiver is a [ParagraphBuilder]. - * @return This builder instance for method chaining. - */ - fun paragraph(builder: ParagraphBuilder.() -> Unit) = apply { - val paragraphBuilder = ParagraphBuilder().apply(builder) - content = content + paragraphBuilder.build() - } - - /** - * Add a table to the document object. - * @param builder Builder function where receiver is a [TableBuilder]. - * @return This builder instance for method chaining. - */ - fun table(builder: TableBuilder.() -> Unit) = apply { - val tableBuilder = TableBuilder().apply(builder) - content = content + tableBuilder.build() - } - - /** - * Add a first match block to the document object - * @param builder Builder function where receiver is a [FirstMatchBuilder]. - * @return This builder instance for method chaining. - */ - fun firstMatch(builder: FirstMatchBuilder.() -> Unit) = apply { - val firstMatchBuilder = FirstMatchBuilder().apply (builder) - content = content + firstMatchBuilder.build() - } - - /** - * Add a select by language block to the document object - * @param builder Builder function where receiver is a [SelectByLanguageBuilder]. - * @return This builder instance for method chaining. - */ - fun selectByLanguage(builder: SelectByLanguageBuilder.() -> Unit) = apply { - val selectByLanguageBuilder = SelectByLanguageBuilder().apply (builder) - content = content + selectByLanguageBuilder.build() - } - - /** - * Add a reference to an image to the document object - * @param imageRef ID of the image to reference. - * @return This builder instance for method chaining. - */ - fun imageRef(imageRef: String) = apply { - imageRef(ImageRef(imageRef)) - } - - /** - * Add a reference to an image to the document object - * @param ref Reference to the image. - * @return This builder instance for method chaining. - */ - fun imageRef(ref: ImageRef) = apply { - content = content + ref - } - - /** - * Add a reference to another document object to the document object. - * @param documentObjectId ID of the document object to reference. - * @return This builder instance for method chaining. - */ - fun documentObjectRef(documentObjectId: String) = apply { - documentObjectRef(DocumentObjectRef(documentObjectId)) - } - - /** - * Add a reference to another document object to the document object. - * @param documentObjectId ID of the document object to reference. - * @param displayRuleId ID of the display rule to reference. - * @return This builder instance for method chaining. - */ - fun documentObjectRef(documentObjectId: String, displayRuleId: String) = apply { - documentObjectRef(DocumentObjectRef(documentObjectId, DisplayRuleRef(displayRuleId))) - } - - /** - * Add a reference to another document object to the document object. - * @param ref Reference to the document object. - * @return This builder instance for method chaining. - */ - fun documentObjectRef(ref: DocumentObjectRef) = apply { - content = content + ref + content.add(AreaBuilder().apply(builder).build()) } /** diff --git a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/AreaBuilder.kt b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/AreaBuilder.kt index 81858d2..e959e09 100644 --- a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/AreaBuilder.kt +++ b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/AreaBuilder.kt @@ -1,33 +1,16 @@ package com.quadient.migration.api.dto.migrationmodel.builder.documentcontent -import com.quadient.migration.api.dto.migrationmodel.DisplayRuleRef import com.quadient.migration.api.dto.migrationmodel.DocumentContent -import com.quadient.migration.api.dto.migrationmodel.DocumentObjectRef import com.quadient.migration.api.dto.migrationmodel.Area -import com.quadient.migration.api.dto.migrationmodel.ImageRef -import com.quadient.migration.api.dto.migrationmodel.builder.ParagraphBuilder +import com.quadient.migration.api.dto.migrationmodel.builder.DocumentContentBuilderBase import com.quadient.migration.api.dto.migrationmodel.builder.PositionBuilder import com.quadient.migration.shared.Position -class AreaBuilder { - private var content = mutableListOf() +class AreaBuilder : DocumentContentBuilderBase { + override val content = mutableListOf() private var position: Position? = null private var interactiveFlowName: String? = null - /** - * Appends the content of the flow area. - * @param content A list of [DocumentContent] to be appended to content of the flow area. - * @return The [AreaBuilder] instance for method chaining. - */ - fun content(content: List) = apply { this.content.addAll(content) } - - /** - * Adds a single [DocumentContent] to the flow area. - * @param content The [DocumentContent] to be added. - * @return The [AreaBuilder] instance for method chaining. - */ - fun content(content: DocumentContent) = apply { this.content.add(content) } - /** * Sets the position of the flow area. * @param position The [Position] to be set for the flow area. @@ -53,70 +36,6 @@ class AreaBuilder { */ fun interactiveFlowName(interactiveFlowName: String) = apply { this.interactiveFlowName = interactiveFlowName } - /** - * Adds a reference to a document object by its ID to the content. - * @param documentObjectId The ID of the document object. - * @return The [AreaBuilder] instance for method chaining. - */ - fun documentObjectRef(documentObjectId: String) = apply { - content.add(DocumentObjectRef(documentObjectId, null)) - } - - /** - * Adds a conditional reference to a document object by its ID and display rule ID to the content. - * @param documentObjectId The ID of the document object. - * @param displayRuleId The ID of the display rule. - * @return The [AreaBuilder] instance for method chaining. - */ - fun documentObjectRef(documentObjectId: String, displayRuleId: String) = apply { - content.add(DocumentObjectRef(documentObjectId, DisplayRuleRef(displayRuleId))) - } - - /** - * Adds a paragraph to the flow area using a builder function. - * @param builder A builder function to build the paragraph. - * @return The [AreaBuilder] instance for method chaining. - */ - fun paragraph(builder: ParagraphBuilder.() -> Unit) = apply { - content.add(ParagraphBuilder().apply(builder).build()) - } - - /** - * Adds a reference to an image by its ID to the content. - * @param imageRefId The ID of the image reference. - * @return The [AreaBuilder] instance for method chaining. - */ - fun imageRef(imageRefId: String) = apply { - content.add(ImageRef(imageRefId)) - } - - /** - * Adds a table to the flow area using a builder function. - * @param builder A builder function to build the table. - * @return The [AreaBuilder] instance for method chaining. - */ - fun table(builder: com.quadient.migration.api.dto.migrationmodel.builder.TableBuilder.() -> Unit) = apply { - content.add(com.quadient.migration.api.dto.migrationmodel.builder.TableBuilder().apply(builder).build()) - } - - /** - * Adds a first match block to the flow area using a builder function. - * @param builder A builder function to build the first match block. - * @return The [AreaBuilder] instance for method chaining. - */ - fun firstMatch(builder: com.quadient.migration.api.dto.migrationmodel.builder.FirstMatchBuilder.() -> Unit) = apply { - content.add(com.quadient.migration.api.dto.migrationmodel.builder.FirstMatchBuilder().apply(builder).build()) - } - - /** - * Adds a select by language block to the flow area using a builder function. - * @param builder A builder function to build the select by language block. - * @return The [AreaBuilder] instance for method chaining. - */ - fun selectByLanguage(builder: SelectByLanguageBuilder.() -> Unit) = apply { - content.add(SelectByLanguageBuilder().apply(builder).build()) - } - /** * Builds the [Area] instance. * @return The constructed [Area] instance. diff --git a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/FirstMatchBuilder.kt b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/FirstMatchBuilder.kt index 8d8c601..7812127 100644 --- a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/FirstMatchBuilder.kt +++ b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/FirstMatchBuilder.kt @@ -2,9 +2,7 @@ package com.quadient.migration.api.dto.migrationmodel.builder import com.quadient.migration.api.dto.migrationmodel.DisplayRuleRef import com.quadient.migration.api.dto.migrationmodel.DocumentContent -import com.quadient.migration.api.dto.migrationmodel.DocumentObjectRef import com.quadient.migration.api.dto.migrationmodel.FirstMatch -import com.quadient.migration.api.dto.migrationmodel.builder.documentcontent.SelectByLanguageBuilder class FirstMatchBuilder { private var default: MutableList = mutableListOf() @@ -84,25 +82,11 @@ class FirstMatchBuilder { default.add(ParagraphBuilder().string(text).build()) } - class CaseBuilder { - var content: MutableList = mutableListOf() + class CaseBuilder : DocumentContentBuilderBase { + override val content: MutableList = mutableListOf() var displayRuleRef: DisplayRuleRef? = null var name: String? = null - /** - * Replaces the content for the case. - * @param content The [DocumentContent] to be used in the case. - * @return A CaseBuilder instance for method chaining. - */ - fun content(content: DocumentContent) = apply { this.content = mutableListOf(content) } - - /** - * Appends additional content to the case. - * @param content The [DocumentContent] to be added to the case. - * @return A CaseBuilder instance for method chaining. - */ - fun appendContent(content: DocumentContent) = apply { this.content.add(content) } - /** * Sets the display rule reference for the case. * @param ref The [DisplayRuleRef] to be used in the case. @@ -123,69 +107,5 @@ class FirstMatchBuilder { * @return A CaseBuilder instance for method chaining. */ fun name(name: String) = apply { this.name = name } - - /** - * Adds a paragraph to the case using a builder function. - * @param builder A builder function to build the paragraph. - * @return A CaseBuilder instance for method chaining. - */ - fun paragraph(builder: ParagraphBuilder.() -> Unit) = apply { - content.add(ParagraphBuilder().apply(builder).build()) - } - - /** - * Adds a table to the case using a builder function. - * @param builder A builder function to build the table. - * @return A CaseBuilder instance for method chaining. - */ - fun table(builder: TableBuilder.() -> Unit) = apply { - content.add(TableBuilder().apply(builder).build()) - } - - /** - * Adds an image reference to the case. - * @param imageId The ID of the image to reference. - * @return A CaseBuilder instance for method chaining. - */ - fun imageRef(imageId: String) = apply { - content.add(com.quadient.migration.api.dto.migrationmodel.ImageRef(imageId)) - } - - /** - * Adds a document object reference to the case. - * @param documentObjectId The ID of the document object to reference. - * @return A CaseBuilder instance for method chaining. - */ - fun documentObjectRef(documentObjectId: String) = apply { - content.add(DocumentObjectRef(documentObjectId, null)) - } - - /** - * Adds a conditional document object reference to the case. - * @param documentObjectId The ID of the document object to reference. - * @param displayRuleId The ID of the display rule. - * @return A CaseBuilder instance for method chaining. - */ - fun documentObjectRef(documentObjectId: String, displayRuleId: String) = apply { - content.add(DocumentObjectRef(documentObjectId, DisplayRuleRef(displayRuleId))) - } - - /** - * Adds a nested first match block to the case. - * @param builder A builder function to build the first match block. - * @return A CaseBuilder instance for method chaining. - */ - fun firstMatch(builder: FirstMatchBuilder.() -> Unit) = apply { - content.add(FirstMatchBuilder().apply(builder).build()) - } - - /** - * Adds a select by language block to the case. - * @param builder A builder function to build the select by language block. - * @return A CaseBuilder instance for method chaining. - */ - fun selectByLanguage(builder: SelectByLanguageBuilder.() -> Unit) = apply { - content.add(SelectByLanguageBuilder().apply(builder).build()) - } } } diff --git a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/SelectByLanguageBuilder.kt b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/SelectByLanguageBuilder.kt index 4ea23e4..d48b147 100644 --- a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/SelectByLanguageBuilder.kt +++ b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/SelectByLanguageBuilder.kt @@ -1,13 +1,8 @@ package com.quadient.migration.api.dto.migrationmodel.builder.documentcontent -import com.quadient.migration.api.dto.migrationmodel.DisplayRuleRef import com.quadient.migration.api.dto.migrationmodel.DocumentContent -import com.quadient.migration.api.dto.migrationmodel.DocumentObjectRef -import com.quadient.migration.api.dto.migrationmodel.ImageRef import com.quadient.migration.api.dto.migrationmodel.SelectByLanguage -import com.quadient.migration.api.dto.migrationmodel.builder.FirstMatchBuilder -import com.quadient.migration.api.dto.migrationmodel.builder.ParagraphBuilder -import com.quadient.migration.api.dto.migrationmodel.builder.TableBuilder +import com.quadient.migration.api.dto.migrationmodel.builder.DocumentContentBuilderBase class SelectByLanguageBuilder { private var cases: MutableList = mutableListOf() @@ -40,93 +35,15 @@ class SelectByLanguageBuilder { cases.add(caseBuilder) } - class CaseBuilder { - var content: MutableList = mutableListOf() + class CaseBuilder : DocumentContentBuilderBase { + override val content: MutableList = mutableListOf() var language: String? = null - /** - * Replaces the content for the case. - * @param content The [DocumentContent] to be used in the case. - * @return A CaseBuilder instance for method chaining. - */ - fun content(content: DocumentContent) = apply { this.content = mutableListOf(content) } - - /** - * Appends additional content to the case. - * @param content The [DocumentContent] to be added to the case. - * @return A CaseBuilder instance for method chaining. - */ - fun appendContent(content: DocumentContent) = apply { this.content.add(content) } - /** * Sets the language for the case. * @param language The language to be used for the case. * @return A CaseBuilder instance for method chaining. */ fun language(language: String) = apply { this.language = language } - - /** - * Adds a paragraph to the case using a builder function. - * @param builder A builder function to build the paragraph. - * @return A CaseBuilder instance for method chaining. - */ - fun paragraph(builder: ParagraphBuilder.() -> Unit) = apply { - content.add(ParagraphBuilder().apply(builder).build()) - } - - /** - * Adds a table to the case using a builder function. - * @param builder A builder function to build the table. - * @return A CaseBuilder instance for method chaining. - */ - fun table(builder: TableBuilder.() -> Unit) = apply { - content.add(TableBuilder().apply(builder).build()) - } - - /** - * Adds an image reference to the case. - * @param imageId The ID of the image to reference. - * @return A CaseBuilder instance for method chaining. - */ - fun imageRef(imageId: String) = apply { - content.add(ImageRef(imageId)) - } - - /** - * Adds a document object reference to the case. - * @param documentObjectId The ID of the document object to reference. - * @return A CaseBuilder instance for method chaining. - */ - fun documentObjectRef(documentObjectId: String) = apply { - content.add(DocumentObjectRef(documentObjectId, null)) - } - - /** - * Adds a conditional document object reference to the case. - * @param documentObjectId The ID of the document object to reference. - * @param displayRuleId The ID of the display rule. - * @return A CaseBuilder instance for method chaining. - */ - fun documentObjectRef(documentObjectId: String, displayRuleId: String) = apply { - content.add(DocumentObjectRef(documentObjectId, DisplayRuleRef(displayRuleId))) - } - - /** - * Adds a first match block to the case. - * @param builder A builder function to build the first match block. - * @return A CaseBuilder instance for method chaining. - */ - fun firstMatch(builder: FirstMatchBuilder.() -> Unit) = apply { - content.add(FirstMatchBuilder().apply(builder).build()) - } - - /** - * Adds a nested select by language block to the case. - * @param builder A builder function to build the select by language block. - * @return A CaseBuilder instance for method chaining. - */ - fun selectByLanguage(builder: SelectByLanguageBuilder.() -> Unit) = apply { - content.add(SelectByLanguageBuilder().apply(builder).build()) - } } } diff --git a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/TableBuilder.kt b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/TableBuilder.kt index 9ce6586..b459949 100644 --- a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/TableBuilder.kt +++ b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/TableBuilder.kt @@ -31,18 +31,18 @@ class TableBuilder { fun columnWidths(width: List) = columnWidths.apply { clear() }.addAll(width) fun build(): Table { - return Table(rows = rows.map { - Table.Row(cells = it.cells.map { + return Table(rows = rows.map { row -> + Table.Row(cells = row.cells.map { cell -> Table.Cell( - content = it.content, - mergeUp = it.mergeUp, - mergeLeft = it.mergeLeft, + content = cell.content, + mergeUp = cell.mergeUp, + mergeLeft = cell.mergeLeft, ) - }, displayRuleRef = it.displayRuleRef) - }, columnWidths = columnWidths.map { + }, displayRuleRef = row.displayRuleRef) + }, columnWidths = columnWidths.map { colWidth -> Table.ColumnWidth( - minWidth = it.minWidth, - percentWidth = it.percentWidth, + minWidth = colWidth.minWidth, + percentWidth = colWidth.percentWidth, ) }) } @@ -60,35 +60,14 @@ class TableBuilder { fun displayRuleRef(ref: DisplayRuleRef) = this.apply { this.displayRuleRef = ref } } - class Cell { - val content = mutableListOf() + class Cell : DocumentContentBuilderBase { + override val content = mutableListOf() var mergeLeft = false var mergeUp = false fun mergeLeft(value: Boolean) = apply { mergeLeft = value } fun mergeUp(value: Boolean) = apply { mergeUp = value } - /** - * Appends content to the cell. - * @param content The content to append to the cell. - * @return The [Cell] instance for method chaining. - */ - fun appendContent(content: DocumentContent) = apply { this.content.add(content) } - - /** - * Replaces all content in the cell with a single item. - * @param content The content to set as the cell content. - * @return The [Cell] instance for method chaining. - */ - fun content(content: DocumentContent) = apply { this.content.apply { clear() }.add(content) } - - /** - * Replaces all content in the cell with multiple items. - * @param content The list of content to set as the cell content. - * @return The [Cell] instance for method chaining. - */ - fun content(content: List) = apply { this@Cell.content.apply { clear() }.addAll(content) } - /** * Adds a paragraph with the given string to the cell. * @param text The string to add in a paragraph. @@ -99,67 +78,14 @@ class TableBuilder { } /** - * Adds a paragraph to the cell using a builder function. - * @param builder A builder function to build the paragraph. - * @return The [Cell] instance for method chaining. - */ - fun paragraph(builder: ParagraphBuilder.() -> Unit) = apply { - content.add(ParagraphBuilder().apply(builder).build()) - } - - /** - * Adds a table to the cell using a builder function. - * @param builder A builder function to build the table. - * @return The [Cell] instance for method chaining. - */ - fun table(builder: TableBuilder.() -> Unit) = apply { - content.add(TableBuilder().apply(builder).build()) - } - - /** - * Adds an image reference to the cell. - * @param imageId The ID of the image to reference. + * Adds a paragraph with the given text to the cell. + * @param text The text content to add in a paragraph. * @return The [Cell] instance for method chaining. + * @deprecated Use [string] for consistency with string() naming pattern. */ - fun imageRef(imageId: String) = apply { - content.add(com.quadient.migration.api.dto.migrationmodel.ImageRef(imageId)) - } - - /** - * Adds a document object reference to the cell. - * @param documentObjectId The ID of the document object to reference. - * @return The [Cell] instance for method chaining. - */ - fun documentObjectRef(documentObjectId: String) = apply { - content.add(DocumentObjectRef(documentObjectId, null)) - } - - /** - * Adds a conditional document object reference to the cell. - * @param documentObjectId The ID of the document object to reference. - * @param displayRuleId The ID of the display rule. - * @return The [Cell] instance for method chaining. - */ - fun documentObjectRef(documentObjectId: String, displayRuleId: String) = apply { - content.add(DocumentObjectRef(documentObjectId, DisplayRuleRef(displayRuleId))) - } - - /** - * Adds a first match block to the cell using a builder function. - * @param builder A builder function to build the first match block. - * @return The [Cell] instance for method chaining. - */ - fun firstMatch(builder: FirstMatchBuilder.() -> Unit) = apply { - content.add(FirstMatchBuilder().apply(builder).build()) - } - - /** - * Adds a select by language block to the cell using a builder function. - * @param builder A builder function to build the select by language block. - * @return The [Cell] instance for method chaining. - */ - fun selectByLanguage(builder: SelectByLanguageBuilder.() -> Unit) = apply { - content.add(SelectByLanguageBuilder().apply(builder).build()) + @Deprecated("Use string() instead", ReplaceWith("string(text)")) + fun text(text: String) = apply { + this.content.add(ParagraphBuilder().string(text).build()) } } From 846195a50a8af28c86f59f313818e8b25e7c453f Mon Sep 17 00:00:00 2001 From: "d.svitak" Date: Mon, 8 Dec 2025 12:44:42 +0100 Subject: [PATCH 3/5] remove new, yet deprecated TableBuilder method, which makes no sense --- .../builder/documentcontent/TableBuilder.kt | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/TableBuilder.kt b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/TableBuilder.kt index b459949..711eefe 100644 --- a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/TableBuilder.kt +++ b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/TableBuilder.kt @@ -76,17 +76,6 @@ class TableBuilder { fun string(text: String) = apply { this.content.add(ParagraphBuilder().string(text).build()) } - - /** - * Adds a paragraph with the given text to the cell. - * @param text The text content to add in a paragraph. - * @return The [Cell] instance for method chaining. - * @deprecated Use [string] for consistency with string() naming pattern. - */ - @Deprecated("Use string() instead", ReplaceWith("string(text)")) - fun text(text: String) = apply { - this.content.add(ParagraphBuilder().string(text).build()) - } } data class ColumnWidth(val minWidth: Size, val percentWidth: Double) From 00d3dcc16baec3ca2cbce306aa688b566802683a Mon Sep 17 00:00:00 2001 From: "d.svitak" Date: Mon, 8 Dec 2025 13:03:57 +0100 Subject: [PATCH 4/5] allow convenient string() method for all DocumentContentBuilders --- .../builder/DocumentContentBuilderBase.kt | 10 ++++++++++ .../builder/documentcontent/TableBuilder.kt | 9 --------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/DocumentContentBuilderBase.kt b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/DocumentContentBuilderBase.kt index 9dbbd63..bf17efe 100644 --- a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/DocumentContentBuilderBase.kt +++ b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/DocumentContentBuilderBase.kt @@ -110,4 +110,14 @@ interface DocumentContentBuilderBase { fun selectByLanguage(builder: SelectByLanguageBuilder.() -> Unit): T = apply { this.content.add(SelectByLanguageBuilder().apply(builder).build()) } as T + + /** + * Adds a paragraph with the given string to the content (convenience method). + * Creates: Paragraph → Text → StringValue + * @param text The string to add in a paragraph. + * @return This builder instance for method chaining. + */ + fun string(text: String): T = apply { + this.content.add(ParagraphBuilder().string(text).build()) + } as T } \ No newline at end of file diff --git a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/TableBuilder.kt b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/TableBuilder.kt index 711eefe..d7ff22c 100644 --- a/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/TableBuilder.kt +++ b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/documentcontent/TableBuilder.kt @@ -67,15 +67,6 @@ class TableBuilder { fun mergeLeft(value: Boolean) = apply { mergeLeft = value } fun mergeUp(value: Boolean) = apply { mergeUp = value } - - /** - * Adds a paragraph with the given string to the cell. - * @param text The string to add in a paragraph. - * @return The [Cell] instance for method chaining. - */ - fun string(text: String) = apply { - this.content.add(ParagraphBuilder().string(text).build()) - } } data class ColumnWidth(val minWidth: Size, val percentWidth: Double) From 31a37f9cfbf42bd9cfefaa305dd0298240d27c78 Mon Sep 17 00:00:00 2001 From: "d.svitak" Date: Mon, 8 Dec 2025 14:30:35 +0100 Subject: [PATCH 5/5] changelog update for builder refactor changes --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ebc391..207b253 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) ## [Unreleased] ### Added +- Added `string()` convenience method to all DocumentContent builders for creating paragraphs with simple text content ### Changed +- Refactored DocumentContent builders to use shared interface, reducing code duplication and improving consistency +- Standardized string-handling methods: deprecated `content(String)` in favor of `string(String)` for clarity ### Fixed