View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.portal.business.right;
35  
36  import fr.paris.lutece.util.sql.DAOUtil;
37  
38  import java.util.ArrayList;
39  import java.util.Collection;
40  
41  /**
42   * This class provides Data Access methods for right objects
43   */
44  public final class RightDAO implements IRightDAO
45  {
46      // Constants
47      private static final String SQL_QUERY_SELECT = " SELECT id_right, name, level_right, "
48              + " admin_url, description, plugin_name, id_feature_group, icon_url, documentation_url, id_order, is_external_feature " + " FROM core_admin_right "
49              + " WHERE id_right = ? ";
50      private static final String SQL_QUERY_INSERT = " INSERT INTO core_admin_right ( id_right , name, level_right, admin_url , "
51              + " description, plugin_name, id_feature_group, icon_url, documentation_url, id_order, is_external_feature ) "
52              + " VALUES ( ?, ?, ?, ? , ?, ?, ?, ?, ?, ?, ? )";
53      private static final String SQL_QUERY_UPDATE = " UPDATE core_admin_right SET name = ?, admin_url = ? , description = ? , "
54              + " plugin_name = ?, id_feature_group = ?, icon_url = ?, level_right = ?, documentation_url = ?, id_order = ?, is_external_feature = ? WHERE id_right = ?";
55      private static final String SQL_QUERY_SELECTALL_EXTERNAL_FEATURES = " SELECT id_right, name, level_right, admin_url, description, plugin_name, id_feature_group, icon_url, documentation_url, id_order, is_external_feature "
56              + " FROM core_admin_right WHERE level_right >= ? AND is_external_feature = 1 ORDER BY id_order ASC, id_right ASC";
57      private static final String SQL_QUERY_SELECTALL = " SELECT id_right, name, level_right, admin_url, description, plugin_name, id_feature_group, icon_url, documentation_url, id_order, is_external_feature "
58              + " FROM core_admin_right WHERE level_right >= ? ORDER BY id_order ASC, id_right ASC";
59      private static final String SQL_QUERY_SELECTALL_FOR_FEATUREGROUP = " SELECT id_right, name, level_right, admin_url, description, plugin_name, id_feature_group, icon_url, documentation_url, id_order, is_external_feature "
60              + " FROM core_admin_right WHERE id_feature_group = ? ORDER BY id_order ASC, id_right ASC";
61      private static final String SQL_QUERY_SELECTALL_FOR_FEATUREGROUP_IS_NULL = " SELECT id_right, name, level_right, admin_url, description, plugin_name, id_feature_group, icon_url, documentation_url, id_order, is_external_feature "
62              + " FROM core_admin_right WHERE id_feature_group IS null ORDER BY id_order ASC, id_right ASC";
63      private static final String SQL_QUERY_DELETE_USERRIGHT = " DELETE FROM core_user_right WHERE id_right = ?";
64      private static final String SQL_QUERY_DELETE_ADMINRIGHT = " DELETE FROM core_admin_right WHERE id_right = ?";
65  
66      // /////////////////////////////////////////////////////////////////////////////////////
67      // Access methods to data
68  
69      /**
70       * Insert a new record in the table.
71       * 
72       * @param right
73       *            instance of the right to insert
74       */
75      public void insert( Right right )
76      {
77          try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT ) )
78          {
79  
80              daoUtil.setString( 1, right.getId( ) );
81              daoUtil.setString( 2, right.getNameKey( ) );
82              daoUtil.setInt( 3, right.getLevel( ) );
83              daoUtil.setString( 4, right.getUrl( ) );
84              daoUtil.setString( 5, right.getDescriptionKey( ) );
85              daoUtil.setString( 6, right.getPluginName( ) );
86              daoUtil.setString( 7, right.getFeatureGroup( ) );
87              daoUtil.setString( 8, right.getIconUrl( ) );
88              daoUtil.setString( 9, right.getDocumentationUrl( ) );
89              daoUtil.setInt( 10, right.getOrder( ) );
90              daoUtil.setBoolean( 11, right.isExternalFeature( ) );
91  
92              daoUtil.executeUpdate( );
93          }
94      }
95  
96      /**
97       * load the data of the right from the table
98       * 
99       * @param strId
100      *            The identifier of the admin right
101      * @return The instance of the admin right
102      */
103     public Right load( String strId )
104     {
105         Right right = null;
106         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) )
107         {
108             daoUtil.setString( 1, strId );
109 
110             daoUtil.executeQuery( );
111 
112             if ( daoUtil.next( ) )
113             {
114                 right = new Right( );
115                 right.setId( daoUtil.getString( 1 ) );
116                 right.setNameKey( daoUtil.getString( 2 ) );
117                 right.setLevel( daoUtil.getInt( 3 ) );
118                 right.setUrl( daoUtil.getString( 4 ) );
119                 right.setDescriptionKey( daoUtil.getString( 5 ) );
120                 right.setPluginName( daoUtil.getString( 6 ) );
121                 right.setFeatureGroup( daoUtil.getString( 7 ) );
122                 right.setIconUrl( daoUtil.getString( 8 ) );
123                 right.setDocumentationUrl( daoUtil.getString( 9 ) );
124                 right.setOrder( daoUtil.getInt( 10 ) );
125                 right.setExternalFeature( daoUtil.getBoolean( 11 ) );
126             }
127 
128         }
129 
130         return right;
131     }
132 
133     /**
134      * Delete a record from the table
135      * 
136      * @param strIdRight
137      *            string identifier of the admin right to delete
138      */
139     public void delete( String strIdRight )
140     {
141         // Clear user right
142         try ( DAOUtilUtil.html#DAOUtil">DAOUtil daoUtilUser = new DAOUtil( SQL_QUERY_DELETE_USERRIGHT ) )
143         {
144             daoUtilUser.setString( 1, strIdRight );
145             daoUtilUser.executeUpdate( );
146         }
147 
148         // Clear admin right
149         try ( DAOUtiltil.html#DAOUtil">DAOUtil daoUtilAdmin = new DAOUtil( SQL_QUERY_DELETE_ADMINRIGHT ) )
150         {
151             daoUtilAdmin.setString( 1, strIdRight );
152             daoUtilAdmin.executeUpdate( );
153         }
154     }
155 
156     /**
157      * Update the record in the table
158      * 
159      * @param right
160      *            the reference of the admin right
161      */
162     public void store( Right right )
163     {
164         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE ) )
165         {
166 
167             daoUtil.setString( 1, right.getNameKey( ) );
168             daoUtil.setString( 2, right.getUrl( ) );
169             daoUtil.setString( 3, right.getDescriptionKey( ) );
170             daoUtil.setString( 4, right.getPluginName( ) );
171             daoUtil.setString( 5, right.getFeatureGroup( ) );
172             daoUtil.setString( 6, right.getIconUrl( ) );
173             daoUtil.setInt( 7, right.getLevel( ) );
174             daoUtil.setString( 8, right.getDocumentationUrl( ) );
175             daoUtil.setInt( 9, right.getOrder( ) );
176             daoUtil.setBoolean( 10, right.isExternalFeature( ) );
177             daoUtil.setString( 11, right.getId( ) );
178 
179             daoUtil.executeUpdate( );
180         }
181     }
182 
183     /**
184      * Loads the data of all the rights and returns them in form of a collection
185      * 
186      * @return the collection which contains the data of all the rights
187      */
188     public Collection<Right> selectRightsList( )
189     {
190         return selectRightsList( 0 );
191     }
192 
193     /**
194      * Loads the data of all the rights with level greater or equal than nLevel and returns them in form of a collection
195      *
196      * @param nLevel
197      *            The right level
198      * @return the collection which contains the data of all the rights
199      */
200     public Collection<Right> selectRightsList( int nLevel )
201     {
202         Collection<Right> rightList = new ArrayList<>( );
203         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL ) )
204         {
205             daoUtil.setInt( 1, nLevel );
206             daoUtil.executeQuery( );
207 
208             while ( daoUtil.next( ) )
209             {
210                 Rightal/business/right/Right.html#Right">Right right = new Right( );
211 
212                 right.setId( daoUtil.getString( 1 ) );
213                 right.setNameKey( daoUtil.getString( 2 ) );
214                 right.setLevel( daoUtil.getInt( 3 ) );
215                 right.setUrl( daoUtil.getString( 4 ) );
216                 right.setDescriptionKey( daoUtil.getString( 5 ) );
217                 right.setPluginName( daoUtil.getString( 6 ) );
218                 right.setFeatureGroup( daoUtil.getString( 7 ) );
219                 right.setIconUrl( daoUtil.getString( 8 ) );
220                 right.setDocumentationUrl( daoUtil.getString( 9 ) );
221                 right.setOrder( daoUtil.getInt( 10 ) );
222                 right.setExternalFeature( daoUtil.getBoolean( 11 ) );
223 
224                 rightList.add( right );
225             }
226 
227         }
228 
229         return rightList;
230     }
231 
232     /**
233      * Loads the data of all the external admin features and returns them in form of a collection
234      * 
235      * @return the collection which contains the data of all the external admin features
236      */
237     public Collection<Right> selectExternalRightsList( )
238     {
239         return selectExternalRightsList( 0 );
240     }
241 
242     /**
243      * Loads the data of all the external admin features with level greater or equal than nLevel and returns them in form of a collection
244      *
245      * @param nLevel
246      *            The right level
247      * @return the collection which contains the data of all the external admin features
248      */
249     @Override
250     public Collection<Right> selectExternalRightsList( int nLevel )
251     {
252         Collection<Right> rightList = new ArrayList<>( );
253         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_EXTERNAL_FEATURES ) )
254         {
255             daoUtil.setInt( 1, nLevel );
256             daoUtil.executeQuery( );
257 
258             while ( daoUtil.next( ) )
259             {
260                 Rightal/business/right/Right.html#Right">Right right = new Right( );
261 
262                 right.setId( daoUtil.getString( 1 ) );
263                 right.setNameKey( daoUtil.getString( 2 ) );
264                 right.setLevel( daoUtil.getInt( 3 ) );
265                 right.setUrl( daoUtil.getString( 4 ) );
266                 right.setDescriptionKey( daoUtil.getString( 5 ) );
267                 right.setPluginName( daoUtil.getString( 6 ) );
268                 right.setFeatureGroup( daoUtil.getString( 7 ) );
269                 right.setIconUrl( daoUtil.getString( 8 ) );
270                 right.setDocumentationUrl( daoUtil.getString( 9 ) );
271                 right.setOrder( daoUtil.getInt( 10 ) );
272                 right.setExternalFeature( daoUtil.getBoolean( 11 ) );
273 
274                 rightList.add( right );
275             }
276 
277         }
278 
279         return rightList;
280     }
281 
282     /**
283      * Loads the data of all the rights with the specified feature group and returns them in form of a collection
284      *
285      * @param strFeatureGroup
286      *            the name of the feature group
287      * @return the collection which contains the data of all the rights
288      */
289     public Collection<Right> selectRightsList( String strFeatureGroup )
290     {
291         Collection<Right> rightList = new ArrayList<>( );
292         String strQuery = SQL_QUERY_SELECTALL_FOR_FEATUREGROUP;
293 
294         if ( strFeatureGroup == null )
295         {
296             strQuery = SQL_QUERY_SELECTALL_FOR_FEATUREGROUP_IS_NULL;
297         }
298 
299         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( strQuery ) )
300         {
301 
302             if ( strFeatureGroup != null )
303             {
304                 daoUtil.setString( 1, strFeatureGroup );
305             }
306 
307             daoUtil.executeQuery( );
308 
309             while ( daoUtil.next( ) )
310             {
311                 Rightal/business/right/Right.html#Right">Right right = new Right( );
312 
313                 right.setId( daoUtil.getString( 1 ) );
314                 right.setNameKey( daoUtil.getString( 2 ) );
315                 right.setLevel( daoUtil.getInt( 3 ) );
316                 right.setUrl( daoUtil.getString( 4 ) );
317                 right.setDescriptionKey( daoUtil.getString( 5 ) );
318                 right.setPluginName( daoUtil.getString( 6 ) );
319                 right.setFeatureGroup( daoUtil.getString( 7 ) );
320                 right.setIconUrl( daoUtil.getString( 8 ) );
321                 right.setDocumentationUrl( daoUtil.getString( 9 ) );
322                 right.setOrder( daoUtil.getInt( 10 ) );
323                 right.setExternalFeature( daoUtil.getBoolean( 11 ) );
324 
325                 rightList.add( right );
326             }
327 
328         }
329 
330         return rightList;
331     }
332 }