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 36 import fr.paris.lutece.portal.service.spring.SpringContextService; 37 38 import java.util.ArrayList; 39 import java.util.Collections; 40 import java.util.Comparator; 41 import java.util.List; 42 43 /** 44 * PanelService 45 * 46 * @param <T> 47 * Abstract type of panels to display. Every class extending the class T will create a panel. 48 */ 49 public final class LutecePanelService<T extends LutecePanel> 50 { 51 private static List<LutecePanelService<? extends LutecePanel>> _listSingletons = new ArrayList<>( ); 52 private List<T> _listPanels; 53 private Class<T> _genericTypeClass; 54 55 /** 56 * Instantiates a new lutece panel service. 57 */ 58 private LutecePanelService( ) 59 { 60 } 61 62 /** 63 * Instantiates a new lutece panel service. 64 * 65 * @param clazz 66 * the clazz 67 */ 68 private LutecePanelService( Class<T> clazz ) 69 { 70 _listPanels = SpringContextService.getBeansOfType( clazz ); 71 Collections.sort( _listPanels, new PanelComparator( ) ); 72 _genericTypeClass = clazz; 73 } 74 75 /** 76 * Gets the generic type class. 77 * 78 * @return the generic type class 79 */ 80 private Class<T> getGenericTypeClass( ) 81 { 82 return _genericTypeClass; 83 } 84 85 /** 86 * Get the instance of a PanelService for a given type. 87 * 88 * @param <T> 89 * Specialized type of the PanelService 90 * @param clazz 91 * Class associated to the type A. 92 * @return The instance of the PanelService with the generic type A. This instance is unique. 93 */ 94 public static synchronized <T extends LutecePanel> LutecePanelService<T> instance( Class<T> clazz ) 95 { 96 for ( LutecePanelService<? extends LutecePanel> storedPanelService : _listSingletons ) 97 { 98 if ( storedPanelService.getGenericTypeClass( ).equals( clazz ) ) 99 { 100 return (LutecePanelService<T>) storedPanelService; 101 } 102 } 103 104 LutecePanelService<T> panelService = new LutecePanelService<>( clazz ); 105 _listSingletons.add( panelService ); 106 107 return panelService; 108 } 109 110 /** 111 * Get the index of a panel from its key 112 * 113 * @param strPanelKey 114 * Key of the panel to get the index. 115 * @return The index of the panel with the given key, or -1 if the panel could not be found. 116 */ 117 public int getIndex( String strPanelKey ) 118 { 119 int nIndex = 1; 120 121 for ( LutecePanel panel : getPanels( ) ) 122 { 123 if ( panel.getPanelKey( ).equals( strPanelKey ) ) 124 { 125 return nIndex; 126 } 127 128 nIndex++; 129 } 130 131 return -1; 132 } 133 134 /** 135 * Get the list of panels associated to this PanelService. One panel is used for every class extending the class T. 136 * 137 * @return The list of panels associated to this PanelService. 138 */ 139 public List<T> getPanels( ) 140 { 141 return _listPanels; 142 } 143 144 /** 145 * PanelComparator 146 */ 147 private class PanelComparator implements Comparator<T> 148 { 149 /** 150 * {@inheritDoc} 151 */ 152 @Override 153 public int compare( T p1, T p2 ) 154 { 155 return p1.getPanelOrder( ) - p2.getPanelOrder( ); 156 } 157 } 158 }