DashboardComponent.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.dashboard;

  35. import fr.paris.lutece.portal.service.i18n.I18nService;
  36. import fr.paris.lutece.portal.service.plugin.Plugin;
  37. import fr.paris.lutece.portal.service.plugin.PluginService;
  38. import fr.paris.lutece.portal.web.l10n.LocaleService;
  39. import java.util.Locale;

  40. import org.apache.commons.lang3.ObjectUtils;

  41. /**
  42.  * Dashboard Component
  43.  */
  44. public abstract class DashboardComponent implements IDashboardComponent
  45. {
  46.     private String _strName;
  47.     private String _strRight;
  48.     private int _nZone;
  49.     private int _nOrder;
  50.     private Plugin _plugin;
  51.     private Locale _locale;

  52.     /**
  53.      * Returns the Name
  54.      *
  55.      * @return The Name
  56.      */
  57.     @Override
  58.     public String getName( )
  59.     {
  60.         return _strName;
  61.     }

  62.     /**
  63.      * Sets the Name
  64.      *
  65.      * @param strName
  66.      *            The Name
  67.      */
  68.     @Override
  69.     public void setName( String strName )
  70.     {
  71.         _strName = strName;
  72.     }

  73.     /**
  74.      * Returns the PluginName
  75.      *
  76.      * @return The PluginName
  77.      */

  78.     /**
  79.      * Returns the Right
  80.      *
  81.      * @return The Right
  82.      */
  83.     @Override
  84.     public String getRight( )
  85.     {
  86.         return _strRight;
  87.     }

  88.     /**
  89.      * Sets the Right
  90.      *
  91.      * @param strRight
  92.      *            The Right
  93.      */
  94.     @Override
  95.     public void setRight( String strRight )
  96.     {
  97.         _strRight = strRight;
  98.     }

  99.     /**
  100.      * Returns the Zone
  101.      *
  102.      * @return The Zone
  103.      */
  104.     @Override
  105.     public int getZone( )
  106.     {
  107.         return _nZone;
  108.     }

  109.     /**
  110.      * Sets the Zone
  111.      *
  112.      * @param nZone
  113.      *            The Zone
  114.      */
  115.     @Override
  116.     public void setZone( int nZone )
  117.     {
  118.         _nZone = nZone;
  119.     }

  120.     /**
  121.      * Returns the Order
  122.      *
  123.      * @return The Order
  124.      */
  125.     @Override
  126.     public int getOrder( )
  127.     {
  128.         return _nOrder;
  129.     }

  130.     /**
  131.      * Sets the Order
  132.      *
  133.      * @param nOrder
  134.      *            The Order
  135.      */
  136.     @Override
  137.     public void setOrder( int nOrder )
  138.     {
  139.         _nOrder = nOrder;
  140.     }

  141.     /**
  142.      * Returns the Plugin
  143.      *
  144.      * @return The Plugin
  145.      */
  146.     @Override
  147.     public Plugin getPlugin( )
  148.     {
  149.         return _plugin;
  150.     }

  151.     /**
  152.      * Sets the Plugin
  153.      *
  154.      * @param plugin
  155.      *            The plugin
  156.      */
  157.     @Override
  158.     public void setPlugin( Plugin plugin )
  159.     {
  160.         _plugin = plugin;
  161.     }

  162.     /**
  163.      * Compare component order
  164.      *
  165.      * @param o
  166.      *            The component to compare to
  167.      * @return less than 0 if the order is lower, 0 if equals and greater than 0 if higher
  168.      */
  169.     @Override
  170.     public int compareTo( IDashboardComponent o )
  171.     {
  172.         return getOrder( ) - o.getOrder( );
  173.     }

  174.     /**
  175.      * Tells if the component is enabled
  176.      *
  177.      * @return true if enabled
  178.      */
  179.     @Override
  180.     public boolean isEnabled( )
  181.     {
  182.         return PluginService.isPluginEnable( _plugin.getName( ) );
  183.     }

  184.     /**
  185.      *
  186.      * {@inheritDoc}
  187.      */
  188.     @Override
  189.     public boolean equals( Object obj )
  190.     {
  191.         if ( obj instanceof IDashboardComponent )
  192.         {
  193.             IDashboardComponent other = (IDashboardComponent) obj;

  194.             return ObjectUtils.equals( this.getName( ), other.getName( ) );
  195.         }

  196.         return false;
  197.     }

  198.     /**
  199.      *
  200.      * {@inheritDoc}
  201.      */
  202.     @Override
  203.     public int hashCode( )
  204.     {
  205.         return ObjectUtils.hashCode( this.getName( ) );
  206.     }

  207.     /**
  208.      *
  209.      * {@inheritDoc}
  210.      */
  211.     @Override
  212.     public String toString( )
  213.     {
  214.         return getClass( ).getName( ) + "[name=" + this.getName( ) + ", zone=" + this.getZone( ) + ", order=" + this.getOrder( ) + "]";
  215.     }

  216.     /**
  217.      *
  218.      * {@inheritDoc}
  219.      */
  220.     @Override
  221.     public void setLocale( Locale locale )
  222.     {
  223.         _locale = locale;
  224.     }

  225.     @Override
  226.     public String getDescription( )
  227.     {
  228.         String strKey;
  229.         Locale locale = ( _locale != null ) ? _locale : LocaleService.getDefault( );

  230.         if ( _plugin == PluginService.getCore( ) )
  231.         {
  232.             strKey = "portal.dashboard.dashboardComponent." + _strName + ".description";
  233.         }
  234.         else
  235.         {
  236.             strKey = _plugin.getName( ) + ".dashboardComponent." + _strName + ".description";
  237.         }
  238.         return I18nService.getLocalizedString( strKey, locale );
  239.     }
  240. }