Skip to content

Commit

Permalink
Fix source formattation and klint integration
Browse files Browse the repository at this point in the history
  • Loading branch information
foresti-smeup committed Nov 29, 2023
1 parent ee27f33 commit 6c8b74a
Show file tree
Hide file tree
Showing 34 changed files with 1,032 additions and 950 deletions.
91 changes: 48 additions & 43 deletions jt400/src/main/kotlin/com/smeup/dbnative/jt400/JT400DBFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ import com.smeup.dbnative.model.FileMetadata
import java.math.BigDecimal

private enum class CursorAction {
NONE, SETLL, SETGT
NONE,
SETLL,
SETGT,
}

class JT400DBFile(override var name: String,
override var fileMetadata: FileMetadata,
var file: KeyedFile,
override var logger: Logger? = null) : DBFile {

class JT400DBFile(
override var name: String,
override var fileMetadata: FileMetadata,
var file: KeyedFile,
override var logger: Logger? = null,
) : DBFile {
private var equalFlag: Boolean = false
private var eofReached: Boolean = false
private var previousAction: CursorAction = CursorAction.NONE
Expand Down Expand Up @@ -96,9 +99,9 @@ class JT400DBFile(override var name: String,
} catch (e: AS400Exception) {
}
try {
//file.positionCursorBefore(keys2Array(keys))
// file.positionCursorBefore(keys2Array(keys))
file.positionCursor(keys2Array(keys), KeyedFile.KEY_LT)
//file.positionCursor(keys2Array(keys), KeyedFile.KEY_LT)
// file.positionCursor(keys2Array(keys), KeyedFile.KEY_LT)
return true
} catch (e: AS400Exception) {
handleAS400Error(e)
Expand Down Expand Up @@ -142,14 +145,14 @@ class JT400DBFile(override var name: String,
override fun chain(keys: List<String>): Result {
this.previousAction = CursorAction.NONE
resetStatus()
//TODO("Attenzione alla gestione del lock")
// TODO("Attenzione alla gestione del lock")
try {
/*
file.positionCursor(keys2Array(keys), KeyedFile.KEY_EQ)
var r : Result? = Result(as400RecordToSmeUPRecord(file.read()))
//file.positionCursorToNext();
return r ?: fail("Read failed");
*/
*/
return Result(as400RecordToSmeUPRecord(file.read(keys2Array(keys))))
} catch (e: AS400Exception) {
handleAS400Error(e)
Expand All @@ -161,13 +164,13 @@ class JT400DBFile(override var name: String,
* The READ operation reads the record, currently pointed to, from a full procedural file.
*/
override fun read(): Result {
//https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzasd/zzread.htm
var r : Result
// https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzasd/zzread.htm
var r: Result
try {
if (this.previousAction==CursorAction.SETLL) {
if (this.previousAction == CursorAction.SETLL) {
file.positionCursorToNext()
}
r = Result(as400RecordToSmeUPRecord(file.read()))
r = Result(as400RecordToSmeUPRecord(file.read()))
} catch (e: AS400Exception) {
handleAS400Error(e)
r = Result(Record())
Expand All @@ -186,12 +189,12 @@ class JT400DBFile(override var name: String,
*/
override fun readPrevious(): Result {
resetStatus()
var r : Result
var r: Result
try {
if (this.previousAction!=CursorAction.SETLL) {
if (this.previousAction != CursorAction.SETLL) {
file.positionCursorToPrevious()
}
r = Result(as400RecordToSmeUPRecord(file.read()))
r = Result(as400RecordToSmeUPRecord(file.read()))
} catch (e: AS400Exception) {
handleAS400Error(e)
r = Result(Record())
Expand Down Expand Up @@ -223,10 +226,10 @@ class JT400DBFile(override var name: String,

override fun readEqual(keys: List<String>): Result {
resetStatus()
//https://code400.com/forum/forum/iseries-programming-languages/java/8386-noobie-question
// https://code400.com/forum/forum/iseries-programming-languages/java/8386-noobie-question
return try {
//val r = if (this.previousAction==CursorAction.SETGT) file.read(keys2Array(keys)) else file.readNextEqual(keys2Array(keys))
if (this.previousAction==CursorAction.SETGT) {
// val r = if (this.previousAction==CursorAction.SETGT) file.read(keys2Array(keys)) else file.readNextEqual(keys2Array(keys))
if (this.previousAction == CursorAction.SETGT) {
file.positionCursorToPrevious()
}
val r = file.readNextEqual(keys2Array(keys))
Expand Down Expand Up @@ -274,7 +277,7 @@ class JT400DBFile(override var name: String,
override fun readPreviousEqual(keys: List<String>): Result {
resetStatus()
return try {
if (this.previousAction==CursorAction.SETLL) {
if (this.previousAction == CursorAction.SETLL) {
file.positionCursorToNext()
}
val r = file.readPreviousEqual(keys2Array(keys))
Expand Down Expand Up @@ -324,8 +327,8 @@ class JT400DBFile(override var name: String,
* After a conversion mapping message on a read operation, the file is positioned to the record containing the data that caused the message.
*/
private fun handleAS400Error(e: AS400Exception) {
//CPF5001 End of file reached
//CPF5006 Record not found
// CPF5001 End of file reached
// CPF5006 Record not found
val eid = as400ErrorID(e).toUpperCase()
if (eid.startsWith("CPF5001")) {
this.eofReached = true
Expand All @@ -335,20 +338,22 @@ class JT400DBFile(override var name: String,
}
throw RuntimeException()
}
private fun as400ErrorID(e: AS400Exception) : String {
//CPF5001 End of file reached
//CPF5006 Record not found
if (e.aS400Message != null
&& e.aS400Message.id != null) {

private fun as400ErrorID(e: AS400Exception): String {
// CPF5001 End of file reached
// CPF5006 Record not found
if (e.aS400Message != null &&
e.aS400Message.id != null
) {
return e.aS400Message.id
}
return ""
}

private fun keys2Array(keys: List<String>): Array<Any> {
//return keys.map { it.value }.toTypedArray()
// return keys.map { it.value }.toTypedArray()
val keysValues = mutableListOf<Any>()
//for (key in fileMetadata.fileKeys) {
// for (key in fileMetadata.fileKeys) {
for (i in keys.indices) {
val keyName = fileMetadata.fileKeys[i]
val keyValue = keys.get(i)
Expand All @@ -373,7 +378,7 @@ class JT400DBFile(override var name: String,

private fun as400RecordToSmeUPRecord(r: com.ibm.as400.access.Record?): Record {
// TODO create a unit test for the isAfterLast condition
if (r == null) { //TODO || this.isAfterLast
if (r == null) { // TODO || this.isAfterLast
return Record()
}
val result = Record()
Expand All @@ -385,31 +390,31 @@ class JT400DBFile(override var name: String,
return result
}

//fun com.ibm.as400.access.Record?.currentRecordToValues(): Record {
// fun com.ibm.as400.access.Record?.currentRecordToValues(): Record {
private fun smeUPRecordToAS400Record(r: Record?): com.ibm.as400.access.Record? {
if (r == null) {
return null //com.ibm.as400.access.Record()
return null // com.ibm.as400.access.Record()
}
val result = com.ibm.as400.access.Record()
result.recordFormat = file.recordFormat
for ((name, value) in r) {
if (numericField(name)) {
result.setField(name, BigDecimal(value))
} else {
//try {
result.setField(name, value.trimEnd())
//} catch (ex : Exception) {
// try {
result.setField(name, value.trimEnd())
// } catch (ex : Exception) {
// println(ex.message)
//}
// }
}
}
return result
}

private fun numericField(name : String) : Boolean {
private fun numericField(name: String): Boolean {
val dataType = file.recordFormat.getFieldDescription(name).dataType.instanceType
//val field : Field? = this.fileMetadata.getField(name)
//val type : FieldType? = field?.type
// val field : Field? = this.fileMetadata.getField(name)
// val type : FieldType? = field?.type
return when (dataType) {
AS400DataType.TYPE_ZONED,
AS400DataType.TYPE_PACKED,
Expand All @@ -423,11 +428,11 @@ class JT400DBFile(override var name: String,
AS400DataType.TYPE_UBIN4,
AS400DataType.TYPE_UBIN8,
AS400DataType.TYPE_FLOAT4,
AS400DataType.TYPE_FLOAT8 ->
AS400DataType.TYPE_FLOAT8,
->
true
else ->
false
}
}

}
}
23 changes: 10 additions & 13 deletions jt400/src/main/kotlin/com/smeup/dbnative/jt400/JT400DBMManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,28 @@ import com.smeup.dbnative.ConnectionConfig
import com.smeup.dbnative.DBManagerBaseImpl
import com.smeup.dbnative.file.DBFile

open class JT400DBMManager(final override val connectionConfig: ConnectionConfig) : DBManagerBaseImpl() {

open class JT400DBMManager(final override val connectionConfig: ConnectionConfig) : DBManagerBaseImpl() {
private var openedFile = mutableMapOf<String, JT400DBFile>()

// as400://SRVLAB01.SMEUP.COM/W_SCAARM
private val match = Regex("as400://((?:\\w|\\.)+)/(\\w+)").find(connectionConfig.url)
private val host : String by lazy {
private val host: String by lazy {
match!!.destructured.component1()
}

private val library : String by lazy {
private val library: String by lazy {
match!!.destructured.component2()
}

private val connection : AS400 by lazy {
private val connection: AS400 by lazy {
val as400 = AS400(host, connectionConfig.user, connectionConfig.password)
as400.isGuiAvailable = false
//as400.addConnectionListener
// as400.addConnectionListener
as400.connectService(AS400.RECORDACCESS)
as400
}

override fun openFile(name: String) : DBFile {
override fun openFile(name: String): DBFile {
require(existFile(name)) {
"Cannot open unregistered file $name"
}
Expand All @@ -58,16 +57,15 @@ open class JT400DBMManager(final override val connectionConfig: ConnectionConfig
//
val fileName = QSYSObjectPathName(library, name, "*FILE", "MBR")
val path = fileName.path
//println("Path: $path")
// println("Path: $path")
val file = KeyedFile(connection, path)
//val rf = AS400FileRecordDescription(system, path).retrieveRecordFormat()
//file.recordFormat = rf[0]
// val rf = AS400FileRecordDescription(system, path).retrieveRecordFormat()
// file.recordFormat = rf[0]
file.setRecordFormat() // Loads the record format directly from the server.
file.open(AS400File.READ_WRITE, 0, AS400File.COMMIT_LOCK_LEVEL_NONE)
val jt400File = JT400DBFile(name, metadataOf(name), file, logger)
openedFile.putIfAbsent(name, jt400File)
return jt400File

}

override fun closeFile(name: String) {
Expand All @@ -84,5 +82,4 @@ open class JT400DBMManager(final override val connectionConfig: ConnectionConfig
override fun close() {
connection.disconnectAllServices()
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import kotlin.test.assertEquals
import kotlin.test.assertTrue

class JT400Chain1KeyTest {

private lateinit var dbManager: JT400DBMManager

@Before
Expand Down Expand Up @@ -60,6 +59,4 @@ class JT400Chain1KeyTest {
assertTrue(dbFile.chain("XYZ").record.isEmpty())
dbManager.closeFile(TSTTAB_TABLE_NAME)
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import kotlin.test.assertEquals
import kotlin.test.assertTrue

class JT400Chain2KeysTest {

private lateinit var dbManager: JT400DBMManager

@Before
Expand All @@ -48,10 +47,11 @@ class JT400Chain2KeysTest {
@Test
fun findRecordsIfChainWithExistingKey() {
val dbFile = dbManager.openFile(TST2TAB_TABLE_NAME)
val key2 = listOf(
"ABC",
"12.00"
)
val key2 =
listOf(
"ABC",
"12.00",
)
val chainResult = dbFile.chain(key2)
assertEquals("ABC", chainResult.record["TSTFLDCHR"])
assertEquals("12.00", chainResult.record["TSTFLDNBR"])
Expand All @@ -62,14 +62,12 @@ class JT400Chain2KeysTest {
@Test
fun doesNotFindRecordsIfChainWithNotExistingKey() {
val dbFile = dbManager.openFile(TST2TAB_TABLE_NAME)
val key2 = listOf(
"ZZZ",
"12"
)
val key2 =
listOf(
"ZZZ",
"12",
)
assertTrue(dbFile.chain(key2).record.isEmpty())
dbManager.closeFile(TST2TAB_TABLE_NAME)
}


}

Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue


class JT400MunicipalityTest {

private lateinit var dbManager: JT400DBMManager

@Before
Expand Down Expand Up @@ -80,11 +78,11 @@ class JT400MunicipalityTest {
assertTrue(dbFile.setll(buildMunicipalityKey("IT", "LOM", "BS", "ERBUSCO")))
assertEquals(
"EDOLO",
getMunicipalityName(dbFile.readPreviousEqual(buildMunicipalityKey("IT", "LOM", "BS")).record)
getMunicipalityName(dbFile.readPreviousEqual(buildMunicipalityKey("IT", "LOM", "BS")).record),
)
assertEquals(
"DESENZANO DEL GARDA",
getMunicipalityName(dbFile.readPreviousEqual(buildMunicipalityKey("IT", "LOM", "BS")).record)
getMunicipalityName(dbFile.readPreviousEqual(buildMunicipalityKey("IT", "LOM", "BS")).record),
)
dbManager.closeFile(MUNICIPALITY_TABLE_NAME)
}
Expand Down Expand Up @@ -404,7 +402,7 @@ class JT400MunicipalityTest {
fun t15_eof() {
val dbFile = dbManager.openFile(MUNICIPALITY_TABLE_NAME)
val key3A = buildMunicipalityKey("IT", "BAS", "MT")
//val key3A = buildMunicipalityKey("IT", "LOM", "PV", "PALESTRO")
// val key3A = buildMunicipalityKey("IT", "LOM", "PV", "PALESTRO")
assertTrue(dbFile.setll(key3A))
var count = 0
while (!dbFile.eof()) {
Expand All @@ -414,7 +412,4 @@ class JT400MunicipalityTest {
assertEquals(32, count)
dbManager.closeFile(MUNICIPALITY_TABLE_NAME)
}


}

Loading

0 comments on commit 6c8b74a

Please sign in to comment.