summaryrefslogtreecommitdiff
path: root/module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java
diff options
context:
space:
mode:
Diffstat (limited to 'module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java')
-rw-r--r--module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java49
1 files changed, 33 insertions, 16 deletions
diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java b/module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java
index b402173..b8ee03a 100644
--- a/module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java
+++ b/module/ri-engine/src/main/java/io/trygvis/rules/engine/DbIo.java
@@ -1,12 +1,18 @@
package io.trygvis.rules.engine;
import ch.qos.logback.core.util.FileUtil;
+import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.PropertyName;
import com.fasterxml.jackson.databind.SerializationFeature;
+import com.fasterxml.jackson.databind.introspect.Annotated;
+import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
+import com.fasterxml.jackson.databind.introspect.ObjectIdInfo;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import org.drools.core.common.DefaultFactHandle;
+import org.drools.core.factmodel.GeneratedFact;
import org.kie.api.KieBase;
import org.kie.api.runtime.rule.FactHandle;
@@ -21,6 +27,8 @@ import java.util.function.Function;
public class DbIo {
private final ObjectMapper mapper;
+ private static final List<String> prioritizedKeys = List.of("key", "name", "fqdn");
+
public DbIo(KieBase kieBase) {
var factory = new YAMLFactory();
factory.enable(YAMLGenerator.Feature.USE_NATIVE_TYPE_ID);
@@ -31,6 +39,29 @@ public class DbIo {
.withClassLoader(new AcmeClassLoader(kieBase));
mapper.setTypeFactory(typeFactory);
mapper.findAndRegisterModules();
+
+ mapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
+ @Override
+ public ObjectIdInfo findObjectIdInfo(Annotated a) {
+ final Class<?> klass = a.getRawType();
+ if (GeneratedFact.class.isAssignableFrom(klass)) {
+ System.out.println("klass = " + klass);
+
+ for (String name : prioritizedKeys) {
+ try {
+ final String getter = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
+ var f = klass.getMethod(getter);
+ return new ObjectIdInfo(PropertyName.construct(name), null, ObjectIdGenerators.PropertyGenerator.class, null);
+ } catch (NoSuchMethodException ignore) {
+ }
+ }
+ System.out.println("a.getRawType() = " + klass);
+ return new ObjectIdInfo(null, null, ObjectIdGenerators.IntSequenceGenerator.class, null);
+ }
+
+ return super.findObjectIdInfo(a);
+ }
+ });
}
public List<Object> load(String file) throws IOException {
@@ -86,8 +117,6 @@ public class DbIo {
// TODO: check if klass is a Comparable directly.
- var prioritizedKeys = List.of("key", "name", "fqdn");
-
var discoveredFieldsP1 = new LinkedHashMap<String, Function<Object, Object>>();
var discoveredFieldsP2 = new LinkedHashMap<String, Function<Object, Object>>();
@@ -228,18 +257,6 @@ public class DbIo {
}
}
- /*
- var x = new ArrayList<DbObject2>();
- x.add(new DbObject2("io.trygvis.rules.dba.Container", null));
- x.add(new DbObject2("io.trygvis.rules.machine.Machine", null));
- x.add(new DbObject2("io.trygvis.acme.apps.AcmeMyApp", null));
-
- System.out.println("xxxxxx");
- x.sort(new DbObjectComparator());
- x.forEach(System.out::println);
- System.out.println("xxxxxx");
- */
-
objects.sort(new DbObjectComparator());
var factory = mapper.getFactory();
@@ -282,12 +299,12 @@ public class DbIo {
private static class DbObjectComparator implements Comparator<DbObject2> {
private final List<String> prioritizedPackages = List.of(
- "io.trygvis.rules.core",
"io.trygvis.rules.machine",
"io.trygvis.rules.network",
"io.trygvis.rules.dns",
"io.trygvis.rules.dba",
- "io.trygvis.rules");
+ "io.trygvis.rules",
+ "io.trygvis.rules.core");
@Override
public int compare(DbObject2 a, DbObject2 b) {