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.importexport.business.export;
35
36 import fr.paris.lutece.plugins.importexport.service.ImportExportPlugin;
37 import fr.paris.lutece.portal.service.plugin.Plugin;
38 import fr.paris.lutece.portal.service.plugin.PluginService;
39 import fr.paris.lutece.util.sql.DAOUtil;
40
41 import java.util.ArrayList;
42 import java.util.List;
43
44 import org.apache.commons.lang3.StringUtils;
45
46
47
48
49 public class AutomaticExportConfigDAO implements IAutomaticExportConfigDAO
50 {
51 private static final String SQL_QUERY_NEW_PRIMARY_KEY = " SELECT MAX(id) FROM importexport_export_config ";
52 private static final String SQL_QUERY_FIND_CONFIG_BY_PRIMARY_KEY = " SELECT id, table_name, output_file_name, xsl_stylesheet_id, plugin FROM importexport_export_config WHERE id = ? ";
53 private static final String SQL_QUERY_FIND_ALL_CONFIG = " SELECT id, table_name, output_file_name, xsl_stylesheet_id, plugin FROM importexport_export_config ";
54 private static final String SQL_QUERY_INSERT_CONFIG = " INSERT INTO importexport_export_config( id, table_name, output_file_name, xsl_stylesheet_id, plugin ) VALUES (?,?,?,?,?) ";
55 private static final String SQL_QUERY_UPDATE_CONFIG = " UPDATE importexport_export_config SET table_name = ?, output_file_name = ?, xsl_stylesheet_id = ?, plugin = ? WHERE id = ? ";
56 private static final String SQL_QUERY_DELETE_CONFIG = " DELETE FROM importexport_export_config WHERE id = ? ";
57 private static final String SQL_QUERY_FIND_COLUMNS = " SELECT column_name FROM importexport_export_config_columns WHERE id_config = ? ";
58 private static final String SQL_QUERY_INSERT_COLUMNS = " INSERT INTO importexport_export_config_columns(id_config, column_name) VALUES (?,?) ";
59 private static final String SQL_QUERY_DELETE_COLUMNS = " DELETE FROM importexport_export_config_columns WHERE id_config = ? ";
60
61 private Plugin _plugin;
62
63
64
65
66 @Override
67 public AutomaticExportConfig findById( int nId )
68 {
69 AutomaticExportConfig result = null;
70 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_CONFIG_BY_PRIMARY_KEY, getPlugin( ) );
71 daoUtil.setInt( 1, nId );
72 daoUtil.executeQuery( );
73 if ( daoUtil.next( ) )
74 {
75 int nIndex = 1;
76 result = new AutomaticExportConfig( );
77 result.setId( daoUtil.getInt( nIndex++ ) );
78 result.setTableName( daoUtil.getString( nIndex++ ) );
79 result.setOutputFileName( daoUtil.getString( nIndex++ ) );
80 result.setXslStylesheetId( daoUtil.getInt( nIndex++ ) );
81 result.setPlugin( PluginService.getPlugin( daoUtil.getString( nIndex ) ) );
82 }
83 daoUtil.free( );
84 getListColumns( result );
85 return result;
86 }
87
88
89
90
91 @Override
92 public List<AutomaticExportConfig> findAll( boolean bLoadColumns )
93 {
94 List<AutomaticExportConfig> listResult = new ArrayList<AutomaticExportConfig>( );
95 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_ALL_CONFIG, getPlugin( ) );
96 daoUtil.executeQuery( );
97 while ( daoUtil.next( ) )
98 {
99 int nIndex = 1;
100 AutomaticExportConfigrt/business/export/AutomaticExportConfig.html#AutomaticExportConfig">AutomaticExportConfig config = new AutomaticExportConfig( );
101 config.setId( daoUtil.getInt( nIndex++ ) );
102 config.setTableName( daoUtil.getString( nIndex++ ) );
103 config.setOutputFileName( daoUtil.getString( nIndex++ ) );
104 config.setXslStylesheetId( daoUtil.getInt( nIndex++ ) );
105 config.setPlugin( PluginService.getPlugin( daoUtil.getString( nIndex ) ) );
106 listResult.add( config );
107 }
108 daoUtil.free( );
109 if ( bLoadColumns )
110 {
111 for ( AutomaticExportConfig config : listResult )
112 {
113 getListColumns( config );
114 }
115 }
116 return listResult;
117 }
118
119
120
121
122 @Override
123 public synchronized void insert( AutomaticExportConfig config )
124 {
125 config.setId( getNewPrimaryKey( ) );
126 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_CONFIG, getPlugin( ) );
127 int nIndex = 1;
128 daoUtil.setInt( nIndex++, config.getId( ) );
129 daoUtil.setString( nIndex++, config.getTableName( ) );
130 daoUtil.setString( nIndex++, config.getOutputFileName( ) );
131 daoUtil.setInt( nIndex++, config.getXslStylesheetId( ) );
132 if ( config.getPlugin( ) == null )
133 {
134 daoUtil.setString( nIndex, StringUtils.EMPTY );
135 }
136 else
137 {
138 daoUtil.setString( nIndex, config.getPlugin( ).getName( ) );
139 }
140 daoUtil.executeUpdate( );
141 daoUtil.free( );
142 insertColumns( config.getId( ), config.getListColumns( ) );
143 }
144
145
146
147
148 @Override
149 public void update( AutomaticExportConfig config )
150 {
151 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_CONFIG, getPlugin( ) );
152 int nIndex = 1;
153 daoUtil.setString( nIndex++, config.getTableName( ) );
154 daoUtil.setString( nIndex++, config.getOutputFileName( ) );
155 daoUtil.setInt( nIndex++, config.getXslStylesheetId( ) );
156 if ( config.getPlugin( ) == null )
157 {
158 daoUtil.setString( nIndex++, StringUtils.EMPTY );
159 }
160 else
161 {
162 daoUtil.setString( nIndex++, config.getPlugin( ).getName( ) );
163 }
164
165 daoUtil.setInt( nIndex, config.getId( ) );
166 daoUtil.executeUpdate( );
167 daoUtil.free( );
168 removeColumns( config.getId( ) );
169 insertColumns( config.getId( ), config.getListColumns( ) );
170 }
171
172
173
174
175 @Override
176 public void delete( int nId )
177 {
178 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_CONFIG, getPlugin( ) );
179 daoUtil.setInt( 1, nId );
180 daoUtil.executeUpdate( );
181 daoUtil.free( );
182 removeColumns( nId );
183 }
184
185
186
187
188
189
190
191
192 private void getListColumns( AutomaticExportConfig config )
193 {
194 if ( config == null )
195 {
196 return;
197 }
198 List<String> listColumns = new ArrayList<String>( );
199 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_COLUMNS, getPlugin( ) );
200 daoUtil.setInt( 1, config.getId( ) );
201 daoUtil.executeQuery( );
202 while ( daoUtil.next( ) )
203 {
204 listColumns.add( daoUtil.getString( 1 ) );
205 }
206 daoUtil.free( );
207 config.setListColumns( listColumns );
208 }
209
210
211
212
213
214
215
216
217
218 private void insertColumns( int nIdConfig, List<String> listColumns )
219 {
220 if ( listColumns == null )
221 {
222 return;
223 }
224 for ( String strColumn : listColumns )
225 {
226 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_COLUMNS, getPlugin( ) );
227 daoUtil.setInt( 1, nIdConfig );
228 daoUtil.setString( 2, strColumn );
229 daoUtil.executeUpdate( );
230 daoUtil.free( );
231 }
232 }
233
234
235
236
237
238
239
240 private void removeColumns( int nIdConfig )
241 {
242 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_COLUMNS, getPlugin( ) );
243 daoUtil.setInt( 1, nIdConfig );
244 daoUtil.executeUpdate( );
245 daoUtil.free( );
246 }
247
248
249
250
251
252
253 private int getNewPrimaryKey( )
254 {
255 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PRIMARY_KEY, getPlugin( ) );
256 int nResult = 1;
257 daoUtil.executeQuery( );
258 if ( daoUtil.next( ) )
259 {
260 nResult = daoUtil.getInt( 1 ) + 1;
261 }
262 daoUtil.free( );
263 return nResult;
264 }
265
266
267
268
269
270
271 private Plugin getPlugin( )
272 {
273 if ( _plugin == null )
274 {
275 _plugin = ImportExportPlugin.getPlugin( );
276 }
277 return _plugin;
278 }
279
280 }