From 6eb9a448efd42cf91a139b9c1bd3b52783ba985b Mon Sep 17 00:00:00 2001 From: Marc Gueury Date: Fri, 8 Mar 2024 13:54:56 +0100 Subject: [PATCH 1/3] v2.0 --- .../microprofile-config.properties.j2.tmpl | 15 +++++++++ .../java/me/opc/mp/database/Message.java.old | 31 ------------------- .../main/java/me/opc/mp/database/Dept.java | 15 +++++++++ .../me/opc/mp/database/DeptRepository.java | 9 ++++++ .../me/opc/mp/database/DeptResource.j2.java | 31 +++++++++++++++++++ .../opc/mp/database/FieldAccessStrategy.java | 18 +++++++++++ .../src/main/resources/application.yaml | 9 ++++++ option/src/j2_macro/java.j2_macro | 11 +++---- 8 files changed, 101 insertions(+), 38 deletions(-) delete mode 100644 option/src/app/java_helidon/src/main/java/me/opc/mp/database/Message.java.old create mode 100644 option/src/app/java_helidon4_nosql/src/main/java/me/opc/mp/database/Dept.java create mode 100644 option/src/app/java_helidon4_nosql/src/main/java/me/opc/mp/database/DeptRepository.java create mode 100644 option/src/app/java_helidon4_nosql/src/main/java/me/opc/mp/database/DeptResource.j2.java create mode 100644 option/src/app/java_helidon4_nosql/src/main/java/me/opc/mp/database/FieldAccessStrategy.java create mode 100644 option/src/app/java_helidon4_nosql/src/main/resources/application.yaml diff --git a/option/src/app/java_helidon/microprofile-config.properties.j2.tmpl b/option/src/app/java_helidon/microprofile-config.properties.j2.tmpl index f5cdd610..f1daa98e 100644 --- a/option/src/app/java_helidon/microprofile-config.properties.j2.tmpl +++ b/option/src/app/java_helidon/microprofile-config.properties.j2.tmpl @@ -22,3 +22,18 @@ javax.sql.DataSource.ds1.dataSource.user=##DB_USER## javax.sql.DataSource.ds1.dataSource.password=##DB_PASSWORD## {%- endif %} +{%- if db_family == "nosql" %} +# Oracle NoSQL Connection +jnosql.keyvalue.database=dept +jnosql.document.database=dept +jnosql.oracle.nosql.table.read.limit=10 +jnosql.oracle.nosql.table.write.limit=10 +jnosql.oracle.nosql.table.storage.gb=1 +jnosql.oracle.nosql.host=${TF_VAR_nosql_endpoint} +jnosql.oracle.nosql.compartment=${TF_VAR_compartment_ocid} +{%- if deploy_type in ["compute", "kubernetes", "instance_pool"] %} +jnosql.oracle.nosql.deployment=CLOUD_INSTANCE_PRINCIPAL +{%- else %} +jnosql.oracle.nosql.deployment=CLOUD_RESOURCE_PRINCIPAL +{%- endif %} +{%- endif %} diff --git a/option/src/app/java_helidon/src/main/java/me/opc/mp/database/Message.java.old b/option/src/app/java_helidon/src/main/java/me/opc/mp/database/Message.java.old deleted file mode 100644 index 84711069..00000000 --- a/option/src/app/java_helidon/src/main/java/me/opc/mp/database/Message.java.old +++ /dev/null @@ -1,31 +0,0 @@ -package me.opc.mp.database; - -public class Message { - - private String message; - - private String greeting; - - public Message() { - } - - public Message(String message) { - this.message = message; - } - - public void setMessage(String message) { - this.message = message; - } - - public String getMessage() { - return this.message; - } - - public void setGreeting(String greeting) { - this.greeting = greeting; - } - - public String getGreeting() { - return this.greeting; - } -} diff --git a/option/src/app/java_helidon4_nosql/src/main/java/me/opc/mp/database/Dept.java b/option/src/app/java_helidon4_nosql/src/main/java/me/opc/mp/database/Dept.java new file mode 100644 index 00000000..973a5450 --- /dev/null +++ b/option/src/app/java_helidon4_nosql/src/main/java/me/opc/mp/database/Dept.java @@ -0,0 +1,15 @@ +package me.opc.mp.database; + +import jakarta.json.bind.annotation.JsonbVisibility; +import jakarta.nosql.*; + +@Entity +@JsonbVisibility(FieldAccessStrategy.class) +public class Dept { + @Id + private int deptno; + @Column + private String dname; + @Column + private String loc; +} \ No newline at end of file diff --git a/option/src/app/java_helidon4_nosql/src/main/java/me/opc/mp/database/DeptRepository.java b/option/src/app/java_helidon4_nosql/src/main/java/me/opc/mp/database/DeptRepository.java new file mode 100644 index 00000000..faa3c8d5 --- /dev/null +++ b/option/src/app/java_helidon4_nosql/src/main/java/me/opc/mp/database/DeptRepository.java @@ -0,0 +1,9 @@ +package me.opc.mp.database; + +import jakarta.data.repository.Query; +import jakarta.data.repository.Repository; +import org.eclipse.jnosql.databases.oracle.mapping.OracleNoSQLRepository; + +@Repository +public interface DeptRepository extends OracleNoSQLRepository { +} \ No newline at end of file diff --git a/option/src/app/java_helidon4_nosql/src/main/java/me/opc/mp/database/DeptResource.j2.java b/option/src/app/java_helidon4_nosql/src/main/java/me/opc/mp/database/DeptResource.j2.java new file mode 100644 index 00000000..e17afbba --- /dev/null +++ b/option/src/app/java_helidon4_nosql/src/main/java/me/opc/mp/database/DeptResource.j2.java @@ -0,0 +1,31 @@ +{% import "java.j2_macro" as m with context %} +package me.opc.mp.database; + +import jakarta.persistence.*; +import jakarta.ws.rs.*; +import jakarta.ws.rs.core.MediaType; +import jakarta.inject.*; +{{ m.import() }} + +/** + * Dept Table + */ +@Path("/") +public class DeptResource { + @Inject + private DeptRepository deptRepository; + + @GET + @Path("dept") + @Produces(MediaType.APPLICATION_JSON) + public List getDept() throws Exception { + return deptRepository.findAll().toList(); + } + + @GET + @Path("info") + @Produces(MediaType.TEXT_PLAIN) + public String getInfo() { + return "Java - Helidon - {{ dbName }}"; + } +} diff --git a/option/src/app/java_helidon4_nosql/src/main/java/me/opc/mp/database/FieldAccessStrategy.java b/option/src/app/java_helidon4_nosql/src/main/java/me/opc/mp/database/FieldAccessStrategy.java new file mode 100644 index 00000000..c0d3ff33 --- /dev/null +++ b/option/src/app/java_helidon4_nosql/src/main/java/me/opc/mp/database/FieldAccessStrategy.java @@ -0,0 +1,18 @@ +package me.opc.mp.database; + +import jakarta.json.bind.config.PropertyVisibilityStrategy; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +public class FieldAccessStrategy implements PropertyVisibilityStrategy { + @Override + public boolean isVisible(Field field) { + return true; + } + + @Override + public boolean isVisible(Method method) { + return true; + } +} \ No newline at end of file diff --git a/option/src/app/java_helidon4_nosql/src/main/resources/application.yaml b/option/src/app/java_helidon4_nosql/src/main/resources/application.yaml new file mode 100644 index 00000000..5852e277 --- /dev/null +++ b/option/src/app/java_helidon4_nosql/src/main/resources/application.yaml @@ -0,0 +1,9 @@ +# Oracle NoSQL Configuration +jnosql: + keyvalue: + database: beers + document: + database: beers + oracle: + nosql: + host: http://localhost:8080 \ No newline at end of file diff --git a/option/src/j2_macro/java.j2_macro b/option/src/j2_macro/java.j2_macro index a4ee8a57..9ec6cf6c 100644 --- a/option/src/j2_macro/java.j2_macro +++ b/option/src/j2_macro/java.j2_macro @@ -225,11 +225,8 @@ import javax.json.*; {% macro nosql() -%} QueryRequest queryRequest = new QueryRequest().setStatement("SELECT deptno, dname, loc FROM dept"); - QueryResult queryResult = handle.query(queryRequest); - do { - List results = queryResult.getResults(); - for (MapValue row : results) { - rows.add( new Dept( row.get("deptno").asInteger().getValue(), row.get("dname").asString().getValue(), row.get("loc").asString().getValue() ) ); - } - } while (!queryRequest.isDone()); + QueryIterableResult results = handle.queryIterable(queryRequest); + for (MapValue res : results) { + rows.add( new Dept( row.get("deptno").asInteger().getValue(), row.get("dname").asString().getValue(), row.get("loc").asString().getValue() ) ); + } {%- endmacro -%} From 5544d9173cec5cea0ab31b4219472d84f202ddf7 Mon Sep 17 00:00:00 2001 From: Marc Gueury Date: Fri, 8 Mar 2024 14:07:01 +0100 Subject: [PATCH 2/3] v2.0 --- option/src/j2_macro/java.j2_macro | 1 + 1 file changed, 1 insertion(+) diff --git a/option/src/j2_macro/java.j2_macro b/option/src/j2_macro/java.j2_macro index 9ec6cf6c..e20628d1 100644 --- a/option/src/j2_macro/java.j2_macro +++ b/option/src/j2_macro/java.j2_macro @@ -224,6 +224,7 @@ import javax.json.*; {%- endmacro -%} {% macro nosql() -%} + // See https://github.com/oracle/nosql-examples/blob/master/examples-nosql-java-sdk/sqlexamples/QueryData.java QueryRequest queryRequest = new QueryRequest().setStatement("SELECT deptno, dname, loc FROM dept"); QueryIterableResult results = handle.queryIterable(queryRequest); for (MapValue res : results) { From d56096bc2c8f2c9dc3df5201db74d6d7d89c34af Mon Sep 17 00:00:00 2001 From: Marc Gueury Date: Fri, 8 Mar 2024 14:15:57 +0100 Subject: [PATCH 3/3] v2.0 --- option/src/j2_macro/java.j2_macro | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/option/src/j2_macro/java.j2_macro b/option/src/j2_macro/java.j2_macro index e20628d1..18d07823 100644 --- a/option/src/j2_macro/java.j2_macro +++ b/option/src/j2_macro/java.j2_macro @@ -227,7 +227,7 @@ import javax.json.*; // See https://github.com/oracle/nosql-examples/blob/master/examples-nosql-java-sdk/sqlexamples/QueryData.java QueryRequest queryRequest = new QueryRequest().setStatement("SELECT deptno, dname, loc FROM dept"); QueryIterableResult results = handle.queryIterable(queryRequest); - for (MapValue res : results) { + for (MapValue row : results) { rows.add( new Dept( row.get("deptno").asInteger().getValue(), row.get("dname").asString().getValue(), row.get("loc").asString().getValue() ) ); } {%- endmacro -%}