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