InsertService.java

  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. import fr.paris.lutece.portal.service.i18n.I18nService;
  36. import fr.paris.lutece.portal.service.i18n.Localizable;
  37. import fr.paris.lutece.portal.service.plugin.Plugin;
  38. import fr.paris.lutece.portal.service.plugin.PluginService;
  39. import fr.paris.lutece.portal.service.rbac.RBACResource;
  40. import fr.paris.lutece.portal.service.util.AppException;
  41. import fr.paris.lutece.portal.web.insert.InsertServiceSelectionBean;

  42. import java.util.Locale;

  43. import javax.servlet.http.HttpServletRequest;

  44. /**
  45.  * The insert Service
  46.  */
  47. public class InsertService implements RBACResource, Localizable
  48. {
  49.     public static final String RESOURCE_TYPE = "INSERT_SERVICE";

  50.     /** Unique ID of the Service */
  51.     private String _strId;

  52.     /** Name used in the UI */
  53.     private String _strNameKey;

  54.     /** Label used in the UI */
  55.     private String _strLabelKey;

  56.     /** Name of the Bean to use to select a link */
  57.     private String _strActionBean;

  58.     /** Name of the plugin */
  59.     private String _strPluginName;
  60.     private Locale _locale;

  61.     /**
  62.      * Returns the insert service Id
  63.      *
  64.      * @return The Id as a String
  65.      */
  66.     public String getId( )
  67.     {
  68.         return _strId;
  69.     }

  70.     /**
  71.      * Set the Id of the insert service
  72.      *
  73.      * @param strId
  74.      *            the id
  75.      */
  76.     public void setId( String strId )
  77.     {
  78.         _strId = strId;
  79.     }

  80.     /**
  81.      * Returns the insert service name
  82.      *
  83.      * @return The name key as a String
  84.      */
  85.     public String getNameKey( )
  86.     {
  87.         return _strNameKey;
  88.     }

  89.     /**
  90.      * Returns the insert service name
  91.      *
  92.      * @return The name as a String
  93.      */
  94.     public String getName( )
  95.     {
  96.         return I18nService.getLocalizedString( _strNameKey, _locale );
  97.     }

  98.     /**
  99.      * Set the name of the insert service
  100.      *
  101.      * @param strNameKey
  102.      *            the label
  103.      */
  104.     public void setNameKey( String strNameKey )
  105.     {
  106.         _strNameKey = strNameKey;
  107.     }

  108.     /**
  109.      * Returns the insert service label
  110.      *
  111.      * @return The label key as a String
  112.      */
  113.     public String getLabelKey( )
  114.     {
  115.         return _strLabelKey;
  116.     }

  117.     /**
  118.      * Returns the insert service label
  119.      *
  120.      * @return The label as a String
  121.      */
  122.     public String getLabel( )
  123.     {
  124.         return I18nService.getLocalizedString( _strLabelKey, _locale );
  125.     }

  126.     /**
  127.      * Set the name of the insert service
  128.      *
  129.      * @param strLabelKey
  130.      *            the label
  131.      */
  132.     public void setLabelKey( String strLabelKey )
  133.     {
  134.         _strLabelKey = strLabelKey;
  135.     }

  136.     /**
  137.      * Returns the ActionBean
  138.      *
  139.      * @return The ActionBean as a String
  140.      */
  141.     public String getActionBeanString( )
  142.     {
  143.         return _strActionBean;
  144.     }

  145.     /**
  146.      * Set the ActionBean of the insert service
  147.      *
  148.      * @param strActionBean
  149.      *            the ActionBean
  150.      */
  151.     public void setActionBeanString( String strActionBean )
  152.     {
  153.         _strActionBean = strActionBean;
  154.     }

  155.     /**
  156.      * Register the name of the Action Bean associated with the service
  157.      *
  158.      * @param strSelectionBean
  159.      *            The ActionBean to use
  160.      */
  161.     public void setActionBeanClassName( String strSelectionBean )
  162.     {
  163.         _strActionBean = strSelectionBean;
  164.     }

  165.     /**
  166.      * Return the name of the plugin
  167.      *
  168.      * @return The name of the plugin
  169.      */
  170.     public String getPluginName( )
  171.     {
  172.         return _strPluginName;
  173.     }

  174.     /**
  175.      * Set the plugin name of the insert service
  176.      *
  177.      * @param strPluginName
  178.      *            the plugin name
  179.      */
  180.     public void setPluginName( String strPluginName )
  181.     {
  182.         _strPluginName = strPluginName;
  183.     }

  184.     /**
  185.      * Returns the Bean to select an object
  186.      *
  187.      * @return an Instance of the Bean
  188.      * @see #getActionBeanString()
  189.      */
  190.     public InsertServiceSelectionBean getSelectionActionBean( )
  191.     {
  192.         try
  193.         {
  194.             return (InsertServiceSelectionBean) Class.forName( getActionBeanString( ) ).newInstance( );
  195.         }
  196.         catch( ClassNotFoundException | IllegalAccessException | InstantiationException e )
  197.         {
  198.             throw new AppException( "Error instantiating a LinkService Selection Bean : " + e.getMessage( ), e );
  199.         }
  200.     }

  201.     /**
  202.      * Get the UI to select an object
  203.      *
  204.      * @param request
  205.      *            The Http request.
  206.      * @return HTML code of the page as string
  207.      */
  208.     public String getSelectorUI( HttpServletRequest request )
  209.     {
  210.         return getSelectionActionBean( ).getInsertServiceSelectorUI( request );
  211.     }

  212.     /**
  213.      * Tells if the insertservice is enable (plugin activated).
  214.      *
  215.      * @return Returns the state of the insert service : enable or disable
  216.      */
  217.     public boolean isEnabled( )
  218.     {
  219.         Plugin plugin = PluginService.getPlugin( _strPluginName );

  220.         return plugin.isInstalled( );
  221.     }

  222.     // //////////////////////////////////////////////////////////////////////////
  223.     // RBAC Resource implementation

  224.     /**
  225.      * Returns the Resource Type Code that identify the resource type
  226.      *
  227.      * @return The Resource Type Code
  228.      */
  229.     public String getResourceTypeCode( )
  230.     {
  231.         return RESOURCE_TYPE;
  232.     }

  233.     /**
  234.      * Returns the resource Id of the current object
  235.      *
  236.      * @return The resource Id of the current object
  237.      */
  238.     public String getResourceId( )
  239.     {
  240.         return "" + getId( );
  241.     }

  242.     /**
  243.      * Implements Localizable
  244.      *
  245.      * @param locale
  246.      *            The current locale
  247.      */
  248.     public void setLocale( Locale locale )
  249.     {
  250.         _locale = locale;
  251.     }
  252. }