View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.kibana.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.ReferenceList;
38  import fr.paris.lutece.util.sql.DAOUtil;
39  
40  import java.sql.Statement;
41  import java.util.ArrayList;
42  import java.util.List;
43  
44  /**
45   * This class provides Data Access methods for Dashboard objects
46   */
47  public final class DashboardDAO implements IDashboardDAO
48  {
49      // Constants
50      private static final String SQL_QUERY_SELECT = "SELECT id_dashboard, idKibanaDashboard, title, dataSourceName FROM kibana_dashboard WHERE id_dashboard = ?";
51      private static final String SQL_QUERY_SELECT_BY_KIBANA_ID = "SELECT id_dashboard, idKibanaDashboard, title, dataSourceName FROM kibana_dashboard WHERE idKibanaDashboard = ?";
52      private static final String SQL_QUERY_INSERT = "INSERT INTO kibana_dashboard ( idKibanaDashboard, title, dataSourceName ) VALUES ( ?, ?, ? ) ";
53      private static final String SQL_QUERY_SELECTALL = "SELECT id_dashboard, idKibanaDashboard, title, dataSourceName FROM kibana_dashboard";
54      private static final String SQL_QUERY_SELECTALL_ID = "SELECT id_dashboard FROM kibana_dashboard";
55      private static final String SQL_QUERY_DELETE = "DELETE FROM kibana_dashboard WHERE id_dashboard = ?";
56      private static final String SQL_QUERY_DELETE_ALL = "DELETE FROM kibana_dashboard";
57      private static final String SQL_QUERY_COUNT_DASHBOARD_KIBANA_ID = "SELECT count(*) FROM kibana_dashboard WHERE idKibanaDashboard = ?";
58      private static final String SQL_QUERY_UPDATE = "UPDATE kibana_dashboard SET id_dashboard = ?, idKibanaDashboard = ?, title = ? WHERE id_dashboard = ?";
59  
60      /**
61       * {@inheritDoc }
62       */
63      @Override
64      public void insert( Dashboard dashboard, Plugin plugin )
65      {
66      	try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, plugin ) )
67          {
68              int nIndex = 1;
69  
70              daoUtil.setString( nIndex++, dashboard.getIdKibanaDashboard( ) );
71              daoUtil.setString( nIndex++, dashboard.getTitle( ) );
72              daoUtil.setString( nIndex++, dashboard.getIdDataSource( ) );
73  
74              daoUtil.executeUpdate( );
75              if ( daoUtil.nextGeneratedKey( ) )
76              {
77                  dashboard.setId( daoUtil.getGeneratedKeyInt( 1 ) );
78              }
79          }
80      }
81  
82      /**
83       * {@inheritDoc }
84       */
85      @Override
86      public Dashboard load( int nKey, Plugin plugin )
87      {
88          Dashboard dashboard = null;
89          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
90          {
91              daoUtil.setInt( 1, nKey );
92              daoUtil.executeQuery( );
93  
94              if ( daoUtil.next( ) )
95              {
96                  dashboard = new Dashboard( );
97                  int nIndex = 1;
98  
99                  dashboard.setId( daoUtil.getInt( nIndex++ ) );
100                 dashboard.setIdKibanaDashboard( daoUtil.getString( nIndex++ ) );
101                 dashboard.setTitle( daoUtil.getString( nIndex++ ) );
102                 dashboard.setIdDataSource( daoUtil.getString( nIndex++ ) );
103             }
104         }
105         return dashboard;
106     }
107 
108     /**
109      * {@inheritDoc }
110      */
111     @Override
112     public Dashboard loadByKibanaId( String strIdKibana, Plugin plugin )
113     {
114     	Dashboard dashboard = null;
115     	try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_KIBANA_ID, plugin ) )
116         {
117             daoUtil.setString( 1, strIdKibana );
118             daoUtil.executeQuery( );
119 
120             if ( daoUtil.next( ) )
121             {
122                 dashboard = new Dashboard( );
123                 int nIndex = 1;
124 
125                 dashboard.setId( daoUtil.getInt( nIndex++ ) );
126                 dashboard.setIdKibanaDashboard( daoUtil.getString( nIndex++ ) );
127                 dashboard.setTitle( daoUtil.getString( nIndex++ ) );
128                 dashboard.setIdDataSource( daoUtil.getString( nIndex++ ) );
129             }
130         }
131         return dashboard;
132     }
133 
134     /**
135      * {@inheritDoc }
136      */
137     @Override
138     public List<Dashboard> selectDashboardsList( Plugin plugin )
139     {
140         List<Dashboard> dashboardList = new ArrayList<>( );
141         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
142         {
143             daoUtil.executeQuery( );
144 
145             while ( daoUtil.next( ) )
146             {
147                 Dashboardna/business/Dashboard.html#Dashboard">Dashboard dashboard = new Dashboard( );
148                 int nIndex = 1;
149 
150                 dashboard.setId( daoUtil.getInt( nIndex++ ) );
151                 dashboard.setIdKibanaDashboard( daoUtil.getString( nIndex++ ) );
152                 dashboard.setTitle( daoUtil.getString( nIndex++ ) );
153                 dashboard.setIdDataSource( daoUtil.getString( nIndex++ ) );
154 
155                 dashboardList.add( dashboard );
156             }
157         }
158         return dashboardList;
159     }
160 
161     /**
162      * {@inheritDoc }
163      */
164     @Override
165     public List<Integer> selectIdDashboardsList( Plugin plugin )
166     {
167         List<Integer> dashboardList = new ArrayList<>( );
168         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_ID, plugin ) )
169         {
170             daoUtil.executeQuery( );
171 
172             while ( daoUtil.next( ) )
173             {
174                 dashboardList.add( daoUtil.getInt( 1 ) );
175             }
176         }
177         return dashboardList;
178     }
179 
180     /**
181      * {@inheritDoc }
182      */
183     @Override
184     public ReferenceList selectDashboardsReferenceList( Plugin plugin )
185     {
186         ReferenceList dashboardList = new ReferenceList( );
187         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
188         {
189             daoUtil.executeQuery( );
190 
191             while ( daoUtil.next( ) )
192             {
193                 dashboardList.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
194             }
195         }
196         return dashboardList;
197     }
198 
199     /**
200      * {@inheritDoc }
201      */
202     @Override
203     public void delete( int nIdDashboard, Plugin plugin )
204     {
205     	try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ))
206         {
207             daoUtil.setInt( 1, nIdDashboard );
208             daoUtil.executeUpdate( );
209         }
210     }
211 
212     /**
213      * {@inheritDoc }
214      */
215     @Override
216     public void deleteAll( Plugin plugin )
217     {
218     	try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_ALL ) )
219         {
220             daoUtil.executeUpdate( );
221         }
222     }
223 
224     /**
225      * {@inheritDoc }
226      */
227     @Override
228     public boolean isDashboardExists( Dashboard dashboard, Plugin plugin )
229     {
230     	boolean bExist = false;
231     	try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_COUNT_DASHBOARD_KIBANA_ID, plugin ) )
232     	{
233             daoUtil.setString( 1, dashboard.getIdKibanaDashboard( ) );
234             daoUtil.executeQuery( );
235 
236             while ( daoUtil.next( ) )
237             {
238                 if ( daoUtil.getInt( 1 ) >= 1 )
239                 {
240                     bExist = true;
241                 }
242             }
243     	}
244         return bExist;
245     }
246 
247     /**
248      * {@inheritDoc }
249      */
250     @Override
251     public void store( Dashboard dashboard, Plugin plugin )
252     {
253     	try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin )  )
254     	{	
255             int nIndex = 1;
256 
257             daoUtil.setInt( nIndex++, dashboard.getId( ) );
258             daoUtil.setString( nIndex++, dashboard.getIdKibanaDashboard( ) );
259             daoUtil.setString( nIndex++, dashboard.getTitle( ) );
260             daoUtil.setInt( nIndex, dashboard.getId( ) );
261 
262             daoUtil.executeUpdate( );
263     	}
264     }
265 }