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