-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathRepository.java
More file actions
75 lines (62 loc) · 1.48 KB
/
Repository.java
File metadata and controls
75 lines (62 loc) · 1.48 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
package com.activeandroid.util;
import com.activeandroid.ActiveAndroid;
import com.activeandroid.Model;
import com.activeandroid.query.From;
import com.activeandroid.query.Select;
import java.util.List;
/**
* Created by adifrancesco on 24/11/2015.
*/
public class Repository<T extends Model> {
private Class<T> clazz;
public Repository(Class<T> clazz)
{
this.clazz = clazz;
}
public void save(T m) {
m.save();
}
public void save(List<T> m) {
ActiveAndroid.beginTransaction();
try {
for(T item : m) {
item.save();
}
ActiveAndroid.setTransactionSuccessful();
}
finally {
ActiveAndroid.endTransaction();
}
}
public List<T> getAll() {
return new Select()
.from(clazz)
.execute();
}
public T get(int id) {
return T.load(clazz, id);
/*
return new Select()
.from(clazz)
.where("Id = ?", id)
.execute();
*/
}
public T getById(int id) {
return new Select()
.from(clazz)
.where("Id = ?", id)
.executeSingle();
}
public From query() {
return new Select()
.from(clazz);
}
public void delete(long i) {
T item = T.load(clazz, i);
item.delete();
}
public void delete(T item) {
item.delete();
}
}