LutecePanelService.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.panel;

  35. import fr.paris.lutece.portal.service.spring.SpringContextService;

  36. import java.util.ArrayList;
  37. import java.util.Collections;
  38. import java.util.Comparator;
  39. import java.util.List;

  40. /**
  41.  * PanelService
  42.  *
  43.  * @param <T>
  44.  *            Abstract type of panels to display. Every class extending the class T will create a panel.
  45.  */
  46. public final class LutecePanelService<T extends LutecePanel>
  47. {
  48.     private static List<LutecePanelService<? extends LutecePanel>> _listSingletons = new ArrayList<>( );
  49.     private List<T> _listPanels;
  50.     private Class<T> _genericTypeClass;

  51.     /**
  52.      * Instantiates a new lutece panel service.
  53.      */
  54.     private LutecePanelService( )
  55.     {
  56.     }

  57.     /**
  58.      * Instantiates a new lutece panel service.
  59.      *
  60.      * @param clazz
  61.      *            the clazz
  62.      */
  63.     private LutecePanelService( Class<T> clazz )
  64.     {
  65.         _listPanels = SpringContextService.getBeansOfType( clazz );
  66.         Collections.sort( _listPanels, new PanelComparator( ) );
  67.         _genericTypeClass = clazz;
  68.     }

  69.     /**
  70.      * Gets the generic type class.
  71.      *
  72.      * @return the generic type class
  73.      */
  74.     private Class<T> getGenericTypeClass( )
  75.     {
  76.         return _genericTypeClass;
  77.     }

  78.     /**
  79.      * Get the instance of a PanelService for a given type.
  80.      *
  81.      * @param <T>
  82.      *            Specialized type of the PanelService
  83.      * @param clazz
  84.      *            Class associated to the type A.
  85.      * @return The instance of the PanelService with the generic type A. This instance is unique.
  86.      */
  87.     public static synchronized <T extends LutecePanel> LutecePanelService<T> instance( Class<T> clazz )
  88.     {
  89.         for ( LutecePanelService<? extends LutecePanel> storedPanelService : _listSingletons )
  90.         {
  91.             if ( storedPanelService.getGenericTypeClass( ).equals( clazz ) )
  92.             {
  93.                 return (LutecePanelService<T>) storedPanelService;
  94.             }
  95.         }

  96.         LutecePanelService<T> panelService = new LutecePanelService<>( clazz );
  97.         _listSingletons.add( panelService );

  98.         return panelService;
  99.     }

  100.     /**
  101.      * Get the index of a panel from its key
  102.      *
  103.      * @param strPanelKey
  104.      *            Key of the panel to get the index.
  105.      * @return The index of the panel with the given key, or -1 if the panel could not be found.
  106.      */
  107.     public int getIndex( String strPanelKey )
  108.     {
  109.         int nIndex = 1;

  110.         for ( LutecePanel panel : getPanels( ) )
  111.         {
  112.             if ( panel.getPanelKey( ).equals( strPanelKey ) )
  113.             {
  114.                 return nIndex;
  115.             }

  116.             nIndex++;
  117.         }

  118.         return -1;
  119.     }

  120.     /**
  121.      * Get the list of panels associated to this PanelService. One panel is used for every class extending the class T.
  122.      *
  123.      * @return The list of panels associated to this PanelService.
  124.      */
  125.     public List<T> getPanels( )
  126.     {
  127.         return _listPanels;
  128.     }

  129.     /**
  130.      * PanelComparator
  131.      */
  132.     private class PanelComparator implements Comparator<T>
  133.     {
  134.         /**
  135.          * {@inheritDoc}
  136.          */
  137.         @Override
  138.         public int compare( T p1, T p2 )
  139.         {
  140.             return p1.getPanelOrder( ) - p2.getPanelOrder( );
  141.         }
  142.     }
  143. }