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.portal.service.spring.SpringContextService;
37  
38  import java.util.Collection;
39  
40  /**
41   * This class provides instances management methods (create, find, ...) for Right objects
42   */
43  public final class RightHome
44  {
45      // Constants
46      private static final int CONSTANT_ERROR_ORDER = -2; // this value must be negative
47      private static final int CONSTANT_STEP_ORDER = 1;
48      private static final int CONSTANT_FIRST_ID_ORDER = 1;
49  
50      // Static variable pointed at the DAO instance
51      private static IRightDAO _dao = SpringContextService.getBean( "rightDAO" );
52  
53      /**
54       * Creates a new RightHome object.
55       */
56      private RightHome( )
57      {
58      }
59  
60      /**
61       * Creation of an instance of an admin right
62       *
63       * @param right
64       *            An instance of an admin right which contains the informations to store
65       * @return The instance of an admin right which has been created with its primary key.
66       */
67      public static Rightf="../../../../../../fr/paris/lutece/portal/business/right/Right.html#Right">Right create( Right right )
68      {
69          right.setOrder( getRightsList( right.getFeatureGroup( ) ).size( ) + CONSTANT_STEP_ORDER );
70          _dao.insert( right );
71  
72          return right;
73      }
74  
75      /**
76       * Update of the admin right which is specified
77       *
78       * @param right
79       *            The instance of the admin right which contains the data to store
80       * @return The instance of the admin right which has been updated
81       */
82      public static Rightf="../../../../../../fr/paris/lutece/portal/business/right/Right.html#Right">Right update( Right right )
83      {
84          Right oldRight = findByPrimaryKey( right.getId( ) );
85  
86          if ( oldRight == null )
87          {
88              return null;
89          }
90  
91          // The feature group have changed
92          if ( ( ( right.getFeatureGroup( ) != null ) && !right.getFeatureGroup( ).equals( oldRight.getFeatureGroup( ) ) )
93                  || ( ( right.getFeatureGroup( ) == null ) && ( oldRight.getFeatureGroup( ) != null ) ) )
94          {
95              deleteEntryFromList( oldRight.getFeatureGroup( ), oldRight.getOrder( ) );
96              right.setOrder( getRightsList( right.getFeatureGroup( ) ).size( ) + CONSTANT_STEP_ORDER );
97          }
98  
99          // The order have changed
100         else
101             if ( right.getOrder( ) != oldRight.getOrder( ) )
102             {
103                 right.setOrder( changeRightOrder( oldRight, right.getOrder( ) ) );
104             }
105 
106         _dao.store( right );
107 
108         return right;
109     }
110 
111     /**
112      * Remove the admin right whose identifier is specified in parameter
113      *
114      * @param strId
115      *            The identifier of the admin right to remove
116      */
117     public static void remove( String strId )
118     {
119         Right oldRight = findByPrimaryKey( strId );
120 
121         if ( oldRight != null )
122         {
123             deleteEntryFromList( oldRight.getFeatureGroup( ), oldRight.getOrder( ) );
124         }
125 
126         _dao.delete( strId );
127     }
128 
129     // /////////////////////////////////////////////////////////////////////////
130     // Finders
131 
132     /**
133      * Returns an instance of an admin right whose identifier is specified in parameter
134      *
135      * @param strKey
136      *            The admin right primary key
137      * @return an instance of an admin right
138      */
139     public static Right findByPrimaryKey( String strKey )
140     {
141         return _dao.load( strKey );
142     }
143 
144     /**
145      * Loads the data of all the rights and returns them in form of a collection
146      *
147      * @return the collection which contains the data of all the rights
148      */
149     public static Collection<Right> getRightsList( )
150     {
151         return _dao.selectRightsList( );
152     }
153 
154     /**
155      * Loads the data of all the rights with level greater or equal than nLevel and returns them in form of a collection
156      * 
157      * @param nLevel
158      *            The right's level
159      *
160      * @return the collection which contains the data of all the rights
161      */
162     public static Collection<Right> getRightsList( int nLevel )
163     {
164         return _dao.selectRightsList( nLevel );
165     }
166 
167     /**
168      * Loads the data of all the rights with the specified feature group and returns them in form of a collection
169      *
170      * @param strFeatureGroup
171      *            the name of the feature group
172      * @return the collection which contains the data of all the rights
173      */
174     public static Collection<Right> getRightsList( String strFeatureGroup )
175     {
176         return _dao.selectRightsList( strFeatureGroup );
177     }
178 
179     /**
180      * Change the order in a {@link Right}
181      *
182      * @param right
183      *            The right to update order
184      * @param nNewOrder
185      *            The new place in the list or END_OF_LIST to place Right at the end
186      * @return The new order
187      */
188     public static int changeRightOrder( Right right, int nNewOrder )
189     {
190         if ( right == null )
191         {
192             return CONSTANT_ERROR_ORDER;
193         }
194 
195         if ( nNewOrder < right.getOrder( ) )
196         {
197             for ( Right rightGroup : getRightsList( right.getFeatureGroup( ) ) )
198             {
199                 int nRightToUpdateOrder = rightGroup.getOrder( );
200 
201                 if ( ( nRightToUpdateOrder >= nNewOrder ) && ( nRightToUpdateOrder < right.getOrder( ) ) )
202                 {
203                     rightGroup.setOrder( nRightToUpdateOrder + CONSTANT_STEP_ORDER );
204                     _dao.store( rightGroup );
205                 }
206             }
207         }
208         else
209             if ( nNewOrder > right.getOrder( ) )
210             {
211                 for ( Right rightGroup : getRightsList( right.getFeatureGroup( ) ) )
212                 {
213                     int nRightToUpdateOrder = rightGroup.getOrder( );
214 
215                     if ( ( nRightToUpdateOrder <= nNewOrder ) && ( nRightToUpdateOrder > right.getOrder( ) ) )
216                     {
217                         rightGroup.setOrder( nRightToUpdateOrder - CONSTANT_STEP_ORDER );
218                         _dao.store( rightGroup );
219                     }
220                 }
221             }
222 
223         return nNewOrder;
224     }
225 
226     /**
227      * Delete entry (specify by nOrderId)
228      * 
229      * @param strFeatureGroup
230      *            The {@link FeatureGroup} impacted
231      * @param nOrderId
232      *            The order to delete
233      */
234     public static void deleteEntryFromList( String strFeatureGroup, int nOrderId )
235     {
236         for ( Right rightGroup : getRightsList( strFeatureGroup ) )
237         {
238             int nRightToUpdateOrder = rightGroup.getOrder( );
239 
240             if ( ( nRightToUpdateOrder > nOrderId ) )
241             {
242                 rightGroup.setOrder( nRightToUpdateOrder - CONSTANT_STEP_ORDER );
243                 _dao.store( rightGroup );
244             }
245         }
246     }
247 
248     /**
249      * Reinitialize feature order groups
250      *
251      * @param strFeatureGroup
252      *            The feature group key
253      */
254     public static void reinitFeatureOrders( String strFeatureGroup )
255     {
256         if ( ( strFeatureGroup == null ) || strFeatureGroup.equals( "" ) )
257         {
258             return;
259         }
260 
261         int nOrder = CONSTANT_FIRST_ID_ORDER;
262 
263         for ( Right rightGroup : getRightsList( strFeatureGroup ) )
264         {
265             rightGroup.setOrder( nOrder++ );
266             _dao.store( rightGroup );
267         }
268     }
269 
270     /**
271      * Check feature orders and return false if at least one order is twice
272      *
273      * @param strFeatureGroup
274      *            The feature group key
275      * @return true if order list is ok, false else.
276      */
277     public static boolean checkFeatureOrders( String strFeatureGroup )
278     {
279         if ( ( strFeatureGroup == null ) || strFeatureGroup.equals( "" ) )
280         {
281             return false;
282         }
283 
284         int nOrder = CONSTANT_FIRST_ID_ORDER;
285 
286         for ( Right rightGroup : getRightsList( strFeatureGroup ) )
287         {
288             if ( nOrder != rightGroup.getOrder( ) )
289             {
290                 return false;
291             }
292 
293             nOrder++;
294         }
295 
296         return true;
297     }
298 
299     public static Collection<Right> getExternalRightList( )
300     {
301         return _dao.selectExternalRightsList( 0 );
302     }
303 }