Skip to content

Commit

Permalink
fix checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
cbellone committed Nov 4, 2018
1 parent 9196bc6 commit 90425fb
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
5 changes: 5 additions & 0 deletions backend/src/main/kotlin/alfio/pi/manager/KVStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ 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
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) {

Expand Down Expand Up @@ -259,6 +263,7 @@ open class KVStore(private val gson: Gson) {

fun loadLabelConfiguration(eventKey: String) : Optional<LabelConfiguration> {
val res = labelConfigurationTable.getAsString(eventKey)
logger.trace("loaded labelConfiguration: {}", res)
return if (res == null) {
Optional.empty()
} else {
Expand Down
8 changes: 4 additions & 4 deletions backend/src/main/kotlin/alfio/pi/manager/LabelManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}, {
Expand Down
22 changes: 15 additions & 7 deletions backend/src/main/kotlin/alfio/pi/manager/PrintManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -60,7 +62,6 @@ open class PrinterAnnouncer(private val trustManager: X509TrustManager,
private val gson: Gson) {

private val masterUrl = AtomicReference<String>()
private val MDNS_NAME = "alfio-server"

init {

Expand Down Expand Up @@ -193,17 +194,24 @@ open class LocalPrintManager(private val labelTemplates: List<LabelTemplate>,
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<String> =
layout.content.thirdRow.orEmpty().asSequence()
private fun retrieveAllAdditionalInfo(layout: LabelLayout, ticket: Ticket) : List<String> {
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<String> {
val info = getAllTicketInfo(ticket)
Expand Down Expand Up @@ -321,7 +329,7 @@ data class ConfigurableLabelContent(val firstRow: String,
val additionalRows: List<String>?,
val qrContent: String,
val partialID: String,
val checkbox: Boolean = false)
val checkbox: Boolean)



4 changes: 3 additions & 1 deletion backend/src/main/kotlin/alfio/pi/model/ScanModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
29 changes: 27 additions & 2 deletions backend/src/test/kotlin/alfio/pi/manager/LabelManagerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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<String, String>? = mapOf("company" to "company")): Ticket = Ticket(uuid = "12345-678", firstName = "firstName", lastName = "lastName", additionalInfo = additionalInfo, email = null)


Expand Down

0 comments on commit 90425fb

Please sign in to comment.