forked from telosys-tools-beta/java8-commons-T312
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenericFakeDAO_java.vm
170 lines (145 loc) · 4.13 KB
/
GenericFakeDAO_java.vm
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
#parse("include/java_header.vm")
package ${target.javaPackageFromFolder(${SRC})};
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* Generic abstract DAO for 'FAKE' persistence
*
* @author Telosys
*
* @param <T>
*/
public abstract class GenericFakeDAO<T> {
private final Class<?> clazz ;
/**
* Constructor
* @param clazz
*/
protected GenericFakeDAO(Class<?> clazz) {
super();
this.clazz = clazz;
}
//-----------------------------------------------------------------------------------------
// ABSTRACT METHODS
//-----------------------------------------------------------------------------------------
/**
* Abstract method used to get the bean key as a string <br>
* @return the key
*/
protected abstract String getKey(T bean);
/**
* Abstract method used to set the computed value for an auto-incremented key <br>
* This method is supposed to set the key if the bean has an auto-incremented key <br>
* or to throw an exception if the bean doesn't have an auto-incremented key
* @param bean
* @param id
*/
protected abstract void setAutoIncrementedKey(T bean, long id);
//-----------------------------------------------------------------------------------------
// UTILITY METHODS
//-----------------------------------------------------------------------------------------
protected int toInt(long value) {
return (int)value;
}
protected short toShort(long value) {
return (short)value;
}
@SuppressWarnings("unchecked")
private Map<String,T> getMap() {
return (Map<String,T>) MapProvider.getMap(clazz);
}
protected void checkNotNull(Object o) {
if ( o == null ) {
throw new IllegalArgumentException("Key element is null");
}
}
private final void checkArguments(Object... args ) {
if ( args == null ) {
throw new IllegalArgumentException("Cannot build key string (args array is null)");
}
if ( args.length == 0 ) {
throw new IllegalArgumentException("Cannot build key string (no args, length=0)");
}
for (int i = 0; i < args.length; i++) {
if ( args[i] == null ) {
throw new IllegalArgumentException("Cannot build key string (argument["+i+"] is null)");
}
}
}
protected String buildKeyString( Object ... args ) {
checkArguments(args);
StringBuilder sb = new StringBuilder();
sb.append(args[0].toString());
for (int i = 1; i < args.length; i++) {
sb.append("|");
sb.append(args[i].toString());
}
return sb.toString();
}
//-----------------------------------------------------------------------------------------
// PERSISTENCE METHODS
//-----------------------------------------------------------------------------------------
protected long doCountAll() {
Map<String,T> map = getMap();
return map.size() ;
}
protected T doCreate(T entity) {
Map<String,T> map = getMap();
String key = getKey(entity);
if ( map.get(key) == null ) {
map.put(key, entity );
return entity ;
}
else {
throw new RuntimeException("CarRecord - Duplicate key : " + key );
}
}
protected boolean doCreateAutoIncremented(T entity) {
long generatedValue = Sequence.getNextValue(clazz);
setAutoIncrementedKey(entity, generatedValue);
doCreate(entity);
return true ;
}
protected boolean doDelete(T entity) {
Map<String,T> map = getMap();
String key = getKey(entity);
T deleted = map.remove(key);
return deleted != null ;
}
protected boolean doExists(T entity) {
return doFind(entity) != null ;
}
protected T doFind(T entity) {
Map<String,T> map = getMap();
String key = getKey(entity);
return map.get(key);
}
protected List<T> doFindAll() {
Map<String,T> map = getMap();
List<T> list = new LinkedList<T>();
for ( T entity : map.values() ) {
list.add(entity);
}
return list;
}
protected T doSave(T entity) {
Map<String,T> map = getMap();
String key = getKey(entity);
map.put(key, entity );
return entity;
}
protected boolean doUpdate(T entity) {
Map<String,T> map = getMap();
String key = getKey(entity);
if ( map.get(key) == null ) {
// Doesn't Exist => no update
return false ;
}
else {
// Exists => update
map.put(key, entity );
return true ;
}
}
}