1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
package org.apache.tiles.test.db; |
23 | |
|
24 | |
import java.sql.ResultSet; |
25 | |
import java.sql.SQLException; |
26 | |
import java.util.List; |
27 | |
import java.util.Locale; |
28 | |
import java.util.Map; |
29 | |
|
30 | |
import org.apache.tiles.Attribute; |
31 | |
import org.apache.tiles.Definition; |
32 | |
import org.apache.tiles.definition.dao.DefinitionDAO; |
33 | |
import org.apache.tiles.request.locale.LocaleUtil; |
34 | |
import org.springframework.jdbc.core.RowMapper; |
35 | |
import org.springframework.jdbc.core.support.JdbcDaoSupport; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public class LocaleDbDefinitionDAO extends JdbcDaoSupport implements |
44 | |
DefinitionDAO<Locale> { |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
private static final String SELECT_CUSTOMIZATION_BY_NAME_SQL = |
50 | |
"select ID, PARENT_ID, NAME from CUSTOMIZATION " |
51 | |
+ "where NAME = ? "; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
private static final String SELECT_CUSTOMIZATION_BY_ID_SQL = |
57 | |
"select ID, PARENT_ID, NAME from CUSTOMIZATION " |
58 | |
+ "where ID = ? "; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
private static final String SELECT_DEFINITION_SQL = |
64 | |
"select ID, PARENT_NAME, NAME, PREPARER, TEMPLATE from DEFINITION " |
65 | |
+ "where NAME = ? and CUSTOMIZATION_ID = ? "; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
private static final String SELECT_ATTRIBUTES_SQL = |
71 | |
"select ID, NAME, TYPE, VALUE, CASCADE_ATTRIBUTE from ATTRIBUTE " |
72 | |
+ "where DEFINITION_ID = ? "; |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | 0 | private final DefinitionRowMapper definitionRowMapper = new DefinitionRowMapper(); |
78 | |
|
79 | |
|
80 | |
@SuppressWarnings("unchecked") |
81 | |
public Definition getDefinition(String name, Locale locale) { |
82 | 0 | List<Map<String, Object>> customizations = null; |
83 | 0 | Long customizationId = null, parentCustomizationId = null; |
84 | |
do { |
85 | 0 | customizations = getJdbcTemplate().queryForList( |
86 | |
SELECT_CUSTOMIZATION_BY_NAME_SQL, |
87 | |
new Object[] { locale.toString() }); |
88 | 0 | if (!customizations.isEmpty()) { |
89 | 0 | Map<String, Object> customization = customizations.get(0); |
90 | 0 | customizationId = ((Number) customization.get("ID")).longValue(); |
91 | 0 | parentCustomizationId = numberToLong((Number) customization.get("PARENT_ID")); |
92 | 0 | } else { |
93 | 0 | locale = LocaleUtil.getParentLocale(locale); |
94 | |
} |
95 | 0 | } while (customizations.isEmpty()); |
96 | |
|
97 | 0 | return getDefinition(name, customizationId, parentCustomizationId, |
98 | |
locale); |
99 | |
} |
100 | |
|
101 | |
|
102 | |
public Map<String, Definition> getDefinitions(Locale locale) { |
103 | 0 | throw new UnsupportedOperationException( |
104 | |
"Currently the 'getDefinitions' method is not supported"); |
105 | |
} |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
@SuppressWarnings("unchecked") |
117 | |
protected DbDefinition getDefinition(String name, Long baseCustomizationId, |
118 | |
Long baseParentCustomizationId, @SuppressWarnings("unused") Locale locale) { |
119 | 0 | DbDefinition definition = null; |
120 | 0 | Long customizationId = baseCustomizationId; |
121 | 0 | Long parentCustomizationId = baseParentCustomizationId; |
122 | 0 | List<DbDefinition> definitions = null; |
123 | 0 | boolean finished = false; |
124 | |
do { |
125 | 0 | definitions = getJdbcTemplate() |
126 | |
.query(SELECT_DEFINITION_SQL, |
127 | |
new Object[] { name, customizationId }, |
128 | |
definitionRowMapper); |
129 | 0 | if (definitions.isEmpty()) { |
130 | 0 | if (parentCustomizationId != null) { |
131 | 0 | Map<String, Object> customization = getJdbcTemplate().queryForMap( |
132 | |
SELECT_CUSTOMIZATION_BY_ID_SQL, |
133 | |
new Object[] { parentCustomizationId }); |
134 | 0 | customizationId = ((Number) customization.get("ID")).longValue(); |
135 | 0 | parentCustomizationId = numberToLong((Number) customization.get("PARENT_ID")); |
136 | 0 | } else { |
137 | 0 | finished = true; |
138 | |
} |
139 | |
} else { |
140 | 0 | definition = definitions.get(0); |
141 | 0 | finished = true; |
142 | |
} |
143 | 0 | } while (!finished); |
144 | |
|
145 | 0 | if (definition != null) { |
146 | 0 | AttributeRowMapper attributeRowMapper = new AttributeRowMapper(definition); |
147 | 0 | getJdbcTemplate().query(SELECT_ATTRIBUTES_SQL, |
148 | |
new Object[] { definition.getId() }, attributeRowMapper); |
149 | |
} |
150 | 0 | return definition; |
151 | |
} |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
private static Long numberToLong(Number number) { |
161 | 0 | Long retValue = null; |
162 | 0 | if (number != null) { |
163 | 0 | retValue = number.longValue(); |
164 | |
} |
165 | 0 | return retValue; |
166 | |
} |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
private static class DbDefinition extends Definition { |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
private Long id; |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
public DbDefinition() { |
182 | 0 | super(); |
183 | 0 | } |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
public Long getId() { |
191 | 0 | return id; |
192 | |
} |
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
public void setId(Long id) { |
200 | 0 | this.id = id; |
201 | 0 | } |
202 | |
|
203 | |
} |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | 0 | private static final class DefinitionRowMapper implements RowMapper { |
209 | |
|
210 | |
|
211 | |
public Object mapRow(ResultSet rs, int row) throws SQLException { |
212 | 0 | DbDefinition definition = new DbDefinition(); |
213 | 0 | definition.setId(numberToLong((Number) rs.getObject("ID"))); |
214 | 0 | definition.setName(rs.getString("NAME")); |
215 | 0 | definition.setTemplateAttribute(Attribute |
216 | |
.createTemplateAttribute(rs.getString("TEMPLATE"))); |
217 | 0 | definition.setPreparer(rs.getString("PREPARER")); |
218 | 0 | definition.setExtends(rs.getString("PARENT_NAME")); |
219 | 0 | return definition; |
220 | |
} |
221 | |
|
222 | |
} |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | 0 | private static final class AttributeRowMapper implements RowMapper { |
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
private Definition definition; |
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | 0 | private AttributeRowMapper(Definition definition) { |
242 | 0 | this.definition = definition; |
243 | 0 | } |
244 | |
|
245 | |
|
246 | |
public Object mapRow(ResultSet rs, int row) throws SQLException { |
247 | 0 | Attribute attribute = new Attribute(); |
248 | 0 | attribute.setRenderer(rs.getString("TYPE")); |
249 | 0 | attribute.setValue(rs.getString("VALUE")); |
250 | 0 | definition.putAttribute(rs.getString("NAME"), attribute, rs |
251 | |
.getBoolean("CASCADE_ATTRIBUTE")); |
252 | 0 | return attribute; |
253 | |
} |
254 | |
|
255 | |
} |
256 | |
} |