Add Transformation model
This commit is contained in:
38
src/main/java/com/rak/model/transform/TestRegistry.java
Normal file
38
src/main/java/com/rak/model/transform/TestRegistry.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package com.rak.model.transform;
|
||||
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ApplicationScoped
|
||||
public class TestRegistry {
|
||||
private final Map<String, Transformation> registry = new ConcurrentHashMap<>();
|
||||
|
||||
public TestRegistry() {
|
||||
// Register built-in transformations
|
||||
register("trim", string -> string.trim());
|
||||
register("upper", String::toUpperCase);
|
||||
register("parseInt", s -> Integer.parseInt((String) s));
|
||||
register("parseFloat", s -> Float.parseFloat((String) s));
|
||||
register("parseDate", s -> LocalDate.parse((String) s));
|
||||
register("extract", this::extract);
|
||||
}
|
||||
|
||||
public void register(String name, Transformation transformation) {
|
||||
registry.put(name, transformation);
|
||||
}
|
||||
|
||||
public Transformation get(String name) {
|
||||
return registry.get(name);
|
||||
}
|
||||
|
||||
private Object extract(Object input, String pattern) {
|
||||
Pattern regex = Pattern.compile(pattern);
|
||||
Matcher matcher = regex.matcher((String) input);
|
||||
return matcher.find() ? matcher.group(1) : input;
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,7 @@ class ExampleResource(
|
||||
provider: String,
|
||||
@RestQuery
|
||||
setName: String
|
||||
): Map<String, String> {
|
||||
): List<Map<String, String>> {
|
||||
return scrapeService.extractSet(
|
||||
provider,
|
||||
setName
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.rak.model.transform
|
||||
|
||||
interface AbstractTransformation
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.rak.model.transform
|
||||
|
||||
@FunctionalInterface
|
||||
fun interface ParameterizedTransformation : AbstractTransformation {
|
||||
fun apply(input: Any, vararg parameters: Any): Any?
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.rak.model.transform
|
||||
|
||||
@FunctionalInterface
|
||||
fun interface Transformation : AbstractTransformation {
|
||||
fun apply(input: Any): Any?
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.rak.model.transform
|
||||
|
||||
import jakarta.enterprise.context.ApplicationScoped
|
||||
|
||||
@ApplicationScoped
|
||||
class TransformationRegistry {
|
||||
|
||||
private val transformations = hashMapOf<String, (input: Any) -> Any>()
|
||||
|
||||
init {
|
||||
register<String>("trim") {
|
||||
(it as String).trim()
|
||||
}
|
||||
register<String>("replace", { s: Any ->
|
||||
Integer.
|
||||
})
|
||||
}
|
||||
|
||||
// fun <T : Any> register(name: String, transformation: (input: Any) -> T) {
|
||||
// transformations[name] = transformation
|
||||
// }
|
||||
|
||||
fun <T : Any> register(name: String, transformation: Transformation) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -46,7 +46,7 @@ class ScrapeService(
|
||||
fun extractSet(
|
||||
provider: String,
|
||||
setName: String,
|
||||
): Map<String, String> {
|
||||
): List<Map<String, String>> {
|
||||
val source =
|
||||
sourceService.getSourceById(provider) ?: throw IllegalArgumentException("Provider $provider not found")
|
||||
|
||||
@@ -55,26 +55,28 @@ class ScrapeService(
|
||||
val document: Document = Jsoup.connect("https://${source.getDomain()}/$path").get()
|
||||
val regionalSetSelector = source.getItems().regionalSet().get()
|
||||
|
||||
val regionalSetRoot = document.selectFirst(regionalSetSelector.rootSelector().value())!!
|
||||
val regionalSetRoot = document.select(regionalSetSelector.rootSelector().value())
|
||||
|
||||
val setId: String? = extractTextFromRootBySteps(
|
||||
regionalSetRoot,
|
||||
regionalSetSelector.idSelector().steps()
|
||||
)
|
||||
val setLanguage: String? = extractTextFromRootBySteps(
|
||||
regionalSetRoot,
|
||||
regionalSetSelector.languageSelector().steps()
|
||||
)
|
||||
val setKey: String? = extractTextFromRootBySteps(
|
||||
regionalSetRoot,
|
||||
regionalSetSelector.regionKeySelector().steps()
|
||||
)
|
||||
return regionalSetRoot.map {
|
||||
val setId: String? = extractTextFromRootBySteps(
|
||||
it,
|
||||
regionalSetSelector.idSelector().steps()
|
||||
)
|
||||
val setLanguage: String? = extractTextFromRootBySteps(
|
||||
it,
|
||||
regionalSetSelector.languageSelector().steps()
|
||||
)
|
||||
val setKey: String? = extractTextFromRootBySteps(
|
||||
it,
|
||||
regionalSetSelector.regionKeySelector().steps()
|
||||
)
|
||||
|
||||
return mapOf(
|
||||
Pair("id", setId ?: "N/A"),
|
||||
Pair("language", setLanguage ?: "N/A"),
|
||||
Pair("key", setKey ?: "N/A"),
|
||||
)
|
||||
mapOf(
|
||||
Pair("id", setId ?: "N/A"),
|
||||
Pair("language", setLanguage ?: "N/A"),
|
||||
Pair("key", setKey ?: "N/A"),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user