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.plugins.calendar.business.stylesheet;
35
36 import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
37 import fr.paris.lutece.portal.service.plugin.Plugin;
38 import fr.paris.lutece.util.sql.DAOUtil;
39
40 import java.util.ArrayList;
41 import java.util.Collection;
42
43 import org.apache.commons.lang.StringUtils;
44
45
46
47
48
49 public final class CalendarStyleSheetDAO implements ICalendarStyleSheetDAO
50 {
51
52 private static final String SQL_QUERY_NEW_PK = " SELECT max(id_stylesheet) FROM calendar_export_stylesheets ";
53 private static final String SQL_QUERY_SELECT = " SELECT a.description , a.file_name , a.source"
54 + " FROM calendar_export_stylesheets a WHERE a.id_stylesheet = ? ";
55 private static final String SQL_QUERY_SELECT_LIST = " SELECT a.id_stylesheet, a.description , a.file_name"
56 + " FROM calendar_export_stylesheets a where a.description NOT LIKE '%rss%' ORDER BY a.description ";
57 private static final String SQL_QUERY_INSERT = " INSERT INTO calendar_export_stylesheets ( id_stylesheet , description , file_name, source ) "
58 + " VALUES ( ?, ? ,?, ? )";
59 private static final String SQL_QUERY_DELETE = " DELETE FROM calendar_export_stylesheets WHERE id_stylesheet = ? ";
60 private static final String SQL_QUERY_UPDATE = " UPDATE calendar_export_stylesheets SET id_stylesheet = ?, description = ?, file_name = ?, source = ? WHERE id_stylesheet = ? ";
61 private static final String SQL_QUERY_UPDATE_EXTENSION = " UPDATE calendar_export_stylesheets SET extension = ? WHERE id_stylesheet = ? ";
62 private static final String SQL_QUERY_SELECT_EXTENSION = " SELECT a.extension FROM calendar_export_stylesheets a WHERE a.id_stylesheet = ? ";
63 private static final String SQL_QUERY_INSERT_EXTENSION = " UPDATE calendar_export_stylesheets SET extension = ? WHERE id_stylesheet = ? ";
64
65
66
67
68
69
70
71
72
73 int newPrimaryKey( Plugin plugin )
74 {
75 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
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
98
99 public synchronized void insert( StyleSheet stylesheet, String strExtension, Plugin plugin )
100 {
101 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
102
103 stylesheet.setId( newPrimaryKey( plugin ) );
104
105 daoUtil.setInt( 1, stylesheet.getId( ) );
106 daoUtil.setString( 2, stylesheet.getDescription( ) );
107 daoUtil.setString( 3, stylesheet.getFile( ) );
108 daoUtil.setBytes( 4, stylesheet.getSource( ) );
109
110 daoUtil.executeUpdate( );
111 daoUtil.free( );
112
113 insertExtension( stylesheet.getId( ), strExtension, plugin );
114 }
115
116
117
118
119
120
121
122 public StyleSheet load( int nIdStylesheet, Plugin plugin )
123 {
124 StyleSheet stylesheet = null;
125 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
126 daoUtil.setInt( 1, nIdStylesheet );
127 daoUtil.executeQuery( );
128
129 if ( daoUtil.next( ) )
130 {
131 stylesheet = new StyleSheet( );
132 stylesheet.setId( nIdStylesheet );
133 stylesheet.setDescription( daoUtil.getString( 1 ) );
134 stylesheet.setFile( daoUtil.getString( 2 ) );
135 stylesheet.setSource( daoUtil.getBytes( 3 ) );
136 }
137
138 daoUtil.free( );
139
140 return stylesheet;
141 }
142
143
144
145
146
147
148
149 public void delete( int nIdStylesheet, Plugin plugin )
150 {
151 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
152 daoUtil.setInt( 1, nIdStylesheet );
153 daoUtil.executeUpdate( );
154 daoUtil.free( );
155 }
156
157
158
159
160
161
162
163 public Collection<StyleSheet> selectStyleSheetList( Plugin plugin )
164 {
165 Collection<StyleSheet> stylesheetList = new ArrayList<StyleSheet>( );
166
167 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_LIST, plugin );
168
169 daoUtil.executeQuery( );
170
171 while ( daoUtil.next( ) )
172 {
173 StyleSheet stylesheet = new StyleSheet( );
174
175 stylesheet.setId( daoUtil.getInt( 1 ) );
176 stylesheet.setDescription( daoUtil.getString( 2 ) );
177 stylesheet.setFile( daoUtil.getString( 3 ) );
178 stylesheetList.add( stylesheet );
179 }
180
181 daoUtil.free( );
182
183 return stylesheetList;
184 }
185
186
187
188
189
190
191 public void store( StyleSheet stylesheet, Plugin plugin )
192 {
193 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
194 daoUtil.setInt( 1, stylesheet.getId( ) );
195 daoUtil.setString( 2, stylesheet.getDescription( ) );
196 daoUtil.setString( 3, stylesheet.getFile( ) );
197 daoUtil.setBytes( 4, stylesheet.getSource( ) );
198 daoUtil.setInt( 5, stylesheet.getId( ) );
199 daoUtil.executeUpdate( );
200 daoUtil.free( );
201 }
202
203
204
205
206
207
208
209 public void insertExtension( int nId, String strExtension, Plugin plugin )
210 {
211 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_EXTENSION, plugin );
212 daoUtil.setString( 1, strExtension );
213 daoUtil.setInt( 2, nId );
214 daoUtil.executeUpdate( );
215 daoUtil.free( );
216 }
217
218
219
220
221
222
223
224 public String selectExtension( int nId, Plugin plugin )
225 {
226 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_EXTENSION, plugin );
227 daoUtil.setInt( 1, nId );
228 daoUtil.executeQuery( );
229
230 String strExtension = StringUtils.EMPTY;
231
232 if ( daoUtil.next( ) )
233 {
234 strExtension = daoUtil.getString( 1 );
235 }
236
237 daoUtil.free( );
238
239 return strExtension;
240 }
241
242
243
244
245
246
247
248 public void updateExtension( int nId, String strExtension, Plugin plugin )
249 {
250 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_EXTENSION, plugin );
251 daoUtil.setString( 1, strExtension );
252 daoUtil.setInt( 2, nId );
253 daoUtil.executeUpdate( );
254 daoUtil.free( );
255 }
256 }