AdminUserMenuItemProviderRegistrar.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.service.user.menu;

  35. import javax.inject.Inject;

  36. import org.springframework.beans.factory.BeanNameAware;

  37. import fr.paris.lutece.portal.business.user.menu.IAdminUserMenuItemProvider;
  38. import javax.annotation.PostConstruct;

  39. /**
  40.  * Admin user menu provider registrar.
  41.  *
  42.  * Used to add item providers to the admin user menu provider registry. One of <code>className</code> or <code>provider</code> must be set. Ordering can be
  43.  * altered by using <code>insertAfter</code> or <code>insertBefore</code> properties.
  44.  *
  45.  * Normally used from Spring.
  46.  *
  47.  * @since 6.2.0
  48.  */
  49. public class AdminUserMenuItemProviderRegistrar implements BeanNameAware
  50. {
  51.     private final AdminUserMenuService _service;
  52.     private IAdminUserMenuItemProvider _provider;
  53.     private String _strAfterName;
  54.     private String _strBeforeName;
  55.     private String _strName;

  56.     /**
  57.      * Constructor
  58.      *
  59.      * @param service
  60.      *            the admin user item service
  61.      */
  62.     @Inject
  63.     public AdminUserMenuItemProviderRegistrar( AdminUserMenuService service )
  64.     {
  65.         _service = service;
  66.     }

  67.     /**
  68.      * Class name of the admin user menu item provider
  69.      *
  70.      * @param strClassName
  71.      *            class name
  72.      * @throws InstantiationException
  73.      *             if the class cannot be instantiated
  74.      * @throws IllegalAccessException
  75.      *             if the class cannot be instantiated
  76.      * @throws IllegalStateException
  77.      *             if the item provider is already set
  78.      * @throws ClassNotFoundException
  79.      *             if the class cannot be found
  80.      */
  81.     public void setClassName( String strClassName ) throws InstantiationException, IllegalAccessException, ClassNotFoundException
  82.     {
  83.         assertItemProviderNotSet( );
  84.         _provider = (IAdminUserMenuItemProvider) Class.forName( strClassName ).newInstance( );
  85.     }

  86.     /**
  87.      * Provider
  88.      *
  89.      * @param provider
  90.      *            the provider
  91.      * @throws IllegalStateException
  92.      *             if the item provider is already set
  93.      */
  94.     public void setProvider( IAdminUserMenuItemProvider provider )
  95.     {
  96.         assertItemProviderNotSet( );
  97.         _provider = provider;
  98.     }

  99.     /**
  100.      * Name of the item provider after which to insert this one
  101.      *
  102.      * @param strAfterName
  103.      *            item provider name
  104.      */
  105.     public void setInsertAfter( String strAfterName )
  106.     {
  107.         _strAfterName = strAfterName;
  108.     }

  109.     /**
  110.      * Name of the item provider before which to insert this one
  111.      *
  112.      * @param strBeforeName
  113.      *            item provider name
  114.      */
  115.     public void setInsertBefore( String strBeforeName )
  116.     {
  117.         _strBeforeName = strBeforeName;
  118.     }

  119.     /**
  120.      * Assert the item provider is not already set
  121.      */
  122.     private void assertItemProviderNotSet( )
  123.     {
  124.         if ( _provider != null )
  125.         {
  126.             throw new IllegalStateException( "Only one of className or provider must be specifed" );
  127.         }
  128.     }

  129.     /**
  130.      * Registers the item provider
  131.      */
  132.     @PostConstruct
  133.     public void registerAdminUserMenuItemProvider( )
  134.     {
  135.         if ( _provider == null )
  136.         {
  137.             throw new IllegalStateException( "Either className or provider must be specifed" );
  138.         }
  139.         _provider.setName( _strName );
  140.         _service.addItemProvider( _provider, _strAfterName, _strBeforeName );
  141.     }

  142.     /**
  143.      * Set the name under which to register the admin user menu item provider
  144.      *
  145.      * @param strName
  146.      *            name of the admin user menu item provider
  147.      */
  148.     @Override
  149.     public void setBeanName( String strName )
  150.     {
  151.         _strName = strName;
  152.     }
  153. }