Repository navigation
Expand file tree
/
Copy pathModel.java
More file actions
130 lines (114 loc) · 3.31 KB
/
Copy pathModel.java
File metadata and controls
130 lines (114 loc) · 3.31 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
package org.codebrewery;
import java.util.List;
public abstract class Model {
public static ApiInterface api() {
return ApiFactory.getDefaultImplementation();
}
public abstract String getIdentifierValue();
/**
* Return a named ApiInterface that is typically different to the default apiImpl.
*
* <p>
* If you are using multiple restApis then each api has a name and maps to a single ApiInterface. You can use this method to get an ApiInterface for another
* restApi.
*
* @param apiImpl
* The name of the ApiInterface. If this is null then the default ApiInterface is returned.
*/
public static ApiInterface api(String apiImpl) {
return null;
}
/**
* Insert or update this model depending on its state.
*
* <p>
* ApiFactory will detect if this is a new model or a previously fetched model and perform either an insert or an update based on that.
*
* You may see this as an smart method for update/insert.
*
* @see ApiInterface#save(Model)
*/
@SuppressWarnings("unchecked")
public <T extends Model> T save() throws JavaOrmenException {
return api().save((T) this);
}
/**
* Update this model.
*
* @see ApiInterface#update(Model)
*/
@SuppressWarnings("unchecked")
public <T extends Model> T update() throws JavaOrmenException {
return api().update((T) this);
}
/**
* Insert this model.
*
* @see ApiInterface#insert(Model)
*/
@SuppressWarnings("unchecked")
public <T extends Model> T insert() throws JavaOrmenException {
return api().insert((T) this);
}
/**
* Delete this model.
*
* @see ApiInterface#delete(Model)
*/
public void delete() throws JavaOrmenException {
api().delete(this);
}
/**
* Refreshes this model from the API.
*
* @see ApiInterface#fetch(Model)
*/
@SuppressWarnings("unchecked")
public <T extends Model> T fetch() throws JavaOrmenException {
return api().fetch((T) this);
}
/**
* A concrete implementation of Find.
* <p>
* .
* </p>
*/
public static class Finder<T extends Model> {
private final Class<T> type;
/**
* Create with the type of the model model.
*
* <pre>
* {@code
*
* public class Customer extends BaseModel {
*
* public static final Finder<Long,Customer> find = new Finder<Long,Customer>(Customer.class);
* ...
*
* }
* </pre>
*
* <p/>
* The preferred approach is to instead use <code>Find</code> as below. This approach is more DRY in that it does not require the class literal
* Customer.class to be passed into the constructor.
*
* <pre>
* {@code
*
* public class Customer extends BaseModel {
*
* public static final Find<Long,Customer> find = new Find<Long,Customer>(){};
* ...
*
* }
* </pre>
*/
public Finder(Class<T> type) {
this.type = type;
}
public List<T> all() throws JavaOrmenException {
return api().getAll(this.type);
}
}
}