diff --git a/backend/src/main/kotlin/alfio/pi/manager/KVStore.kt b/backend/src/main/kotlin/alfio/pi/manager/KVStore.kt index 4002dbfe6..b324cd8eb 100644 --- a/backend/src/main/kotlin/alfio/pi/manager/KVStore.kt +++ b/backend/src/main/kotlin/alfio/pi/manager/KVStore.kt @@ -5,6 +5,8 @@ import ch.digitalfondue.synckv.SyncKV import ch.digitalfondue.synckv.SyncKVTable import com.google.gson.Gson import org.jgroups.util.Tuple +import org.slf4j.Logger +import org.slf4j.LoggerFactory import org.springframework.stereotype.Component import java.time.Instant import java.time.ZoneId @@ -12,6 +14,8 @@ import java.time.ZonedDateTime import java.util.* import kotlin.collections.ArrayList +private val logger: Logger = LoggerFactory.getLogger("alfio.pi.manager.KVStore") + @Component open class KVStore(private val gson: Gson) { @@ -259,6 +263,7 @@ open class KVStore(private val gson: Gson) { fun loadLabelConfiguration(eventKey: String) : Optional { val res = labelConfigurationTable.getAsString(eventKey) + logger.trace("loaded labelConfiguration: {}", res) return if (res == null) { Optional.empty() } else { diff --git a/backend/src/main/kotlin/alfio/pi/manager/LabelManager.kt b/backend/src/main/kotlin/alfio/pi/manager/LabelManager.kt index b335737d3..b04f74b6e 100644 --- a/backend/src/main/kotlin/alfio/pi/manager/LabelManager.kt +++ b/backend/src/main/kotlin/alfio/pi/manager/LabelManager.kt @@ -141,19 +141,19 @@ open class ZebraZD410: LabelTemplate { val font = fontLoader.invoke(ZebraZD410::class.java.getResourceAsStream("/font/DejaVuSansMono.ttf")) stream.use { it.transform(Matrix(0F, 1F, -1F, 0F, pageWidth, 0F)) - val firstRowContent = optimizeText(labelContent.firstRow, arrayOf(11 to 26F, 12 to 24F, 14 to 22F, 15 to 20F, 17 to 18F), true) + val firstRowContent = optimizeText(labelContent.firstRow, arrayOf(11 to 26F, 12 to 24F, 14 to 22F, 15 to 20F, 16 to 18F, 17 to 17F), true) it.setFont(font, firstRowContent.second) it.beginText() it.newLineAtOffset(10F, 115F) it.showText(firstRowContent.first) - val secondRowContent = optimizeText(labelContent.secondRow, arrayOf(16 to 18F, 18 to 16F, 21 to 14F), true) + val secondRowContent = optimizeText(labelContent.secondRow, arrayOf(16 to 18F, 17 to 17F, 18 to 16F, 21 to 14F), true) it.setFont(font, secondRowContent.second) it.newLineAtOffset(0F, -25F) it.showText(secondRowContent.first) - val maxLengthAdditionalRows = arrayOf(29 to 10F) - printAdditionalRows(labelContent.additionalRows.orEmpty().take(3), it, -20F, labelContent, font, maxLengthAdditionalRows) + val maxLengthAdditionalRows = arrayOf(20 to 11F, 29 to 10F) + printAdditionalRows(labelContent.additionalRows.orEmpty().take(3), it, -25F, labelContent, font, maxLengthAdditionalRows) it.endText() diff --git a/backend/src/main/kotlin/alfio/pi/manager/LocalResourceManager.kt b/backend/src/main/kotlin/alfio/pi/manager/LocalResourceManager.kt index c6f6472e6..aad239ff5 100644 --- a/backend/src/main/kotlin/alfio/pi/manager/LocalResourceManager.kt +++ b/backend/src/main/kotlin/alfio/pi/manager/LocalResourceManager.kt @@ -116,7 +116,9 @@ fun reprintBadge(scanLogId: String, printerId: Int?, username: String, content: else -> Optional.empty() } }.map { (printer, ticket, eventKey) -> - val labelConfiguration = kvStore.loadLabelConfiguration(eventKey).map { LabelConfigurationAndContent(it, content) }.orElse(LabelConfigurationAndContent(null, content)) + val labelConfiguration = kvStore.loadLabelConfiguration(eventKey).map { + LabelConfigurationAndContent(it, content?.copy(checkbox = it.layout?.content?.checkbox ?: false)) + }.orElse(LabelConfigurationAndContent(null, content)) printManager.printLabel(printer, ticket, labelConfiguration) }.orElse(false) }, { diff --git a/backend/src/main/kotlin/alfio/pi/manager/PrintManager.kt b/backend/src/main/kotlin/alfio/pi/manager/PrintManager.kt index ed6a51aff..3363417cf 100644 --- a/backend/src/main/kotlin/alfio/pi/manager/PrintManager.kt +++ b/backend/src/main/kotlin/alfio/pi/manager/PrintManager.kt @@ -41,6 +41,8 @@ import javax.jmdns.ServiceListener import javax.net.ssl.SSLContext import javax.net.ssl.X509TrustManager +internal const val STATIC_TEXT_PREFIX = "static-text:" +private const val MDNS_NAME = "alfio-server" private val logger = LoggerFactory.getLogger(PrintManager::class.java) @@ -60,7 +62,6 @@ open class PrinterAnnouncer(private val trustManager: X509TrustManager, private val gson: Gson) { private val masterUrl = AtomicReference() - private val MDNS_NAME = "alfio-server" init { @@ -193,17 +194,24 @@ open class LocalPrintManager(private val labelTemplates: List, val ticketInfo = getAllTicketInfo(ticket) val firstRow = ticketInfo[layout.content.firstRow] ?: ticket.firstName val secondRow = ticketInfo[layout.content.secondRow] ?: ticket.lastName - ConfigurableLabelContent(firstRow, secondRow, retrieveAllAdditionalInfo(layout, ticket), qrContent, partialID) + ConfigurableLabelContent(firstRow, secondRow, retrieveAllAdditionalInfo(layout, ticket), qrContent, partialID, layout.content.checkbox?:false) } else { - ConfigurableLabelContent(ticket.firstName, ticket.lastName, listOf(ticket.additionalInfo?.get("company").orEmpty()), ticket.uuid, ticket.uuid.substringBefore('-').toUpperCase()) + ConfigurableLabelContent(ticket.firstName, ticket.lastName, listOf(ticket.additionalInfo?.get("company").orEmpty()), ticket.uuid, ticket.uuid.substringBefore('-').toUpperCase(), false) } } - private fun retrieveAllAdditionalInfo(layout: LabelLayout, ticket: Ticket) : List = - layout.content.thirdRow.orEmpty().asSequence() + private fun retrieveAllAdditionalInfo(layout: LabelLayout, ticket: Ticket) : List { + return layout.content.thirdRow.orEmpty().asSequence() .plus(layout.content.additionalRows.orEmpty()) .filter { it.isNotEmpty() } - .map { ticket.additionalInfo?.get(it).orEmpty() }.toList() + .map { + if(it.startsWith(STATIC_TEXT_PREFIX)) { + it.substringAfter(":", "") + } else { + ticket.additionalInfo?.get(it).orEmpty() + } + }.toList() + } private fun retrieveQRCodeContent(layout: LabelLayout, ticket: Ticket) : List { val info = getAllTicketInfo(ticket) @@ -321,7 +329,7 @@ data class ConfigurableLabelContent(val firstRow: String, val additionalRows: List?, val qrContent: String, val partialID: String, - val checkbox: Boolean = false) + val checkbox: Boolean) diff --git a/backend/src/main/kotlin/alfio/pi/model/ScanModel.kt b/backend/src/main/kotlin/alfio/pi/model/ScanModel.kt index 706705bbf..e3284d132 100644 --- a/backend/src/main/kotlin/alfio/pi/model/ScanModel.kt +++ b/backend/src/main/kotlin/alfio/pi/model/ScanModel.kt @@ -85,7 +85,9 @@ data class UserAndPrinter(@Column("username") private val username: String, val printer = Printer(printerId, printerName, printerDescription, printerActive) } -data class LabelConfiguration(@Column("event_key_fk") val eventKey: String, @Column("json") val json: String?, @Column("enabled") val enabled: Boolean) : Serializable { +data class LabelConfiguration(@Column("event_key_fk") val eventKey: String, + @Column("json") val json: String?, + @Column("enabled") val enabled: Boolean) : Serializable { val layout: LabelLayout? = GsonContainer.GSON?.fromJson(json, LabelLayout::class.java) } diff --git a/backend/src/test/kotlin/alfio/pi/manager/LabelManagerTest.kt b/backend/src/test/kotlin/alfio/pi/manager/LabelManagerTest.kt index 885172f5d..fb295d5ef 100644 --- a/backend/src/test/kotlin/alfio/pi/manager/LabelManagerTest.kt +++ b/backend/src/test/kotlin/alfio/pi/manager/LabelManagerTest.kt @@ -20,8 +20,7 @@ package alfio.pi.manager import alfio.pi.model.LabelLayout import alfio.pi.model.Ticket import com.google.gson.Gson -import org.junit.Assert.assertEquals -import org.junit.Assert.assertTrue +import org.junit.Assert.* import org.junit.Test import org.mockito.Mockito import java.util.* @@ -115,6 +114,32 @@ class LabelManagerTest { assertTrue(result.partialID.isEmpty()) } + @Test + fun testBuildLabelWithStaticText() { + val localPrintManager = LocalPrintManager(emptyList(), Mockito.mock(SystemEventHandler::class.java), Mockito.mock(KVStore::class.java)) + val jsonString = """ + { + "qrCode": { + "additionalInfo": ["firstName", "lastName", "fullName", "emailAddress", "word1","word2","word3"], + "infoSeparator": "::" + }, + "content": { + "additionalRows": ["word1", "${STATIC_TEXT_PREFIX}this is a test"] + }, + "general": { + "printPartialID": true + } + }""" + val labelLayout = Gson().fromJson(jsonString, LabelLayout::class.java) + val ticket = ticket(mapOf("word1" to "thisIsTheFirstWord", "word2" to "thisIsTheSecondWord", "word3" to "thisIsTheThirdWord")) + val result = localPrintManager.buildConfigurableLabelContent(labelLayout, ticket) + assertEquals(ticket.firstName, result.firstRow) + assertEquals(ticket.lastName, result.secondRow) + assertEquals("thisIsTheFirstWord this is a test", result.additionalRows.orEmpty().joinToString(" ")) + assertEquals("${ticket.uuid}::${ticket.firstName}::${ticket.lastName}::${ticket.fullName}::${ticket.email.orEmpty()}::thisIsTheFirstWord::thisIsTheSecondWord::thisIsTheThirdWord", result.qrContent) + assertFalse(result.partialID.isEmpty()) + } + private fun ticket(additionalInfo: Map? = mapOf("company" to "company")): Ticket = Ticket(uuid = "12345-678", firstName = "firstName", lastName = "lastName", additionalInfo = additionalInfo, email = null)