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 package fr.paris.lutece.portal.business.stylesheet;
35
36 import fr.paris.lutece.portal.service.util.AppException;
37 import fr.paris.lutece.util.sql.DAOUtil;
38
39 import java.util.ArrayList;
40 import java.util.Collection;
41
42
43
44
45
46 public final class StyleSheetDAO implements IStyleSheetDAO
47 {
48
49 private static final String SQL_QUERY_NEW_PK = " SELECT max(id_stylesheet) FROM core_stylesheet ";
50 private static final String SQL_QUERY_SELECT = " SELECT a.description , a.file_name , a.source , b.id_style , b.id_mode " +
51 " FROM core_stylesheet a " + " LEFT JOIN core_style_mode_stylesheet b ON a.id_stylesheet = b.id_stylesheet " +
52 " WHERE a.id_stylesheet = ? ";
53 private static final String SQL_QUERY_INSERT = " INSERT INTO core_stylesheet ( id_stylesheet , description , file_name, source ) " +
54 " VALUES ( ?, ? ,?, ? )";
55 private static final String SQL_QUERY_DELETE = " DELETE FROM core_stylesheet WHERE id_stylesheet = ? ";
56 private static final String SQL_QUERY_UPDATE = " UPDATE core_stylesheet SET id_stylesheet = ?, description = ?, file_name = ?, source = ? WHERE id_stylesheet = ? ";
57 private static final String SQL_QUERY_SELECT_MODEID = " SELECT a.id_mode FROM core_mode a , core_style_mode_stylesheet b " +
58 " WHERE a.id_mode = b.id_mode AND b.id_stylesheet = ?";
59 private static final String SQL_QUERY_COUNT_STYLESHEET = " SELECT count(*) FROM core_style_mode_stylesheet WHERE id_style = ? AND id_mode = ? ";
60 private static final String SQL_QUERY_INSERT_STYLEMODESTYLESHEET = " INSERT INTO core_style_mode_stylesheet ( id_style , id_mode , id_stylesheet ) " +
61 " VALUES ( ?, ? ,? )";
62 private static final String SQL_QUERY_UPDATE_STYLEMODESTYLESHEET = " UPDATE core_style_mode_stylesheet SET id_style = ? , id_mode = ? " +
63 " WHERE id_stylesheet = ? ";
64 private static final String SQL_QUERY_DELETEE_STYLEMODESTYLESHEET = " DELETE FROM core_style_mode_stylesheet WHERE id_stylesheet = ? ";
65
66
67
68
69
70
71
72
73 int newPrimaryKey( )
74 {
75 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK );
76 daoUtil.executeQuery( );
77
78 int nKey;
79
80 if ( !daoUtil.next( ) )
81 {
82
83 nKey = 1;
84 }
85
86 nKey = daoUtil.getInt( 1 ) + 1;
87
88 daoUtil.free( );
89
90 return nKey;
91 }
92
93
94
95
96
97 public synchronized void insert( StyleSheet stylesheet )
98 {
99 stylesheet.setId( newPrimaryKey( ) );
100
101 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
102
103 daoUtil.setInt( 1, stylesheet.getId( ) );
104 daoUtil.setString( 2, stylesheet.getDescription( ) );
105 daoUtil.setString( 3, stylesheet.getFile( ) );
106 daoUtil.setBytes( 4, stylesheet.getSource( ) );
107
108 daoUtil.executeUpdate( );
109
110
111 insertStyleModeStyleSheet( stylesheet );
112 daoUtil.free( );
113 }
114
115
116
117
118
119
120 public StyleSheet load( int nIdStylesheet )
121 {
122 StyleSheet stylesheet = null;
123 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
124 daoUtil.setInt( 1, nIdStylesheet );
125 daoUtil.executeQuery( );
126
127 if ( daoUtil.next( ) )
128 {
129 stylesheet = new StyleSheet( );
130 stylesheet.setId( nIdStylesheet );
131 stylesheet.setDescription( daoUtil.getString( 1 ) );
132 stylesheet.setFile( daoUtil.getString( 2 ) );
133 stylesheet.setSource( daoUtil.getBytes( 3 ) );
134 stylesheet.setStyleId( daoUtil.getInt( 4 ) );
135 stylesheet.setModeId( daoUtil.getInt( 5 ) );
136 }
137
138 daoUtil.free( );
139
140 return stylesheet;
141 }
142
143
144
145
146
147
148 public void delete( int nIdStylesheet )
149 {
150 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
151 daoUtil.setInt( 1, nIdStylesheet );
152 daoUtil.executeUpdate( );
153 daoUtil.free( );
154
155
156 deleteStyleModeStyleSheet( nIdStylesheet );
157 }
158
159
160
161
162
163
164 public Collection<StyleSheet> selectStyleSheetList( int nModeId )
165 {
166 Collection<StyleSheet> stylesheetList = new ArrayList<StyleSheet>( );
167
168 String strSelect = " SELECT a.id_stylesheet , a.description , a.file_name ";
169 String strFrom = " FROM core_stylesheet a ";
170
171 if ( nModeId != -1 )
172 {
173 strFrom = " FROM core_stylesheet a , core_style_mode_stylesheet b " +
174 " WHERE a.id_stylesheet = b.id_stylesheet " + " AND b.id_mode = ? ";
175 }
176
177 strFrom += " ORDER BY a.description ";
178
179 String strSQL = strSelect + strFrom;
180
181 DAOUtil daoUtil = new DAOUtil( strSQL );
182
183 if ( nModeId != -1 )
184 {
185 daoUtil.setInt( 1, nModeId );
186 }
187
188 daoUtil.executeQuery( );
189
190 while ( daoUtil.next( ) )
191 {
192 StyleSheet stylesheet = new StyleSheet( );
193
194 stylesheet.setId( daoUtil.getInt( 1 ) );
195 stylesheet.setDescription( daoUtil.getString( 2 ) );
196 stylesheet.setFile( daoUtil.getString( 3 ) );
197 stylesheetList.add( stylesheet );
198 }
199
200 daoUtil.free( );
201
202 return stylesheetList;
203 }
204
205
206
207
208
209 public void store( StyleSheet stylesheet )
210 {
211 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
212 daoUtil.setInt( 1, stylesheet.getId( ) );
213 daoUtil.setString( 2, stylesheet.getDescription( ) );
214 daoUtil.setString( 3, stylesheet.getFile( ) );
215 daoUtil.setBytes( 4, stylesheet.getSource( ) );
216 daoUtil.setInt( 5, stylesheet.getId( ) );
217
218 daoUtil.executeUpdate( );
219 daoUtil.free( );
220
221
222 updateStyleModeStyleSheet( stylesheet );
223 }
224
225
226
227
228
229 private void insertStyleModeStyleSheet( StyleSheet stylesheet )
230 {
231 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_STYLEMODESTYLESHEET );
232
233 daoUtil.setInt( 1, stylesheet.getStyleId( ) );
234 daoUtil.setInt( 2, stylesheet.getModeId( ) );
235 daoUtil.setInt( 3, stylesheet.getId( ) );
236
237 daoUtil.executeUpdate( );
238 daoUtil.free( );
239 }
240
241
242
243
244
245
246 private void updateStyleModeStyleSheet( StyleSheet stylesheet )
247 {
248 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_STYLEMODESTYLESHEET );
249
250 daoUtil.setInt( 1, stylesheet.getStyleId( ) );
251 daoUtil.setInt( 2, stylesheet.getModeId( ) );
252 daoUtil.setInt( 3, stylesheet.getId( ) );
253
254 daoUtil.executeUpdate( );
255 daoUtil.free( );
256 }
257
258
259
260
261
262
263 private void deleteStyleModeStyleSheet( int nStyleSheetId )
264 {
265 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETEE_STYLEMODESTYLESHEET );
266 daoUtil.setInt( 1, nStyleSheetId );
267 daoUtil.executeUpdate( );
268 daoUtil.free( );
269 }
270
271
272
273
274
275
276
277
278 public int selectStyleSheetNbPerStyleMode( int nStyleId, int nModeId )
279 {
280 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_COUNT_STYLESHEET );
281
282 daoUtil.setInt( 1, nStyleId );
283 daoUtil.setInt( 2, nModeId );
284
285 daoUtil.executeQuery( );
286
287 if ( !daoUtil.next( ) )
288 {
289 daoUtil.free( );
290 throw new AppException( DAOUtil.MSG_EXCEPTION_SELECT_ERROR + nModeId + " StyleId " + nStyleId );
291 }
292
293 int nCount = ( daoUtil.getInt( 1 ) );
294
295 daoUtil.free( );
296
297 return nCount;
298 }
299
300
301
302
303
304
305
306 public int selectModeId( int nIdStylesheet )
307 {
308 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_MODEID );
309
310 daoUtil.setInt( 1, nIdStylesheet );
311 daoUtil.executeQuery( );
312
313 if ( !daoUtil.next( ) )
314 {
315 daoUtil.free( );
316 throw new AppException( DAOUtil.MSG_EXCEPTION_SELECT_ERROR + nIdStylesheet );
317 }
318
319 int nModeId = ( daoUtil.getInt( 1 ) );
320
321 daoUtil.free( );
322
323 return nModeId;
324 }
325 }