some updates

This commit is contained in:
gowthaman.b 2024-02-09 14:49:25 +05:30
parent fc94c7aacc
commit 338f86a5f5
2 changed files with 148 additions and 58 deletions

View File

@ -0,0 +1,74 @@
package com.restapi
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.restapi.controllers.QueryParam
import com.restapi.controllers.RawQuery
class FDeserializer : JsonDeserializer<F>() {
override fun deserialize(p: JsonParser, p1: DeserializationContext?): F {
//custom logic to do thia
val node = p.readValueAsTree<JsonNode>()
if (node.isObject) {
if (node.has("name")) {
return F.P(name = node.get("name").textValue())
} else if (node.has("num")) {
return F.Q(num = node.get("num").textValue())
} else if (node.has("tag")) {
return F.D(tag = node.get("tag").textValue())
}
} else {
//incorrect
}
throw IllegalArgumentException()
}
}
val om = jacksonObjectMapper()
@JsonDeserialize(using = FDeserializer::class)
sealed interface F {
data class P(val name: String) : F
data class Q(val num: String) : F
data class D(val tag: String) : F
}
val j = """
{"name":"a"}
""".trimIndent()
val j2 = """
{"num":"a"}
""".trimIndent()
val j3 = """
{"tag":"a"}
""".trimIndent()
val j4 = """
{
"sql":"aaaa",
"params": {
"a":"b",
"c": {
"type":"STRING",
"value":"aaaa"
}
}
}
""".trimIndent()
fun main() {
println(om.readValue<F>(j))
println(om.readValue<F>(j2))
println(om.readValue<F>(j3))
println(om.readValue<RawQuery>(j4))
}

View File

