View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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.portal.service.rbac;
35  
36  import fr.paris.lutece.portal.service.i18n.I18nService;
37  import fr.paris.lutece.portal.service.i18n.Localizable;
38  import fr.paris.lutece.portal.service.util.AppLogService;
39  
40  import java.util.Collection;
41  import java.util.HashMap;
42  import java.util.Locale;
43  import java.util.Map;
44  
45  
46  /**
47   * This class provides the methods for resource types.
48   * It gives the key and label of the resource type
49   * It allows to define and access to the permissions available.
50   * It links with the resourceIdService class that retrieves specific data to be managed.
51   */
52  public class ResourceType implements Localizable
53  {
54      private String _strResourceTypeKey;
55      private String _strResourceTypeLabel;
56      private Map<String, Permission> _mapPermissions;
57      private String _strResourceIdServiceClass;
58      private String _strPluginName;
59      private Locale _locale;
60  
61      /**
62       *
63       */
64      public ResourceType(  )
65      {
66          _mapPermissions = new HashMap<String, Permission>(  );
67      }
68  
69      /**
70       * Implements Localizable
71       * @param locale The current locale
72       */
73      public void setLocale( Locale locale )
74      {
75          _locale = locale;
76      }
77  
78      /**
79       * Registers a permission
80       *
81       * @param permission the permission object to register
82       */
83      public void registerPermission( Permission permission )
84      {
85          _mapPermissions.put( permission.getPermissionKey(  ), permission );
86      }
87  
88      /**
89       * Returns all registered permissions
90       *
91       * @return A collection containing all registered permissions
92       */
93      public Collection<Permission> getPermissionList(  )
94      {
95          return _mapPermissions.values(  );
96      }
97  
98      /**
99       * Get a particular permission
100      *
101      * @param strPermissionId Identifier of the seeked permission
102      * @return the selected resource type
103      */
104     public Permission getPermission( String strPermissionId )
105     {
106         return _mapPermissions.get( strPermissionId );
107     }
108 
109     /**
110      * Returns the resource type Key
111      * @return Returns the _strResourceTypeKey.
112      */
113     public String getResourceTypeKey(  )
114     {
115         return _strResourceTypeKey;
116     }
117 
118     /**
119      *  Sets the resource type Key
120      * @param strResourceTypeCode The _strResourceTypeKey to set.
121      */
122     public void setResourceTypeKey( String strResourceTypeCode )
123     {
124         _strResourceTypeKey = strResourceTypeCode;
125     }
126 
127     /**
128      *  Returns the resource type label
129      * @return Returns the _strResourceTypeLabel.
130      */
131     public String getResourceTypeLabel(  )
132     {
133         return I18nService.getLocalizedString( _strResourceTypeLabel, _locale );
134     }
135 
136     /**
137      *  Sets the resource type label
138      * @param strResourceTypeLabel The _strResourceTypeLabel to set.
139      */
140     public void setResourceTypeLabelKey( String strResourceTypeLabel )
141     {
142         _strResourceTypeLabel = strResourceTypeLabel;
143     }
144 
145     /**
146      *  Returns the name of the resourceIdService class
147      * @return Returns the _strResourceIdServiceClass.
148      */
149     public String getResourceIdServiceClass(  )
150     {
151         return _strResourceIdServiceClass;
152     }
153 
154     /**
155      * Sets the name of the resourceIdService class
156      * @param strResourceIdServiceClass The _strResourceIdServiceClass to set.
157      */
158     public void setResourceIdServiceClass( String strResourceIdServiceClass )
159     {
160         _strResourceIdServiceClass = strResourceIdServiceClass;
161     }
162 
163     /**
164      * Returns an instance of the resourceIdService class
165      * @return a ResourceIdService object with the plugin name initialised
166      */
167     public ResourceIdService getResourceIdService(  )
168     {
169         try
170         {
171             ResourceIdService service = (ResourceIdService) Class.forName( getResourceIdServiceClass(  ) ).newInstance(  );
172             if ( service != null )
173             {
174             	service.setPluginName( getPluginName(  ) );
175             	return service;
176             }
177         }
178         catch ( InstantiationException e )
179         {
180             AppLogService.error( e.getMessage(  ), e );
181         }
182         catch ( IllegalAccessException e )
183         {
184             AppLogService.error( e.getMessage(  ), e );
185         }
186         catch ( ClassNotFoundException e )
187         {
188             AppLogService.error( e.getMessage(  ), e );
189         }
190 
191         return null;
192     }
193 
194     /**
195      * Sets the plugin name
196      * @param strPluginName The __strPluginName to set.
197      */
198     public void setPluginName( String strPluginName )
199     {
200         _strPluginName = strPluginName;
201     }
202 
203     /**
204      * Returns the plugin name
205      * @return Returns the __strPluginName.
206      */
207     public String getPluginName(  )
208     {
209         return _strPluginName;
210     }
211 }