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 <Table > tables ;
22+
23+ public BuildTables (Map <String , JSONTypes > keysToJSONTypes , JSONObject baseJsonObj ) {
24+ convertToTable (null , keysToJSONTypes , baseJsonObj );
25+ buildTables (baseJsonObj , keysToJSONTypes );
26+ }
27+
28+ public List <Table > getTables (){
29+ return tables ;
30+ }
31+
32+ private List <Table > buildTables (JSONObject json , Map <String , JSONTypes > 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 <String , JSONTypes > attributes = new HashMap <>();
55+
56+ Iterator <String > 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 <String , JSONTypes > attributes , JSONObject json ) {
75+
76+ Collection <JSONTypes > currentDataTypes = attributes .values ();
77+
78+ List <JSONTypes > dataTypes = Arrays .asList (JSONTypes .BOOLEAN ,
79+ JSONTypes .CHARACTER , JSONTypes .INTEGER , JSONTypes .DATE , JSONTypes .STRING );
80+
81+
82+ List <JSONTypes > 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+ }
0 commit comments