156 lines
7.0 KiB
Kotlin
156 lines
7.0 KiB
Kotlin
package database.table
|
|
|
|
import database.data.QueuePaging
|
|
import database.dbFunctions
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook
|
|
import org.tinylog.Logger
|
|
import java.sql.Connection
|
|
import java.util.function.Consumer
|
|
|
|
class Table_QueuePaging(connection : Connection) : dbFunctions<QueuePaging>("queue_paging", connection, listOf("index", "Date_Time", "Source", "Type", "Message", "BroadcastZones")) {
|
|
override fun Create() {
|
|
val tabledefinition ="CREATE TABLE IF NOT EXISTS ${super.dbName} (" +
|
|
"`index` INT AUTO_INCREMENT PRIMARY KEY," +
|
|
"Date_Time VARCHAR(45) NOT NULL," + // Date and time of the entry
|
|
"Source VARCHAR(45) NOT NULL," + // Source of the entry
|
|
"Type VARCHAR(45) NOT NULL," + // Type of the entry
|
|
"Message VARCHAR(1024) NOT NULL," + // Message content
|
|
"BroadcastZones VARCHAR(1024)" + // Comma-separated soundbank tags
|
|
")"
|
|
super.Create(tabledefinition)
|
|
}
|
|
|
|
override fun Get(cbOK: Consumer<Unit>?, cbFail: Consumer<String>?) {
|
|
List.clear()
|
|
val queueList = ArrayList<QueuePaging>()
|
|
try {
|
|
val statement = connection.createStatement()
|
|
val resultSet = statement?.executeQuery("SELECT * FROM ${super.dbName}")
|
|
while (resultSet?.next() == true) {
|
|
val queuePaging = QueuePaging(
|
|
resultSet.getLong("index").toUInt(),
|
|
resultSet.getString("Date_Time"),
|
|
resultSet.getString("Source"),
|
|
resultSet.getString("Type"),
|
|
resultSet.getString("Message"),
|
|
resultSet.getString("BroadcastZones"),
|
|
)
|
|
queueList.add(queuePaging)
|
|
List.add(queuePaging)
|
|
}
|
|
cbOK?.accept(Unit)
|
|
} catch (e: Exception) {
|
|
Logger.error("Error fetching ${super.dbName} : ${e.message}" as Any)
|
|
cbFail?.accept("Error fetching ${super.dbName} : ${e.message}" )
|
|
}
|
|
}
|
|
|
|
override fun Add(data: QueuePaging): Boolean {
|
|
try {
|
|
val statement = connection.prepareStatement(
|
|
"INSERT INTO ${super.dbName} (Date_Time, Source, Type, Message, BroadcastZones) VALUES (?, ?, ?, ?, ?)"
|
|
)
|
|
statement?.setString(1, data.Date_Time)
|
|
statement?.setString(2, data.Source)
|
|
statement?.setString(3, data.Type)
|
|
statement?.setString(4, data.Message)
|
|
statement?.setString(5, data.BroadcastZones)
|
|
val rowsAffected = statement?.executeUpdate()
|
|
if (rowsAffected != null && rowsAffected > 0) {
|
|
Logger.info("QueuePaging added: ${data.Message}" as Any)
|
|
return true
|
|
} else {
|
|
Logger.warn("No QueuePaging entry added for: ${data.Message}" as Any)
|
|
}
|
|
} catch (e: Exception) {
|
|
Logger.error("Error adding QueuePaging entry: ${e.message}" as Any)
|
|
}
|
|
return false
|
|
}
|
|
|
|
override fun AddAll(data: ArrayList<QueuePaging>): Boolean {
|
|
return try {
|
|
connection.autoCommit = false
|
|
val sql =
|
|
"INSERT INTO ${super.dbName} (Date_Time, Source, Type, Message, BroadcastZones) VALUES (?, ?, ?, ?, ?)"
|
|
val statement = connection.prepareStatement(sql)
|
|
for (qp in data) {
|
|
statement.setString(1, qp.Date_Time)
|
|
statement.setString(2, qp.Source)
|
|
statement.setString(3, qp.Type)
|
|
statement.setString(4, qp.Message)
|
|
statement.setString(5, qp.BroadcastZones)
|
|
statement.addBatch()
|
|
}
|
|
statement.executeBatch()
|
|
connection.commit()
|
|
Logger.info("Bulk QueuePaging insert successful: ${data.size} entries" as Any)
|
|
connection.autoCommit = true
|
|
true
|
|
} catch (e: Exception) {
|
|
Logger.error("Error adding QueuePaging entries: ${e.message}" as Any)
|
|
false
|
|
}
|
|
}
|
|
|
|
override fun UpdateByIndex(index: Int, data: QueuePaging): Boolean {
|
|
throw Exception("Update not supported")
|
|
}
|
|
|
|
override fun Resort(): Boolean {
|
|
try {
|
|
val statement = connection.createStatement()
|
|
val tempdb_name = "temp_${super.dbName}"
|
|
// use a temporary table to reorder the index
|
|
statement?.executeUpdate("CREATE TABLE IF NOT EXISTS $tempdb_name LIKE ${super.dbName}")
|
|
statement?.executeUpdate("TRUNCATE TABLE $tempdb_name")
|
|
statement?.executeUpdate("INSERT INTO $tempdb_name (Date_Time, Source, Type, Message, BroadcastZones) SELECT Date_Time, Source, Type, Message, BroadcastZones FROM ${super.dbName} ")
|
|
statement?.executeUpdate("TRUNCATE TABLE ${super.dbName}")
|
|
statement?.executeUpdate("INSERT INTO ${super.dbName} (Date_Time, Source, Type, Message, BroadcastZones) SELECT Date_Time, Source, Type, Message, BroadcastZones FROM $tempdb_name")
|
|
statement?.executeUpdate("DROP TABLE $tempdb_name")
|
|
Logger.info("${super.dbName} table resorted by index" as Any)
|
|
// reload the local list
|
|
Get()
|
|
return true
|
|
} catch (e: Exception) {
|
|
Logger.error("Error resorting ${super.dbName} table by index: ${e.message}" as Any)
|
|
}
|
|
return false
|
|
}
|
|
|
|
override fun Import_XLSX(workbook: XSSFWorkbook): Boolean {
|
|
throw Exception("Importing QueuePaging from XLSX is not supported")
|
|
}
|
|
|
|
override fun Export_XLSX(): XSSFWorkbook? {
|
|
try {
|
|
val statement = connection.createStatement()
|
|
val resultSet = statement?.executeQuery("SELECT * FROM ${super.dbName}")
|
|
val workbook = XSSFWorkbook()
|
|
val sheet = workbook.createSheet("QueuePaging")
|
|
val headerRow = sheet.createRow(0)
|
|
val headers = arrayOf("Index", "Date_Time", "Source", "Type", "Message", "BroadcastZones")
|
|
for ((colIndex, header) in headers.withIndex()) {
|
|
val cell = headerRow.createCell(colIndex)
|
|
cell.setCellValue(header)
|
|
}
|
|
var rowIndex = 1
|
|
while (resultSet?.next() == true) {
|
|
val row = sheet.createRow(rowIndex++)
|
|
row.createCell(0).setCellValue(resultSet.getString("index"))
|
|
row.createCell(1).setCellValue(resultSet.getString("Date_Time"))
|
|
row.createCell(2).setCellValue(resultSet.getString("Source"))
|
|
row.createCell(3).setCellValue(resultSet.getString("Type"))
|
|
row.createCell(4).setCellValue(resultSet.getString("Message"))
|
|
row.createCell(5).setCellValue(resultSet.getString("BroadcastZones"))
|
|
}
|
|
for (i in headers.indices) {
|
|
sheet.autoSizeColumn(i)
|
|
}
|
|
return workbook
|
|
} catch (e: Exception) {
|
|
Logger.error { "Error exporting QueuePaging, Msg: ${e.message}" }
|
|
}
|
|
return null
|
|
}
|
|
} |