Skip to content

Commit 1cace0b

Browse files
committed
Splitted table and column analysis into two methods
1 parent d9c3c2e commit 1cace0b

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

src/main/java/org/javawebstack/orm/TableInfo.java

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public TableInfo(Class<? extends Model> model, ORMConfig config) throws ORMConfi
4747
while (!superClasses.isEmpty()) {
4848
Class<? extends Model> superClass = (Class<? extends Model>) superClasses.pop();
4949
if (Modifier.isAbstract(superClass.getModifiers())) {
50-
constructInfo(superClass);
50+
analyzeColumns(superClass);
5151
} else {
5252
throw new ORMConfigurationException("The parent model has to be abstract!");
5353
}
@@ -57,6 +57,21 @@ public TableInfo(Class<? extends Model> model, ORMConfig config) throws ORMConfi
5757
}
5858

5959
private void constructInfo (Class<? extends Model> model) throws ORMConfigurationException {
60+
analyzeTable(model);
61+
analyzeColumns(model);
62+
63+
if (!fields.containsKey(idField))
64+
idField = "uuid";
65+
if (!fields.containsKey(idField))
66+
throw new ORMConfigurationException("No id field found!");
67+
68+
if (config.isIdPrimaryKey()) {
69+
if (primaryKey == null)
70+
primaryKey = idField;
71+
}
72+
}
73+
74+
private void analyzeTable(Class<? extends Model> model) throws ORMConfigurationException {
6075
if (model.isAnnotationPresent(Table.class)) {
6176
Table table = model.getDeclaredAnnotationsByType(Table.class)[0];
6277
tableName = table.value();
@@ -74,6 +89,26 @@ private void constructInfo (Class<? extends Model> model) throws ORMConfiguratio
7489
} catch (NoSuchMethodException e) {
7590
throw new ORMConfigurationException("The model class has no empty constructor!");
7691
}
92+
if (model.isAnnotationPresent(RelationField.class)) {
93+
relationField = model.getDeclaredAnnotationsByType(RelationField.class)[0].value();
94+
} else {
95+
relationField = Helper.pascalToCamelCase(model.getSimpleName()) + ((getIdType().equals(UUID.class) && !idField.equalsIgnoreCase("id")) ? "UUID" : "Id");
96+
}
97+
if (model.isAnnotationPresent(SoftDelete.class)) {
98+
softDelete = model.getDeclaredAnnotationsByType(SoftDelete.class)[0];
99+
if (!fields.containsKey(softDelete.value()))
100+
throw new ORMConfigurationException("Missing soft-delete field '" + softDelete.value() + "'");
101+
}
102+
if (model.isAnnotationPresent(Dates.class)) {
103+
dates = model.getDeclaredAnnotationsByType(Dates.class)[0];
104+
if (!fields.containsKey(dates.create()))
105+
throw new ORMConfigurationException("Missing dates field '" + dates.create() + "'");
106+
if (!fields.containsKey(dates.update()))
107+
throw new ORMConfigurationException("Missing dates field '" + dates.update() + "'");
108+
}
109+
}
110+
111+
private void analyzeColumns(Class<? extends Model> model) throws ORMConfigurationException {
77112
for (Field field : model.getDeclaredFields()) {
78113
if (Modifier.isStatic(field.getModifiers()))
79114
continue;
@@ -121,31 +156,6 @@ private void constructInfo (Class<? extends Model> model) throws ORMConfiguratio
121156
if (field.isAnnotationPresent(Searchable.class))
122157
this.searchable.add(fieldName);
123158
}
124-
if (!fields.containsKey(idField))
125-
idField = "uuid";
126-
if (!fields.containsKey(idField))
127-
throw new ORMConfigurationException("No id field found!");
128-
if (model.isAnnotationPresent(RelationField.class)) {
129-
relationField = model.getDeclaredAnnotationsByType(RelationField.class)[0].value();
130-
} else {
131-
relationField = Helper.pascalToCamelCase(model.getSimpleName()) + ((getIdType().equals(UUID.class) && !idField.equalsIgnoreCase("id")) ? "UUID" : "Id");
132-
}
133-
if (config.isIdPrimaryKey()) {
134-
if (primaryKey == null)
135-
primaryKey = idField;
136-
}
137-
if (model.isAnnotationPresent(SoftDelete.class)) {
138-
softDelete = model.getDeclaredAnnotationsByType(SoftDelete.class)[0];
139-
if (!fields.containsKey(softDelete.value()))
140-
throw new ORMConfigurationException("Missing soft-delete field '" + softDelete.value() + "'");
141-
}
142-
if (model.isAnnotationPresent(Dates.class)) {
143-
dates = model.getDeclaredAnnotationsByType(Dates.class)[0];
144-
if (!fields.containsKey(dates.create()))
145-
throw new ORMConfigurationException("Missing dates field '" + dates.create() + "'");
146-
if (!fields.containsKey(dates.update()))
147-
throw new ORMConfigurationException("Missing dates field '" + dates.update() + "'");
148-
}
149159
}
150160

151161
public boolean isSoftDelete() {

0 commit comments

Comments
 (0)