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.kibana.business;
35
36 import fr.paris.lutece.plugins.elasticdata.service.DataSourceService;
37 import fr.paris.lutece.portal.service.plugin.Plugin;
38 import fr.paris.lutece.util.ReferenceList;
39 import fr.paris.lutece.util.sql.DAOUtil;
40
41 import java.util.ArrayList;
42 import java.util.List;
43
44
45
46
47 public final class DashboardDAO implements IDashboardDAO
48 {
49
50 private static final String SQL_QUERY_NEW_PK = "SELECT max( id_dashboard ) FROM kibana_dashboard";
51 private static final String SQL_QUERY_SELECT = "SELECT id_dashboard, idKibanaDashboard, title, dataSourceName FROM kibana_dashboard WHERE id_dashboard = ?";
52 private static final String SQL_QUERY_SELECT_BY_KIBANA_ID = "SELECT id_dashboard, idKibanaDashboard, title, dataSourceName FROM kibana_dashboard WHERE idKibanaDashboard = ?";
53 private static final String SQL_QUERY_INSERT = "INSERT INTO kibana_dashboard ( id_dashboard, idKibanaDashboard, title, dataSourceName ) VALUES ( ?, ?, ?, ? ) ";
54 private static final String SQL_QUERY_SELECTALL = "SELECT id_dashboard, idKibanaDashboard, title, dataSourceName FROM kibana_dashboard";
55 private static final String SQL_QUERY_SELECTALL_ID = "SELECT id_dashboard FROM kibana_dashboard";
56 private static final String SQL_QUERY_DELETE = "DELETE FROM kibana_dashboard WHERE id_dashboard = ?";
57 private static final String SQL_QUERY_DELETE_ALL = "DELETE FROM kibana_dashboard";
58 private static final String SQL_QUERY_COUNT_DASHBOARD_KIBANA_ID = "SELECT count(*) FROM kibana_dashboard WHERE idKibanaDashboard = ?";
59 private static final String SQL_QUERY_UPDATE = "UPDATE kibana_dashboard SET id_dashboard = ?, idKibanaDashboard = ?, title = ? WHERE id_dashboard = ?";
60
61
62
63
64
65
66
67
68 public int newPrimaryKey( Plugin plugin )
69 {
70 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
71 daoUtil.executeQuery( );
72 int nKey = 1;
73
74 if ( daoUtil.next( ) )
75 {
76 nKey = daoUtil.getInt( 1 ) + 1;
77 }
78
79 daoUtil.free( );
80 return nKey;
81 }
82
83
84
85
86 @Override
87 public void insert( Dashboard dashboard, Plugin plugin )
88 {
89 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
90 dashboard.setId( newPrimaryKey( plugin ) );
91 int nIndex = 1;
92
93 daoUtil.setInt( nIndex++, dashboard.getId( ) );
94 daoUtil.setString( nIndex++, dashboard.getIdKibanaDashboard( ) );
95 daoUtil.setString( nIndex++, dashboard.getTitle( ) );
96 daoUtil.setString( nIndex++, dashboard.getIdDataSource( ) );
97
98 daoUtil.executeUpdate( );
99 daoUtil.free( );
100 }
101
102
103
104
105 @Override
106 public Dashboard load( int nKey, Plugin plugin )
107 {
108 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
109 daoUtil.setInt( 1, nKey );
110 daoUtil.executeQuery( );
111 Dashboard dashboard = null;
112
113 if ( daoUtil.next( ) )
114 {
115 dashboard = new Dashboard( );
116 int nIndex = 1;
117
118 dashboard.setId( daoUtil.getInt( nIndex++ ) );
119 dashboard.setIdKibanaDashboard( daoUtil.getString( nIndex++ ) );
120 dashboard.setTitle( daoUtil.getString( nIndex++ ) );
121 dashboard.setIdDataSource( daoUtil.getString( nIndex++ ) );
122
123 }
124
125 daoUtil.free( );
126 return dashboard;
127 }
128
129
130
131
132 @Override
133 public Dashboard loadByKibanaId( String strIdKibana, Plugin plugin )
134 {
135 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_KIBANA_ID, plugin );
136 daoUtil.setString( 1, strIdKibana );
137 daoUtil.executeQuery( );
138 Dashboard dashboard = null;
139
140 if ( daoUtil.next( ) )
141 {
142 dashboard = new Dashboard( );
143 int nIndex = 1;
144
145 dashboard.setId( daoUtil.getInt( nIndex++ ) );
146 dashboard.setIdKibanaDashboard( daoUtil.getString( nIndex++ ) );
147 dashboard.setTitle( daoUtil.getString( nIndex++ ) );
148 dashboard.setIdDataSource( daoUtil.getString( nIndex++ ) );
149
150 }
151
152 daoUtil.free( );
153 return dashboard;
154 }
155
156
157
158
159 @Override
160 public List<Dashboard> selectDashboardsList( Plugin plugin )
161 {
162 List<Dashboard> dashboardList = new ArrayList<Dashboard>( );
163 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
164 daoUtil.executeQuery( );
165
166 while ( daoUtil.next( ) )
167 {
168 Dashboardna/business/Dashboard.html#Dashboard">Dashboard dashboard = new Dashboard( );
169 int nIndex = 1;
170
171 dashboard.setId( daoUtil.getInt( nIndex++ ) );
172 dashboard.setIdKibanaDashboard( daoUtil.getString( nIndex++ ) );
173 dashboard.setTitle( daoUtil.getString( nIndex++ ) );
174 dashboard.setIdDataSource( daoUtil.getString( nIndex++ ) );
175
176 dashboardList.add( dashboard );
177 }
178
179 daoUtil.free( );
180 return dashboardList;
181 }
182
183
184
185
186 @Override
187 public List<Integer> selectIdDashboardsList( Plugin plugin )
188 {
189 List<Integer> dashboardList = new ArrayList<Integer>( );
190 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_ID, plugin );
191 daoUtil.executeQuery( );
192
193 while ( daoUtil.next( ) )
194 {
195 dashboardList.add( daoUtil.getInt( 1 ) );
196 }
197
198 daoUtil.free( );
199 return dashboardList;
200 }
201
202
203
204
205 @Override
206 public ReferenceList selectDashboardsReferenceList( Plugin plugin )
207 {
208 ReferenceList dashboardList = new ReferenceList( );
209 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
210 daoUtil.executeQuery( );
211
212 while ( daoUtil.next( ) )
213 {
214 dashboardList.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
215 }
216
217 daoUtil.free( );
218 return dashboardList;
219 }
220
221
222
223
224 @Override
225 public void delete( int nIdDashboard, Plugin plugin )
226 {
227 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
228 daoUtil.setInt( 1, nIdDashboard );
229 daoUtil.executeUpdate( );
230 daoUtil.free( );
231 }
232
233
234
235
236 @Override
237 public void deleteAll( Plugin plugin )
238 {
239 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_ALL, plugin );
240
241 daoUtil.executeUpdate( );
242 daoUtil.free( );
243 }
244
245
246
247
248 @Override
249 public boolean isDashboardExists( Dashboard dashboard, Plugin plugin )
250 {
251 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_COUNT_DASHBOARD_KIBANA_ID, plugin );
252 daoUtil.setString( 1, dashboard.getIdKibanaDashboard( ) );
253 daoUtil.executeQuery( );
254 boolean bExist = false;
255 while ( daoUtil.next( ) )
256 {
257 if ( daoUtil.getInt( 1 ) >= 1 )
258 {
259 bExist = true;
260 }
261 }
262 daoUtil.free( );
263 return bExist;
264 }
265
266
267
268
269 @Override
270 public void store( Dashboard dashboard, Plugin plugin )
271 {
272 DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
273 int nIndex = 1;
274
275 daoUtil.setInt( nIndex++, dashboard.getId( ) );
276 daoUtil.setString( nIndex++, dashboard.getIdKibanaDashboard( ) );
277 daoUtil.setString( nIndex++, dashboard.getTitle( ) );
278 daoUtil.setInt( nIndex, dashboard.getId( ) );
279
280 daoUtil.executeUpdate( );
281 daoUtil.free( );
282 }
283 }