FeatureGroupHome.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 java.util.List;

  36. import fr.paris.lutece.portal.service.spring.SpringContextService;
  37. import fr.paris.lutece.util.ReferenceList;

  38. /**
  39.  * This class provides instances management methods (create, find, ...) for feature groups objects
  40.  */
  41. public final class FeatureGroupHome
  42. {
  43.     // Static variable pointed at the DAO instance
  44.     private static IFeatureGroupDAO _dao = SpringContextService.getBean( "featureGroupDAO" );
  45.     private static final int CONSTANT_ERROR_ORDER = -2; // this value must be negative
  46.     private static final int CONSTANT_STEP_ORDER = 1;

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

  53.     /**
  54.      * Creation of an instance of an feature group
  55.      *
  56.      * @param featureGroup
  57.      *            An instance of an feature group which contains the informations to store
  58.      * @return The instance of an feature group which has been created with its primary key.
  59.      */
  60.     public static FeatureGroup create( FeatureGroup featureGroup )
  61.     {
  62.         featureGroup.setOrder( getFeatureGroupsCount( ) + CONSTANT_STEP_ORDER );
  63.         _dao.insert( featureGroup );

  64.         return featureGroup;
  65.     }

  66.     /**
  67.      * Update of the feature group which is specified
  68.      *
  69.      * @param featureGroup
  70.      *            The instance of the feature group which contains the data to store
  71.      * @return The instance of the feature group which has been updated
  72.      */
  73.     public static FeatureGroup update( FeatureGroup featureGroup )
  74.     {
  75.         FeatureGroup oldFeatureGroup = findByPrimaryKey( featureGroup.getId( ) );

  76.         if ( oldFeatureGroup == null )
  77.         {
  78.             return null;
  79.         }

  80.         // The order have changed
  81.         else
  82.             if ( featureGroup.getOrder( ) != oldFeatureGroup.getOrder( ) )
  83.             {
  84.                 featureGroup.setOrder( changeRightOrder( oldFeatureGroup, featureGroup.getOrder( ) ) );
  85.             }

  86.         _dao.store( featureGroup );

  87.         return featureGroup;
  88.     }

  89.     /**
  90.      * Remove the feature group whose identifier is specified in parameter
  91.      *
  92.      * @param strId
  93.      *            The identifier of the feature group to remove
  94.      */
  95.     public static void remove( String strId )
  96.     {
  97.         FeatureGroup oldFeature = findByPrimaryKey( strId );

  98.         if ( oldFeature != null )
  99.         {
  100.             deleteEntryFromList( oldFeature.getOrder( ) );
  101.         }

  102.         _dao.delete( strId );
  103.     }

  104.     /**
  105.      * Delete entry (specify by nOrderId)
  106.      *
  107.      * @param nOrderId
  108.      *            The order to delete
  109.      */
  110.     public static void deleteEntryFromList( int nOrderId )
  111.     {
  112.         for ( FeatureGroup featureGroupChange : getFeatureGroupsList( ) )
  113.         {
  114.             int nFeatureGroupToUpdateOrder = featureGroupChange.getOrder( );

  115.             if ( ( nFeatureGroupToUpdateOrder > nOrderId ) )
  116.             {
  117.                 featureGroupChange.setOrder( nFeatureGroupToUpdateOrder - CONSTANT_STEP_ORDER );
  118.                 _dao.store( featureGroupChange );
  119.             }
  120.         }
  121.     }

  122.     // /////////////////////////////////////////////////////////////////////////
  123.     // Finders

  124.     /**
  125.      * Returns an instance of an feature group whose identifier is specified in parameter
  126.      *
  127.      * @param strKey
  128.      *            The feature group primary key
  129.      * @return an instance of an feature group
  130.      */
  131.     public static FeatureGroup findByPrimaryKey( String strKey )
  132.     {
  133.         return _dao.load( strKey );
  134.     }

  135.     /**
  136.      * Loads the data of all the feature groups and returns them in form of a collection
  137.      *
  138.      * @return the collection which contains the data of all the feature groups
  139.      */
  140.     public static List<FeatureGroup> getFeatureGroupsList( )
  141.     {
  142.         return _dao.selectFeatureGroupsList( );
  143.     }

  144.     /**
  145.      * Loads the data of all the feature groups and returns them in form of a reference list
  146.      *
  147.      * @return the reference list of all the feature groups
  148.      */
  149.     public static ReferenceList getFeatureGroupsReferenceList( )
  150.     {
  151.         ReferenceList featuresGroupsReferenceList = new ReferenceList( );
  152.         for ( FeatureGroup featureGroup : getFeatureGroupsList( ) )
  153.         {
  154.             featuresGroupsReferenceList.add( featureGroup.getReferenceItem( ) );
  155.         }

  156.         return featuresGroupsReferenceList;
  157.     }

  158.     /**
  159.      * Gets the count of groups
  160.      *
  161.      * @return the count of groups
  162.      */
  163.     public static int getFeatureGroupsCount( )
  164.     {
  165.         return _dao.selectFeatureGroupsCount( );
  166.     }

  167.     /**
  168.      * Change the order in a {@link Right}
  169.      *
  170.      * @param featureGroup
  171.      *            The {@link FeatureGroup}
  172.      * @param nNewOrder
  173.      *            The new place in the list or END_OF_LIST to place Right at the end
  174.      * @return The new order
  175.      */
  176.     public static int changeRightOrder( FeatureGroup featureGroup, int nNewOrder )
  177.     {
  178.         if ( featureGroup == null )
  179.         {
  180.             return CONSTANT_ERROR_ORDER;
  181.         }

  182.         if ( nNewOrder < featureGroup.getOrder( ) )
  183.         {
  184.             for ( FeatureGroup featureGroupChange : getFeatureGroupsList( ) )
  185.             {
  186.                 int nFeatureGroupToUpdateOrder = featureGroupChange.getOrder( );

  187.                 if ( ( nFeatureGroupToUpdateOrder >= nNewOrder ) && ( nFeatureGroupToUpdateOrder < featureGroup.getOrder( ) ) )
  188.                 {
  189.                     featureGroupChange.setOrder( nFeatureGroupToUpdateOrder + CONSTANT_STEP_ORDER );
  190.                     _dao.store( featureGroupChange );
  191.                 }
  192.             }
  193.         }
  194.         else
  195.             if ( nNewOrder > featureGroup.getOrder( ) )
  196.             {
  197.                 for ( FeatureGroup featureGroupChange : getFeatureGroupsList( ) )
  198.                 {
  199.                     int nFeatureGroupToUpdateOrder = featureGroupChange.getOrder( );

  200.                     if ( ( nFeatureGroupToUpdateOrder <= nNewOrder ) && ( nFeatureGroupToUpdateOrder > featureGroup.getOrder( ) ) )
  201.                     {
  202.                         featureGroupChange.setOrder( nFeatureGroupToUpdateOrder - CONSTANT_STEP_ORDER );
  203.                         _dao.store( featureGroupChange );
  204.                     }
  205.                 }
  206.             }

  207.         return nNewOrder;
  208.     }
  209. }