1 | |
package org.jtheque.primary.dao.impl; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
import org.jtheque.core.managers.persistence.GenericDao; |
20 | |
import org.jtheque.core.managers.persistence.Query; |
21 | |
import org.jtheque.core.managers.persistence.QueryMapper; |
22 | |
import org.jtheque.core.managers.persistence.able.Entity; |
23 | |
import org.jtheque.core.managers.persistence.context.IDaoPersistenceContext; |
24 | |
import org.jtheque.primary.PrimaryUtils; |
25 | |
import org.jtheque.primary.dao.able.IDaoTypes; |
26 | |
import org.jtheque.primary.od.able.Type; |
27 | |
import org.jtheque.primary.od.impl.TypeImpl; |
28 | |
import org.jtheque.utils.StringUtils; |
29 | |
import org.springframework.jdbc.core.simple.ParameterizedRowMapper; |
30 | |
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; |
31 | |
|
32 | |
import javax.annotation.Resource; |
33 | |
import java.sql.ResultSet; |
34 | |
import java.sql.SQLException; |
35 | |
import java.util.ArrayList; |
36 | |
import java.util.Collection; |
37 | |
import java.util.List; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | public final class DaoTypes extends GenericDao<Type> implements IDaoTypes { |
45 | 0 | private final ParameterizedRowMapper<Type> rowMapper = new TypeRowMapper(); |
46 | 0 | private final QueryMapper queryMapper = new TypeQueryMapper(); |
47 | |
|
48 | |
@Resource |
49 | |
private IDaoPersistenceContext persistenceContext; |
50 | |
|
51 | |
@Resource |
52 | |
private SimpleJdbcTemplate jdbcTemplate; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public DaoTypes() { |
58 | 0 | super(TABLE); |
59 | 0 | } |
60 | |
|
61 | |
@Override |
62 | |
public void create(Type entity) { |
63 | 0 | entity.setPrimaryImpl(PrimaryUtils.getPrimaryImpl()); |
64 | |
|
65 | 0 | super.create(entity); |
66 | 0 | } |
67 | |
|
68 | |
@Override |
69 | |
public boolean exists(Type type) { |
70 | 0 | return getType(type.getName()) != null; |
71 | |
} |
72 | |
|
73 | |
@Override |
74 | |
public Collection<Type> getTypes() { |
75 | 0 | return getTypes(PrimaryUtils.getPrimaryImpl()); |
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
private Collection<Type> getTypes(CharSequence impl) { |
85 | 0 | if (StringUtils.isEmpty(impl)) { |
86 | 0 | return getAll(); |
87 | |
} |
88 | |
|
89 | 0 | load(); |
90 | |
|
91 | 0 | Collection<Type> types = new ArrayList<Type>(getCache().size() / 2); |
92 | |
|
93 | 0 | for (Type type : getCache().values()) { |
94 | 0 | if (impl.equals(type.getPrimaryImpl())) { |
95 | 0 | types.add(type); |
96 | |
} |
97 | |
} |
98 | |
|
99 | 0 | return types; |
100 | |
} |
101 | |
|
102 | |
@Override |
103 | |
public Type getType(int id) { |
104 | 0 | return get(id); |
105 | |
} |
106 | |
|
107 | |
@Override |
108 | |
public Type getType(String name) { |
109 | 0 | List<Type> types = jdbcTemplate.query("SELECT * FROM " + TABLE + " WHERE NAME = ? AND IMPL = ?", |
110 | |
rowMapper, name, PrimaryUtils.getPrimaryImpl()); |
111 | |
|
112 | 0 | if (types.isEmpty()) { |
113 | 0 | return null; |
114 | |
} |
115 | |
|
116 | 0 | Type type = types.get(0); |
117 | |
|
118 | 0 | if (isNotInCache(type.getId())) { |
119 | 0 | getCache().put(type.getId(), type); |
120 | |
} |
121 | |
|
122 | 0 | return getCache().get(type.getId()); |
123 | |
} |
124 | |
|
125 | |
@Override |
126 | |
protected ParameterizedRowMapper<Type> getRowMapper() { |
127 | 0 | return rowMapper; |
128 | |
} |
129 | |
|
130 | |
@Override |
131 | |
protected QueryMapper getQueryMapper() { |
132 | 0 | return queryMapper; |
133 | |
} |
134 | |
|
135 | |
@Override |
136 | |
protected void loadCache() { |
137 | 0 | Collection<Type> types = persistenceContext.getSortedList(TABLE, rowMapper); |
138 | |
|
139 | 0 | for (Type type : types) { |
140 | 0 | getCache().put(type.getId(), type); |
141 | |
} |
142 | |
|
143 | 0 | setCacheEntirelyLoaded(); |
144 | 0 | } |
145 | |
|
146 | |
@Override |
147 | |
protected void load(int i) { |
148 | 0 | Type type = persistenceContext.getDataByID(TABLE, i, rowMapper); |
149 | |
|
150 | 0 | getCache().put(i, type); |
151 | 0 | } |
152 | |
|
153 | |
@Override |
154 | |
public Type createType() { |
155 | 0 | return new TypeImpl(); |
156 | |
} |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | 0 | private final class TypeRowMapper implements ParameterizedRowMapper<Type> { |
164 | |
@Override |
165 | |
public Type mapRow(ResultSet rs, int i) throws SQLException { |
166 | 0 | Type type = createType(); |
167 | |
|
168 | 0 | type.setId(rs.getInt("ID")); |
169 | 0 | type.setName(rs.getString("NAME")); |
170 | 0 | type.setPrimaryImpl(rs.getString("IMPL")); |
171 | |
|
172 | 0 | return type; |
173 | |
} |
174 | |
} |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | 0 | private static final class TypeQueryMapper implements QueryMapper { |
182 | |
@Override |
183 | |
public Query constructInsertQuery(Entity entity) { |
184 | 0 | Type type = (Type) entity; |
185 | |
|
186 | 0 | String query = "INSERT INTO " + TABLE + " (NAME, IMPL) VALUES(?, ?)"; |
187 | |
|
188 | 0 | Object[] parameters = { |
189 | |
type.getName(), |
190 | |
type.getPrimaryImpl() |
191 | |
}; |
192 | |
|
193 | 0 | return new Query(query, parameters); |
194 | |
} |
195 | |
|
196 | |
@Override |
197 | |
public Query constructUpdateQuery(Entity entity) { |
198 | 0 | Type type = (Type) entity; |
199 | |
|
200 | 0 | String query = "UPDATE " + TABLE + " SET NAME = ?, IMPL = ? WHERE ID = ?"; |
201 | |
|
202 | 0 | Object[] parameters = { |
203 | |
type.getName(), |
204 | |
type.getPrimaryImpl(), |
205 | |
type.getId()}; |
206 | |
|
207 | 0 | return new Query(query, parameters); |
208 | |
} |
209 | |
} |
210 | |
} |