├── .gitignore ├── images ├── personJson.png └── person-djangoModel.png ├── target ├── maven-status │ └── maven-compiler-plugin │ │ ├── testCompile │ │ └── default-testCompile │ │ │ ├── createdFiles.lst │ │ │ └── inputFiles.lst │ │ └── compile │ │ └── default-compile │ │ ├── inputFiles.lst │ │ └── createdFiles.lst ├── jsonToOrmMapper-0.0.1-SNAPSHOT.jar ├── original-jsonToOrmMapper-0.0.1-SNAPSHOT.jar ├── classes │ └── com │ │ └── p2sdev │ │ └── jsonToOrmMapper │ │ ├── App.class │ │ ├── CommandLine.class │ │ ├── convert │ │ ├── Table.class │ │ ├── JSONTypes.class │ │ ├── MapJsonToTable.class │ │ ├── Table$Cardinalities.class │ │ ├── Table$TableBuilder.class │ │ └── MapJsonToTable$BuildTables.class │ │ └── mapper │ │ ├── JSONTo.class │ │ ├── orm │ │ ├── ORM.class │ │ ├── DjangoModel.class │ │ ├── RoomModel.class │ │ ├── DoctrineModel.class │ │ ├── HibernateModel.class │ │ ├── DjangoModel$Model.class │ │ ├── HibernateModel$Template.class │ │ └── HibernateModel$DataTypes.class │ │ ├── JSONTo$DATABASE.class │ │ ├── JSONTo$ORMModel.class │ │ ├── JSONToConverter.class │ │ ├── database │ │ ├── MySQL.class │ │ ├── Database.class │ │ ├── SqlLite.class │ │ ├── PostgreSQL.class │ │ ├── PostgreSQL$ColumnType.class │ │ ├── PostgreSQL$TableConstraint.class │ │ └── PostgreSQL$ColumnConstraint.class │ │ └── JSONTo$SQLOperation.class ├── maven-archiver │ └── pom.properties ├── test-classes │ └── com │ │ └── p2sdev │ │ └── jsonToOrmMapper │ │ └── AppTest.class └── surefire-reports │ ├── com.p2sdev.jsonToOrmMapper.AppTest.txt │ └── TEST-com.p2sdev.jsonToOrmMapper.AppTest.xml ├── src ├── main │ └── java │ │ └── com │ │ └── p2sdev │ │ └── jsonToOrmMapper │ │ ├── enums │ │ ├── Cardinalities.java │ │ └── JSONTypes.java │ │ ├── mapper │ │ ├── orm │ │ │ ├── ORM.java │ │ │ ├── RoomModel.java │ │ │ ├── DoctrineModel.java │ │ │ ├── DjangoModel.java │ │ │ └── HibernateModel.java │ │ ├── JSONTo.java │ │ ├── database │ │ │ ├── MySQL.java │ │ │ ├── SqlLite.java │ │ │ ├── Database.java │ │ │ └── PostgreSQL.java │ │ └── JSONToConverter.java │ │ ├── App.java │ │ ├── convert │ │ ├── MapJsonToTable.java │ │ ├── JsonTypes.java │ │ ├── BuildTables.java │ │ └── Table.java │ │ └── CommandLine.java └── test │ └── java │ └── com │ └── p2sdev │ └── jsonToOrmMapper │ └── AppTest.java ├── README.md ├── pom.xml └── dependency-reduced-pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | .idea/ -------------------------------------------------------------------------------- /images/personJson.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/images/personJson.png -------------------------------------------------------------------------------- /images/person-djangoModel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/images/person-djangoModel.png -------------------------------------------------------------------------------- /target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst: -------------------------------------------------------------------------------- 1 | com\p2sdev\jsonToOrmMapper\AppTest.class 2 | -------------------------------------------------------------------------------- /target/jsonToOrmMapper-0.0.1-SNAPSHOT.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/jsonToOrmMapper-0.0.1-SNAPSHOT.jar -------------------------------------------------------------------------------- /target/original-jsonToOrmMapper-0.0.1-SNAPSHOT.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/original-jsonToOrmMapper-0.0.1-SNAPSHOT.jar -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/App.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/App.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/CommandLine.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/CommandLine.class -------------------------------------------------------------------------------- /target/maven-archiver/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven 2 | #Wed Nov 27 15:43:00 GMT 2019 3 | version=0.0.1-SNAPSHOT 4 | groupId=com.p2sdev 5 | artifactId=jsonToOrmMapper 6 | -------------------------------------------------------------------------------- /target/test-classes/com/p2sdev/jsonToOrmMapper/AppTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/test-classes/com/p2sdev/jsonToOrmMapper/AppTest.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/convert/Table.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/convert/Table.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/mapper/JSONTo.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/mapper/JSONTo.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/mapper/orm/ORM.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/mapper/orm/ORM.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/convert/JSONTypes.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/convert/JSONTypes.class -------------------------------------------------------------------------------- /target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst: -------------------------------------------------------------------------------- 1 | C:\Users\stephane\Desktop\jsonToOrmMapper\src\test\java\com\p2sdev\jsonToOrmMapper\AppTest.java 2 | -------------------------------------------------------------------------------- /src/main/java/com/p2sdev/jsonToOrmMapper/enums/Cardinalities.java: -------------------------------------------------------------------------------- 1 | package com.p2sdev.jsonToOrmMapper.enums; 2 | public enum Cardinalities { 3 | ONETOONE, ONETOMANY, MANYTOMANY 4 | } 5 | -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/convert/MapJsonToTable.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/convert/MapJsonToTable.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/mapper/JSONTo$DATABASE.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/mapper/JSONTo$DATABASE.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/mapper/JSONTo$ORMModel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/mapper/JSONTo$ORMModel.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/mapper/JSONToConverter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/mapper/JSONToConverter.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/mapper/database/MySQL.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/mapper/database/MySQL.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/mapper/orm/DjangoModel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/mapper/orm/DjangoModel.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/mapper/orm/RoomModel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/mapper/orm/RoomModel.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/mapper/database/Database.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/mapper/database/Database.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/mapper/database/SqlLite.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/mapper/database/SqlLite.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/mapper/orm/DoctrineModel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/mapper/orm/DoctrineModel.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/convert/Table$Cardinalities.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/convert/Table$Cardinalities.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/convert/Table$TableBuilder.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/convert/Table$TableBuilder.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/mapper/JSONTo$SQLOperation.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/mapper/JSONTo$SQLOperation.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/mapper/database/PostgreSQL.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/mapper/database/PostgreSQL.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/mapper/orm/HibernateModel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/mapper/orm/HibernateModel.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/mapper/orm/DjangoModel$Model.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/mapper/orm/DjangoModel$Model.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/convert/MapJsonToTable$BuildTables.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/convert/MapJsonToTable$BuildTables.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/mapper/orm/HibernateModel$Template.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/mapper/orm/HibernateModel$Template.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/mapper/database/PostgreSQL$ColumnType.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/mapper/database/PostgreSQL$ColumnType.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/mapper/orm/HibernateModel$DataTypes.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/mapper/orm/HibernateModel$DataTypes.class -------------------------------------------------------------------------------- /src/main/java/com/p2sdev/jsonToOrmMapper/enums/JSONTypes.java: -------------------------------------------------------------------------------- 1 | package com.p2sdev.jsonToOrmMapper.enums; 2 | 3 | public enum JSONTypes { 4 | JSONOBJECT, JSONARRAY, INVALID_JSON, BOOLEAN, CHARACTER, INTEGER, DATE, DATETIME, STRING 5 | } -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/mapper/database/PostgreSQL$TableConstraint.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/mapper/database/PostgreSQL$TableConstraint.class -------------------------------------------------------------------------------- /target/classes/com/p2sdev/jsonToOrmMapper/mapper/database/PostgreSQL$ColumnConstraint.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PSteph/jsonToOrmMapper/HEAD/target/classes/com/p2sdev/jsonToOrmMapper/mapper/database/PostgreSQL$ColumnConstraint.class -------------------------------------------------------------------------------- /src/main/java/com/p2sdev/jsonToOrmMapper/mapper/orm/ORM.java: -------------------------------------------------------------------------------- 1 | package com.p2sdev.jsonToOrmMapper.mapper.orm; 2 | 3 | import java.util.List; 4 | 5 | import com.p2sdev.jsonToOrmMapper.convert.Table; 6 | 7 | public interface ORM { 8 | String getOrm(List tables); 9 | } 10 | -------------------------------------------------------------------------------- /target/surefire-reports/com.p2sdev.jsonToOrmMapper.AppTest.txt: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | Test set: com.p2sdev.jsonToOrmMapper.AppTest 3 | ------------------------------------------------------------------------------- 4 | Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec 5 | -------------------------------------------------------------------------------- /src/main/java/com/p2sdev/jsonToOrmMapper/mapper/orm/RoomModel.java: -------------------------------------------------------------------------------- 1 | package com.p2sdev.jsonToOrmMapper.mapper.orm; 2 | 3 | import java.util.List; 4 | 5 | import com.p2sdev.jsonToOrmMapper.convert.Table; 6 | 7 | public class RoomModel implements ORM { 8 | 9 | @Override 10 | public String getOrm(List
tables) { 11 | // TODO Auto-generated method stub 12 | return null; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/p2sdev/jsonToOrmMapper/mapper/orm/DoctrineModel.java: -------------------------------------------------------------------------------- 1 | package com.p2sdev.jsonToOrmMapper.mapper.orm; 2 | 3 | import java.util.List; 4 | 5 | import com.p2sdev.jsonToOrmMapper.convert.Table; 6 | 7 | public class DoctrineModel implements ORM { 8 | 9 | @Override 10 | public String getOrm(List
tables) { 11 | // TODO Auto-generated method stub 12 | return null; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/p2sdev/jsonToOrmMapper/mapper/JSONTo.java: -------------------------------------------------------------------------------- 1 | package com.p2sdev.jsonToOrmMapper.mapper; 2 | 3 | public interface JSONTo { 4 | enum ORMModel{ 5 | HIBERNATE, DOCTRINE, DJANGO_MODEL, ROOM 6 | } 7 | 8 | enum DATABASE { 9 | POSTGRES, MYSQL, SQLITE 10 | } 11 | 12 | enum SQLOperation{ 13 | CREATE, CREATE_INSERT 14 | } 15 | 16 | public String getORMModel(ORMModel model); 17 | 18 | public String getSQL(DATABASE db, SQLOperation operation); 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/p2sdev/jsonToOrmMapper/App.java: -------------------------------------------------------------------------------- 1 | package com.p2sdev.jsonToOrmMapper; 2 | 3 | import com.p2sdev.jsonToOrmMapper.mapper.JSONToConverter; 4 | import com.p2sdev.jsonToOrmMapper.mapper.JSONTo.ORMModel; 5 | 6 | /** 7 | * @author p2sdev.com 8 | * @version 2019-11-28 9 | * 10 | */ 11 | public class App { 12 | public static void main( String[] args ){ 13 | 14 | if(args.length != 0) 15 | CommandLine.run(args); 16 | 17 | else { 18 | String content = "{ 'person': {'f_name':'John', 'l_name':'Doe', 'p_id':1} }"; 19 | System.out.println(new JSONToConverter(content).getORMModel(ORMModel.DJANGO_MODEL)); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/p2sdev/jsonToOrmMapper/mapper/database/MySQL.java: -------------------------------------------------------------------------------- 1 | package com.p2sdev.jsonToOrmMapper.mapper.database; 2 | 3 | import java.util.List; 4 | 5 | import com.p2sdev.jsonToOrmMapper.convert.Table; 6 | 7 | public class MySQL implements Database { 8 | 9 | @Override 10 | public String getSQL(List
tables) { 11 | // TODO Auto-generated method stub 12 | return null; 13 | } 14 | 15 | @Override 16 | public String getSQLCreate(List
tables) { 17 | // TODO Auto-generated method stub 18 | return null; 19 | } 20 | 21 | @Override 22 | public String getSQLInsert(List
tables) { 23 | // TODO Auto-generated method stub 24 | return null; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/p2sdev/jsonToOrmMapper/mapper/database/SqlLite.java: -------------------------------------------------------------------------------- 1 | package com.p2sdev.jsonToOrmMapper.mapper.database; 2 | 3 | import java.util.List; 4 | 5 | import com.p2sdev.jsonToOrmMapper.convert.Table; 6 | 7 | public class SqlLite implements Database { 8 | 9 | @Override 10 | public String getSQL(List
tables) { 11 | // TODO Auto-generated method stub 12 | return null; 13 | } 14 | 15 | @Override 16 | public String getSQLCreate(List
tables) { 17 | // TODO Auto-generated method stub 18 | return null; 19 | } 20 | 21 | @Override 22 | public String getSQLInsert(List
tables) { 23 | // TODO Auto-generated method stub 24 | return null; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/p2sdev/jsonToOrmMapper/mapper/database/Database.java: -------------------------------------------------------------------------------- 1 | package com.p2sdev.jsonToOrmMapper.mapper.database; 2 | 3 | import java.util.List; 4 | 5 | import com.p2sdev.jsonToOrmMapper.convert.Table; 6 | 7 | public interface Database { 8 | 9 | /** 10 | * returns SQL Create and Insert 11 | * @param tables @Table 12 | * @return 13 | */ 14 | public abstract String getSQL(List
tables); 15 | 16 | /** 17 | * Returns a string representing the SQL Create statement for the tables in parameter 18 | * @param tables 19 | * @return 20 | */ 21 | public abstract String getSQLCreate(List
tables); 22 | 23 | /** 24 | * Returns a string representing the SQL Insert statement for the tables in parameter 25 | * @param tables 26 | * @return 27 | */ 28 | public abstract String getSQLInsert(List
tables); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JSON to ORM converter 2 | 3 | This tool will allow you to quickly prototype your ORM for Django and Hibernate from a JSON Object you will provide. It can also generate SQL create statements for Postgresql from the same JSON Object. 4 | 5 | For more information about the tool you can head to [this article on medium](https://medium.com/@piedjoustephane/prototyping-object-relational-mapping-orm-model-from-a-json-object-96eb0ea05ad4) that provides further information about the tool, the why it was build and the approach. 6 | You can also visit [Json to ORM converter tool online](https://www.p2sdev.com/projects/json-to-orm-converter) 7 | 8 | ## How to use it 9 | You can use the command line tool 10 | ``` 11 | git clone https://github.com/PSteph/jsonToOrmMapper.git && cd jsonToOrmMapper 12 | java -jar target/jsonToOrmMapper-0.0.1-SNAPSHOT.jar --filename /pathToFile.json --option [hibernate|django|postgres] 13 | ``` 14 | You can also include the jar in your project and call the converter this way for your desired ORM 15 | ``` 16 | new JSONToConverter(stringJsonContent).getORMModel(ORMModel.DJANGO_MODEL); 17 | // or 18 | new JSONToConverter(stringJsonContent).getORMModel(ORMModel.HIBERNATE); 19 | ``` 20 | or if you want to generate SQL 21 | ``` 22 | new JSONToConverter(stringJsonContent).getSQL(DATABASE.POSTGRES, SQLOperation.CREATE); 23 | ``` 24 | 25 | ## Example 26 | The following JSON 27 | 28 | ![json to be converted](https://github.com/PSteph/jsonToOrmMapper/blob/master/images/personJson.png ) 29 | 30 | is converted into the following Django Model 31 | 32 | ![corresponding Django Model](https://github.com/PSteph/jsonToOrmMapper/blob/master/images/person-djangoModel.png) 33 | -------------------------------------------------------------------------------- /target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst: -------------------------------------------------------------------------------- 1 | C:\Users\stephane\Desktop\jsonToOrmMapper\src\main\java\com\p2sdev\jsonToOrmMapper\mapper\orm\RoomModel.java 2 | C:\Users\stephane\Desktop\jsonToOrmMapper\src\main\java\com\p2sdev\jsonToOrmMapper\mapper\JSONToConverter.java 3 | C:\Users\stephane\Desktop\jsonToOrmMapper\src\main\java\com\p2sdev\jsonToOrmMapper\mapper\database\SqlLite.java 4 | C:\Users\stephane\Desktop\jsonToOrmMapper\src\main\java\com\p2sdev\jsonToOrmMapper\mapper\database\PostgreSQL.java 5 | C:\Users\stephane\Desktop\jsonToOrmMapper\src\main\java\com\p2sdev\jsonToOrmMapper\mapper\orm\DoctrineModel.java 6 | C:\Users\stephane\Desktop\jsonToOrmMapper\src\main\java\com\p2sdev\jsonToOrmMapper\mapper\database\Database.java 7 | C:\Users\stephane\Desktop\jsonToOrmMapper\src\main\java\com\p2sdev\jsonToOrmMapper\convert\Table.java 8 | C:\Users\stephane\Desktop\jsonToOrmMapper\src\main\java\com\p2sdev\jsonToOrmMapper\App.java 9 | C:\Users\stephane\Desktop\jsonToOrmMapper\src\main\java\com\p2sdev\jsonToOrmMapper\mapper\JSONTo.java 10 | C:\Users\stephane\Desktop\jsonToOrmMapper\src\main\java\com\p2sdev\jsonToOrmMapper\mapper\database\MySQL.java 11 | C:\Users\stephane\Desktop\jsonToOrmMapper\src\main\java\com\p2sdev\jsonToOrmMapper\convert\JSONTypes.java 12 | C:\Users\stephane\Desktop\jsonToOrmMapper\src\main\java\com\p2sdev\jsonToOrmMapper\convert\MapJsonToTable.java 13 | C:\Users\stephane\Desktop\jsonToOrmMapper\src\main\java\com\p2sdev\jsonToOrmMapper\mapper\orm\HibernateModel.java 14 | C:\Users\stephane\Desktop\jsonToOrmMapper\src\main\java\com\p2sdev\jsonToOrmMapper\CommandLine.java 15 | C:\Users\stephane\Desktop\jsonToOrmMapper\src\main\java\com\p2sdev\jsonToOrmMapper\mapper\orm\DjangoModel.java 16 | C:\Users\stephane\Desktop\jsonToOrmMapper\src\main\java\com\p2sdev\jsonToOrmMapper\mapper\orm\ORM.java 17 | -------------------------------------------------------------------------------- /src/main/java/com/p2sdev/jsonToOrmMapper/convert/MapJsonToTable.java: -------------------------------------------------------------------------------- 1 | package com.p2sdev.jsonToOrmMapper.convert; 2 | 3 | 4 | import com.p2sdev.jsonToOrmMapper.enums.JSONTypes; 5 | import static com.p2sdev.jsonToOrmMapper.convert.JsonTypes.getType; 6 | 7 | import java.util.ArrayList; 8 | import java.util.HashMap; 9 | import java.util.Iterator; 10 | import java.util.List; 11 | import java.util.Map; 12 | 13 | import org.json.JSONObject; 14 | 15 | /** 16 | * Mapping string content into a List
. 17 | * If the string content is a @JSONObject we can proceed with the mapping. 18 | * If the String content is a @JSONArray we throw and exception. 19 | * 20 | * @author p2sdev.com 21 | * @version 2019-11-27 22 | * 23 | */ 24 | public class MapJsonToTable { 25 | 26 | private JSONObject baseJsonObj; 27 | 28 | private Map keysToJSONTypes; 29 | 30 | private List
tables = new ArrayList<>(); 31 | 32 | /** 33 | * JSONObject throws a checked JSONException if there 34 | * is a syntax error in the source string or a duplicated key. 35 | * It will also throw an error if the source string is a json array 36 | */ 37 | protected MapJsonToTable(String content) { 38 | if(content == null) 39 | throw new IllegalArgumentException("JSON cannot be null"); 40 | 41 | baseJsonObj = new JSONObject(content); 42 | keysToJSONTypes = mapJsonKeyToJSONType(baseJsonObj); 43 | tables = new BuildTables(keysToJSONTypes, baseJsonObj).getTables(); 44 | } 45 | 46 | protected List
getTables(){ 47 | return tables; 48 | } 49 | 50 | /** 51 | * @param @JSONObject 52 | * @return hashMap mapping JSON key to a specific @JSONTypes 53 | */ 54 | private Map mapJsonKeyToJSONType(JSONObject json) { 55 | Map attrs = new HashMap<>(); 56 | Iterator keys = json.keys(); 57 | while(keys.hasNext()) { 58 | String key = keys.next(); 59 | Object value = json.get(key); 60 | attrs.put(key, getType(value)); 61 | } 62 | return attrs; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/com/p2sdev/jsonToOrmMapper/mapper/JSONToConverter.java: -------------------------------------------------------------------------------- 1 | package com.p2sdev.jsonToOrmMapper.mapper; 2 | 3 | import com.p2sdev.jsonToOrmMapper.convert.MapJsonToTable; 4 | import com.p2sdev.jsonToOrmMapper.mapper.database.Database; 5 | import com.p2sdev.jsonToOrmMapper.mapper.database.PostgreSQL; 6 | import com.p2sdev.jsonToOrmMapper.mapper.orm.DjangoModel; 7 | import com.p2sdev.jsonToOrmMapper.mapper.orm.HibernateModel; 8 | 9 | /** 10 | * Converting the JSON content into the desired ORM or SQL 11 | * @author p2sdev.com 12 | * @version 2019-11-27 13 | * 14 | */ 15 | public class JSONToConverter extends MapJsonToTable implements JSONTo{ 16 | 17 | /** 18 | * Mapping JSON Object to List
by calling @MapJsonToTable constructor 19 | * @param json : the string content representing the @JSONObject to be mapped 20 | */ 21 | public JSONToConverter(String json) { 22 | super(json); 23 | } 24 | 25 | @Override 26 | public String getORMModel(ORMModel model) { 27 | String orm = ""; 28 | switch(model) { 29 | case DJANGO_MODEL: 30 | orm = new DjangoModel().getOrm(getTables()); 31 | break; 32 | case DOCTRINE: 33 | break; 34 | case HIBERNATE: 35 | orm = new HibernateModel().getOrm(getTables()); 36 | break; 37 | case ROOM: 38 | break; 39 | default: 40 | break; 41 | 42 | } 43 | return orm; 44 | } 45 | 46 | @Override 47 | public String getSQL(DATABASE db, SQLOperation operation) { 48 | String sql = ""; 49 | switch(db) { 50 | case MYSQL: 51 | break; 52 | case POSTGRES: 53 | sql = getSql(new PostgreSQL(), operation); 54 | break; 55 | case SQLITE: 56 | break; 57 | } 58 | return sql; 59 | } 60 | 61 | private String getSql(Database postgreSQL, SQLOperation operation) { 62 | String sql = ""; 63 | switch(operation) { 64 | case CREATE: 65 | sql = postgreSQL.getSQLCreate(getTables()); 66 | break; 67 | case CREATE_INSERT: 68 | sql = postgreSQL.getSQLInsert(getTables()); 69 | break; 70 | } 71 | return sql; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst: -------------------------------------------------------------------------------- 1 | com\p2sdev\jsonToOrmMapper\mapper\orm\HibernateModel$Template.class 2 | com\p2sdev\jsonToOrmMapper\mapper\JSONTo$DATABASE.class 3 | com\p2sdev\jsonToOrmMapper\mapper\JSONTo$ORMModel.class 4 | com\p2sdev\jsonToOrmMapper\mapper\database\PostgreSQL$ColumnConstraint.class 5 | com\p2sdev\jsonToOrmMapper\App.class 6 | com\p2sdev\jsonToOrmMapper\CommandLine.class 7 | com\p2sdev\jsonToOrmMapper\convert\JSONTypes.class 8 | com\p2sdev\jsonToOrmMapper\mapper\JSONTo$SQLOperation.class 9 | com\p2sdev\jsonToOrmMapper\mapper\JSONToConverter.class 10 | com\p2sdev\jsonToOrmMapper\mapper\database\MySQL.class 11 | com\p2sdev\jsonToOrmMapper\convert\MapJsonToTable.class 12 | com\p2sdev\jsonToOrmMapper\mapper\orm\DjangoModel.class 13 | com\p2sdev\jsonToOrmMapper\mapper\orm\HibernateModel.class 14 | com\p2sdev\jsonToOrmMapper\mapper\JSONTo.class 15 | com\p2sdev\jsonToOrmMapper\mapper\orm\HibernateModel$DataTypes.class 16 | com\p2sdev\jsonToOrmMapper\convert\Table.class 17 | com\p2sdev\jsonToOrmMapper\convert\Table$TableBuilder.class 18 | com\p2sdev\jsonToOrmMapper\mapper\database\PostgreSQL.class 19 | com\p2sdev\jsonToOrmMapper\convert\Table$Cardinalities.class 20 | com\p2sdev\jsonToOrmMapper\mapper\database\SqlLite.class 21 | com\p2sdev\jsonToOrmMapper\mapper\JSONToConverter$1.class 22 | com\p2sdev\jsonToOrmMapper\convert\Table$1.class 23 | com\p2sdev\jsonToOrmMapper\mapper\orm\DjangoModel$1.class 24 | com\p2sdev\jsonToOrmMapper\mapper\database\PostgreSQL$1.class 25 | com\p2sdev\jsonToOrmMapper\mapper\database\Database.class 26 | com\p2sdev\jsonToOrmMapper\mapper\orm\HibernateModel$1.class 27 | com\p2sdev\jsonToOrmMapper\mapper\orm\DoctrineModel.class 28 | com\p2sdev\jsonToOrmMapper\mapper\orm\ORM.class 29 | com\p2sdev\jsonToOrmMapper\mapper\orm\DjangoModel$Model.class 30 | com\p2sdev\jsonToOrmMapper\mapper\database\PostgreSQL$TableConstraint.class 31 | com\p2sdev\jsonToOrmMapper\convert\MapJsonToTable$BuildTables.class 32 | com\p2sdev\jsonToOrmMapper\mapper\database\PostgreSQL$ColumnType.class 33 | com\p2sdev\jsonToOrmMapper\mapper\orm\RoomModel.class 34 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.p2sdev 6 | jsonToOrmMapper 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | jsonToOrmMapper 11 | http://maven.apache.org 12 | 13 | 14 | 15 | 16 | org.apache.maven.plugins 17 | maven-shade-plugin 18 | 3.2.1 19 | 20 | 21 | package 22 | 23 | shade 24 | 25 | 26 | 27 | 28 | com.p2sdev.jsonToOrmMapper.App 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | UTF-8 41 | 1.8 42 | 1.8 43 | 44 | 45 | 46 | 47 | 48 | org.json 49 | json 50 | 20190722 51 | 52 | 53 | 54 | 55 | org.junit.jupiter 56 | junit-jupiter-api 57 | 5.5.2 58 | test 59 | 60 | 61 | 62 | 63 | junit 64 | junit 65 | 4.13.1 66 | test 67 | 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /src/main/java/com/p2sdev/jsonToOrmMapper/convert/JsonTypes.java: -------------------------------------------------------------------------------- 1 | package com.p2sdev.jsonToOrmMapper.convert; 2 | 3 | import com.p2sdev.jsonToOrmMapper.enums.JSONTypes; 4 | 5 | import org.json.JSONArray; 6 | import org.json.JSONException; 7 | import org.json.JSONObject; 8 | 9 | import java.time.LocalDate; 10 | import java.time.format.DateTimeFormatter; 11 | 12 | public class JsonTypes { 13 | 14 | /** 15 | * Given the value of a json key identify the matching @JSONTypes 16 | * @param value : the value of a particular json key 17 | * @return @JSONTypes 18 | */ 19 | public static JSONTypes getType(Object value) { 20 | String data = value.toString(); 21 | if(data == null) { 22 | return JSONTypes.STRING; 23 | } 24 | 25 | // Check if it is a JSONObject 26 | try { 27 | new JSONObject(data); 28 | return JSONTypes.JSONOBJECT; 29 | }catch(JSONException e) {} 30 | 31 | // Then check if it is JSONArray 32 | try { 33 | new JSONArray(data); 34 | return JSONTypes.JSONARRAY; 35 | }catch(JSONException e) {} 36 | 37 | // Then check if it is a Boolean 38 | try { 39 | if(data.equals("true") || data.equals("false")) 40 | return JSONTypes.BOOLEAN; 41 | }catch(Exception e) {} 42 | 43 | // Then check if it is a Character 44 | try { 45 | if(data.length() == 1) { 46 | try { 47 | Integer.parseInt(data); 48 | return JSONTypes.INTEGER; 49 | }catch(Exception e) {} 50 | 51 | data.charAt(0); 52 | return JSONTypes.STRING; 53 | } 54 | }catch(Exception e) {} 55 | 56 | // Then check if it is an Integer/Number 57 | try { 58 | Integer.parseInt(data); 59 | return JSONTypes.INTEGER; 60 | }catch(Exception e) {} 61 | 62 | // Then check if it is a Date 63 | try { 64 | // make date to accept date and date/time format 65 | DateTimeFormatter f1 = DateTimeFormatter.ofPattern("MM-dd-yyyy"); 66 | // DateTimeFormatter f2 = DateTimeFormatter.ofPattern("MM/dd/yyyy"); 67 | LocalDate.parse(data, f1); 68 | return JSONTypes.DATE; 69 | }catch(Exception e) {} 70 | 71 | // Lastly if none of the above return String type 72 | return JSONTypes.STRING; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /dependency-reduced-pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | com.p2sdev 5 | jsonToOrmMapper 6 | jsonToOrmMapper 7 | 0.0.1-SNAPSHOT 8 | http://maven.apache.org 9 | 10 | 11 | 12 | maven-shade-plugin 13 | 3.2.1 14 | 15 | 16 | package 17 | 18 | shade 19 | 20 | 21 | 22 | 23 | com.p2sdev.jsonToOrmMapper.App 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | org.junit.jupiter 35 | junit-jupiter-api 36 | 5.5.2 37 | test 38 | 39 | 40 | apiguardian-api 41 | org.apiguardian 42 | 43 | 44 | opentest4j 45 | org.opentest4j 46 | 47 | 48 | junit-platform-commons 49 | org.junit.platform 50 | 51 | 52 | 53 | 54 | junit 55 | junit 56 | 4.12 57 | test 58 | 59 | 60 | hamcrest-core 61 | org.hamcrest 62 | 63 | 64 | 65 | 66 | 67 | 1.8 68 | UTF-8 69 | 1.8 70 | 71 | 72 | -------------------------------------------------------------------------------- /src/test/java/com/p2sdev/jsonToOrmMapper/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.p2sdev.jsonToOrmMapper; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertThrows; 4 | 5 | import org.json.JSONException; 6 | import org.junit.jupiter.api.DisplayName; 7 | 8 | import com.p2sdev.jsonToOrmMapper.mapper.JSONToConverter; 9 | import com.p2sdev.jsonToOrmMapper.mapper.JSONTo.ORMModel; 10 | 11 | import junit.framework.Test; 12 | import junit.framework.TestCase; 13 | import junit.framework.TestSuite; 14 | 15 | /** 16 | * Unit test for simple App. 17 | */ 18 | public class AppTest 19 | extends TestCase 20 | { 21 | /** 22 | * Create the test case 23 | * 24 | * @param testName name of the test case 25 | */ 26 | public AppTest( String testName ) 27 | { 28 | super( testName ); 29 | } 30 | 31 | /** 32 | * @return the suite of tests being tested 33 | */ 34 | public static Test suite() 35 | { 36 | return new TestSuite( AppTest.class ); 37 | } 38 | 39 | @DisplayName("If an empty string is passed in a JSONException should be thrown") 40 | public void testWhenEmptyContentThrowsException_ThenAssertionSucceeds() { 41 | assertThrows(JSONException.class, ()->{ 42 | new JSONToConverter("").getORMModel(ORMModel.DJANGO_MODEL); 43 | }); 44 | } 45 | 46 | @DisplayName("If a JSONArray is passed in a JSONException should be thrown") 47 | public void testWhenJSONArrayContentThrowsException_ThenAssertionSucceeds() { 48 | assertThrows(JSONException.class, ()->{ 49 | new JSONToConverter("[{ 'person': {'f_name':'John', 'l_name':'Doe'} }]").getORMModel(ORMModel.DJANGO_MODEL); 50 | }); 51 | } 52 | 53 | @DisplayName("If an invalid JSONObject is passed in a JSONException should be thrown") 54 | public void testWhenInvalidJSONObjectContentThrowsException_ThenAssertionSucceeds() { 55 | assertThrows(JSONException.class, ()->{ 56 | new JSONToConverter("{ 'person': 'f_name':'John', 'l_name':'Doe'} }").getORMModel(ORMModel.DJANGO_MODEL); 57 | }); 58 | } 59 | 60 | @DisplayName("Testing a valid JSONObject") 61 | public void testValidJSONObject() { 62 | String expected = "from django.db import modelsclass" + 63 | " Person(models.Model):" + 64 | "person_id = models.AutoField(primary_key=True)" + 65 | "f_name = models.CharField(max_length=100)" + 66 | "p_id = models.IntegerField()" + 67 | "l_name = models.CharField(max_length=100)"; 68 | 69 | expected = expected.replaceAll("\\n|\\r\\n|\\r|\\t", ""); 70 | 71 | String actual = new JSONToConverter("{'person': {'f_name':'John', 'l_name':'Doe', 'p_id':1} }").getORMModel(ORMModel.DJANGO_MODEL); 72 | actual = actual.replaceAll("\\n|\\r\\n|\\r|\\t", ""); 73 | 74 | assertEquals(expected, actual); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/com/p2sdev/jsonToOrmMapper/CommandLine.java: -------------------------------------------------------------------------------- 1 | package com.p2sdev.jsonToOrmMapper; 2 | 3 | import java.io.IOException; 4 | import java.nio.file.Files; 5 | import java.nio.file.Path; 6 | import java.nio.file.Paths; 7 | import java.util.Arrays; 8 | import java.util.List; 9 | 10 | import com.p2sdev.jsonToOrmMapper.mapper.JSONTo.ORMModel; 11 | import com.p2sdev.jsonToOrmMapper.mapper.JSONToConverter; 12 | import com.p2sdev.jsonToOrmMapper.mapper.JSONTo.DATABASE; 13 | import com.p2sdev.jsonToOrmMapper.mapper.JSONTo.SQLOperation; 14 | 15 | /** 16 | * java -jar /path/to/executable.jar --filename /path/to/file.json --option [hibernate|django|postgres] 17 | * @author p2sdev.com 18 | * 19 | */ 20 | public class CommandLine { 21 | 22 | private static List options = Arrays.asList("hibernate", "django", "postgres"); 23 | private static String cmdFormat = "java -jar /path/to/executable.jar --filename /path/to/file.json --option [hibernate|django|postgres]"; 24 | 25 | public static void run(String[] args) { 26 | if(args.length < 4) { 27 | System.out.println("[ERROR] You can use the command as follow"); 28 | System.out.println(cmdFormat); 29 | return; 30 | } 31 | 32 | List argsList = Arrays.asList(args); 33 | int indexOfFilename = argsList.indexOf("--filename"); 34 | int indexOfOption = argsList.indexOf("--option"); 35 | String filePath = null; 36 | String option = null; 37 | 38 | if(indexOfFilename == -1) { 39 | System.out.println("[ERROR] You need to specify the file location with --filename"); 40 | System.out.println(cmdFormat); 41 | return; 42 | } 43 | 44 | if(indexOfOption == -1) { 45 | System.out.println("[ERROR] You need to specify the option with --option"); 46 | System.out.println(cmdFormat); 47 | return; 48 | } 49 | 50 | if( !isFileExist(argsList.get(indexOfFilename+1)) ) { 51 | System.out.printf("[ERROR] File %s doesn't exist", (args[indexOfFilename+1])); 52 | return; 53 | } 54 | 55 | filePath = argsList.get(indexOfFilename+1); 56 | 57 | if( !isValidOption(argsList.get(indexOfOption+1)) ) { 58 | System.out.println("[ERROR] Wrong ORM. You should choose one of hibernate or django"); 59 | return; 60 | } 61 | 62 | option = argsList.get(indexOfOption+1); 63 | 64 | ORMModel modelToGenerate = null; 65 | DATABASE db = null; 66 | SQLOperation operation = SQLOperation.CREATE; 67 | String model = null; 68 | 69 | if(argsList.get(indexOfOption+1).equals("hibernate")) 70 | modelToGenerate = ORMModel.HIBERNATE; 71 | else if(argsList.get(indexOfOption+1).equals("django")) 72 | modelToGenerate = ORMModel.DJANGO_MODEL; 73 | else if(option.equals("postgres")) 74 | db = DATABASE.POSTGRES; 75 | 76 | 77 | if(modelToGenerate != null) 78 | model = new JSONToConverter(getContent(filePath)).getORMModel(modelToGenerate); 79 | 80 | if(db != null) 81 | model = new JSONToConverter(getContent(filePath)).getSQL(db, operation); 82 | 83 | System.out.println(model); 84 | 85 | } 86 | 87 | private static String getContent(String name) { 88 | name = name.replace("~", System.getProperty("user.home")); 89 | Path path = Paths.get(name); 90 | StringBuffer content = new StringBuffer(); 91 | 92 | try { 93 | List contentList = Files.readAllLines(path); 94 | contentList.forEach(data -> content.append(data)); 95 | }catch(IOException e) { 96 | e.printStackTrace(); 97 | } 98 | 99 | return content.toString(); 100 | } 101 | 102 | private static boolean isFileExist(String name) { 103 | name = name.replace("~", System.getProperty("user.home")); 104 | Path path = Paths.get(name); 105 | return Files.isRegularFile(path); 106 | } 107 | 108 | private static boolean isValidOption(String option) { 109 | return options.contains(option); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/main/java/com/p2sdev/jsonToOrmMapper/convert/BuildTables.java: -------------------------------------------------------------------------------- 1 | package com.p2sdev.jsonToOrmMapper.convert; 2 | 3 | import com.p2sdev.jsonToOrmMapper.enums.JSONTypes; 4 | 5 | import org.json.JSONException; 6 | import org.json.JSONObject; 7 | 8 | import java.util.*; 9 | import java.util.stream.Collectors; 10 | 11 | /** 12 | * 13 | * @author p2sdev.com 14 | * This class build the @Table representing each @JSONObject of 15 | * the source JSON data. The keys of the @JSONObject will be the attributes 16 | * of the @Table. 17 | * 18 | */ 19 | class BuildTables { 20 | 21 | private List
tables; 22 | 23 | public BuildTables(Map keysToJSONTypes, JSONObject baseJsonObj) { 24 | convertToTable(null, keysToJSONTypes, baseJsonObj); 25 | buildTables(baseJsonObj, keysToJSONTypes); 26 | } 27 | 28 | public List
getTables(){ 29 | return tables; 30 | } 31 | 32 | private List
buildTables(JSONObject json, Map jsonAttributes) { 33 | for(String key : jsonAttributes.keySet()) { 34 | if(jsonAttributes.get(key) == JSONTypes.JSONOBJECT) { 35 | JSONObject jsonSub = json.getJSONObject(key); 36 | buildTable(jsonSub, key); 37 | }else if(jsonAttributes.get(key) == JSONTypes.JSONARRAY) { 38 | // get the first element and ignore the rest 39 | // Only process if array element is JSONObject 40 | try { 41 | JSONObject jsonSub = json.getJSONArray(key).getJSONObject(0); 42 | buildTable(jsonSub, key); 43 | }catch(JSONException e) { 44 | // JSONArray doesn't hold JSONObjects 45 | System.out.println("Error: JSONArray doesn't hold JSONObjects"); 46 | } 47 | 48 | } 49 | } 50 | return tables; 51 | } 52 | 53 | private void buildTable(JSONObject json, String key) { 54 | Map attributes = new HashMap<>(); 55 | 56 | Iterator keys = json.keys(); 57 | 58 | while(keys.hasNext()) { 59 | String aKey = keys.next(); 60 | Object value = json.get(aKey); 61 | attributes.put(aKey, JsonTypes.getType(value)); 62 | } 63 | convertToTable(key, attributes, json); 64 | buildTables(json, attributes); 65 | } 66 | 67 | /** 68 | * Check if the data object can be converted into a table and convert if convertible 69 | * To be converted into a table the data Object should have one or more @DataTypes 70 | * @param tableName : Used as table name. 71 | * @param attributes : a mapping of JSON keys to @JSONTypes 72 | * @param json : a json Object from the source data that will be converted into a @Table 73 | */ 74 | private void convertToTable(String tableName, Map attributes, JSONObject json) { 75 | 76 | Collection currentDataTypes = attributes.values(); 77 | 78 | List dataTypes = Arrays.asList(JSONTypes.BOOLEAN, 79 | JSONTypes.CHARACTER, JSONTypes.INTEGER, JSONTypes.DATE, JSONTypes.STRING); 80 | 81 | 82 | List intersection = currentDataTypes.stream() 83 | .distinct() 84 | .filter(dataTypes::contains) 85 | .collect(Collectors.toList()); 86 | 87 | if((tableName == null || tableName.isEmpty()) && intersection.size()==0 ) { 88 | // the jsonObject cannot be converted into a table 89 | return; 90 | } 91 | 92 | Table table; 93 | 94 | if((tableName == null || tableName.isEmpty()) && intersection.size() != 0) { 95 | table = new Table.TableBuilder() 96 | .setAttributes(attributes) 97 | .setRawJson(json) 98 | .build(); 99 | } 100 | 101 | else { 102 | table = new Table.TableBuilder() 103 | .setAttributes(attributes) 104 | .setTableName(tableName) 105 | .setRawJson(json) 106 | .build(); 107 | } 108 | 109 | if(table != null) { 110 | if(tables == null) { 111 | tables = new ArrayList<>(); 112 | } 113 | tables.add(table); 114 | } 115 | } 116 | 117 | } -------------------------------------------------------------------------------- /src/main/java/com/p2sdev/jsonToOrmMapper/mapper/orm/DjangoModel.java: -------------------------------------------------------------------------------- 1 | package com.p2sdev.jsonToOrmMapper.mapper.orm; 2 | 3 | import java.util.HashMap; 4 | import java.util.List; 5 | import java.util.Map; 6 | 7 | import com.p2sdev.jsonToOrmMapper.enums.JSONTypes; 8 | import com.p2sdev.jsonToOrmMapper.convert.Table; 9 | 10 | /** 11 | * Build Django model that correspond to the List
12 | * @author p2sdev.com 13 | * @version 2019-11-27 14 | * 15 | */ 16 | public class DjangoModel implements ORM{ 17 | 18 | private StringBuilder orm = new StringBuilder(""); 19 | private Map modelClasses = new HashMap<>(); 20 | private List
tables; 21 | 22 | private enum Model{ 23 | IMPORT("from django.db import models"), CLASS_DEF("class {name}(models.Model):"), 24 | PRIMARY_KEY(" = models.AutoField(primary_key=True)"), MANYTOONE(" = models.ForeignKey({to}, on_delete=models.CASCADE)"), 25 | MANYTOMANY(" = models.ManyToManyField({to})"), 26 | ONETOONE(" = models.OneToOneField({to},on_delete=models.CASCADE)"), 27 | VARCHAR_30("models.CharField(max_length=30)"),VARCHAR_100(" = models.CharField(max_length=100)"), 28 | INTEGER(" = models.IntegerField()"),BOOLEAN(" = models.BooleanField()"), DATE(" = models.DateField()"), 29 | DATETIME(" = models.DateTimeField()"), EMAIL(" = models.EmailField(max_length=254)"); 30 | 31 | private String value; 32 | private Model(String value) { 33 | this.value = value; 34 | } 35 | 36 | public String getValue() { 37 | return value; 38 | } 39 | } 40 | 41 | @Override 42 | public String getOrm(List
tables) { 43 | this.tables = tables; 44 | this.initOrm() 45 | .buildClasses() 46 | .setRelationships(); 47 | 48 | for(Table table : tables) { 49 | orm.append(modelClasses.get(table.getTableName()).toString()).append("\n"); 50 | } 51 | 52 | return orm.toString(); 53 | } 54 | 55 | private DjangoModel initOrm() { 56 | orm.append(Model.IMPORT.getValue()) 57 | .append("\n\n"); 58 | return this; 59 | } 60 | 61 | private DjangoModel buildClasses() { 62 | tables.forEach(table ->{ 63 | buildClass(table); 64 | }); 65 | return this; 66 | } 67 | 68 | private void buildClass(Table table) { 69 | StringBuilder modelClass = new StringBuilder(""); 70 | 71 | String classDef = Model.CLASS_DEF.getValue(); 72 | String nameUppercase = table.getTableName().substring(0, 1).toUpperCase() + table.getTableName().substring(1); 73 | classDef = classDef.replace("{name}", nameUppercase); 74 | 75 | modelClass.append(classDef).append("\n\t"); 76 | 77 | // set pk 78 | modelClass.append(table.getTableName()+"_id") 79 | .append(Model.PRIMARY_KEY.getValue()) 80 | .append("\n\t"); 81 | 82 | Map tableColumnsDef = table.getTableColumnsDef(); 83 | 84 | for(String key : tableColumnsDef.keySet()) { 85 | switch(tableColumnsDef.get(key)) { 86 | case BOOLEAN: 87 | modelClass.append(key) 88 | .append(Model.BOOLEAN.getValue()) 89 | .append("\n\t"); 90 | break; 91 | case CHARACTER: 92 | break; 93 | case DATE: 94 | break; 95 | case INVALID_JSON: 96 | break; 97 | case INTEGER: 98 | modelClass.append(key) 99 | .append(Model.INTEGER.getValue()) 100 | .append("\n\t"); 101 | break; 102 | case JSONARRAY: 103 | break; 104 | case JSONOBJECT: 105 | break; 106 | case STRING: 107 | modelClass.append(key) 108 | .append(Model.VARCHAR_100.getValue()) 109 | .append("\n\t"); 110 | break; 111 | default: 112 | break; 113 | 114 | } 115 | } 116 | 117 | modelClasses.put(table.getTableName(), modelClass); 118 | 119 | } 120 | 121 | private DjangoModel setRelationships() { 122 | for(Table table : tables) { 123 | for(String tableName : table.getRelationships().keySet()) { 124 | String toUppercase = table.getTableName().substring(0, 1).toUpperCase() + table.getTableName().substring(1); 125 | 126 | StringBuilder modelClass = modelClasses.get(tableName); 127 | switch(table.getRelationships().get(tableName)) { 128 | case MANYTOMANY: 129 | 130 | break; 131 | case ONETOMANY: 132 | String manytoone = Model.MANYTOONE.getValue(); 133 | manytoone = manytoone.replace("{to}", toUppercase); 134 | modelClass.append(table.getTableName()+"_id") 135 | .append(manytoone) 136 | .append("\n\t"); 137 | 138 | break; 139 | case ONETOONE: 140 | String onetoone = Model.ONETOONE.getValue(); 141 | onetoone = onetoone.replace("{to}", toUppercase); 142 | modelClass.append(table.getTableName()+"_id") 143 | .append(onetoone) 144 | .append("\n\t"); 145 | 146 | break; 147 | default: 148 | break; 149 | } 150 | 151 | } 152 | 153 | } 154 | return this; 155 | } 156 | 157 | } 158 | -------------------------------------------------------------------------------- /src/main/java/com/p2sdev/jsonToOrmMapper/convert/Table.java: -------------------------------------------------------------------------------- 1 | package com.p2sdev.jsonToOrmMapper.convert; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | import java.util.TreeMap; 6 | 7 | import com.p2sdev.jsonToOrmMapper.enums.Cardinalities; 8 | import com.p2sdev.jsonToOrmMapper.enums.JSONTypes; 9 | import org.json.JSONException; 10 | import org.json.JSONObject; 11 | 12 | public class Table { 13 | 14 | private String tableName; 15 | private Map relationships = new HashMap<>(); 16 | private Map tableColumnsDef; 17 | private JSONObject json; 18 | 19 | private Table(Map attrs, String name, JSONObject json) { 20 | this.json = json; 21 | tableColumnsDef = formatColumnName(checkJSONArray(attrs)); 22 | tableName = formatName(name); 23 | relationships = buildRelationShips(); 24 | } 25 | 26 | /** 27 | * Constructor for tables that do not have a table name 28 | * @param attrs 29 | * @param json 30 | */ 31 | private Table(Map attrs, JSONObject json) { 32 | this(attrs, "BaseTable", json); 33 | } 34 | 35 | /** 36 | * Transform JSONArray not containing JSONObjects into String type 37 | */ 38 | private Map checkJSONArray(Map attrs) { 39 | for(String key : attrs.keySet()) { 40 | if(attrs.get(key) == JSONTypes.JSONARRAY) { 41 | // Only process if array element is JSONObject 42 | try { 43 | json.getJSONArray(key).getJSONObject(0); 44 | }catch(JSONException e) { 45 | attrs.put(key, JSONTypes.STRING); 46 | } 47 | } 48 | } 49 | 50 | return attrs; 51 | } 52 | 53 | /** 54 | * Build relationships between this table and other tables 55 | * Given a table name Person with an attribute object {'address':{'post code':'', 'city':'London'}} 56 | * We have a ONETOONE relationship between Person and Address 57 | * Given a table name Person with an attribute object {'Visited Cities':[{'Name':'London', 'Country':'UK'}]} 58 | * We have a ONETOMANY relationship between Person and Visited cities table 59 | */ 60 | private Map buildRelationShips() { 61 | Map relationships = new HashMap<>(); 62 | for(String key : tableColumnsDef.keySet()) { 63 | if(tableColumnsDef.get(key) == JSONTypes.JSONOBJECT) 64 | relationships.put(key, Cardinalities.ONETOONE); 65 | else if(tableColumnsDef.get(key) == JSONTypes.JSONARRAY) 66 | relationships.put(key, Cardinalities.ONETOMANY); 67 | } 68 | return relationships; 69 | } 70 | 71 | /** 72 | * Format the name to return a valid SQL table / column name 73 | * Should have 122 (128-6 to add fk prefix to table if needed) characters all lower case. Separates words with underscore (_) 74 | * @return 75 | */ 76 | private String formatName(String name) { 77 | String[] names = name.split(" "); 78 | String theName = String.join("_", names); 79 | theName = theName.toLowerCase(); 80 | return theName.length() < 128 ? theName : theName.substring(0, 128); 81 | } 82 | 83 | /** 84 | * Format the table column to return a valid SQL table column 85 | * @return @tableColumnsDef 86 | */ 87 | private Map formatColumnName(Map attrs) { 88 | Map attributes = new HashMap<>(); 89 | for(String key : attrs.keySet()) { 90 | attributes.put(formatName(key), attrs.get(key)); 91 | } 92 | return attributes; 93 | } 94 | 95 | public String getTableName() { 96 | return tableName; 97 | } 98 | 99 | public Map getRelationships() { 100 | return new HashMap(relationships); 101 | } 102 | 103 | public Map getTableColumnsDef() { 104 | return new HashMap(tableColumnsDef); 105 | } 106 | 107 | public JSONObject getJson() { 108 | return new JSONObject(json); 109 | } 110 | 111 | public static class TableBuilder{ 112 | 113 | private Map attributes = new TreeMap<>(); 114 | private String name; 115 | private JSONObject json; 116 | 117 | public TableBuilder setAttributes(Map attrs){ 118 | if(attrs == null || attrs.isEmpty()) 119 | throw new IllegalArgumentException("Table attributes cannot be null or empty"); 120 | attributes = attrs; 121 | return this; 122 | } 123 | 124 | public TableBuilder setTableName(String name) { 125 | if(name == null || name.isEmpty()) 126 | throw new IllegalArgumentException("Table name cannot be null empty"); 127 | this.name = name; 128 | return this; 129 | } 130 | 131 | public TableBuilder setRawJson(JSONObject json) { 132 | if(json == null || json.isEmpty()) 133 | throw new IllegalArgumentException("json cannot be null or empty"); 134 | this.json = json; 135 | return this; 136 | } 137 | 138 | public Table build() { 139 | 140 | if(attributes == null || attributes.isEmpty()) 141 | throw new IllegalArgumentException("Table attributes cannot be null or empty. Set with setAttributes"); 142 | 143 | if(json == null || json.isEmpty()) 144 | throw new IllegalArgumentException("json cannot be null or empty. Set with setRawJson"); 145 | 146 | if(name== null || name.isEmpty()) 147 | return new Table(attributes, json); 148 | 149 | return new Table(attributes, name, json); 150 | } 151 | } 152 | 153 | @Override 154 | public String toString() { 155 | return "Table [tableName=" + tableName + ", relationships=" + relationships + ", tableColumnsDef=" 156 | + tableColumnsDef + "]"; 157 | } 158 | } 159 | -------------------------------------------------------------------------------- /target/surefire-reports/TEST-com.p2sdev.jsonToOrmMapper.AppTest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /src/main/java/com/p2sdev/jsonToOrmMapper/mapper/database/PostgreSQL.java: -------------------------------------------------------------------------------- 1 | package com.p2sdev.jsonToOrmMapper.mapper.database; 2 | 3 | import java.util.HashMap; 4 | import java.util.List; 5 | import java.util.Map; 6 | 7 | import com.p2sdev.jsonToOrmMapper.enums.JSONTypes; 8 | import com.p2sdev.jsonToOrmMapper.convert.Table; 9 | 10 | /** 11 | * Class to generate Create and insert statements. 12 | * By default all classes will have id as their primary key 13 | * 14 | * @author p2sdev.com 15 | * @version 2019-11-27 16 | * 17 | */ 18 | public class PostgreSQL implements Database { 19 | 20 | public enum ColumnType { 21 | VARCHAR_40("varchar(40)"), VARCHAR_100("varchar(100)"), INTEGER("integer"), DATE("date"), 22 | TIME("time"), BOOLEAN("bool"), FLOAT8("float8"); 23 | private String value; 24 | private ColumnType(String value) { 25 | this.value = value; 26 | } 27 | 28 | public String getValue() { 29 | return value; 30 | } 31 | } 32 | 33 | public enum ColumnConstraint { 34 | PRIMARY_KEY("PRIMARY KEY"), IDENTITY("GENERATED BY DEFAULT AS IDENTITY"), NOT_NULL("NOT NULL"), 35 | NULL("NULL"), UNIQUE("UNIQUE"), REFERENCES("REFERENCES"); 36 | private String value; 37 | private ColumnConstraint(String value) { 38 | this.value = value; 39 | } 40 | 41 | public String getValue() { 42 | return value; 43 | } 44 | } 45 | 46 | public enum TableConstraint { 47 | PRIMARY_KEY("PRIMARY KEY"), FOREIGN_KEY("FOREIGN KEY"), REFERENCES("REFERENCES"), 48 | CONSTRAINT("CONSTRAINT"), DELETE_CASCADE("ON DELETE CASCADE"); 49 | private String value; 50 | private TableConstraint(String value) { 51 | this.value = value; 52 | } 53 | 54 | public String getValue() { 55 | return value; 56 | } 57 | } 58 | 59 | @Override 60 | public String getSQL(List
tables) { 61 | // TODO Auto-generated method stub 62 | return null; 63 | } 64 | 65 | @Override 66 | public String getSQLCreate(List
tables) { 67 | 68 | Map tablesSQLMap = new HashMap<>(); 69 | 70 | for(Table table : tables) { 71 | StringBuilder tableDefinition = new StringBuilder(""); 72 | tableDefinition.append(getCreateStatementOpen(table.getTableName())) 73 | .append(getColumns(table)) 74 | .append(getCreateStatementClose()); 75 | tablesSQLMap.put(table.getTableName(), tableDefinition); 76 | } 77 | 78 | StringBuilder sql = new StringBuilder(); 79 | // setting tables constraints 80 | for(Table table : tables) { 81 | setRelationships(tablesSQLMap, table); 82 | sql.append(tablesSQLMap.get(table.getTableName()).toString()) 83 | .append("\n"); 84 | } 85 | 86 | return sql.toString(); 87 | } 88 | 89 | @Override 90 | public String getSQLInsert(List
tables) { 91 | // TODO Auto-generated method stub 92 | return null; 93 | } 94 | 95 | /** 96 | * Explicitly called by getSQLCreate before it returns to set 97 | * tables constraints 98 | * @param sql 99 | */ 100 | private void setRelationships(Map tablesSQLMap, 101 | Table table) { 102 | for(String tableName : table.getRelationships().keySet()) { 103 | StringBuilder createStatement = tablesSQLMap.get(tableName); 104 | int length; 105 | StringBuilder constraint; 106 | 107 | switch(table.getRelationships().get(tableName)) { 108 | case MANYTOMANY: 109 | 110 | break; 111 | case ONETOMANY: 112 | constraint = new StringBuilder(); 113 | constraint 114 | .append(",\n\t"+table.getTableName()+"_id ") 115 | .append(ColumnType.INTEGER.getValue()) 116 | .append(" "+ColumnConstraint.NOT_NULL.getValue()+", \n\t"); 117 | 118 | constraint 119 | .append(TableConstraint.FOREIGN_KEY.getValue()) 120 | .append(" ("+tableName+"_id)") 121 | .append(" "+TableConstraint.REFERENCES) 122 | .append(" "+table.getTableName()+"("+table.getTableName()+"_id)") 123 | .append(" "+TableConstraint.DELETE_CASCADE.getValue()); 124 | 125 | length = createStatement.length(); 126 | createStatement.replace(length-6, length-5, constraint.toString()); 127 | 128 | break; 129 | case ONETOONE: 130 | constraint = new StringBuilder(); 131 | constraint 132 | .append(",\n\t"+table.getTableName()+"_id ") 133 | .append(ColumnType.INTEGER.getValue()) 134 | .append(" "+ColumnConstraint.NOT_NULL.getValue()+", \n\t"); 135 | 136 | constraint.append(TableConstraint.CONSTRAINT.getValue()+" ") 137 | .append("fk_"+table.getTableName()+"_id"+" ") 138 | .append(TableConstraint.FOREIGN_KEY.getValue()) 139 | .append(" ("+table.getTableName()+"_id)") 140 | .append(" "+TableConstraint.REFERENCES) 141 | .append(" "+table.getTableName()+"("+table.getTableName()+"_id)") 142 | .append(" "+TableConstraint.DELETE_CASCADE.getValue()); 143 | 144 | length = createStatement.length(); 145 | createStatement.replace(length-6, length-5, constraint.toString()); 146 | 147 | break; 148 | default: 149 | break; 150 | } 151 | 152 | } 153 | 154 | } 155 | 156 | private String getCreateStatementOpen(String t_name) { 157 | StringBuilder opening = new StringBuilder("CREATE TABLE "); 158 | opening.append(t_name).append(" (\n\t"); 159 | return opening.toString(); 160 | } 161 | 162 | private String getCreateStatementClose() { 163 | return ");\n"; 164 | } 165 | 166 | private String primaryKey(String t_name) { 167 | StringBuilder pkeyColumn = new StringBuilder(t_name); 168 | pkeyColumn.append("_id") 169 | .append(" ") 170 | .append(ColumnType.INTEGER.getValue()) 171 | .append(" ") 172 | .append(ColumnConstraint.PRIMARY_KEY.getValue()) 173 | .append(" ") 174 | .append(ColumnConstraint.IDENTITY.getValue()) 175 | .append(", \n\t"); 176 | 177 | return pkeyColumn.toString(); 178 | } 179 | 180 | private String getColumns(Table table) { 181 | 182 | StringBuilder columns = new StringBuilder(""); 183 | columns.append(primaryKey(table.getTableName())); 184 | Map columnDef = table.getTableColumnsDef(); 185 | 186 | for(String key : columnDef.keySet()) { 187 | 188 | // the column looks like this: "[column_name] [column_type] [column_constraint],\n" 189 | 190 | switch(columnDef.get(key)) { 191 | case STRING: 192 | columns.append(key).append(" ") 193 | .append(ColumnType.VARCHAR_100.getValue()) 194 | .append(",\n\t"); 195 | break; 196 | case INTEGER: 197 | columns.append(key).append(" ") 198 | .append(ColumnType.INTEGER.getValue()) 199 | .append(",\n\t"); 200 | break; 201 | case BOOLEAN: 202 | columns.append(key).append(" ") 203 | .append(ColumnType.BOOLEAN.getValue()) 204 | .append(",\n\t"); 205 | break; 206 | case CHARACTER: 207 | break; 208 | case DATE: 209 | columns.append(key).append(" ") 210 | .append(ColumnType.DATE.getValue()) 211 | .append(",\n\t"); 212 | break; 213 | case JSONARRAY: 214 | // use setRelationships() after all tables are created 215 | break; 216 | case JSONOBJECT: 217 | // use setRelationships() after all tables are created 218 | break; 219 | default: 220 | break; 221 | } 222 | 223 | } 224 | 225 | // removing last comma 226 | int length = columns.length(); 227 | columns.replace(length-3, length-2, ""); 228 | columns.append("\n"); 229 | return columns.toString(); 230 | } 231 | 232 | } 233 | 234 | -------------------------------------------------------------------------------- /src/main/java/com/p2sdev/jsonToOrmMapper/mapper/orm/HibernateModel.java: -------------------------------------------------------------------------------- 1 | package com.p2sdev.jsonToOrmMapper.mapper.orm; 2 | 3 | import java.util.Arrays; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | import com.p2sdev.jsonToOrmMapper.enums.JSONTypes; 9 | import com.p2sdev.jsonToOrmMapper.convert.Table; 10 | 11 | /** 12 | * This class follows JPA 2.1 specifications. Returned model 13 | * should therefore be compatible with ORMs implementing JPA 2.1 14 | * specifications, Hibernate included. 15 | * 16 | * @author p2sdev.com 17 | * @version 2019-11-27 18 | * 19 | */ 20 | public class HibernateModel implements ORM { 21 | 22 | private StringBuilder orm = new StringBuilder(""); 23 | private Map modelClasses = new HashMap<>(); 24 | private List
tables; 25 | private Template template = new Template(); 26 | private Map attributeConstraints = new HashMap<>(); 27 | private Map getterSetterConstraints = new HashMap<>(); 28 | 29 | private enum DataTypes{ 30 | STRING("String"), LONG("Long"), DATE("Date"), TIME("Time"), TIMESTAMP("Timestamp"), BOOLEAN("boolean"); 31 | 32 | private String value; 33 | private DataTypes(String value) { 34 | this.value = value; 35 | } 36 | 37 | public String getValue() { 38 | return this.value; 39 | } 40 | } 41 | 42 | @Override 43 | public String getOrm(List
tables) { 44 | this.tables = tables; 45 | this.initOrm() 46 | .buildClasses() 47 | .setRelationships(); 48 | 49 | for(Table table : tables) { 50 | orm.append(modelClasses.get(table.getTableName()).toString()).append("\n\n"); 51 | } 52 | return orm.toString(); 53 | } 54 | 55 | private HibernateModel initOrm() { 56 | return this; 57 | } 58 | 59 | private HibernateModel buildClasses() { 60 | tables.forEach(table ->{ 61 | buildClass(table); 62 | }); 63 | return this; 64 | } 65 | 66 | private void buildClass(Table table) { 67 | String entity = buildBaseClass(table.getTableName()); 68 | entity = buildPrimaryKey(entity, table.getTableName()); 69 | String[] attrGetterSetter = buildAttributes(table); 70 | entity = entity.replace("attr_list", attrGetterSetter[0]); 71 | entity = entity.replace("getters_setters", attrGetterSetter[1]); 72 | StringBuilder value = new StringBuilder(entity); 73 | modelClasses.put(table.getTableName(), value); 74 | } 75 | 76 | private String buildBaseClass(String tableName) { 77 | String entity = template.getBaseTemplate(); 78 | entity = entity.replaceAll("class_name", formatClassName(tableName)); 79 | return entity.replaceAll("table_name", tableName); 80 | } 81 | 82 | /** 83 | * The primary key is a concatenation of the table name and "Id" 84 | * @param entity 85 | * @return 86 | */ 87 | private String buildPrimaryKey(String entity, String tableName) { 88 | String attr = formatAttrName(tableName); 89 | attr = attr+"Id"; 90 | entity = entity.replaceAll("pk_name", attr); 91 | entity = entity.replaceAll("db_name_pk", tableName+"_id"); 92 | return entity; 93 | } 94 | 95 | private String[] buildAttributes(Table table) { 96 | Map columnsDef = table.getTableColumnsDef(); 97 | StringBuilder attrsBuilder = new StringBuilder(); 98 | StringBuilder getterSettersBuilder = new StringBuilder(); 99 | String attr = null; 100 | String getterSetter = null; 101 | for(String key : columnsDef.keySet()) { 102 | switch(columnsDef.get(key)) { 103 | case BOOLEAN: 104 | attr = template.getAttributeTemplate(); 105 | attr = attr.replaceAll("attr_type", DataTypes.BOOLEAN.getValue()); 106 | attr = attr.replaceAll("attr_name", formatAttrName(key)); 107 | attr = attr.replaceAll("db_att_name", key); 108 | 109 | getterSetter = template.getGetterSetterTemplate(); 110 | getterSetter = getterSetter.replaceAll("attr_type", DataTypes.BOOLEAN.getValue()); 111 | getterSetter = getterSetter.replaceAll("attr_name", formatAttrName(key)); 112 | getterSetter = getterSetter.replaceAll("setter_name", formatSetterName(key)); 113 | getterSetter = getterSetter.replaceAll("getter_name", formatGetterName(key)); 114 | 115 | break; 116 | case CHARACTER: 117 | break; 118 | case DATE: 119 | attr = template.getAttributeTemplate(); 120 | attr = attr.replaceAll("attr_type", DataTypes.DATE.getValue()); 121 | attr = attr.replaceAll("attr_name", formatAttrName(key)); 122 | attr = attr.replaceAll("db_att_name", key); 123 | 124 | getterSetter = template.getGetterSetterTemplate(); 125 | getterSetter = getterSetter.replaceAll("attr_type", DataTypes.DATE.getValue()); 126 | getterSetter = getterSetter.replaceAll("attr_name", formatAttrName(key)); 127 | getterSetter = getterSetter.replaceAll("setter_name", formatSetterName(key)); 128 | getterSetter = getterSetter.replaceAll("getter_name", formatGetterName(key)); 129 | break; 130 | case INVALID_JSON: 131 | break; 132 | case INTEGER: 133 | attr = template.getAttributeTemplate(); 134 | attr = attr.replaceAll("attr_type", DataTypes.LONG.getValue()); 135 | attr = attr.replaceAll("attr_name", formatAttrName(key)); 136 | attr = attr.replaceAll("db_att_name", key); 137 | 138 | getterSetter = template.getGetterSetterTemplate(); 139 | getterSetter = getterSetter.replaceAll("attr_type", DataTypes.LONG.getValue()); 140 | getterSetter = getterSetter.replaceAll("attr_name", formatAttrName(key)); 141 | getterSetter = getterSetter.replaceAll("setter_name", formatSetterName(key)); 142 | getterSetter = getterSetter.replaceAll("getter_name", formatGetterName(key)); 143 | break; 144 | case JSONARRAY: 145 | break; 146 | case JSONOBJECT: 147 | break; 148 | case STRING: 149 | attr = template.getAttributeTemplate(); 150 | attr = attr.replaceAll("attr_type", DataTypes.STRING.getValue()); 151 | attr = attr.replaceAll("attr_name", formatAttrName(key)); 152 | attr = attr.replaceAll("db_att_name", key); 153 | 154 | getterSetter = template.getGetterSetterTemplate(); 155 | getterSetter = getterSetter.replaceAll("attr_type", DataTypes.STRING.getValue()); 156 | getterSetter = getterSetter.replaceAll("attr_name", formatAttrName(key)); 157 | getterSetter = getterSetter.replaceAll("setter_name", formatSetterName(key)); 158 | getterSetter = getterSetter.replaceAll("getter_name", formatGetterName(key)); 159 | break; 160 | default: 161 | break; 162 | 163 | } 164 | 165 | if(attr != null) 166 | attrsBuilder.append(attr); 167 | if(getterSetter != null) 168 | getterSettersBuilder.append(getterSetter); 169 | } 170 | 171 | String[] data = {attrsBuilder.toString(), getterSettersBuilder.toString()}; 172 | return data; 173 | } 174 | 175 | /** 176 | * @Table getTableName() method returns a name in lower case. 177 | * Words forming the name are separated by underscore. We want 178 | * to format the name for Java. Example: visited_cities to VisitedCities 179 | * @return 180 | */ 181 | private String formatClassName(String tableNameToFormat) { 182 | StringBuilder formatted = new StringBuilder(); 183 | String[] splitedName = tableNameToFormat.split("_"); 184 | Arrays.asList(splitedName).forEach( name -> { 185 | formatted.append(name.toString().substring(0, 1).toUpperCase()+name.toString().substring(1)); 186 | }); 187 | 188 | return formatted.toString(); 189 | } 190 | 191 | /** 192 | * The Java attribute name should be camelCase 193 | * @param attr 194 | * @return 195 | */ 196 | private String formatAttrName(String attr) { 197 | StringBuilder formatted = new StringBuilder(); 198 | String[] splitedName = attr.split("_"); 199 | for(int i=0; i