Skip to content

Commit ece87e5

Browse files
authored
Finished work on creating Models from JSON
-created models -changed json file structure -changed directory structure
1 parent 1eb2ef0 commit ece87e5

File tree

9 files changed

+500
-47
lines changed

9 files changed

+500
-47
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.othree.apx;
7+
8+
import com.fasterxml.jackson.databind.JsonNode;
9+
import com.google.gson.annotations.SerializedName;
10+
import com.j256.ormlite.field.DatabaseField;
11+
import com.sun.codemodel.JDefinedClass;
12+
import com.sun.codemodel.JFieldVar;
13+
import com.sun.codemodel.JMethod;
14+
import com.sun.codemodel.JMod;
15+
import org.jsonschema2pojo.Schema;
16+
import org.jsonschema2pojo.rules.Rule;
17+
18+
/**
19+
*
20+
* @author root
21+
*/
22+
public class APXCustomRule implements Rule<JDefinedClass, JDefinedClass> {
23+
24+
APXCustomRuleFactory fx;
25+
26+
APXCustomRule(APXCustomRuleFactory aThis) {
27+
this.fx = aThis;
28+
29+
}
30+
31+
@Override
32+
public JDefinedClass apply(String string, JsonNode jn, JDefinedClass t, Schema schema) {
33+
34+
35+
36+
JFieldVar idField = t.field(JMod.PRIVATE, int.class, "id");
37+
JMethod getter = t.method(JMod.PUBLIC, idField.type(), "getId");
38+
getter.body()._return(idField);
39+
40+
idField.javadoc().add("This is compulsory for both the database and to access models");
41+
42+
43+
JMethod setter = t.method(JMod.PUBLIC, void.class, "setId");
44+
setter.param(idField.type(), idField.name())
45+
.assign(idField);
46+
47+
idField.annotate(DatabaseField.class)
48+
.param("generatedId", true)
49+
.param("columnName", idField.name());
50+
51+
idField.annotate(SerializedName.class)
52+
.param("value", "id");
53+
54+
return t;
55+
}
56+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.othree.apx;
7+
8+
import com.fasterxml.jackson.databind.JsonNode;
9+
import com.sun.codemodel.JDefinedClass;
10+
import com.sun.codemodel.JType;
11+
import java.time.LocalDate;
12+
import org.jsonschema2pojo.Annotator;
13+
import org.jsonschema2pojo.GenerationConfig;
14+
import org.jsonschema2pojo.Schema;
15+
import org.jsonschema2pojo.SchemaStore;
16+
import org.jsonschema2pojo.rules.FormatRule;
17+
import org.jsonschema2pojo.rules.PropertiesRule;
18+
import org.jsonschema2pojo.rules.Rule;
19+
import org.jsonschema2pojo.rules.RuleFactory;
20+
21+
/**
22+
*
23+
* @author root
24+
*/
25+
public class APXCustomRuleFactory extends RuleFactory{
26+
27+
public APXCustomRuleFactory(GenerationConfig generationConfig, Annotator annotator, SchemaStore schemaStore) {
28+
super(generationConfig, annotator, schemaStore);
29+
}
30+
31+
@Override
32+
public Rule<JDefinedClass, JDefinedClass> getPropertiesRule() {
33+
return new PropertiesRule(this){
34+
35+
@Override
36+
public JDefinedClass apply(String nodeName, JsonNode node, JDefinedClass jclass, Schema schema) {
37+
new APXCustomRule(APXCustomRuleFactory.this).apply(nodeName, node, jclass, schema);
38+
return super.apply(nodeName, node, jclass, schema);
39+
}
40+
41+
};
42+
}
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
}

src/com/othree/apx/Database.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,19 @@ public void closeDB() {
5757

5858
public boolean createTable(String where,String query) {
5959

60-
boolean done = false;
60+
boolean done = true;
6161
try {
6262
// db parameters
6363
String url = "jdbc:sqlite:" + where;
6464
// create a connection to the database
6565
conn = DriverManager.getConnection(url);
6666
pt = conn.prepareStatement(query);
6767

68-
done = pt.execute();
69-
if(done){
70-
System.out.println("Operation Completed");
68+
pt.execute();
69+
7170

72-
}
7371
} catch (SQLException e) {
72+
done = false;
7473
System.out.println(e.getMessage());
7574
} finally {
7675
try {
@@ -82,6 +81,10 @@ public boolean createTable(String where,String query) {
8281
} catch (SQLException ex) {
8382
System.out.println(ex.getMessage());
8483
}
84+
if(done){
85+
System.out.println("Operation Completed");
86+
87+
}
8588
}
8689
return done;
8790
}

src/com/othree/apx/Main.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class Main {
1818
* @param args
1919
*/
2020
public static void main(String[] args) {
21+
22+
System.out.println(System.getProperty("os.name"));
2123
Operations operations = new Operations();
2224

2325

@@ -28,6 +30,7 @@ public static void main(String[] args) {
2830

2931
System.out.println("\t\tapx start ${Projectname} - Creates a new Apx project\n");
3032
System.out.println("\t\tapx g page ${PageName} - Creates a new page\n\t\twith the .fxml, .css and Controller files");
33+
System.out.println("\t\tapx create model ${Location of JSON File} - Creates a model corresponding\n\t\tto the JSON. Also creates a matching table in database");
3134

3235

3336
} else if (args[0].equalsIgnoreCase("start")) {
@@ -65,7 +68,7 @@ public static void main(String[] args) {
6568
System.exit(0);
6669
}
6770

68-
if (args[1].equalsIgnoreCase("table")) {
71+
if (args[1].equalsIgnoreCase("model")) {
6972

7073
//File loction of the json file Containing the Json File to
7174
String fileLocation = args[2];
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.othree.apx;
7+
8+
import com.fasterxml.jackson.annotation.JsonInclude;
9+
import com.fasterxml.jackson.annotation.JsonView;
10+
import com.fasterxml.jackson.databind.JsonNode;
11+
import com.google.gson.annotations.SerializedName;
12+
import com.j256.ormlite.field.DatabaseField;
13+
import com.j256.ormlite.table.DatabaseTable;
14+
import com.sun.codemodel.JDefinedClass;
15+
import com.sun.codemodel.JFieldRef;
16+
import com.sun.codemodel.JFieldVar;
17+
import org.jsonschema2pojo.AbstractAnnotator;
18+
19+
/**
20+
*
21+
* @author root
22+
*/
23+
public class ORMLiteAnotator extends AbstractAnnotator{
24+
25+
@Override
26+
public void propertyField(JFieldVar field, JDefinedClass clazz, String propertyName, JsonNode propertyNode) {
27+
try {
28+
29+
field.annotate(DatabaseField.class).param("columnName", propertyName)
30+
.param("canBeNull", propertyNode.get("null").asBoolean())
31+
.param("unique", propertyNode.get("unique").asBoolean());
32+
33+
field.annotate(SerializedName.class).param("value", propertyName);
34+
35+
} catch (Exception e) {
36+
}
37+
38+
39+
40+
}
41+
42+
@Override
43+
public void propertyInclusion(JDefinedClass clazz, JsonNode schema) {
44+
clazz.annotate(DatabaseTable.class).param("tableName", schema.get("table").asText());
45+
46+
47+
}
48+
49+
50+
51+
}

0 commit comments

Comments
 (0)