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.util;
35  
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.HashMap;
39  import java.util.Map;
40  import java.util.Map.Entry;
41  
42  /**
43   * This class provides the structure for a list of references values Each element includes a code and a name
44   */
45  public class ReferenceList extends ArrayList<ReferenceItem>
46  {
47      /**
48       * Generated serialVersionUID
49       */
50      private static final long serialVersionUID = 5456351278712947650L;
51  
52      /**
53       * Default constructor.
54       */
55      public ReferenceList( )
56      {
57          super( );
58      }
59  
60      /**
61       * Creates a new reference list with a specified initial capacity.
62       * 
63       * @param nInitialCapacity
64       *            The initial capacity of the collection
65       */
66      public ReferenceList( int nInitialCapacity )
67      {
68          super( nInitialCapacity );
69      }
70  
71      /**
72       * This method adds a new element (code, name) to the list
73       *
74       * @param strCode
75       *            The code as a String
76       * @param strName
77       *            The name corresponding to the code as a String
78       */
79      public void addItem( String strCode, String strName )
80      {
81          ReferenceItemm.html#ReferenceItem">ReferenceItem item = new ReferenceItem( );
82          item.setCode( strCode );
83          item.setName( strName );
84          add( item );
85      }
86  
87      /**
88       * This method converts the int code specified in parameter as a String and adds a new element (code, name) to the list
89       *
90       * @param nCode
91       *            The code to convert an add
92       * @param strName
93       *            The name corresponding to the code as a String
94       */
95      public void addItem( int nCode, String strName )
96      {
97          ReferenceItemm.html#ReferenceItem">ReferenceItem item = new ReferenceItem( );
98          String strCode = String.valueOf( nCode );
99          item.setCode( strCode );
100         item.setName( strName );
101         add( item );
102     }
103 
104     /**
105      * Converts a collection to a ReferenceList
106      * 
107      * @param collection
108      *            The collection to convert
109      * @param strCodeAttribute
110      *            The name of the code attribute. Ex : "id" to call getId()
111      * @param strNameAttribute
112      *            The name of the code attribute. Ex : "name" to call getName()
113      * @param bNumericCode
114      *            true if the code getter returns an Integer, false otherwise
115      * @return The ReferenceList filled
116      * @since v1.1
117      */
118     public static ReferenceList convert( Collection collection, String strCodeAttribute, String strNameAttribute, boolean bNumericCode )
119     {
120         ReferenceListt.html#ReferenceList">ReferenceList list = new ReferenceList( );
121         String strCodeGetter = "get" + Character.toUpperCase( strCodeAttribute.charAt( 0 ) ) + strCodeAttribute.substring( 1 );
122         String strNameGetter = "get" + Character.toUpperCase( strNameAttribute.charAt( 0 ) ) + strNameAttribute.substring( 1 );
123         String strCode;
124         String strName;
125 
126         try
127         {
128             for ( Object o : collection )
129             {
130                 // build getter method name
131                 if ( bNumericCode )
132                 {
133                     Integer nCode = (Integer) o.getClass( ).getMethod( strCodeGetter, (Class [ ]) null ).invoke( o, (Object [ ]) null );
134                     strCode = nCode.toString( );
135                 }
136                 else
137                 {
138                     strCode = (String) o.getClass( ).getMethod( strCodeGetter, (Class [ ]) null ).invoke( o, (Object [ ]) null );
139                 }
140 
141                 strName = (String) o.getClass( ).getMethod( strNameGetter, (Class [ ]) null ).invoke( o, (Object [ ]) null );
142                 list.addItem( strCode, strName );
143             }
144         }
145         catch( Exception ex )
146         {
147             return null;
148         }
149 
150         return list;
151     }
152 
153     /**
154      * Convert a Map&lt;String, String&gt; into a ReferenceList
155      * 
156      * @param map
157      *            the map to convert
158      * @return the converted ReferenceList
159      */
160     public static ReferenceList convert( Map<String, String> map )
161     {
162         if ( map != null )
163         {
164             ReferenceListt.html#ReferenceList">ReferenceList list = new ReferenceList( );
165 
166             for ( Entry<String, String> param : map.entrySet( ) )
167             {
168                 list.addItem( param.getKey( ), param.getValue( ) );
169             }
170 
171             return list;
172         }
173 
174         return null;
175     }
176 
177     /**
178      * Convert the ReferenceList into a map
179      * 
180      * @return the converted map
181      */
182     public Map<String, String> toMap( )
183     {
184         Map<String, String> map = new HashMap<>( );
185 
186         if ( !this.isEmpty( ) )
187         {
188             for ( ReferenceItem item : this )
189             {
190                 map.put( item.getCode( ), item.getName( ) );
191             }
192         }
193 
194         return map;
195     }
196 
197     /**
198      * CheckItems when are selected
199      * 
200      * @param valuesToCheck
201      *            The list of string
202      */
203     public void checkItems( String [ ] valuesToCheck )
204     {
205         for ( int i = 0; i < valuesToCheck.length; i++ )
206         {
207             for ( ReferenceItem item : this )
208             {
209                 if ( item.getCode( ).equals( valuesToCheck [i] ) )
210                 {
211                     item.setChecked( true );
212                 }
213             }
214         }
215     }
216 }