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.portal.service.insert;
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.plugin.Plugin;
39  import fr.paris.lutece.portal.service.plugin.PluginService;
40  import fr.paris.lutece.portal.service.rbac.RBACResource;
41  import fr.paris.lutece.portal.service.util.AppException;
42  import fr.paris.lutece.portal.web.insert.InsertServiceSelectionBean;
43  
44  import java.util.Locale;
45  
46  import javax.servlet.http.HttpServletRequest;
47  
48  /**
49   * The insert Service
50   */
51  public class InsertService implements RBACResource, Localizable
52  {
53      public static final String RESOURCE_TYPE = "INSERT_SERVICE";
54  
55      /** Unique ID of the Service */
56      private String _strId;
57  
58      /** Name used in the UI */
59      private String _strNameKey;
60  
61      /** Label used in the UI */
62      private String _strLabelKey;
63  
64      /** Name of the Bean to use to select a link */
65      private String _strActionBean;
66  
67      /** Name of the plugin */
68      private String _strPluginName;
69      private Locale _locale;
70  
71      /**
72       * Returns the insert service Id
73       *
74       * @return The Id as a String
75       */
76      public String getId( )
77      {
78          return _strId;
79      }
80  
81      /**
82       * Set the Id of the insert service
83       *
84       * @param strId
85       *            the id
86       */
87      public void setId( String strId )
88      {
89          _strId = strId;
90      }
91  
92      /**
93       * Returns the insert service name
94       *
95       * @return The name key as a String
96       */
97      public String getNameKey( )
98      {
99          return _strNameKey;
100     }
101 
102     /**
103      * Returns the insert service name
104      *
105      * @return The name as a String
106      */
107     public String getName( )
108     {
109         return I18nService.getLocalizedString( _strNameKey, _locale );
110     }
111 
112     /**
113      * Set the name of the insert service
114      *
115      * @param strNameKey
116      *            the label
117      */
118     public void setNameKey( String strNameKey )
119     {
120         _strNameKey = strNameKey;
121     }
122 
123     /**
124      * Returns the insert service label
125      *
126      * @return The label key as a String
127      */
128     public String getLabelKey( )
129     {
130         return _strLabelKey;
131     }
132 
133     /**
134      * Returns the insert service label
135      *
136      * @return The label as a String
137      */
138     public String getLabel( )
139     {
140         return I18nService.getLocalizedString( _strLabelKey, _locale );
141     }
142 
143     /**
144      * Set the name of the insert service
145      *
146      * @param strLabelKey
147      *            the label
148      */
149     public void setLabelKey( String strLabelKey )
150     {
151         _strLabelKey = strLabelKey;
152     }
153 
154     /**
155      * Returns the ActionBean
156      *
157      * @return The ActionBean as a String
158      */
159     public String getActionBeanString( )
160     {
161         return _strActionBean;
162     }
163 
164     /**
165      * Set the ActionBean of the insert service
166      *
167      * @param strActionBean
168      *            the ActionBean
169      */
170     public void setActionBeanString( String strActionBean )
171     {
172         _strActionBean = strActionBean;
173     }
174 
175     /**
176      * Register the name of the Action Bean associated with the service
177      *
178      * @param strSelectionBean
179      *            The ActionBean to use
180      */
181     public void setActionBeanClassName( String strSelectionBean )
182     {
183         _strActionBean = strSelectionBean;
184     }
185 
186     /**
187      * Return the name of the plugin
188      *
189      * @return The name of the plugin
190      */
191     public String getPluginName( )
192     {
193         return _strPluginName;
194     }
195 
196     /**
197      * Set the plugin name of the insert service
198      *
199      * @param strPluginName
200      *            the plugin name
201      */
202     public void setPluginName( String strPluginName )
203     {
204         _strPluginName = strPluginName;
205     }
206 
207     /**
208      * Returns the Bean to select an object
209      *
210      * @return an Instance of the Bean
211      * @see #getActionBeanString()
212      */
213     public InsertServiceSelectionBean getSelectionActionBean( )
214     {
215         try
216         {
217             return (InsertServiceSelectionBean) Class.forName( getActionBeanString( ) ).newInstance( );
218         }
219         catch( ClassNotFoundException | IllegalAccessException | InstantiationException e )
220         {
221             throw new AppException( "Error instantiating a LinkService Selection Bean : " + e.getMessage( ), e );
222         }
223     }
224 
225     /**
226      * Get the UI to select an object
227      *
228      * @param request
229      *            The Http request.
230      * @return HTML code of the page as string
231      */
232     public String getSelectorUI( HttpServletRequest request )
233     {
234         return getSelectionActionBean( ).getInsertServiceSelectorUI( request );
235     }
236 
237     /**
238      * Tells if the insertservice is enable (plugin activated).
239      * 
240      * @return Returns the state of the insert service : enable or disable
241      */
242     public boolean isEnabled( )
243     {
244         Plugin plugin = PluginService.getPlugin( _strPluginName );
245 
246         return plugin.isInstalled( );
247     }
248 
249     // //////////////////////////////////////////////////////////////////////////
250     // RBAC Resource implementation
251 
252     /**
253      * Returns the Resource Type Code that identify the resource type
254      * 
255      * @return The Resource Type Code
256      */
257     public String getResourceTypeCode( )
258     {
259         return RESOURCE_TYPE;
260     }
261 
262     /**
263      * Returns the resource Id of the current object
264      * 
265      * @return The resource Id of the current object
266      */
267     public String getResourceId( )
268     {
269         return "" + getId( );
270     }
271 
272     /**
273      * Implements Localizable
274      * 
275      * @param locale
276      *            The current locale
277      */
278     public void setLocale( Locale locale )
279     {
280         _locale = locale;
281     }
282 }