Repository navigation
Expand file tree
/
Copy pathModel.java
More file actions
160 lines (136 loc) · 5.57 KB
/
Copy pathModel.java
File metadata and controls
160 lines (136 loc) · 5.57 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
package org.javawebstack.orm;
import org.javawebstack.orm.util.Helper;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.stream.Collectors;
public class Model {
private static Method saveMethod;
private static Method deleteMethod;
private static Method finalDeleteMethod;
private static Method restoreMethod;
private static Method refreshMethod;
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 boolean entryExists = false;
boolean doesEntryExist(){
return entryExists;
}
void setEntryExists(boolean exists){
this.entryExists = exists;
}
public void save(){
try {
saveMethod.invoke(ORM.repo(getClass()), this);
} catch (IllegalAccessException | InvocationTargetException e) {
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> T belongsTo(Class<T> parent){
return belongsTo(parent, Helper.pascalToCamelCase(parent.getSimpleName())+"Id");
}
public <T extends Model> T belongsTo(Class<T> parent, String fieldName){
try {
Integer id = (Integer) Repo.get(getClass()).getInfo().getField(fieldName).get(this);
if(id == null)
return null;
return Repo.get(parent).get(id);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public <T extends Model> void assignTo(Class<T> parent, T value){
assignTo(parent, value, Helper.pascalToCamelCase(parent.getSimpleName())+"Id");
}
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);
Integer id = (Integer) repo.getInfo().getField(repo.getInfo().getIdField()).get(value);
f.set(this, id);
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public <T extends Model> List<T> hasMany(Class<T> child){
return hasManyRelation(child).all();
}
public <T extends Model> List<T> hasMany(Class<T> child, String fieldName){
return hasManyRelation(child, fieldName).all();
}
public <T extends Model> QueryBuilder<T> hasManyRelation(Class<T> child){
return hasManyRelation(child, Helper.pascalToCamelCase(getClass().getSimpleName())+"Id");
}
public <T extends Model> QueryBuilder<T> hasManyRelation(Class<T> child, String fieldName){
try {
Repo<?> ownRepo = Repo.get(getClass());
Integer id = (Integer) 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> List<T> belongsToMany(Class<T> other, Class<? extends Model> pivot){
return belongsToMany(other, pivot, Helper.pascalToCamelCase(getClass().getSimpleName())+"Id", Helper.pascalToCamelCase(other.getSimpleName())+"Id");
}
public <T extends Model> List<T> belongsToMany(Class<T> other, Class<? extends Model> pivot, String selfFieldName, String otherFieldName){
try {
Repo<?> selfRepo = Repo.get(getClass());
Repo<T> otherRepo = Repo.get(other);
Field otherField = Repo.get(pivot).getInfo().getField(otherFieldName);
Integer id = (Integer) selfRepo.getInfo().getField(selfRepo.getInfo().getIdField()).get(this);
return Repo.get(pivot).where(selfFieldName, id).stream().map(p -> {
try {
Integer otherId = (Integer) otherField.get(p);
return otherRepo.get(otherId);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}).collect(Collectors.toList());
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}