Skip to content

Commit

Permalink
start for java support
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengoldbaum committed Jun 9, 2024
1 parent 36dd0d7 commit 2f22276
Show file tree
Hide file tree
Showing 10 changed files with 764 additions and 21 deletions.
5 changes: 4 additions & 1 deletion DataThread/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ node_modules

# Temp
C4-PlantUML
.idea/
.idea/
.gradle/
generated/
build/
65 changes: 65 additions & 0 deletions DataThread/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.jsonschema2pojo:jsonschema2pojo-gradle-plugin:1.1.1'
}
}

plugins {
id 'java'
}

group 'com.example'
version '1.0-SNAPSHOT'

repositories {
mavenCentral()
}

/*
//id "com.netflix.dgs.codegen" version "6.2.1"
dependencyManagement {
imports {
mavenBom("com.netflix.graphql.dgs:graphql-dgs-platform-dependencies:latest.release")
}
}
generateJava{
schemaPaths = ["${projectDir}/DataThread/src/main/resources"]
packageName = 'org.datathread.query'
generateClient = true
}
*/

dependencies {
implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.3'
implementation 'org.jsonschema2pojo:jsonschema2pojo-gradle-plugin:1.1.1'
implementation 'com.squareup:javapoet:1.13.0'
// implementation "com.netflix.graphql.dgs:graphql-dgs-spring-graphql-starter"

testImplementation 'junit:junit:4.13.1'
}

test {
useJUnitPlatform()
}

apply plugin: 'jsonschema2pojo'

jsonSchema2Pojo {
source = files("$projectDir/generated/tsp-output")
targetPackage = 'org.datathread.grammar'
targetDirectory = file("$projectDir/generated/main/java")
}

sourceSets {
main {
java {
srcDirs 'src/main/java', 'generated/main/java'
}
}
}
1 change: 0 additions & 1 deletion DataThread/data/Foo/Bah/foo.element.json

This file was deleted.

13 changes: 0 additions & 13 deletions DataThread/data/Foo/Bah/foos.dataset.json

This file was deleted.

11 changes: 5 additions & 6 deletions DataThread/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
"scripts": {
"graph-data": "node ./src/graphql-server.js"
},
"devDependencies": {
},
"dependencies": {
"@apollo/client": "^3.9.11",
"@typespec/json-schema": "^0.56.0",
"apollo-server": "^3.13.0",
"express": "^4.19.2",
"graphql": "^16.8.1",
Expand All @@ -27,9 +26,9 @@
"ruru": "^2.0.0-beta.11"
},
"files": [
"morphir-ir.json"
,"metastore"
,"bin"
,"src"
"morphir-ir.json",
"metastore",
"bin",
"src"
]
}
59 changes: 59 additions & 0 deletions DataThread/src/APIService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
String baseDirArg = System.getProperty("baseDir", "metastore");
Path baseDir = Paths.get(baseDirArg).toAbsolutePath().normalize();

System.out.println("Using base folder: " + baseDir);

if (!Files.exists(baseDir)) {
try {
Files.createDirectories(baseDir);
} catch (Exception e) {
e.printStackTrace();
}
}

SpringApplication.run(Application.class, args);
}
}

@RestController
class RequestController {

@PostMapping("/element")
public void createElement(@RequestBody String body) {

processRequest(body, "element");
}

@PostMapping("/dataset")
public void createDataset(@RequestBody String body) {
processRequest(body, "dataset");
}

private void processRequest(String body, String type) {
// TODO: Implement the logic to process the request here.
}


private void saveToFile(Map<String, Object> artifact) throws IOException {
// Save as a JSON file in the data folder
String id = (String) artifact.get("id");
String[] items = id.split(":");
String type = items[0];
Path filePath = Paths.get("data", type + ".json");
Files.write(filePath, artifact.toString().getBytes());
}
}
52 changes: 52 additions & 0 deletions DataThread/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.squareup.javapoet.TypeSpec;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.File;

import org.datathread.grammar._typespec.json_schema.Element;
import javax.lang.model.element.Modifier;

public class Main {
public static void main(String[] args) {
String json = null;
try {
System.out.println(new File(".").getAbsolutePath());
File baseDir = new File("DataThread/example/metastore");
String id = "element:person:active";
File file = new File(baseDir, resolveForID(id).getPath());

json = new String(Files.readAllBytes(Paths.get(file.getAbsolutePath())));
} catch (IOException e) {
e.printStackTrace();
}
ObjectMapper mapper = new ObjectMapper();

try {
Element element = mapper.readValue(json, Element.class);
TypeSpec java = elementToJava(element);
System.out.println(java);
} catch (IOException e) {
e.printStackTrace();
}
}

public static TypeSpec elementToJava(Element element) {
TypeSpec java = TypeSpec.classBuilder(element.getName())
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.build();

return java;
}

public static File resolveForID(String id) {
String[] parts = id.split(":");
return resolveFile(parts[0], parts[1], parts[2]);
}

public static File resolveFile(String schemaType, String domain, String name) {
return new File(domain, name + "." + schemaType + ".json");
}
}
14 changes: 14 additions & 0 deletions DataThread/src/main/resources/Dataset.schema.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
type Dataset {
id: ID!
name: String!
version: Int!
fields: [Field!]!
}

type Field {
name: String!
element: Element!
optional: Boolean
key: Boolean
}

90 changes: 90 additions & 0 deletions DataThread/src/main/resources/Element.schema.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
type Element {
id: ID!
name: String!
element_type: ElementType!
info: ElementInfo
}

union ElementType = TextType | NumberType | ReferenceType | DateType | TimeType | DateTimeType | BooleanType | EnumType | RecordType

scalar Date

type DateType {
Date : Date
}

scalar Time

type TimeType {
Time : Time
}

scalar DateTime

type DateTimeType {
DateTime : DateTime
}

scalar Bool

type BooleanType {
Boolean : Bool
}

type TextType {
Text : Text!
}

type Text {
min_length: Int
max_length: Int
}

type NumberType {
Number : Number!
}

type Number {
minimum: Int
maximum: Int
precision: Int
}

type ReferenceType {
Reference : Reference!
}

type Reference {
ref: Element!
}

type EnumType {
Enum : Enum!
}

type Enum {
values: [String]!
}

type RecordType {
Record : Record!
}

type Record {
name: String
fields: [Field]
}

type Field {
name: String!
element: Element!
optional: Boolean
}


type ElementInfo {
id: ID!
description: String
display_name: String
short_display_name: String
}
Loading

0 comments on commit 2f22276

Please sign in to comment.