ReferenceList.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.util;

  35. import java.util.ArrayList;
  36. import java.util.Collection;
  37. import java.util.HashMap;
  38. import java.util.Map;
  39. import java.util.Map.Entry;

  40. /**
  41.  * This class provides the structure for a list of references values Each element includes a code and a name
  42.  */
  43. public class ReferenceList extends ArrayList<ReferenceItem>
  44. {
  45.     /**
  46.      * Generated serialVersionUID
  47.      */
  48.     private static final long serialVersionUID = 5456351278712947650L;

  49.     /**
  50.      * Default constructor.
  51.      */
  52.     public ReferenceList( )
  53.     {
  54.         super( );
  55.     }

  56.     /**
  57.      * Creates a new reference list with a specified initial capacity.
  58.      *
  59.      * @param nInitialCapacity
  60.      *            The initial capacity of the collection
  61.      */
  62.     public ReferenceList( int nInitialCapacity )
  63.     {
  64.         super( nInitialCapacity );
  65.     }

  66.     /**
  67.      * This method adds a new element (code, name) to the list
  68.      *
  69.      * @param strCode
  70.      *            The code as a String
  71.      * @param strName
  72.      *            The name corresponding to the code as a String
  73.      */
  74.     public void addItem( String strCode, String strName )
  75.     {
  76.         ReferenceItem item = new ReferenceItem( );
  77.         item.setCode( strCode );
  78.         item.setName( strName );
  79.         add( item );
  80.     }

  81.     /**
  82.      * This method converts the int code specified in parameter as a String and adds a new element (code, name) to the list
  83.      *
  84.      * @param nCode
  85.      *            The code to convert an add
  86.      * @param strName
  87.      *            The name corresponding to the code as a String
  88.      */
  89.     public void addItem( int nCode, String strName )
  90.     {
  91.         ReferenceItem item = new ReferenceItem( );
  92.         String strCode = String.valueOf( nCode );
  93.         item.setCode( strCode );
  94.         item.setName( strName );
  95.         add( item );
  96.     }

  97.     /**
  98.      * Converts a collection to a ReferenceList
  99.      *
  100.      * @param collection
  101.      *            The collection to convert
  102.      * @param strCodeAttribute
  103.      *            The name of the code attribute. Ex : "id" to call getId()
  104.      * @param strNameAttribute
  105.      *            The name of the code attribute. Ex : "name" to call getName()
  106.      * @param bNumericCode
  107.      *            true if the code getter returns an Integer, false otherwise
  108.      * @return The ReferenceList filled
  109.      * @since v1.1
  110.      */
  111.     public static ReferenceList convert( Collection collection, String strCodeAttribute, String strNameAttribute, boolean bNumericCode )
  112.     {
  113.         ReferenceList list = new ReferenceList( );
  114.         String strCodeGetter = "get" + Character.toUpperCase( strCodeAttribute.charAt( 0 ) ) + strCodeAttribute.substring( 1 );
  115.         String strNameGetter = "get" + Character.toUpperCase( strNameAttribute.charAt( 0 ) ) + strNameAttribute.substring( 1 );
  116.         String strCode;
  117.         String strName;

  118.         try
  119.         {
  120.             for ( Object o : collection )
  121.             {
  122.                 // build getter method name
  123.                 if ( bNumericCode )
  124.                 {
  125.                     Integer nCode = (Integer) o.getClass( ).getMethod( strCodeGetter, (Class [ ]) null ).invoke( o, (Object [ ]) null );
  126.                     strCode = nCode.toString( );
  127.                 }
  128.                 else
  129.                 {
  130.                     strCode = (String) o.getClass( ).getMethod( strCodeGetter, (Class [ ]) null ).invoke( o, (Object [ ]) null );
  131.                 }

  132.                 strName = (String) o.getClass( ).getMethod( strNameGetter, (Class [ ]) null ).invoke( o, (Object [ ]) null );
  133.                 list.addItem( strCode, strName );
  134.             }
  135.         }
  136.         catch( Exception ex )
  137.         {
  138.             return null;
  139.         }

  140.         return list;
  141.     }

  142.     /**
  143.      * Convert a Map&lt;String, String&gt; into a ReferenceList
  144.      *
  145.      * @param map
  146.      *            the map to convert
  147.      * @return the converted ReferenceList
  148.      */
  149.     public static ReferenceList convert( Map<String, String> map )
  150.     {
  151.         if ( map != null )
  152.         {
  153.             ReferenceList list = new ReferenceList( );

  154.             for ( Entry<String, String> param : map.entrySet( ) )
  155.             {
  156.                 list.addItem( param.getKey( ), param.getValue( ) );
  157.             }

  158.             return list;
  159.         }

  160.         return null;
  161.     }

  162.     /**
  163.      * Convert the ReferenceList into a map
  164.      *
  165.      * @return the converted map
  166.      */
  167.     public Map<String, String> toMap( )
  168.     {
  169.         Map<String, String> map = new HashMap<>( );

  170.         if ( !this.isEmpty( ) )
  171.         {
  172.             for ( ReferenceItem item : this )
  173.             {
  174.                 map.put( item.getCode( ), item.getName( ) );
  175.             }
  176.         }

  177.         return map;
  178.     }

  179.     /**
  180.      * CheckItems when are selected
  181.      *
  182.      * @param valuesToCheck
  183.      *            The list of string
  184.      */
  185.     public void checkItems( String [ ] valuesToCheck )
  186.     {
  187.         for ( int i = 0; i < valuesToCheck.length; i++ )
  188.         {
  189.             for ( ReferenceItem item : this )
  190.             {
  191.                 if ( item.getCode( ).equals( valuesToCheck [i] ) )
  192.                 {
  193.                     item.setChecked( true );
  194.                 }
  195.             }
  196.         }
  197.     }
  198. }