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.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   * 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_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       * Generates a new primary key
63       * 
64       * @param plugin
65       *            The Plugin
66       * @return The new primary key
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       * {@inheritDoc }
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      * {@inheritDoc }
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      * {@inheritDoc }
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      * {@inheritDoc }
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      * {@inheritDoc }
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      * {@inheritDoc }
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      * {@inheritDoc }
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      * {@inheritDoc }
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      * {@inheritDoc }
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      * {@inheritDoc }
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 }