forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDao.java
More file actions
272 lines (239 loc) · 9.65 KB
/
Dao.java
File metadata and controls
272 lines (239 loc) · 9.65 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
270
271
272
package act.db;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.Destroyable;
import act.app.AppContextAware;
import act.app.security.SecurityContextAware;
import java.util.Collection;
import java.util.List;
/**
* The Data Access Object interface
* @param <ID_TYPE> the generic key type
* @param <MODEL_TYPE> the generic model type
*/
public interface Dao<ID_TYPE, MODEL_TYPE, QUERY_TYPE extends Dao.Query<MODEL_TYPE, QUERY_TYPE>>
extends AppContextAware, SecurityContextAware, Destroyable {
/**
* Returns the identifier type
*/
Class<ID_TYPE> idType();
/**
* Returns the class of the Model entity this Dao operates on
*/
Class<MODEL_TYPE> modelType();
/**
* Returns the class of the bounded query type
*/
Class<QUERY_TYPE> queryType();
/**
* Find an entity by id, the primary key
* @param id the id to find the entity
* @return the entity found, or {@code null} if not found
*/
MODEL_TYPE findById(ID_TYPE id);
/**
* Find the last created record.
*
* @return the last created record
*/
MODEL_TYPE findLatest();
/**
* Find last modified record.
*
* @return last modified record.
*/
MODEL_TYPE findLastModified();
/**
* Find a collection of entities by fields and values.
* <p>The fields is specified in a {@code String} separated by any
* combination of the following separators</p>
* <ul>
* <li>comma: {@code ,}</li>
* <li>[space characters]</li>
* <li>semi colon: {@code ;}</li>
* <li>colon: {@code :}</li>
* </ul>
* <p>The values are specified in an object array. The number of values
* must match the number of fields specified. Otherwise {@link IllegalArgumentException}
* will be thrown out</p>
* <p>If entities found then they are returned in an {@link Iterable}. Otherwise
* an empty {@link Iterable} will be returned</p>
* @param fields the fields specification in {@code String}
* @param values the value array corresponding to the fields specification
* @return A collection of entities in {@link Iterable}
* @throws IllegalArgumentException if fields number and value number doesn't match
*/
Iterable<MODEL_TYPE> findBy(String fields, Object... values) throws IllegalArgumentException;
/**
* Find one entity with fields and values specified.
* @param fields the fields specification in {@code String}
* @param values the value array corresponding to the fields specification
* @return the entity matches or {@code null} if not found
* @throws IllegalArgumentException
* @see #findBy(String, Object...)
*/
MODEL_TYPE findOneBy(String fields, Object... values) throws IllegalArgumentException;
/**
* Find all entities from a give list of IDs. If there are certain ID in the list does not have
* an entity associated with then that ID will be ignored. The order of the returned iterator
* is not defined and shall be implemented as per specific implementation
* @param idList the ID list specifies the entities shall be returned
* @return a collection of entities
*/
Iterable<MODEL_TYPE> findByIdList(Collection<ID_TYPE> idList);
/**
* Find all entities of the collection/table specified by {@code MODEL_TYPE}
* @return all entities of the type bound to this Dao object in {@link Iterable}
*/
Iterable<MODEL_TYPE> findAll();
/**
* Find all entities of the collection/table specified by {@code MODEL_TYPE}
* @return all entities of the type bound to this Dao object in {@link List}
*/
List<MODEL_TYPE> findAllAsList();
/**
* Reload a model entity from persistent storage by it's {@link ModelBase#_id()}. This method
* returns the model been reloaded. Depending on the implementation, it could be the model
* passed in as parameter if it's mutable object or a fresh new object instance with the
* same ID as the model been passed in.
*
* @param entity the model to be reloaded
* @return a model been reloaded
*/
MODEL_TYPE reload(MODEL_TYPE entity);
/**
* Extract ID value from the give model entity
* @param entity the model entity object
* @return the ID of the entity
*/
ID_TYPE getId(MODEL_TYPE entity);
/**
* Returns total number of entities of the model type of this {@code Dao} object.
*/
long count();
/**
* Count the number of entities matches the fields and values specified. For the
* rule of fields and value specification, please refer to {@link #findBy(String, Object...)}
* @param fields the fields specification in {@code String}
* @param values the value array corresponding to the fields specification
* @return the number of matched entities
* @throws IllegalArgumentException if fields number and value number doesn't match
*/
long countBy(String fields, Object ... values) throws IllegalArgumentException;
/**
* Save new or update existing the entity in persistent layer with all properties
* of the entity
* @param entity the entity to be saved or updated
* @return the entity that has been saved
*/
MODEL_TYPE save(MODEL_TYPE entity);
/**
* Update existing entity in persistent layer with specified fields and value. This allows
* partial updates of the entity to save the bandwidth.
* <p>Note the properties of the entity
* does not impact the update operation, however the {@link ModelBase#_id()} will be used to
* locate the record/document in the persistent layer corresponding to this entity.</p>
* <p>For fields and value specification rule, please refer to {@link #findBy(String, Object...)}</p>
* @param entity the entity
* @param fields the fields specification in {@code String}
* @param values the value array corresponding to the fields specification
*/
void save(MODEL_TYPE entity, String fields, Object ... values);
/**
* Batch save entities
* @param entities an iterable to get entities to be saved
*/
List<MODEL_TYPE> save(Iterable<MODEL_TYPE> entities);
/**
* Remove the entity specified
* @param entity the entity to be removed
*/
void delete(MODEL_TYPE entity);
/**
* Remove entities specified by Query
* @param query the query specifies entities to be removed
*/
void delete(QUERY_TYPE query);
/**
* Remove entity by ID
* @param id the ID of the entity to be removed
*/
void deleteById(ID_TYPE id);
/**
* Delete a collection of entities by fields and values.
* <p>The fields is specified in a {@code String} separated by any
* combination of the following separators</p>
* <ul>
* <li>comma: {@code ,}</li>
* <li>[space characters]</li>
* <li>semi colon: {@code ;}</li>
* <li>colon: {@code :}</li>
* </ul>
* <p>The values are specified in an object array. The number of values
* must match the number of fields specified. Otherwise {@link IllegalArgumentException}
* will be thrown out</p>
* <p>If entities found then they are returned in an {@link Iterable}. Otherwise
* an empty {@link Iterable} will be returned</p>
* @param fields the fields specification in {@code String}
* @param values the value array corresponding to the fields specification
* @throws IllegalArgumentException if fields number and value number doesn't match
*/
void deleteBy(String fields, Object... values) throws IllegalArgumentException;
/**
* Delete all entities in the table/collection inferred by this DAO
*/
void deleteAll();
/**
* Drop all entities (and optionally all indexes) from persistent storage
*/
void drop();
/**
* Return a {@link act.db.Dao.Query} bound to the {@code MODEL_TYPE}
* @return an new {@link Query} instance on this Dao
*/
QUERY_TYPE q();
/**
* Alias of {@link #q()}
* @return an new {@link Query} instance on this Dao
*/
QUERY_TYPE createQuery();
/**
* Return a {@link act.db.Dao.Query} bound to the {@code MODEL_TYPE} by fields and value arguments
* @param fields the fields specification in {@code String}
* @param values the value array corresponding to the fields specification
* @return the query instance as described above
*/
QUERY_TYPE q(String fields, Object... values);
/**
* Alias of {@link #q(String, Object...)}
* @param fields the fields specification in {@code String}
* @param values the value array corresponding to the fields specification
* @return the query instance as described in {@link #q(String, Object...)}
*/
QUERY_TYPE createQuery(String fields, Object... values);
interface Query<MODEL_TYPE, QUERY_TYPE extends Query<MODEL_TYPE, QUERY_TYPE>> {
QUERY_TYPE offset(int pos);
QUERY_TYPE limit(int limit);
QUERY_TYPE orderBy(String ... fieldList);
MODEL_TYPE first();
Iterable<MODEL_TYPE> fetch();
long count();
}
}