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.plugins.resource.service;
35  
36  import fr.paris.lutece.plugins.resource.business.IResource;
37  import fr.paris.lutece.plugins.resource.business.IResourceType;
38  import fr.paris.lutece.plugins.resource.service.provider.IResourceProvider;
39  import fr.paris.lutece.portal.service.spring.SpringContextService;
40  
41  import java.util.ArrayList;
42  import java.util.List;
43  
44  /**
45   * Resource service
46   */
47  public class ResourceService
48  {
49      private static final String BEAN_NAME = "resource.resourceService";
50      private static volatile ResourceService _instance;
51  
52      /**
53       * Default constructor
54       */
55      private ResourceService( )
56      {
57          // Private constructor
58      }
59  
60      /**
61       * Get the instance of the service
62       * 
63       * @return The instance of the service
64       */
65      public static ResourceService getInstance( )
66      {
67          if ( _instance == null )
68          {
69              _instance = SpringContextService.getBean( BEAN_NAME );
70          }
71  
72          return _instance;
73      }
74  
75      /**
76       * Get the list of available resource types
77       * 
78       * @return The list of available resource types
79       */
80      public List<IResourceType> getResourceTypesList( )
81      {
82          String strCacheKey = ResourceCacheService.getResourceTypesListCacheKey( );
83          List<IResourceType> listResourceTypes = (List<IResourceType>) ResourceCacheService.getInstance( ).getFromCache( strCacheKey );
84  
85          if ( listResourceTypes != null )
86          {
87              return listResourceTypes;
88          }
89  
90          listResourceTypes = new ArrayList<>( );
91  
92          for ( IResourceProvider provider : SpringContextService.getBeansOfType( IResourceProvider.class ) )
93          {
94              listResourceTypes.addAll( provider.getResourceTypeList( ) );
95          }
96  
97          ResourceCacheService.getInstance( ).putInCache( strCacheKey, listResourceTypes );
98  
99          return listResourceTypes;
100     }
101 
102     /**
103      * Check if a resource type is managed by any provider
104      * 
105      * @param strResourceTypeName
106      *            The resource type
107      * @return True if the resource type is managed by any provider, false otherwise
108      */
109     public boolean isResourceTypeManaged( String strResourceTypeName )
110     {
111         return getResourceProvider( strResourceTypeName ) != null;
112     }
113 
114     /**
115      * Declare a resource type as created. This method is used to keep cache up to date.
116      * 
117      * @param strResourceTypeName
118      *            The created resource type
119      */
120     public void resourceTypeCreated( String strResourceTypeName )
121     {
122         ResourceCacheService.getInstance( ).removeKey( ResourceCacheService.getResourceTypesListCacheKey( ) );
123     }
124 
125     /**
126      * Declare a resource type as removed. This method is used to keep cache up to date.
127      * 
128      * @param strResourceTypeName
129      *            The removed resource type
130      */
131     public void resourceTypeRemoved( String strResourceTypeName )
132     {
133         ResourceCacheService.getInstance( ).removeKey( ResourceCacheService.getResourceTypeProviderCacheKey( strResourceTypeName ) );
134         ResourceCacheService.getInstance( ).removeKey( ResourceCacheService.getResourceTypesListCacheKey( ) );
135     }
136 
137     /**
138      * Get a resource from its id and type
139      * 
140      * @param strIdResource
141      *            the id of the resource to get
142      * @param strResourceTypeName
143      *            the type of the resource to get
144      * @return The resource, or null if the resource could not be found
145      */
146     public IResource getResource( String strIdResource, String strResourceTypeName )
147     {
148         IResourceProvider provider = getResourceProvider( strResourceTypeName );
149 
150         if ( provider != null )
151         {
152             return provider.getResource( strIdResource, strResourceTypeName );
153         }
154 
155         return null;
156     }
157 
158     /**
159      * Get the list of resources of a given type
160      * 
161      * @param strResourceTypeName
162      *            the resource type
163      * @return the list of resource of the given type, or an empty list if not resource was found
164      */
165     public List<IResource> getListResources( String strResourceTypeName )
166     {
167         IResourceProvider resourceProvider = getResourceProvider( strResourceTypeName );
168 
169         if ( resourceProvider != null )
170         {
171             return resourceProvider.getListResources( strResourceTypeName );
172         }
173 
174         return new ArrayList<>( 0 );
175     }
176 
177     /**
178      * Get the resource provider of a resource type
179      * 
180      * @param strResourceTypeName
181      *            The resource provider of a resource type
182      * @return The resource provider, or null if no provider was found for the given resource type
183      */
184     public IResourceProvider getResourceProvider( String strResourceTypeName )
185     {
186         String strCacheKey = ResourceCacheService.getResourceTypeProviderCacheKey( strResourceTypeName );
187         IResourceProvider./../fr/paris/lutece/plugins/resource/service/provider/IResourceProvider.html#IResourceProvider">IResourceProvider provider = (IResourceProvider) ResourceCacheService.getInstance( ).getFromCache( strCacheKey );
188 
189         if ( provider != null )
190         {
191             return provider;
192         }
193 
194         for ( IResourceProvider resourceProvider : SpringContextService.getBeansOfType( IResourceProvider.class ) )
195         {
196             if ( resourceProvider.isResourceTypeManaged( strResourceTypeName ) )
197             {
198                 ResourceCacheService.getInstance( ).putInCache( strCacheKey, resourceProvider );
199 
200                 return resourceProvider;
201             }
202         }
203 
204         return null;
205     }
206 }