Commit 043b4381 authored by Sebastian Mate's avatar Sebastian Mate

Code hinzugefügt

parent 528d3958
/target/
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.miracum</groupId>
<artifactId>FhirLoescher</artifactId>
<version>0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer>
<mainClass>org.miracum.fhirloescher.gui.UserInterface</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?>
<actions>
<action>
<actionName>run</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:1.5.0:exec</goal>
</goals>
<properties>
<exec.args>-classpath %classpath org.miracum.fhirloescher.gui.FhirLoescher</exec.args>
<exec.executable>java</exec.executable>
<exec.workingdir></exec.workingdir>
</properties>
</action>
<action>
<actionName>debug</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:1.5.0:exec</goal>
</goals>
<properties>
<exec.args>-agentlib:jdwp=transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath org.miracum.fhirloescher.gui.FhirLoescher</exec.args>
<exec.executable>java</exec.executable>
<jpda.listen>true</jpda.listen>
<exec.workingdir></exec.workingdir>
</properties>
</action>
<action>
<actionName>profile</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:1.5.0:exec</goal>
</goals>
<properties>
<exec.args>-classpath %classpath org.miracum.fhirloescher.gui.FhirLoescher</exec.args>
<exec.executable>java</exec.executable>
<exec.workingdir></exec.workingdir>
</properties>
</action>
</actions>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.miracum</groupId>
<artifactId>FhirLoescher</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.xsom/xsom -->
<dependency>
<groupId>com.sun.xsom</groupId>
<artifactId>xsom</artifactId>
<version>20100725</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.miracum.fhirloescher.gui.UserInterface</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
java -jar target/FhirLoescher-0.1-SNAPSHOT.jar
\ No newline at end of file
package org.miracum.fhirloescher.ddlgen;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
import com.google.gson.*;
import org.apache.commons.lang3.StringEscapeUtils;
public class DdlGenerator {
static String generatedDDL = "";
static String description = "";
private static String translateType(String simpleType) {
return "text";
}
JsonElement jsonElement = null;
public DdlGenerator() {
String lines = readLineByLineJava8("fhir.schema.json");
JsonParser parser = new JsonParser();
jsonElement = parser.parse(lines);
processJsonElement(jsonElement, 0, "");
}
static int depth = 0;
public void setDepth(int depth) {
this.depth = depth;
}
static String ressources = "";
public void setRessources(String ressources) {
this.ressources = ressources;
}
static boolean addComment = false;
public void setCreateComments(boolean addComment) {
this.addComment = addComment;
}
static List<String> pathLines = new ArrayList<String>();
static FHIRRessource currRes = new FHIRRessource("");
static List<FHIRRessource> resList = new ArrayList<FHIRRessource>();
public static int count(String str, String target) {
return (str.length() - str.replace(target, "").length()) / target.length();
}
private static String readLineByLineJava8(String filePath) {
StringBuilder contentBuilder = new StringBuilder();
try (Stream<String> stream = Files.lines(Paths.get(filePath), StandardCharsets.UTF_8)) {
stream.forEach(s -> contentBuilder.append(s).append("\n"));
} catch (IOException e) {
e.printStackTrace();
}
return contentBuilder.toString();
}
public String generate() {
String[] res = ressources.split(" ");
generatedDDL = "";
for (int a = 0; a < res.length; a++) {
// Create a new table:
description = "";
generatedDDL += "CREATE TABLE " + res[a] + " ( \n";
// Append the columns for that table:
printProps(res[a], 0, "");
generatedDDL = generatedDDL.substring(0, generatedDDL.length() - 2); // Remove last comma
generatedDDL += "\n); \n\n";
generatedDDL += description.replaceAll("#tbl#", res[a] + ".") + "\n";
}
return generatedDDL;
}
private static void printProps(String name, int recDepth, String prefix) {
if (recDepth > depth) { // Ignore everything that's above the recursion depth
return;
}
if (name.contains("xtension")) { // Ignore extensions (TODO)
return;
}
for (int a = 0; a < resList.size(); a++) {
FHIRRessource fr = resList.get(a);
if (fr.getName().equals(name)) {
List<FHIRProperty> props = fr.getProperties();
for (int p = 0; p < props.size(); p++) {
if (!props.get(p).getName().startsWith("_")) {
if (!props.get(p).getIsComplex()) {
printIndents(recDepth);
if (!prefix.equals("")) {
generatedDDL += " " + prefix + "_" + props.get(p).getName() + " " + translateType(props.get(p).getSimpleType()) /*+ " " + props.get(p).getDesc()*/ + ",\n";
if(addComment) description += "COMMENT ON COLUMN #tbl#" + prefix + "_" + props.get(p).getName() + " IS '" + StringEscapeUtils.unescapeJava(props.get(p).getDesc()).replaceAll("\n", "").replaceAll("'", "").replaceAll("\"", "") + "';\n";
} else {
generatedDDL += " " + props.get(p).getName() + " " + translateType(props.get(p).getSimpleType()) /*+ " " + props.get(p).getDesc()*/ + ",\n";
if(addComment) description += "COMMENT ON COLUMN #tbl#" + props.get(p).getName() + " IS '" + StringEscapeUtils.unescapeJava(props.get(p).getDesc()).replaceAll("\n", "").replaceAll("'", "").replaceAll("\"", "") + "';\n";
}
} else {
if (props.get(p).getSimpleType().equals(fr.getName())) {
//printIndents(recDepth);
//System.out.println(" ##" + prefix + "_" + props.get(p).getName() + " " + props.get(p).getSimpleType() + " " + props.get(p).getDesc());
} else {
//System.out.println("\n");
printIndents(recDepth);
//System.out.println(" #" + prefix + props.get(p).getName() + ": " + props.get(p).getDesc());
String newPrefix = prefix + "_" + props.get(p).getName();
if (recDepth == 0) {
newPrefix = prefix + "" + props.get(p).getName();
}
int rD = recDepth + 1;
printProps(props.get(p).getSimpleType(), rD, newPrefix);
}
}
}
}
}
}
}
// ---- Recursion functions to generate the JSON paths ----
private static void printIndents(int recDepth) {
for (int a = 0; a <= recDepth; a++) {
//System.out.print(" ");
}
}
private static void processJsonElement(JsonElement e, int depth, String parent) {
// Recursion based on: https://stackoverflow.com/a/44331104
if (e.isJsonArray()) {
processJsonArray(e.getAsJsonArray(), depth + 1, parent);
} else if (e.isJsonNull()) {
processJsonNull(e.getAsJsonNull(), depth + 1, parent);
} else if (e.isJsonObject()) {
processJsonObject(e.getAsJsonObject(), depth + 1, parent);
} else if (e.isJsonPrimitive()) {
processJsonPrimitive(e.getAsJsonPrimitive(), depth + 1, parent);
}
}
private static void processJsonArray(JsonArray a, int depth, String parent) {
for (JsonElement e : a) {
processJsonElement(e, depth + 1, parent);
}
}
private static void processJsonNull(JsonNull n, int depth, String parent) {
}
private static void processJsonObject(JsonObject o, int depth, String parent) {
Set<Map.Entry<String, JsonElement>> members = o.entrySet();
for (Map.Entry<String, JsonElement> e : members) {
if (parent.equals("definitions") && depth == 3) {
// Create new Ressource
currRes = new FHIRRessource(e.getKey());
resList.add(currRes);
}
if (parent.equals("properties") && depth == 7) {
FHIRProperty prop = new FHIRProperty(e.getKey());
currRes.addProperty(prop);
}
if (depth > 7) {
if (e.getKey().equals("description")) {
FHIRProperty currProp = currRes.getCurrentProperty();
currProp.setDesc(e.getValue().toString());
currRes.setCurrentProperty(currProp);
} else if (e.getKey().equals("type") && e.getValue().toString().equals("\"array\"")) {
FHIRProperty currProp = currRes.getCurrentProperty();
currProp.setCanBeArray(true);
currRes.setCurrentProperty(currProp);
} else if (e.getKey().equals("type") && !e.getValue().toString().equals("\"array\"")) {
FHIRProperty currProp = currRes.getCurrentProperty();
currProp.setType(e.getValue().toString());
currRes.setCurrentProperty(currProp);
} else if (e.getKey().equals("const")) {
FHIRProperty currProp = currRes.getCurrentProperty();
if (currProp.getType().equals("")) {
currProp.setType(e.getValue().toString());
currRes.setCurrentProperty(currProp);
}
} else if (e.getKey().equals("enum")) {
FHIRProperty currProp = currRes.getCurrentProperty();
if (currProp.getType().equals("")) {
currProp.setEnumerated(e.getValue().toString());
currProp.setType("stringEnumerated");
currRes.setCurrentProperty(currProp);
}
} else if (e.getKey().equals("pattern")) {
FHIRProperty currProp = currRes.getCurrentProperty();
currProp.setPattern(e.getValue().toString());
currRes.setCurrentProperty(currProp);
} else if (e.getKey().equals("$ref")) {
FHIRProperty currProp = currRes.getCurrentProperty();
if (currProp.getType().equals("")) {
currProp.setType(e.getValue().toString());
currRes.setCurrentProperty(currProp);
}
} else if (e.getKey().equals("items")) {
// Wird in einer tieferen Rekursionstiefe automatisch aufgelöst!
} else {
System.out.println(e.getKey() + "=" + e.getValue());
}
}
processJsonElement(e.getValue(), depth + 1, e.getKey());
}
}
private static void processJsonPrimitive(JsonPrimitive p, int depth, String parent) {
}
}
package org.miracum.fhirloescher.ddlgen;
import com.google.gson.JsonElement;
public class FHIRProperty {
private String name = "";
private String desc = "";
private String type = "";
private String enumerated = "";
private Boolean canBeArray = false;
private String pattern = "";
private String simpleType = "";
private Boolean isComplex = false;
public String getSimpleType() {
return simpleType;
}
public void setSimpleType(String simpleType) {
this.simpleType = simpleType;
}
public Boolean getIsComplex() {
return isComplex;
}
public void setIsComplex(Boolean isComplex) {
this.isComplex = isComplex;
}
public String getPattern() {
return pattern;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
public String getEnumerated() {
return enumerated;
}
public void setEnumerated(String enumerated) {
this.enumerated = enumerated;
}
public Boolean getCanBeArray() {
return canBeArray;
}
public void setCanBeArray(Boolean canBeArray) {
this.canBeArray = canBeArray;
}
public String getDesc() {
return desc;
}
public String getType() {
return type;
}
public FHIRProperty(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return name;
}
public void setType(String type) {
this.type = type.replaceAll("\"", "").replaceAll("#", "");
this.simpleType = this.type.replaceAll("/definitions/", "");
if (this.simpleType.substring(0, 1).toUpperCase().equals(this.simpleType.substring(0, 1))) {
this.isComplex = true;
}
}
public void setDesc(String desc) {
this.desc = desc;
}
}
package org.miracum.fhirloescher.ddlgen;
import java.util.ArrayList;
import java.util.List;
public class FHIRRessource {
private String name = "";
private List<FHIRProperty> properties = new ArrayList<FHIRProperty>();
public void addProperty(FHIRProperty prop) {
properties.add(prop);
}
public List<FHIRProperty> getProperties() {
return properties;
}
public void setProperties(List<FHIRProperty> properties) {
this.properties = properties;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public FHIRRessource(String name) {
this.name = name;
}
public String toString() {
return name;
}
public FHIRProperty getCurrentProperty() {
if (properties.size() == 0) {
return new FHIRProperty("");
}
return properties.get(properties.size() - 1);
}
public void setCurrentProperty(FHIRProperty currProp) {
if (properties.size() == 0) {
//TODO
return;
}
properties.set(properties.size() - 1, currProp);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.miracum.fhirloescher.gui;
/**
*
* @author matesn
*/
public class FhirLoescher {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
UserInterface gui = new UserInterface();
gui.show();
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
<Properties>
<Property name="defaultCloseOperation" type="int" value="3"/>
<Property name="title" type="java.lang.String" value="FhirLoescher"/>
<Property name="locationByPlatform" type="boolean" value="true"/>