1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
45 public class ReferenceList extends ArrayList<ReferenceItem>
46 {
47
48
49
50 private static final long serialVersionUID = 5456351278712947650L;
51
52
53
54
55 public ReferenceList( )
56 {
57 super( );
58 }
59
60
61
62
63
64
65
66 public ReferenceList( int nInitialCapacity )
67 {
68 super( nInitialCapacity );
69 }
70
71
72
73
74
75
76
77
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
89
90
91
92
93
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
106
107
108
109
110
111
112
113
114
115
116
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
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
155
156
157
158
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
179
180
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
199
200
201
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 }