View Javadoc
1   /*
2    * Copyright (c) 2002-2018, 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.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   * Class which contains necessaries informations for creating a ReferenceList
47   */
48  public class ReferenceListFactory
49  {
50      // Constants
51      private static final String DEFAULT_NAME = StringUtils.EMPTY;
52  
53      // Variables
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       * Constructor
64       * 
65       * @param collectionItem
66       *            The collection of items to create the ReferenceList from
67       * @param strCodeAttr
68       *            The name of the code attribute to retrieve the data from the item
69       * @param strNameAttribute
70       *            The name of the name of attribute to retrieve the data from the item
71       */
72      public ReferenceListFactory( Collection<?> collectionItem, String strCodeAttr, String strNameAttribute )
73      {
74          _collectionItem = collectionItem;
75          _strCodeAttr = strCodeAttr;
76          _strNameAttribute = strNameAttribute;
77      }
78  
79      /**
80       * Constructor
81       * 
82       * @param collectionItem
83       *            The collection of items to create the ReferenceList from
84       * @param strCodeAttr
85       *            The name of the code attribute to retrieve the data from the item
86       * @param strNameAttribute
87       *            The name of the name of attribute to retrieve the data from the item
88       * @param bNumerical
89       *            The boolean which tell if the code of the item is of type numeric or not
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      * Create the ReferenceList with the parameter values
101      * 
102      * @return the ReferenceList created with the parameters values
103      */
104     public ReferenceList createReferenceList( )
105     {
106         ReferenceList referenceListResult = new ReferenceList( );
107 
108         // Add the default ReferenceItem if necessary
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      * Return the given list without the duplicate ReferenceItem and sorted if needed
129      * 
130      * @param referenceList
131      *            The ReferenceList to remove the duplicates and to sort
132      * @return the given list without the duplicate ReferenceItem and sorted if needed
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      * Remove the duplicates of referenceItem. Two ReferenceItems are considered as duplicates if they have the same code value.
148      * 
149      * @param referenceList
150      *            The ReferenceList to remove the duplicates from
151      * @return the list without the duplicates
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      * Check if the name is null or not and if it is true replace it with {@link ReferenceListFactory.DEFAULT_NAME}
174      * 
175      * @param referenceListResult
176      *            The ReferenceList to analyze
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      * Set the default name of the default ReferenceItem
191      * 
192      * @param strDefaultName
193      *            The strDefaultName to set
194      */
195     public void setDefaultName( String strDefaultName )
196     {
197         _strDefaultName = strDefaultName;
198     }
199 
200     /**
201      * Set the boolean which tell if the default sort (based on the name value) is needed or not
202      * 
203      * @param bDefaultSortNeeded
204      *            The boolean which tell if the default sort (based on the name value) is needed or not
205      */
206     public void setDefaultSortNeeded( boolean bDefaultSortNeeded )
207     {
208         _bDefaultSortNeeded = bDefaultSortNeeded;
209     }
210 }