-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModel.java
More file actions
269 lines (227 loc) · 9.92 KB
/
Model.java
File metadata and controls
269 lines (227 loc) · 9.92 KB
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
26
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package org.javawebstack.orm;
import org.javawebstack.commons.inject.Injector;
import org.javawebstack.orm.query.Query;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
public class Model {
private static final Method saveMethod;
private static final Method deleteMethod;
private static final Method finalDeleteMethod;
private static final Method restoreMethod;
private static final Method refreshMethod;
{
updateOriginal();
Injector injector = Repo.get(getClass()).getInfo().getConfig().getInjector();
if(injector != null)
injector.inject(this);
}
static {
try {
saveMethod = Repo.class.getMethod("save", Model.class);
deleteMethod = Repo.class.getMethod("delete", Model.class);
finalDeleteMethod = Repo.class.getMethod("finalDelete", Model.class);
restoreMethod = Repo.class.getMethod("restore", Model.class);
refreshMethod = Repo.class.getMethod("refresh", Model.class);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
private transient boolean internalEntryExists = false;
private transient final Map<Class<? extends Model>, Object> internalJoinedModels = new HashMap<>();
private transient Map<String, Object> internalOriginalValues = new HashMap<>();
private transient Map<String, Object> internalExtraFields = new HashMap<>();
void internalAddJoinedModel(Class<? extends Model> type, Object entity) {
internalJoinedModels.put(type, entity);
}
void updateOriginal() {
internalOriginalValues = getFieldValues();
}
public Map<String, Object> getFieldValues() {
TableInfo info = Repo.get(getClass()).getInfo();
Map<String, Object> values = new HashMap<>();
for (String field : info.getFields()) {
try {
values.put(field, info.getField(field).get(this));
} catch (IllegalAccessException ignored) {
}
}
return values;
}
public Map<String, Object> getExtraFields() {
return internalExtraFields;
}
public <T> T getExtraField(String key) {
return (T) internalExtraFields.get(key);
}
public Map<String, Object> getOriginalValues() {
return internalOriginalValues;
}
public <T> T getOriginalValue(String field) {
if (internalOriginalValues.get(field) == null)
return null;
return (T) internalOriginalValues.get(field);
}
public boolean isDirty(String... fields) {
List<String> dirty = getDirtyFields();
if(fields.length == 0 && dirty.size() > 0)
return true;
for (String f : fields) {
if (dirty.contains(f))
return true;
}
return false;
}
public List<String> getDirtyFields() {
List<String> dirty = new ArrayList<>();
Map<String, Object> original = getOriginalValues();
Map<String, Object> current = getFieldValues();
for (String key : current.keySet()) {
Object o = original.get(key);
Object c = current.get(key);
if (o == null && c == null)
continue;
if (o == null || !o.equals(c))
dirty.add(key);
}
return dirty;
}
public <T extends Model> T getJoined(Class<T> model) {
return (T) internalJoinedModels.get(model);
}
public boolean hasJoined(Class<? extends Model> model) {
return internalJoinedModels.containsKey(model);
}
boolean doesEntryExist() {
return internalEntryExists;
}
void setEntryExists(boolean exists) {
this.internalEntryExists = exists;
}
public void save() {
try {
saveMethod.invoke(ORM.repo(getClass()), this);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public void delete() {
try {
deleteMethod.invoke(ORM.repo(getClass()), this);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
public void finalDelete() {
try {
finalDeleteMethod.invoke(ORM.repo(getClass()), this);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
public void restore() {
try {
restoreMethod.invoke(ORM.repo(getClass()), this);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
public void refresh() {
try {
refreshMethod.invoke(ORM.repo(getClass()), this);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
public <T extends Model> Query<T> belongsTo(Class<T> parent) {
return belongsTo(parent, Repo.get(parent).getInfo().getRelationField());
}
public <T extends Model> Query<T> belongsTo(Class<T> parent, String fieldName) {
return belongsTo(parent, fieldName, Repo.get(parent).getInfo().getIdField());
}
public <T extends Model> Query<T> belongsTo(Class<T> parent, String fieldName, String otherFieldName) {
try {
Object id = Repo.get(getClass()).getInfo().getField(fieldName).get(this);
return Repo.get(parent).where(otherFieldName, id);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public <T extends Model> void assignTo(T value) {
if(value == null)
throw new InvalidParameterException("You need to specify a parent type if the value is null");
assignTo((Class<T>) value.getClass(), value);
}
public <T extends Model> void assignTo(Class<T> parent, T value) {
assignTo(parent, value, Repo.get(parent).getInfo().getRelationField());
}
public <T extends Model> void assignTo(Class<T> parent, T value, String fieldName) {
assignTo(parent, value, fieldName, Repo.get(parent).getInfo().getIdField());
}
public <T extends Model> void assignTo(Class<T> parent, T value, String fieldName, String otherFieldName) {
try {
Field f = Repo.get(getClass()).getInfo().getField(fieldName);
if (value == null) {
f.set(this, null);
} else {
Repo<T> repo = Repo.get(parent);
Object id = repo.getInfo().getField(otherFieldName).get(value);
f.set(this, id);
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public <T extends Model> Query<T> hasMany(Class<T> child) {
return hasMany(child, Repo.get(getClass()).getInfo().getRelationField());
}
public <T extends Model> Query<T> hasMany(Class<T> child, String fieldName) {
return hasMany(child, fieldName, Repo.get(getClass()).getInfo().getIdField());
}
public <T extends Model> Query<T> hasMany(Class<T> child, String fieldName, String ownFieldName) {
try {
Repo<?> ownRepo = Repo.get(getClass());
Object id = ownRepo.getInfo().getField(ownFieldName).get(this);
return Repo.get(child).where(fieldName, id);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public <T extends Model, P extends Model> Query<T> belongsToMany(Class<T> other, Class<P> pivot) {
return belongsToMany(other, pivot, null);
}
public <T extends Model, P extends Model> Query<T> belongsToMany(Class<T> other, Class<P> pivot, Function<Query<P>, Query<P>> pivotFilter) {
return belongsToMany(other, pivot, Repo.get(getClass()).getInfo().getRelationField(), Repo.get(other).getInfo().getRelationField(), pivotFilter);
}
public <T extends Model, P extends Model> Query<T> belongsToMany(Class<T> other, Class<P> pivot, String selfPivotFieldName, String otherPivotFieldName) {
return belongsToMany(other, pivot, selfPivotFieldName, otherPivotFieldName, null);
}
public <T extends Model, P extends Model> Query<T> belongsToMany(Class<T> other, Class<P> pivot, String selfPivotFieldName, String otherPivotFieldName, Function<Query<P>, Query<P>> pivotFilter) {
return belongsToMany(other, pivot, selfPivotFieldName, otherPivotFieldName, Repo.get(getClass()).getInfo().getIdField(), Repo.get(other).getInfo().getIdField(), pivotFilter);
}
public <T extends Model, P extends Model> Query<T> belongsToMany(Class<T> other, Class<P> pivot, String selfPivotFieldName, String otherPivotFieldName, String selfFieldName, String otherFieldName) {
return belongsToMany(other, pivot, selfPivotFieldName, otherPivotFieldName, selfFieldName, otherFieldName, null);
}
public <T extends Model, P extends Model> Query<T> belongsToMany(Class<T> other, Class<P> pivot, String selfPivotFieldName, String otherPivotFieldName, String selfFieldName, String otherFieldName, Function<Query<P>, Query<P>> pivotFilter) {
try {
Repo<?> selfRepo = Repo.get(getClass());
Repo<T> otherRepo = Repo.get(other);
Object id = selfRepo.getInfo().getField(selfFieldName).get(this);
return otherRepo.query().whereExists(pivot, q -> {
q.where(pivot, selfPivotFieldName, "=", id).where(pivot, otherPivotFieldName, "=", other, otherFieldName);
if (pivotFilter != null)
q = pivotFilter.apply(q);
return q;
});
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}