RightHome.java

  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. import fr.paris.lutece.portal.service.spring.SpringContextService;

  36. import java.util.Collection;

  37. /**
  38.  * This class provides instances management methods (create, find, ...) for Right objects
  39.  */
  40. public final class RightHome
  41. {
  42.     // Constants
  43.     private static final int CONSTANT_ERROR_ORDER = -2; // this value must be negative
  44.     private static final int CONSTANT_STEP_ORDER = 1;
  45.     private static final int CONSTANT_FIRST_ID_ORDER = 1;

  46.     // Static variable pointed at the DAO instance
  47.     private static IRightDAO _dao = SpringContextService.getBean( "rightDAO" );

  48.     /**
  49.      * Creates a new RightHome object.
  50.      */
  51.     private RightHome( )
  52.     {
  53.     }

  54.     /**
  55.      * Creation of an instance of an admin right
  56.      *
  57.      * @param right
  58.      *            An instance of an admin right which contains the informations to store
  59.      * @return The instance of an admin right which has been created with its primary key.
  60.      */
  61.     public static Right create( Right right )
  62.     {
  63.         right.setOrder( getRightsList( right.getFeatureGroup( ) ).size( ) + CONSTANT_STEP_ORDER );
  64.         _dao.insert( right );

  65.         return right;
  66.     }

  67.     /**
  68.      * Update of the admin right which is specified
  69.      *
  70.      * @param right
  71.      *            The instance of the admin right which contains the data to store
  72.      * @return The instance of the admin right which has been updated
  73.      */
  74.     public static Right update( Right right )
  75.     {
  76.         Right oldRight = findByPrimaryKey( right.getId( ) );

  77.         if ( oldRight == null )
  78.         {
  79.             return null;
  80.         }

  81.         // The feature group have changed
  82.         if ( ( ( right.getFeatureGroup( ) != null ) && !right.getFeatureGroup( ).equals( oldRight.getFeatureGroup( ) ) )
  83.                 || ( ( right.getFeatureGroup( ) == null ) && ( oldRight.getFeatureGroup( ) != null ) ) )
  84.         {
  85.             deleteEntryFromList( oldRight.getFeatureGroup( ), oldRight.getOrder( ) );
  86.             right.setOrder( getRightsList( right.getFeatureGroup( ) ).size( ) + CONSTANT_STEP_ORDER );
  87.         }

  88.         // The order have changed
  89.         else
  90.             if ( right.getOrder( ) != oldRight.getOrder( ) )
  91.             {
  92.                 right.setOrder( changeRightOrder( oldRight, right.getOrder( ) ) );
  93.             }

  94.         _dao.store( right );

  95.         return right;
  96.     }

  97.     /**
  98.      * Remove the admin right whose identifier is specified in parameter
  99.      *
  100.      * @param strId
  101.      *            The identifier of the admin right to remove
  102.      */
  103.     public static void remove( String strId )
  104.     {
  105.         Right oldRight = findByPrimaryKey( strId );

  106.         if ( oldRight != null )
  107.         {
  108.             deleteEntryFromList( oldRight.getFeatureGroup( ), oldRight.getOrder( ) );
  109.         }

  110.         _dao.delete( strId );
  111.     }

  112.     // /////////////////////////////////////////////////////////////////////////
  113.     // Finders

  114.     /**
  115.      * Returns an instance of an admin right whose identifier is specified in parameter
  116.      *
  117.      * @param strKey
  118.      *            The admin right primary key
  119.      * @return an instance of an admin right
  120.      */
  121.     public static Right findByPrimaryKey( String strKey )
  122.     {
  123.         return _dao.load( strKey );
  124.     }

  125.     /**
  126.      * Loads the data of all the rights and returns them in form of a collection
  127.      *
  128.      * @return the collection which contains the data of all the rights
  129.      */
  130.     public static Collection<Right> getRightsList( )
  131.     {
  132.         return _dao.selectRightsList( );
  133.     }

  134.     /**
  135.      * Loads the data of all the rights with level greater or equal than nLevel and returns them in form of a collection
  136.      *
  137.      * @param nLevel
  138.      *            The right's level
  139.      *
  140.      * @return the collection which contains the data of all the rights
  141.      */
  142.     public static Collection<Right> getRightsList( int nLevel )
  143.     {
  144.         return _dao.selectRightsList( nLevel );
  145.     }

  146.     /**
  147.      * Loads the data of all the rights with the specified feature group and returns them in form of a collection
  148.      *
  149.      * @param strFeatureGroup
  150.      *            the name of the feature group
  151.      * @return the collection which contains the data of all the rights
  152.      */
  153.     public static Collection<Right> getRightsList( String strFeatureGroup )
  154.     {
  155.         return _dao.selectRightsList( strFeatureGroup );
  156.     }

  157.     /**
  158.      * Change the order in a {@link Right}
  159.      *
  160.      * @param right
  161.      *            The right to update order
  162.      * @param nNewOrder
  163.      *            The new place in the list or END_OF_LIST to place Right at the end
  164.      * @return The new order
  165.      */
  166.     public static int changeRightOrder( Right right, int nNewOrder )
  167.     {
  168.         if ( right == null )
  169.         {
  170.             return CONSTANT_ERROR_ORDER;
  171.         }

  172.         if ( nNewOrder < right.getOrder( ) )
  173.         {
  174.             for ( Right rightGroup : getRightsList( right.getFeatureGroup( ) ) )
  175.             {
  176.                 int nRightToUpdateOrder = rightGroup.getOrder( );

  177.                 if ( ( nRightToUpdateOrder >= nNewOrder ) && ( nRightToUpdateOrder < right.getOrder( ) ) )
  178.                 {
  179.                     rightGroup.setOrder( nRightToUpdateOrder + CONSTANT_STEP_ORDER );
  180.                     _dao.store( rightGroup );
  181.                 }
  182.             }
  183.         }
  184.         else
  185.             if ( nNewOrder > right.getOrder( ) )
  186.             {
  187.                 for ( Right rightGroup : getRightsList( right.getFeatureGroup( ) ) )
  188.                 {
  189.                     int nRightToUpdateOrder = rightGroup.getOrder( );

  190.                     if ( ( nRightToUpdateOrder <= nNewOrder ) && ( nRightToUpdateOrder > right.getOrder( ) ) )
  191.                     {
  192.                         rightGroup.setOrder( nRightToUpdateOrder - CONSTANT_STEP_ORDER );
  193.                         _dao.store( rightGroup );
  194.                     }
  195.                 }
  196.             }

  197.         return nNewOrder;
  198.     }

  199.     /**
  200.      * Delete entry (specify by nOrderId)
  201.      *
  202.      * @param strFeatureGroup
  203.      *            The {@link FeatureGroup} impacted
  204.      * @param nOrderId
  205.      *            The order to delete
  206.      */
  207.     public static void deleteEntryFromList( String strFeatureGroup, int nOrderId )
  208.     {
  209.         for ( Right rightGroup : getRightsList( strFeatureGroup ) )
  210.         {
  211.             int nRightToUpdateOrder = rightGroup.getOrder( );

  212.             if ( ( nRightToUpdateOrder > nOrderId ) )
  213.             {
  214.                 rightGroup.setOrder( nRightToUpdateOrder - CONSTANT_STEP_ORDER );
  215.                 _dao.store( rightGroup );
  216.             }
  217.         }
  218.     }

  219.     /**
  220.      * Reinitialize feature order groups
  221.      *
  222.      * @param strFeatureGroup
  223.      *            The feature group key
  224.      */
  225.     public static void reinitFeatureOrders( String strFeatureGroup )
  226.     {
  227.         if ( ( strFeatureGroup == null ) || strFeatureGroup.equals( "" ) )
  228.         {
  229.             return;
  230.         }

  231.         int nOrder = CONSTANT_FIRST_ID_ORDER;

  232.         for ( Right rightGroup : getRightsList( strFeatureGroup ) )
  233.         {
  234.             rightGroup.setOrder( nOrder++ );
  235.             _dao.store( rightGroup );
  236.         }
  237.     }

  238.     /**
  239.      * Check feature orders and return false if at least one order is twice
  240.      *
  241.      * @param strFeatureGroup
  242.      *            The feature group key
  243.      * @return true if order list is ok, false else.
  244.      */
  245.     public static boolean checkFeatureOrders( String strFeatureGroup )
  246.     {
  247.         if ( ( strFeatureGroup == null ) || strFeatureGroup.equals( "" ) )
  248.         {
  249.             return false;
  250.         }

  251.         int nOrder = CONSTANT_FIRST_ID_ORDER;

  252.         for ( Right rightGroup : getRightsList( strFeatureGroup ) )
  253.         {
  254.             if ( nOrder != rightGroup.getOrder( ) )
  255.             {
  256.                 return false;
  257.             }

  258.             nOrder++;
  259.         }

  260.         return true;
  261.     }

  262.     public static Collection<Right> getExternalRightList( )
  263.     {
  264.         return _dao.selectExternalRightsList( 0 );
  265.     }
  266. }