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.service.user.menu;
35  
36  import javax.inject.Inject;
37  
38  import org.springframework.beans.factory.BeanNameAware;
39  
40  import fr.paris.lutece.portal.business.user.menu.IAdminUserMenuItemProvider;
41  import javax.annotation.PostConstruct;
42  
43  /**
44   * Admin user menu provider registrar.
45   * 
46   * 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
47   * altered by using <code>insertAfter</code> or <code>insertBefore</code> properties.
48   * 
49   * Normally used from Spring.
50   * 
51   * @since 6.2.0
52   */
53  public class AdminUserMenuItemProviderRegistrar implements BeanNameAware
54  {
55      private final AdminUserMenuService _service;
56      private IAdminUserMenuItemProvider _provider;
57      private String _strAfterName;
58      private String _strBeforeName;
59      private String _strName;
60  
61      /**
62       * Constructor
63       * 
64       * @param service
65       *            the admin user item service
66       */
67      @Inject
68      public AdminUserMenuItemProviderRegistrar( AdminUserMenuService service )
69      {
70          _service = service;
71      }
72  
73      /**
74       * Class name of the admin user menu item provider
75       * 
76       * @param strClassName
77       *            class name
78       * @throws InstantiationException
79       *             if the class cannot be instantiated
80       * @throws IllegalAccessException
81       *             if the class cannot be instantiated
82       * @throws IllegalStateException
83       *             if the item provider is already set
84       * @throws ClassNotFoundException
85       *             if the class cannot be found
86       */
87      public void setClassName( String strClassName ) throws InstantiationException, IllegalAccessException, ClassNotFoundException
88      {
89          assertItemProviderNotSet( );
90          _provider = (IAdminUserMenuItemProvider) Class.forName( strClassName ).newInstance( );
91      }
92  
93      /**
94       * Provider
95       * 
96       * @param provider
97       *            the provider
98       * @throws IllegalStateException
99       *             if the item provider is already set
100      */
101     public void setProvider( IAdminUserMenuItemProvider provider )
102     {
103         assertItemProviderNotSet( );
104         _provider = provider;
105     }
106 
107     /**
108      * Name of the item provider after which to insert this one
109      * 
110      * @param strAfterName
111      *            item provider name
112      */
113     public void setInsertAfter( String strAfterName )
114     {
115         _strAfterName = strAfterName;
116     }
117 
118     /**
119      * Name of the item provider before which to insert this one
120      * 
121      * @param strBeforeName
122      *            item provider name
123      */
124     public void setInsertBefore( String strBeforeName )
125     {
126         _strBeforeName = strBeforeName;
127     }
128 
129     /**
130      * Assert the item provider is not already set
131      */
132     private void assertItemProviderNotSet( )
133     {
134         if ( _provider != null )
135         {
136             throw new IllegalStateException( "Only one of className or provider must be specifed" );
137         }
138     }
139 
140     /**
141      * Registers the item provider
142      */
143     @PostConstruct
144     public void registerAdminUserMenuItemProvider( )
145     {
146         if ( _provider == null )
147         {
148             throw new IllegalStateException( "Either className or provider must be specifed" );
149         }
150         _provider.setName( _strName );
151         _service.addItemProvider( _provider, _strAfterName, _strBeforeName );
152     }
153 
154     /**
155      * Set the name under which to register the admin user menu item provider
156      * 
157      * @param strName
158      *            name of the admin user menu item provider
159      */
160     @Override
161     public void setBeanName( String strName )
162     {
163         _strName = strName;
164     }
165 }