@ -1,66 +1,74 @@
package com.restapi.controllers
import com.restapi.domain.*
import com.restapi.domain.Quotation
import com.restapi.domain.Session.database
import java.time.LocalDate
//constants
const val IGNORE = "%"
val baseDate :LocalDate = LocalDate.of(1500, 1,1, )
val maxDate :LocalDate = LocalDate.of(3000, 1 ,1)
val baseDate: LocalDate = LocalDate.of(1500, 1, 1)
val maxDate: LocalDate = LocalDate.of(3000, 1, 1)
const val RATING_MAX = 10.0
const val RATING_MIN = 0.0
//common filters would be used by most of the handlers
//require a list of vendor ids to be passed
data class CommonFilters (
val fromDate :LocalDate = baseDate,
val toDate :LocalDate = maxDate,
val vendor :List<Long>? = null,
val sortAsc :Boolean = true,
val sortBy :String = IGNORE
data class CommonFilters(
val fromDate: LocalDate = baseDate,
val toDate: LocalDate = maxDate,
val vendor: List<Long>? = null,
val sortAsc: Boolean = true,
val sortBy: String = IGNORE
)
interface CustomFilters{}
data class POFilters (
val poNumLike :String = IGNORE,
val totalAmountExceeds :Long = Long.MIN_VALUE,
val totalAmountLessThan :Long = Long.MAX_VALUE,
interface CustomFilters {}
data class POFilters(
val poNumLike: String = IGNORE,
val totalAmountExceeds: Long = Long.MIN_VALUE,
val totalAmountLessThan: Long = Long.MAX_VALUE,
val validAfter: LocalDate = baseDate,
val validBefore: LocalDate = maxDate,
val refQuotation :String = IGNORE,
val refQuotation: String = IGNORE,
) : CustomFilters
data class ProductFilters (
val nameLike :String = IGNORE,
val hsnLike :String = IGNORE,
val uom :UOM = UOM.ALL,
data class ProductFilters(
val nameLike: String = IGNORE,
val hsnLike: String = IGNORE,
val uom: UOM = UOM.ALL,
) : CustomFilters
data class DocumentFilters (
val nameLike :String = IGNORE,
val typeOfDoc :DocType = DocType.ALL,
val docDateFrom :LocalDate = baseDate,
val docDataTo :LocalDate = maxDate,
) :CustomFilters
data class RFQFilters (
val validBefore :LocalDate = maxDate,
val validAfter :LocalDate = baseDate,
val reqForQuoteNumLike :String = IGNORE,
)
data class QuoteFilters (
val quoteNumLike :String = IGNORE,
val validBefore :LocalDate = maxDate,
val validAfter :LocalDate = baseDate,
val totalAmountExceeds :Long = Long.MIN_VALUE,
val totalAmountLessThan :Long = Long.MAX_VALUE,
) :CustomFilters
data class VendorFilters (
val nameLike :String = IGNORE,
val msmeLike :String = IGNORE,
val gstNumLike :String = IGNORE,
val addressLike :String = IGNORE,
val ratingExceeds :Double = RATING_MIN,
val ratingLessThan :Double = RATING_MAX,
) :CustomFilters
fun<T> applyVendorHelper(q :io.ebean.ExpressionList<T>, vids :List<Long>?) {
data class DocumentFilters(
val nameLike: String = IGNORE,
val typeOfDoc: DocType = DocType.ALL,
val docDateFrom: LocalDate = baseDate,
val docDataTo: LocalDate = maxDate,
) : CustomFilters
data class RFQFilters(
val validBefore: LocalDate = maxDate,
val validAfter: LocalDate = baseDate,
val reqForQuoteNumLike: String = IGNORE,
): CustomFilters
data class QuoteFilters(
val quoteNumLike: String = IGNORE,
val validBefore: LocalDate = maxDate,
val validAfter: LocalDate = baseDate,
val totalAmountExceeds: Long = Long.MIN_VALUE,
val totalAmountLessThan: Long = Long.MAX_VALUE,
) : CustomFilters
data class VendorFilters(
val nameLike: String = IGNORE,
val msmeLike: String = IGNORE,
val gstNumLike: String = IGNORE,
val addressLike: String = IGNORE,
val ratingExceeds: Double = RATING_MIN,
val ratingLessThan: Double = RATING_MAX,
) : CustomFilters
fun <T> applyVendorHelper(q: io.ebean.ExpressionList<T>, vids: List<Long>?) {
if (vids.isNullOrEmpty()) return
// q.apply {
// q.`in`("vendor", vids)
@ -69,25 +77,29 @@ fun<T> applyVendorHelper(q :io.ebean.ExpressionList<T>, vids :List<Long>?) {
// println(vids[0])
q.eq("vendor_sys_pk", vids[0])
}
fun<T> applySortHelper(q :io.ebean.ExpressionList<T>, sortBy :String, asc :Boolean) {
if(sortBy == IGNORE) return;
fun <T> applySortHelper(q: io.ebean.ExpressionList<T>, sortBy: String, asc: Boolean) {
if (sortBy == IGNORE) return;
val order = if (asc) "ASC" else "DESC"
q.orderBy("$sortBy $order")
}
fun<T> applyFromToHelper(q :io.ebean.ExpressionList<T>, fromDate: LocalDate, toDate: LocalDate, colName :String) {
fun <T> applyFromToHelper(q: io.ebean.ExpressionList<T>, fromDate: LocalDate, toDate: LocalDate, colName: String) {
q.ge(colName, fromDate)
.le(colName, toDate)
}
fun<T> applyCommonFilters(q :io.ebean.ExpressionList<T>, commonFilters: CommonFilters) {
fun <T> applyCommonFilters(q: io.ebean.ExpressionList<T>, commonFilters: CommonFilters) {
applyVendorHelper<T>(q, commonFilters.vendor)
applySortHelper<T>(q, commonFilters.sortBy, commonFilters.sortAsc)
}
fun searchQuotes(commonFilters: CommonFilters, quoteFilters: QuoteFilters) : List<Quotation> {
fun searchQuotes(commonFilters: CommonFilters, quoteFilters: QuoteFilters): List<Quotation> {
val q = database.find(Quotation::class.java)
.where()
.ge("quoteDate", commonFilters.fromDate)
.le("quoteDate", commonFilters.toDate)
.ge("validTill",quoteFilters.validAfter)
.ge("validTill", quoteFilters.validAfter)
.le("validTill", quoteFilters.validBefore)
.ge("totalAmount", quoteFilters.totalAmountExceeds)
.le("totalAmount", quoteFilters.totalAmountLessThan)
@ -97,7 +109,8 @@ fun searchQuotes(commonFilters: CommonFilters, quoteFilters: QuoteFilters) : Lis
applySortHelper(q, commonFilters.sortBy, commonFilters.sortAsc)
return q.findList()
}
fun searchVendors(commonFilters: CommonFilters, vendorFilters: VendorFilters) : List<Vendor> {
fun searchVendors(commonFilters: CommonFilters, vendorFilters: VendorFilters): List<Vendor> {
val q = database.find(Vendor::class.java)
.where()
.ge("rating", vendorFilters.ratingExceeds)
@ -109,11 +122,12 @@ fun searchVendors(commonFilters: CommonFilters, vendorFilters: VendorFilters) :
applySortHelper(q, commonFilters.sortBy, commonFilters.sortAsc)
return q.findList()
}
fun searchDocs(commonFilters: CommonFilters, documentFilters: DocumentFilters) : List<Document> {
fun searchDocs(commonFilters: CommonFilters, documentFilters: DocumentFilters): List<Document> {
val q = database.find(Document::class.java)
.where()
.apply {
if(documentFilters.typeOfDoc != DocType.ALL){
if (documentFilters.typeOfDoc != DocType.ALL) {
this.eq("docType", documentFilters.typeOfDoc)
}
}
@ -122,7 +136,8 @@ fun searchDocs(commonFilters: CommonFilters, documentFilters: DocumentFilters) :
applyVendorHelper(q, commonFilters.vendor)
return q.findList()
}
fun searchPos(commonFilters: CommonFilters, poFilters: POFilters?) : List<PurchaseOrder> {
fun searchPos(commonFilters: CommonFilters, poFilters: POFilters?): List<PurchaseOrder> {
val poFilters = poFilters ?: POFilters()
val q = database.find(PurchaseOrder::class.java)
.where()
@ -137,7 +152,8 @@ fun searchPos(commonFilters: CommonFilters, poFilters: POFilters?) : List<Purcha
applySortHelper(q, commonFilters.sortBy, commonFilters.sortAsc)
return q.findList()
}
fun searchRFQ(commonFilters: CommonFilters, rfqFilters: RFQFilters) : List<ReqForQuote> {
fun searchRFQ(commonFilters: CommonFilters, rfqFilters: RFQFilters): List<ReqForQuote> {
val q = database.find(ReqForQuote::class.java)
.where()
.ge("validTill", rfqFilters.validAfter)