Repository navigation
Expand file tree
/
Copy pathModel.java
More file actions
225 lines (193 loc) · 8.04 KB
/
Copy pathModel.java
File metadata and controls
225 lines (193 loc) · 8.04 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
package org.javawebstack.orm;
import com.google.gson.annotations.Expose;
import org.javawebstack.injector.Injector;
import org.javawebstack.orm.exception.ORMQueryException;
import org.javawebstack.orm.query.Query;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
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;
{
inject();
}
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);
}
}
@Expose(serialize = false, deserialize = false)
private boolean internalEntryExists = false;
@Expose(serialize = false, deserialize = false)
private final Map<Class<? extends Model>, Object> internalJoinedModels = new HashMap<>();
@Expose(serialize = false, deserialize = false)
private final Map<String, Object> internalLastValue = new HashMap<>();
void internalAddJoinedModel(Class<? extends Model> type, Object entity){
internalJoinedModels.put(type, entity);
}
void internalSetLastValue(String key, Object value){
if(value == null) internalLastValue.remove(key);
else internalLastValue.put(key, value);
}
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;
}
@Deprecated
public boolean isDirty(String... fields){
Repo<?> repo = Repo.get(getClass());
for(String field : fields){
try {
Object value = repo.getInfo().getField(field).get(this);
Object oldValue = internalLastValue.get(field);
if((value == null && oldValue != null) || (oldValue == null && value != null))
return true;
if(value == null)
continue;
if(!value.equals(oldValue))
return true;
} catch (IllegalAccessException e) {
throw new ORMQueryException(e);
}
}
return false;
}
public void inject(){
Injector injector = Repo.get(getClass()).getInfo().getConfig().getInjector();
if(injector != null)
injector.inject(this);
}
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){
try {
Object id = Repo.get(getClass()).getInfo().getField(fieldName).get(this);
if(id == null)
return null;
return Repo.get(parent).whereId(id);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
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){
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(repo.getInfo().getIdField()).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){
try {
Repo<?> ownRepo = Repo.get(getClass());
Object id = ownRepo.getInfo().getField(ownRepo.getInfo().getIdField()).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 selfFieldName, String otherFieldName){
return belongsToMany(other, pivot, selfFieldName, otherFieldName, null);
}
public <T extends Model, P extends Model> Query<T> belongsToMany(Class<T> other, Class<P> pivot, 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(selfRepo.getInfo().getIdField()).get(this);
return otherRepo.whereExists(pivot, q -> {
q.where(pivot, selfFieldName, "=", id).where(pivot, otherFieldName, "=", other, otherRepo.getInfo().getIdColumn());
if(pivotFilter != null)
q = pivotFilter.apply(q);
return q;
});
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public void setMorph(String name, Class<? extends Model> type, Object id){
TableInfo info = Repo.get(getClass()).getInfo();
try {
info.getField(name+"Id").set(this, id);
info.getField(name+"Type").set(this, Repo.get(type).getInfo().getMorphType());
} catch (IllegalAccessException ignored) {}
}
public void setMorph(String name, Model model){
setMorph(name, model.getClass(), Repo.get(model.getClass()).getId(model));
}
}