Skip to content

Commit b009707

Browse files
authored
Merge pull request #24 from DatepollSystems/WR-342-virtualPrinter-id
WR-342: Use backend printer id for virtual printer path
2 parents 6e831c3 + 5eaf547 commit b009707

File tree

6 files changed

+23
-22
lines changed

6 files changed

+23
-22
lines changed

src/main/kotlin/org/datepollsystems/waiterrobot/mediator/printer/AbstractLocalPrinter.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.datepollsystems.waiterrobot.mediator.printer
22

33
import org.apache.pdfbox.Loader
44
import org.apache.pdfbox.pdmodel.PDDocument
5+
import org.datepollsystems.waiterrobot.mediator.core.ID
56
import org.koin.core.component.KoinComponent
67
import java.util.*
78

@@ -14,12 +15,12 @@ import java.util.*
1415
* @see org.datepollsystems.waiterrobot.mediator.printer.LocalPrinterTest.testPrint
1516
*/
1617
abstract class AbstractLocalPrinter : LocalPrinterInfo, KoinComponent {
17-
fun printPdf(pdfId: String, base64data: String) {
18+
fun printPdf(pdfId: String, bePrinterId: ID, base64data: String) {
1819
val decoded = Base64.getDecoder().decode(base64data)
19-
printPdf(pdfId, Loader.loadPDF(decoded))
20+
printPdf(pdfId, bePrinterId, Loader.loadPDF(decoded))
2021
}
2122

22-
protected abstract fun printPdf(pdfId: String, document: PDDocument)
23+
protected abstract fun printPdf(pdfId: String, bePrinterId: ID, document: PDDocument)
2324

2425
override fun equals(other: Any?): Boolean {
2526
if (other !is AbstractLocalPrinter) return false

src/main/kotlin/org/datepollsystems/waiterrobot/mediator/printer/LocalPrinter.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@ package org.datepollsystems.waiterrobot.mediator.printer
22

33
import org.apache.pdfbox.pdmodel.PDDocument
44
import org.apache.pdfbox.printing.PDFPageable
5+
import org.datepollsystems.waiterrobot.mediator.core.ID
56
import org.datepollsystems.waiterrobot.mediator.utils.sha256
67
import java.awt.print.PrinterJob
78
import javax.print.PrintService
89

910
class LocalPrinter(private val service: PrintService) : AbstractLocalPrinter() {
1011
override val localId = service.name.sha256() // Name should be unique (on a system level)
11-
override val name get() = service.name
12+
override val name: String get() = service.name
1213

13-
override fun printPdf(
14-
pdfId: String,
15-
document: PDDocument,
16-
) {
14+
override fun printPdf(pdfId: String, bePrinterId: ID, document: PDDocument) {
1715
val pageable = PDFPageable(document)
1816

1917
val printJob = PrinterJob.getPrinterJob()

src/main/kotlin/org/datepollsystems/waiterrobot/mediator/printer/VirtualLocalPrinter.kt

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@ package org.datepollsystems.waiterrobot.mediator.printer
22

33
import org.apache.pdfbox.pdmodel.PDDocument
44
import org.datepollsystems.waiterrobot.mediator.App
5-
import org.datepollsystems.waiterrobot.mediator.core.di.injectLogger
6-
import java.io.File
5+
import org.datepollsystems.waiterrobot.mediator.core.ID
6+
import org.datepollsystems.waiterrobot.mediator.core.di.injectLoggerForClass
77
import kotlin.io.path.Path
8+
import kotlin.io.path.absolutePathString
89

9-
class VirtualLocalPrinter(number: Int) : AbstractLocalPrinter() {
10-
override val localId: String = "virtualPrinter_$number"
11-
override val name: String = "Virtual Printer $number"
12-
private val basePath: File = Path(App.config.basePath, "virtualPrinters", number.toString()).toFile()
13-
private val logger by injectLogger(this::class.simpleName + "#$localId")
10+
object VirtualLocalPrinter : AbstractLocalPrinter() {
11+
override val localId: String = "virtualPrinter"
12+
override val name: String = "Virtual Printer"
13+
private val basePath: String = Path(App.config.basePath, "virtualPrinters").absolutePathString()
14+
private val logger by injectLoggerForClass()
1415

1516
init {
16-
basePath.mkdirs()
17-
logger.i("Will save to: ${basePath.absolutePath}")
17+
logger.i("Will save to: $basePath")
1818
}
1919

20-
override fun printPdf(pdfId: String, document: PDDocument) {
21-
document.save(File(basePath, "$pdfId.pdf"))
20+
override fun printPdf(pdfId: String, bePrinterId: ID, document: PDDocument) {
21+
val file = Path(basePath, bePrinterId.toString(), "$pdfId.pdf").toFile()
22+
file.parentFile.mkdirs()
23+
document.save(file)
2224
}
2325
}

src/main/kotlin/org/datepollsystems/waiterrobot/mediator/printer/service/PrinterDiscoverService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ object PrinterDiscoverService {
2424
.map { LocalPrinter(it) }
2525
.let {
2626
if (App.config !is Config.Prod) {
27-
it.plus(VirtualLocalPrinter(1))
27+
it.plus(VirtualLocalPrinter)
2828
} else {
2929
it
3030
}

src/main/kotlin/org/datepollsystems/waiterrobot/mediator/printer/service/PrinterService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ object PrinterService {
3535
private suspend fun print(pdfId: String, printerId: ID, base64data: String) {
3636
// TODO handle printer is not registered on this mediator (info to BE)
3737
val printerPairing = backendIdToPairing[printerId] ?: throw PrinterWithIdNotFoundException(printerId)
38-
printerPairing.loPrinter.printPdf(pdfId, base64data)
38+
printerPairing.loPrinter.printPdf(pdfId, printerPairing.bePrinter.id, base64data)
3939

4040
printQueue.emit(PrintTransaction(pdfId, LocalDateTime.now(), printerPairing.bePrinter.name))
4141
// test is the id of the test pdf, no response expected by backend

src/test/kotlin/org/datepollsystems/waiterrobot/mediator/printer/LocalPrinterTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ internal class LocalPrinterTest {
2020
val file = File("src/test/resources/testBill.pdf")
2121
val base64String = Base64.getEncoder().encodeToString(file.readBytes())
2222

23-
printer.printPdf("test", base64String)
23+
printer.printPdf("test", 1, base64String)
2424
}
2525
}

0 commit comments

Comments
 (0)