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 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 new file mode 100644 index 0000000..bf17efe --- /dev/null +++ b/migration-library/src/main/kotlin/com/quadient/migration/api/dto/migrationmodel/builder/DocumentContentBuilderBase.kt @@ -0,0 +1,123 @@ +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.ImageRef +import com.quadient.migration.api.dto.migrationmodel.builder.documentcontent.SelectByLanguageBuilder + +/** + * Base interface for builders that contain a list of DocumentContent. + * Provides standard methods for adding various types of document content. + */ +@Suppress("UNCHECKED_CAST") +interface DocumentContentBuilderBase { + /** + * 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 + + /** + * 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/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/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..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,43 +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)) - } - /** * 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..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 @@ -32,7 +32,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,24 +55,37 @@ class FirstMatchBuilder { cases.add(caseBuilder) } - class CaseBuilder { - var content: MutableList = mutableListOf() - var displayRuleRef: DisplayRuleRef? = null - var name: String? = null + /** + * 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 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) } + /** + * 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()) + } - /** - * 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) } + /** + * 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 : DocumentContentBuilderBase { + override val content: MutableList = mutableListOf() + var displayRuleRef: DisplayRuleRef? = null + var name: String? = null /** * Sets the display rule reference for the case. 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..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 @@ -2,8 +2,9 @@ package com.quadient.migration.api.dto.migrationmodel.builder.documentcontent import com.quadient.migration.api.dto.migrationmodel.DocumentContent import com.quadient.migration.api.dto.migrationmodel.SelectByLanguage +import com.quadient.migration.api.dto.migrationmodel.builder.DocumentContentBuilderBase -class SelectByLanguageBuilder { +class SelectByLanguageBuilder { private var cases: MutableList = mutableListOf() /** @@ -11,14 +12,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" }) + }) } /** @@ -37,26 +35,10 @@ class SelectByLanguageBuilder { cases.add(caseBuilder) } - class CaseBuilder { - var content: MutableList = mutableListOf() + class CaseBuilder : DocumentContentBuilderBase { + override val content: MutableList = mutableListOf() var language: String? = null - /** - * Sets 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) } - - /** - * 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) } - /** * Sets the language for the case. * @param language The language to be used for the case. 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..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 @@ -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 { @@ -29,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, ) }) } @@ -58,37 +60,13 @@ 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 } - - /** - * 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]. - */ - 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]. - */ - 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]. - */ - fun content(content: List) = apply { this@Cell.content.apply { clear() }.addAll(content) } } data class ColumnWidth(val minWidth: Size, val percentWidth: Double)