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.plugins.directory.modules.multiview.util;
35
36 import java.util.Collection;
37 import java.util.LinkedHashSet;
38 import java.util.Set;
39
40 import org.apache.commons.lang3.StringUtils;
41
42 import fr.paris.lutece.util.ReferenceItem;
43 import fr.paris.lutece.util.ReferenceList;
44
45
46
47
48 public class ReferenceListFactory
49 {
50
51 private static final String DEFAULT_NAME = StringUtils.EMPTY;
52
53
54 private final Collection<?> _collectionItem;
55 private final String _strCodeAttr;
56 private final String _strNameAttribute;
57 private boolean _bNumerical = Boolean.TRUE;
58 private boolean _bDefaultSortNeeded = Boolean.FALSE;
59 private final String _strDefaultCode = DirectoryMultiviewConstants.REFERENCE_ITEM_DEFAULT_CODE;
60 private String _strDefaultName = DirectoryMultiviewConstants.REFERENCE_ITEM_DEFAULT_NAME;
61
62
63
64
65
66
67
68
69
70
71
72 public ReferenceListFactory( Collection<?> collectionItem, String strCodeAttr, String strNameAttribute )
73 {
74 _collectionItem = collectionItem;
75 _strCodeAttr = strCodeAttr;
76 _strNameAttribute = strNameAttribute;
77 }
78
79
80
81
82
83
84
85
86
87
88
89
90
91 public ReferenceListFactory( Collection<?> collectionItem, String strCodeAttr, String strNameAttribute, boolean bNumerical )
92 {
93 _collectionItem = collectionItem;
94 _strCodeAttr = strCodeAttr;
95 _strNameAttribute = strNameAttribute;
96 _bNumerical = bNumerical;
97 }
98
99
100
101
102
103
104 public ReferenceList createReferenceList( )
105 {
106 ReferenceList referenceListResult = new ReferenceList( );
107
108
109 referenceListResult.addItem( _strDefaultCode, _strDefaultName );
110
111 if ( _collectionItem != null && !_collectionItem.isEmpty( ) )
112 {
113 ReferenceList referenceList = ReferenceList.convert( _collectionItem, _strCodeAttr, _strNameAttribute, _bNumerical );
114
115 if ( referenceList != null && !referenceList.isEmpty( ) )
116 {
117 referenceListResult.addAll( removeDuplicateAndSortList( referenceList ) );
118 }
119
120 manageItemName( referenceListResult );
121
122 }
123
124 return referenceListResult;
125 }
126
127
128
129
130
131
132
133
134 private ReferenceList removeDuplicateAndSortList( ReferenceList referenceList )
135 {
136 ReferenceList referenceListWithoutDuplicate = filterDuplicatesReferenceItem( referenceList );
137
138 if ( _bDefaultSortNeeded )
139 {
140 referenceListWithoutDuplicate.sort( new ReferenceItemComparator( ) );
141 }
142
143 return referenceListWithoutDuplicate;
144 }
145
146
147
148
149
150
151
152
153 private ReferenceList filterDuplicatesReferenceItem( ReferenceList referenceList )
154 {
155 ReferenceList referenceListCleaned = new ReferenceList( );
156 Set<String> setCodeProcessed = new LinkedHashSet<>( );
157
158 for ( ReferenceItem referenceItem : referenceList )
159 {
160 String strCurrentCode = referenceItem.getCode( );
161
162 if ( !setCodeProcessed.contains( strCurrentCode ) )
163 {
164 setCodeProcessed.add( strCurrentCode );
165 referenceListCleaned.add( referenceItem );
166 }
167 }
168
169 return referenceListCleaned;
170 }
171
172
173
174
175
176
177
178 private void manageItemName( ReferenceList referenceListResult )
179 {
180 for ( ReferenceItem referenceItem : referenceListResult )
181 {
182 if ( referenceItem.getName( ) == null )
183 {
184 referenceItem.setName( DEFAULT_NAME );
185 }
186 }
187 }
188
189
190
191
192
193
194
195 public void setDefaultName( String strDefaultName )
196 {
197 _strDefaultName = strDefaultName;
198 }
199
200
201
202
203
204
205
206 public void setDefaultSortNeeded( boolean bDefaultSortNeeded )
207 {
208 _bDefaultSortNeeded = bDefaultSortNeeded;
209 }
210 }