|
|
@@ -11,6 +11,7 @@ import org.springframework.transaction.annotation.Transactional |
|
|
|
import java.io.BufferedReader |
|
|
|
import java.io.IOException |
|
|
|
import java.io.InputStreamReader |
|
|
|
import java.nio.charset.StandardCharsets |
|
|
|
import java.sql.DriverManager.println |
|
|
|
import java.time.LocalDateTime |
|
|
|
import java.time.format.DateTimeFormatter |
|
|
@@ -34,9 +35,8 @@ open class UomConversionService( |
|
|
|
|
|
|
|
// Read the JSON file into a String |
|
|
|
val jsonStringBuilder = StringBuilder() |
|
|
|
BufferedReader(InputStreamReader(templateInputStream)).use { reader -> |
|
|
|
var line: String? |
|
|
|
while (reader.readLine().also { line = it } != null) { |
|
|
|
InputStreamReader(templateInputStream, StandardCharsets.UTF_8).use { reader -> |
|
|
|
BufferedReader(reader).forEachLine { line -> |
|
|
|
jsonStringBuilder.append(line) |
|
|
|
} |
|
|
|
} |
|
|
@@ -45,6 +45,7 @@ open class UomConversionService( |
|
|
|
val bomObjects = bomData.values |
|
|
|
val output = ArrayList<UomConversion>() |
|
|
|
for (i in bomObjects.indices) { |
|
|
|
//kotlin.io.println("${i + 1}. ${bomObjects[i]}"); |
|
|
|
val transformedItem = transformItem(bomObjects[i]) |
|
|
|
output.add(transformedItem) |
|
|
|
} |
|
|
@@ -57,28 +58,24 @@ open class UomConversionService( |
|
|
|
return LocalDateTime.parse(dateString, formatter) |
|
|
|
} |
|
|
|
|
|
|
|
//temp object for storing data from Gson conversion |
|
|
|
class BomData { |
|
|
|
var stSearch: String? = null |
|
|
|
var size = 0 |
|
|
|
lateinit var values: Array<BomObject> |
|
|
|
} |
|
|
|
|
|
|
|
//json structure for m18 data which we would like to save in MTMS |
|
|
|
class BomObject { |
|
|
|
var code: String? = null |
|
|
|
var udfudesc: String? = null |
|
|
|
var lastModifyDate: String? = null |
|
|
|
var id: Long? = null |
|
|
|
override fun toString(): String { |
|
|
|
return "YourObject{" + |
|
|
|
"code='" + code + '\'' + |
|
|
|
", udfudesc='" + udfudesc + '\'' + |
|
|
|
", lastModifyDate=" + lastModifyDate + '\'' + |
|
|
|
", id=" + id + |
|
|
|
'}' |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
open fun transformItem(item: BomObject): UomConversion { |
|
|
|
//Convert m18 bom reocrd to MTMS db structure record |
|
|
|
val code = if (item.code == null) "N/A" else item.code |
|
|
|
val udfudesc = item.udfudesc |
|
|
|
val id = item.id |
|
|
@@ -87,16 +84,17 @@ open class UomConversionService( |
|
|
|
var unit1Qty = 1.0 |
|
|
|
var unit2Qty = 0.0 |
|
|
|
var unit3Qty = 0.0 |
|
|
|
var unit4Qty = 0.0 |
|
|
|
var unit1: String? = null |
|
|
|
var unit2: String? = null |
|
|
|
var unit3: String? = null |
|
|
|
var unit4: String? = null |
|
|
|
val sizeInGram = 0.0 |
|
|
|
val gramPerSmallestUnit = 0.0 |
|
|
|
|
|
|
|
// Regex to extract numbers (including decimals) and units |
|
|
|
// Regex to extract numbers (including decimals) and units |
|
|
|
val pattern = |
|
|
|
Pattern.compile("(?:(\\d*\\.?\\d+)?([A-Za-z]+))?(?:(\\d*\\.?\\d+)([A-Za-z]+))?(?:(\\d*\\.?\\d+)([A-Za-z]+))?") |
|
|
|
Pattern.compile("(?:(\\d*\\.?\\d+)?([A-Za-z]+))?(?:(\\d*\\.?\\d+)([A-Za-z]+))?(?:(\\d*\\.?\\d+)([A-Za-z]+))?(?:(\\d*\\.?\\d+)([A-Za-z]+))?") |
|
|
|
val matcher = pattern.matcher(code) |
|
|
|
if (matcher.find()) { |
|
|
|
// Capture groups |
|
|
@@ -109,6 +107,9 @@ open class UomConversionService( |
|
|
|
val group5 = matcher.group(5) |
|
|
|
unit3Qty = if (group5 == null || group5.isEmpty()) 0.0 else group5.toDouble() |
|
|
|
unit3 = if (matcher.group(6) != null) matcher.group(6) else "" |
|
|
|
val group7 = matcher.group(7) |
|
|
|
unit4Qty = if (group7 == null || group7.isEmpty()) 0.0 else group7.toDouble() |
|
|
|
unit4 = if (matcher.group(8) != null) matcher.group(8) else "" |
|
|
|
} |
|
|
|
|
|
|
|
// Create the transformed JSON object |
|
|
@@ -121,6 +122,8 @@ open class UomConversionService( |
|
|
|
transformedItem.unit2Qty = unit2Qty |
|
|
|
transformedItem.unit3 = unit3 |
|
|
|
transformedItem.unit3Qty = unit3Qty |
|
|
|
transformedItem.unit4 = unit4 |
|
|
|
transformedItem.unit4Qty = unit4Qty |
|
|
|
transformedItem.sizeInGram = sizeInGram |
|
|
|
transformedItem.gramPerSmallestUnit = gramPerSmallestUnit |
|
|
|
if (id != null) { |
|
|
@@ -134,8 +137,11 @@ open class UomConversionService( |
|
|
|
// Define the conversion factors |
|
|
|
val units: MutableMap<String, Double> = HashMap() |
|
|
|
units.put("KG", 1000.0) |
|
|
|
units.put("L", 1000.0) //should be dynamic |
|
|
|
units.put("ML", 1.0) //should be dynamic |
|
|
|
units.put("GAL", 3785.4117)//should be dynamic |
|
|
|
units.put("G", 1.0) |
|
|
|
units.put("LG", 453.59) |
|
|
|
units.put("LB", 453.59) |
|
|
|
units.put("POUNDS", 453.59) |
|
|
|
units.put("CATTY", 604.78982) |
|
|
|
units.put("OZ", 28.3495) |
|
|
@@ -165,9 +171,19 @@ open class UomConversionService( |
|
|
|
smallestUnitGram = item.unit3Qty!! * units[item.unit3!!.uppercase(Locale.getDefault())]!! |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Calculate based on unit4 |
|
|
|
if (item.unit4 != null && item.unit4Qty != null) { |
|
|
|
if (units.containsKey(item.unit4!!.uppercase(Locale.getDefault()))) { |
|
|
|
totalGram += item.unit1Qty!! * item.unit2Qty!! * item.unit3Qty!! * item.unit4Qty!! * units[item.unit4!!.uppercase(Locale.getDefault())]!! |
|
|
|
smallestUnitGram = item.unit4Qty!! * units[item.unit4!!.uppercase(Locale.getDefault())]!! |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (units.containsKey(item.unit1!!.uppercase(Locale.getDefault())) || |
|
|
|
units.containsKey(item.unit2!!.uppercase(Locale.getDefault())) || |
|
|
|
units.containsKey(item.unit3!!.uppercase(Locale.getDefault())) |
|
|
|
units.containsKey(item.unit3!!.uppercase(Locale.getDefault())) || |
|
|
|
units.containsKey(item.unit4!!.uppercase(Locale.getDefault())) |
|
|
|
) { |
|
|
|
// If any valid conversion was made, set sizeInGram and gramPerSmallestUnit |
|
|
|
item.sizeInGram = totalGram |
|
|
@@ -221,6 +237,8 @@ open class UomConversionService( |
|
|
|
unit2Qty = newUomConversion.unit2Qty |
|
|
|
unit3 = newUomConversion.unit3 |
|
|
|
unit3Qty = newUomConversion.unit3Qty |
|
|
|
unit4 = newUomConversion.unit4 |
|
|
|
unit4Qty = newUomConversion.unit4Qty |
|
|
|
sizeInGram = newUomConversion.sizeInGram |
|
|
|
gramPerSmallestUnit = newUomConversion.gramPerSmallestUnit |
|
|
|
lastModifyDate = newUomConversion.lastModifyDate |
|
|
|