View Javadoc
1   /*
2    * Copyright (c) 2002-2015, Mairie de 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.plugins.leaflet.service;
35  
36  import fr.paris.lutece.portal.service.datastore.DatastoreService;
37  import fr.paris.lutece.portal.service.spring.SpringContextService;
38  import fr.paris.lutece.portal.service.util.AppLogService;
39  import fr.paris.lutece.util.ReferenceItem;
40  import fr.paris.lutece.util.ReferenceList;
41  
42  import org.springframework.beans.factory.NoSuchBeanDefinitionException;
43  
44  import java.util.ArrayList;
45  import java.util.Collection;
46  import java.util.HashSet;
47  
48  
49  /**
50   * A service for icons
51   */
52  public class IconService
53  {
54      private static final String BEAN_PREFIX = "leaflet-icon-provider-";
55      private static final String DATASTORE_PREFIX = "leaflet.icon.";
56      private static final String DATASTORE_ICONS_PREFIX = DATASTORE_PREFIX + "icons.";
57  
58  
59      /** Private constructor */
60      private IconService(  )
61      {
62      }
63  
64      /**
65       * Returns the list of installed icons.
66       *
67       * @return the list of icons
68       */
69      public static Collection<String> getList(  )
70      {
71          ReferenceList referenceList = DatastoreService.getDataByPrefix( DATASTORE_ICONS_PREFIX );
72          HashSet<String> hashSet = new HashSet<String>(  );
73  
74          for ( ReferenceItem referenceItem : referenceList )
75          {
76              String codeSuffix = referenceItem.getCode(  ).substring( DATASTORE_ICONS_PREFIX.length(  ) );
77              String code = codeSuffix.substring( 0, codeSuffix.indexOf( '.' ) );
78              hashSet.add( code );
79          }
80  
81          ArrayList<String> result = new ArrayList<String>( hashSet );
82  
83          return result;
84      }
85  
86      /**
87       * Returns the name of an icon based on the provider and the key.
88       *
89       * @param strProvider the provider
90       * @param strIconKey the key
91       * @return the icon name
92       */
93      public static String getIcon( String strProvider, String strIconKey )
94      {
95          try
96          {
97              IIconProvider./../fr/paris/lutece/plugins/leaflet/service/IIconProvider.html#IIconProvider">IIconProvider iconProvider = (IIconProvider) SpringContextService.getBean( BEAN_PREFIX + strProvider );
98              String icon = iconProvider.getIcon( strIconKey );
99  
100             return ( icon == null ) ? "default" : icon;
101         }
102         catch ( NoSuchBeanDefinitionException e )
103         {
104             AppLogService.error( "icon API: Missing strProvider " + strProvider + " , strIconKey " + strIconKey +
105                 ", exception " + e );
106 
107             return "default";
108         }
109     }
110 }