6
6
import org .javawebstack .orm .util .Helper ;
7
7
import org .javawebstack .orm .util .KeyType ;
8
8
9
- import java .lang .reflect .Array ;
10
9
import java .lang .reflect .Constructor ;
11
10
import java .lang .reflect .Field ;
12
11
import java .lang .reflect .Modifier ;
@@ -29,7 +28,7 @@ public class TableInfo {
29
28
private final Class <? extends Model > modelClass ;
30
29
private String primaryKey ;
31
30
private final List <String > uniqueKeys = new ArrayList <>();
32
- private final Constructor <?> constructor ;
31
+ private Constructor <?> constructor ;
33
32
private String relationField ;
34
33
private final Map <String , String > filterable = new HashMap <>();
35
34
private final List <String > searchable = new ArrayList <>();
@@ -42,6 +41,36 @@ public class TableInfo {
42
41
public TableInfo (Class <? extends Model > model , ORMConfig config ) throws ORMConfigurationException {
43
42
this .config = config ;
44
43
this .modelClass = model ;
44
+
45
+ Stack <Class <?>> superClasses = Helper .getSuperClassesTill (model , Model .class );
46
+ while (!superClasses .isEmpty ()) {
47
+ Class <? extends Model > superClass = (Class <? extends Model >) superClasses .pop ();
48
+ if (Modifier .isAbstract (superClass .getModifiers ())) {
49
+ analyzeColumns (superClass );
50
+ } else {
51
+ throw new ORMConfigurationException ("The parent model has to be abstract!" );
52
+ }
53
+ }
54
+
55
+ constructInfo (model );
56
+ }
57
+
58
+ private void constructInfo (Class <? extends Model > model ) throws ORMConfigurationException {
59
+ analyzeColumns (model );
60
+ analyzeTable (model );
61
+
62
+ if (!fields .containsKey (idField ))
63
+ idField = "uuid" ;
64
+ if (!fields .containsKey (idField ))
65
+ throw new ORMConfigurationException ("No id field found!" );
66
+
67
+ if (config .isIdPrimaryKey ()) {
68
+ if (primaryKey == null )
69
+ primaryKey = idField ;
70
+ }
71
+ }
72
+
73
+ private void analyzeTable (Class <? extends Model > model ) throws ORMConfigurationException {
45
74
if (model .isAnnotationPresent (Table .class )) {
46
75
Table table = model .getDeclaredAnnotationsByType (Table .class )[0 ];
47
76
tableName = table .value ();
@@ -59,6 +88,26 @@ public TableInfo(Class<? extends Model> model, ORMConfig config) throws ORMConfi
59
88
} catch (NoSuchMethodException e ) {
60
89
throw new ORMConfigurationException ("The model class has no empty constructor!" );
61
90
}
91
+ if (model .isAnnotationPresent (RelationField .class )) {
92
+ relationField = model .getDeclaredAnnotationsByType (RelationField .class )[0 ].value ();
93
+ } else {
94
+ relationField = Helper .pascalToCamelCase (model .getSimpleName ()) + ((getIdType ().equals (UUID .class ) && !idField .equalsIgnoreCase ("id" )) ? "UUID" : "Id" );
95
+ }
96
+ if (model .isAnnotationPresent (SoftDelete .class )) {
97
+ softDelete = model .getDeclaredAnnotationsByType (SoftDelete .class )[0 ];
98
+ if (!fields .containsKey (softDelete .value ()))
99
+ throw new ORMConfigurationException ("Missing soft-delete field '" + softDelete .value () + "'" );
100
+ }
101
+ if (model .isAnnotationPresent (Dates .class )) {
102
+ dates = model .getDeclaredAnnotationsByType (Dates .class )[0 ];
103
+ if (!fields .containsKey (dates .create ()))
104
+ throw new ORMConfigurationException ("Missing dates field '" + dates .create () + "'" );
105
+ if (!fields .containsKey (dates .update ()))
106
+ throw new ORMConfigurationException ("Missing dates field '" + dates .update () + "'" );
107
+ }
108
+ }
109
+
110
+ private void analyzeColumns (Class <? extends Model > model ) throws ORMConfigurationException {
62
111
for (Field field : model .getDeclaredFields ()) {
63
112
if (Modifier .isStatic (field .getModifiers ()))
64
113
continue ;
@@ -82,7 +131,7 @@ public TableInfo(Class<? extends Model> model, ORMConfig config) throws ORMConfi
82
131
else
83
132
fieldSize = fieldConfig .size ();
84
133
85
- SQLType sqlType = config .getType (field .getType (), fieldSize );
134
+ SQLType sqlType = config .getType (field .getType (), fieldSize );
86
135
if (sqlType != null ) {
87
136
sqlTypes .put (fieldName , sqlType );
88
137
sqlTypeParameters .put (fieldName , config .getTypeParameters (field .getType (), fieldSize ));
@@ -106,31 +155,6 @@ public TableInfo(Class<? extends Model> model, ORMConfig config) throws ORMConfi
106
155
if (field .isAnnotationPresent (Searchable .class ))
107
156
this .searchable .add (fieldName );
108
157
}
109
- if (!fields .containsKey (idField ))
110
- idField = "uuid" ;
111
- if (!fields .containsKey (idField ))
112
- throw new ORMConfigurationException ("No id field found!" );
113
- if (model .isAnnotationPresent (RelationField .class )) {
114
- relationField = model .getDeclaredAnnotationsByType (RelationField .class )[0 ].value ();
115
- } else {
116
- relationField = Helper .pascalToCamelCase (model .getSimpleName ()) + ((getIdType ().equals (UUID .class ) && !idField .equalsIgnoreCase ("id" )) ? "UUID" : "Id" );
117
- }
118
- if (config .isIdPrimaryKey ()) {
119
- if (primaryKey == null )
120
- primaryKey = idField ;
121
- }
122
- if (model .isAnnotationPresent (SoftDelete .class )) {
123
- softDelete = model .getDeclaredAnnotationsByType (SoftDelete .class )[0 ];
124
- if (!fields .containsKey (softDelete .value ()))
125
- throw new ORMConfigurationException ("Missing soft-delete field '" + softDelete .value () + "'" );
126
- }
127
- if (model .isAnnotationPresent (Dates .class )) {
128
- dates = model .getDeclaredAnnotationsByType (Dates .class )[0 ];
129
- if (!fields .containsKey (dates .create ()))
130
- throw new ORMConfigurationException ("Missing dates field '" + dates .create () + "'" );
131
- if (!fields .containsKey (dates .update ()))
132
- throw new ORMConfigurationException ("Missing dates field '" + dates .update () + "'" );
133
- }
134
158
}
135
159
136
160
public boolean isSoftDelete () {
0 commit comments