View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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  /**
43   * This class provides Data Access methods for right objects
44   */
45  public final class RightDAO implements IRightDAO
46  {
47      // Constants
48      private static final String SQL_QUERY_SELECT = " SELECT id_right, name, level_right, " +
49          " admin_url, description, plugin_name, id_feature_group, icon_url, documentation_url, id_order " +
50          " FROM core_admin_right " + " WHERE id_right = ? ";
51      private static final String SQL_QUERY_INSERT = " INSERT INTO core_admin_right ( id_right , name, level_right, admin_url , " +
52          " description, plugin_name, id_feature_group, icon_url, documentation_url, id_order ) " +
53          " VALUES ( ?, ?, ?, ? , ?, ?, ?, ?, ?, ? )";
54      private static final String SQL_QUERY_UPDATE = " UPDATE core_admin_right SET name = ?, admin_url = ? , description = ? , " +
55          " plugin_name = ?, id_feature_group = ?, icon_url = ?, level_right = ?, documentation_url = ?, id_order = ? WHERE id_right = ?";
56      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 " +
57          " FROM core_admin_right WHERE level_right >= ? ORDER BY id_order ASC, id_right ASC";
58      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 " +
59          " FROM core_admin_right WHERE id_feature_group = ? ORDER BY id_order ASC, id_right ASC";
60      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 " +
61          " FROM core_admin_right WHERE id_feature_group IS null ORDER BY id_order ASC, id_right ASC";
62      private static final String SQL_QUERY_DELETE_USERRIGHT = " DELETE FROM core_user_right WHERE id_right = ?";
63      private static final String SQL_QUERY_DELETE_ADMINRIGHT = " DELETE FROM core_admin_right WHERE id_right = ?";
64  
65      ///////////////////////////////////////////////////////////////////////////////////////
66      //Access methods to data
67  
68      /**
69       * Insert a new record in the table.
70       * @param right instance of the right to insert
71       */
72      public void insert( Right right )
73      {
74          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
75  
76          daoUtil.setString( 1, right.getId(  ) );
77          daoUtil.setString( 2, right.getNameKey(  ) );
78          daoUtil.setInt( 3, right.getLevel(  ) );
79          daoUtil.setString( 4, right.getUrl(  ) );
80          daoUtil.setString( 5, right.getDescriptionKey(  ) );
81          daoUtil.setString( 6, right.getPluginName(  ) );
82          daoUtil.setString( 7, right.getFeatureGroup(  ) );
83          daoUtil.setString( 8, right.getIconUrl(  ) );
84          daoUtil.setString( 9, right.getDocumentationUrl(  ) );
85          daoUtil.setInt( 10, right.getOrder(  ) );
86  
87          daoUtil.executeUpdate(  );
88          daoUtil.free(  );
89      }
90  
91      /**
92       * load the data of the right from the table
93       * @param strId The identifier of the admin right
94       * @return The instance of the admin right
95       */
96      public Right load( String strId )
97      {
98          Right right = null;
99          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
100         daoUtil.setString( 1, strId );
101 
102         daoUtil.executeQuery(  );
103 
104         if ( daoUtil.next(  ) )
105         {
106             right = new Right(  );
107             right.setId( daoUtil.getString( 1 ) );
108             right.setNameKey( daoUtil.getString( 2 ) );
109             right.setLevel( daoUtil.getInt( 3 ) );
110             right.setUrl( daoUtil.getString( 4 ) );
111             right.setDescriptionKey( daoUtil.getString( 5 ) );
112             right.setPluginName( daoUtil.getString( 6 ) );
113             right.setFeatureGroup( daoUtil.getString( 7 ) );
114             right.setIconUrl( daoUtil.getString( 8 ) );
115             right.setDocumentationUrl( daoUtil.getString( 9 ) );
116             right.setOrder( daoUtil.getInt( 10 ) );
117         }
118 
119         daoUtil.free(  );
120 
121         return right;
122     }
123 
124     /**
125      * Delete a record from the table
126      * @param strIdRight string identifier of the admin right to delete
127      */
128     public void delete( String strIdRight )
129     {
130         // Clear user right
131         DAOUtil daoUtilUser = new DAOUtil( SQL_QUERY_DELETE_USERRIGHT );
132         daoUtilUser.setString( 1, strIdRight );
133         daoUtilUser.executeUpdate(  );
134         daoUtilUser.free(  );
135 
136         // Clear admin right
137         DAOUtil daoUtilAdmin = new DAOUtil( SQL_QUERY_DELETE_ADMINRIGHT );
138         daoUtilAdmin.setString( 1, strIdRight );
139         daoUtilAdmin.executeUpdate(  );
140         daoUtilAdmin.free(  );
141     }
142 
143     /**
144      * Update the record in the table
145      * @param right the reference of the admin right
146      */
147     public void store( Right right )
148     {
149         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
150 
151         daoUtil.setString( 1, right.getNameKey(  ) );
152         daoUtil.setString( 2, right.getUrl(  ) );
153         daoUtil.setString( 3, right.getDescriptionKey(  ) );
154         daoUtil.setString( 4, right.getPluginName(  ) );
155         daoUtil.setString( 5, right.getFeatureGroup(  ) );
156         daoUtil.setString( 6, right.getIconUrl(  ) );
157         daoUtil.setInt( 7, right.getLevel(  ) );
158         daoUtil.setString( 8, right.getDocumentationUrl(  ) );
159         daoUtil.setInt( 9, right.getOrder(  ) );
160         daoUtil.setString( 10, right.getId(  ) );
161 
162         daoUtil.executeUpdate(  );
163         daoUtil.free(  );
164     }
165 
166     /**
167      * Loads the data of all the rights and returns them in form of a collection
168      * @return the collection which contains the data of all the rights
169      */
170     public Collection<Right> selectRightsList(  )
171     {
172         return selectRightsList( 0 );
173     }
174 
175     /**
176      * Loads the data of all the rights with level greater or equal than nLevel
177      * and returns them in form of a collection
178      *
179      * @param nLevel The right level
180      * @return the collection which contains the data of all the rights
181      */
182     public Collection<Right> selectRightsList( int nLevel )
183     {
184         Collection<Right> rightList = new ArrayList<Right>(  );
185         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL );
186         daoUtil.setInt( 1, nLevel );
187         daoUtil.executeQuery(  );
188 
189         while ( daoUtil.next(  ) )
190         {
191             Right right = new Right(  );
192 
193             right.setId( daoUtil.getString( 1 ) );
194             right.setNameKey( daoUtil.getString( 2 ) );
195             right.setLevel( daoUtil.getInt( 3 ) );
196             right.setUrl( daoUtil.getString( 4 ) );
197             right.setDescriptionKey( daoUtil.getString( 5 ) );
198             right.setPluginName( daoUtil.getString( 6 ) );
199             right.setFeatureGroup( daoUtil.getString( 7 ) );
200             right.setIconUrl( daoUtil.getString( 8 ) );
201             right.setDocumentationUrl( daoUtil.getString( 9 ) );
202             right.setOrder( daoUtil.getInt( 10 ) );
203 
204             rightList.add( right );
205         }
206 
207         daoUtil.free(  );
208 
209         return rightList;
210     }
211 
212     /**
213      * Loads the data of all the rights with the specified feature group
214      * and returns them in form of a collection
215      *
216      * @param strFeatureGroup the name of the feature group
217      * @return the collection which contains the data of all the rights
218      */
219     public Collection<Right> selectRightsList( String strFeatureGroup )
220     {
221         Collection<Right> rightList = new ArrayList<Right>(  );
222         String strQuery = SQL_QUERY_SELECTALL_FOR_FEATUREGROUP;
223 
224         if ( strFeatureGroup == null )
225         {
226             strQuery = SQL_QUERY_SELECTALL_FOR_FEATUREGROUP_IS_NULL;
227         }
228 
229         DAOUtil daoUtil = new DAOUtil( strQuery );
230 
231         if ( strFeatureGroup != null )
232         {
233             daoUtil.setString( 1, strFeatureGroup );
234         }
235 
236         daoUtil.executeQuery(  );
237 
238         while ( daoUtil.next(  ) )
239         {
240             Right right = new Right(  );
241 
242             right.setId( daoUtil.getString( 1 ) );
243             right.setNameKey( daoUtil.getString( 2 ) );
244             right.setLevel( daoUtil.getInt( 3 ) );
245             right.setUrl( daoUtil.getString( 4 ) );
246             right.setDescriptionKey( daoUtil.getString( 5 ) );
247             right.setPluginName( daoUtil.getString( 6 ) );
248             right.setFeatureGroup( daoUtil.getString( 7 ) );
249             right.setIconUrl( daoUtil.getString( 8 ) );
250             right.setDocumentationUrl( daoUtil.getString( 9 ) );
251             right.setOrder( daoUtil.getInt( 10 ) );
252 
253             rightList.add( right );
254         }
255 
256         daoUtil.free(  );
257 
258         return rightList;
259     }
260 }