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.web.record.filter.display.impl;
35  
36  import java.util.ArrayList;
37  import java.util.Iterator;
38  import java.util.LinkedHashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  import javax.servlet.http.HttpServletRequest;
43  
44  import org.apache.commons.collections.CollectionUtils;
45  import org.apache.commons.lang3.StringUtils;
46  import org.apache.commons.lang3.math.NumberUtils;
47  
48  import fr.paris.lutece.plugins.directory.business.Directory;
49  import fr.paris.lutece.plugins.directory.business.DirectoryFilter;
50  import fr.paris.lutece.plugins.directory.business.DirectoryHome;
51  import fr.paris.lutece.plugins.directory.business.EntryFilter;
52  import fr.paris.lutece.plugins.directory.business.EntryHome;
53  import fr.paris.lutece.plugins.directory.business.IEntry;
54  import fr.paris.lutece.plugins.directory.business.RecordField;
55  import fr.paris.lutece.plugins.directory.business.RecordFieldFilter;
56  import fr.paris.lutece.plugins.directory.business.RecordFieldHome;
57  import fr.paris.lutece.plugins.directory.modules.multiview.business.record.column.IRecordColumn;
58  import fr.paris.lutece.plugins.directory.modules.multiview.business.record.column.impl.RecordColumnEntry;
59  import fr.paris.lutece.plugins.directory.modules.multiview.business.record.filter.IRecordFilter;
60  import fr.paris.lutece.plugins.directory.modules.multiview.business.record.filter.configuration.RecordFilterConfiguration;
61  import fr.paris.lutece.plugins.directory.modules.multiview.business.record.filter.configuration.RecordFilterEntryConfiguration;
62  import fr.paris.lutece.plugins.directory.modules.multiview.service.DirectoryMultiviewPlugin;
63  import fr.paris.lutece.plugins.directory.modules.multiview.util.RecordEntryNameConstants;
64  import fr.paris.lutece.plugins.directory.modules.multiview.util.ReferenceListFactory;
65  import fr.paris.lutece.util.ReferenceList;
66  
67  /**
68   * Implementation of the IRecordFilterDisplay interface for the filter on the Entry column
69   */
70  public class RecordFilterDisplayEntry extends AbstractRecordFilterDisplay
71  {
72      // Constants
73      private static final String DEFAULT_ENTRY_VALUE = StringUtils.EMPTY;
74      private static final String PARAMETER_ENTRY_VALUE_PATTERN = "multiview_entry_value_%s";
75      private static final String ENTRY_VALUE_ATTRIBUTE = "value";
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public String getParameterName( )
82      {
83          return buildElementName( PARAMETER_ENTRY_VALUE_PATTERN );
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public Map<String, Object> getFilterDisplayMapValues( HttpServletRequest request )
91      {
92          String strEntryValue = DEFAULT_ENTRY_VALUE;
93          Map<String, Object> mapFilterNameValues = new LinkedHashMap<>( );
94  
95          String strParameterName = buildElementName( PARAMETER_ENTRY_VALUE_PATTERN );
96          String strEntryParameterValue = request.getParameter( strParameterName );
97          if ( StringUtils.isNotBlank( strEntryParameterValue ) )
98          {
99              int nRecordColumnPosition = NumberUtils.INTEGER_MINUS_ONE;
100             IRecordColumn recordColumn = retrieveRecordColumn( );
101             if ( recordColumn != null )
102             {
103                 nRecordColumnPosition = recordColumn.getRecordColumnPosition( );
104             }
105 
106             String strEntryValueColumnName = RecordEntryNameConstants.FILTER_ENTRY_BASE_NAME_PATTERN + nRecordColumnPosition;
107             mapFilterNameValues.put( strEntryValueColumnName, strEntryParameterValue );
108             strEntryValue = strEntryParameterValue;
109         }
110 
111         setValue( strEntryValue );
112 
113         return mapFilterNameValues;
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     @Override
120     public void buildTemplate( HttpServletRequest request )
121     {
122         String strParameterName = buildElementName( PARAMETER_ENTRY_VALUE_PATTERN );
123         manageFilterTemplate( request, createReferenceList( request ), strParameterName );
124     }
125 
126     /**
127      * Create the ReferenceList based on the value of the Entry for an Entry column
128      * 
129      * @return the ReferenceList with all values of the Entry for an Entry column
130      */
131     private ReferenceList createReferenceList( HttpServletRequest request )
132     {
133         List<IEntry> listIEntryToRetrieveValueFrom = new ArrayList<>( );
134 
135         IRecordColumn recordColumn = retrieveRecordColumn( );
136         if ( recordColumn instanceof RecordColumnEntry )
137         {
138             RecordColumnEntry recordColumnEntry = (RecordColumnEntry) recordColumn;
139             List<String> listEntryTitle = recordColumnEntry.getListEntryTitle( );
140             listIEntryToRetrieveValueFrom = getEntryListFromTitle( listEntryTitle );
141         }
142 
143         // Build the list of RecordFilter to use for the filter from the list of entry to search on
144         List<RecordField> listRecordField = getRecordFieldList( listIEntryToRetrieveValueFrom );
145         cleanListRecordField( listRecordField );
146         filterListRecordField( listRecordField, request );
147         ReferenceListFactory referenceListFactory = new ReferenceListFactory( listRecordField, ENTRY_VALUE_ATTRIBUTE, ENTRY_VALUE_ATTRIBUTE, Boolean.FALSE );
148 
149         String strDefaultReferenceListName = getRecordFilterDisplayLabel( );
150         referenceListFactory.setDefaultName( strDefaultReferenceListName );
151         referenceListFactory.setDefaultSortNeeded( Boolean.TRUE );
152 
153         return referenceListFactory.createReferenceList( );
154     }
155 
156     /**
157      * Return the list of entry built from the name of the entry which are stored in configuration of the column
158      * 
159      * @param listEntryTitle
160      *            The list of title of entry to retrieve the value from
161      * @return the list of entry built from the given list of entry title
162      */
163     private static List<IEntry> getEntryListFromTitle( List<String> listEntryTitle )
164     {
165         List<IEntry> listEntry = new ArrayList<>( );
166 
167         if ( listEntryTitle != null && !listEntryTitle.isEmpty( ) )
168         {
169             // Retrieve the list of directory
170             DirectoryFilter directoryFilter = new DirectoryFilter( );
171             List<Directory> listDirectory = DirectoryHome.getDirectoryList( directoryFilter, DirectoryMultiviewPlugin.getPlugin( ) );
172 
173             if ( listDirectory != null && !listDirectory.isEmpty( ) )
174             {
175                 for ( Directory directory : listDirectory )
176                 {
177                     listEntry.addAll( fillEntryListFromTitle( directory.getIdDirectory( ), listEntryTitle ) );
178                 }
179             }
180         }
181 
182         return listEntry;
183     }
184 
185     /**
186      * Return the list of all the IEntry of the specified directory which have the same title than the value in the given list.
187      * 
188      * @param nIdDirectory
189      *            The identifier of the directory to retrieve the IEntry from
190      * @param listEntryTitle
191      *            The list of title of entry to retrieve the value from
192      * @return the list of entry retrieve from the directory from the given list of entry title
193      */
194     private static List<IEntry> fillEntryListFromTitle( int nIdDirectory, List<String> listEntryTitle )
195     {
196         List<IEntry> listIEntry = new ArrayList<>( );
197 
198         EntryFilter entryFilter = new EntryFilter( );
199         entryFilter.setIdDirectory( nIdDirectory );
200         entryFilter.setIsGroup( EntryFilter.FILTER_FALSE );
201         entryFilter.setIsComment( EntryFilter.FILTER_FALSE );
202 
203         List<IEntry> entryList = EntryHome.getEntryList( entryFilter, DirectoryMultiviewPlugin.getPlugin( ) );
204 
205         for ( String strEntryTitleToFind : listEntryTitle )
206         {
207             for ( IEntry entry : entryList )
208             {
209                 if ( strEntryTitleToFind.equals( entry.getTitle( ) ) )
210                 {
211                     listIEntry.add( entry );
212                 }
213             }
214         }
215 
216         return listIEntry;
217     }
218 
219     /**
220      * Return the list of RecordField which are associated to the specified entry of the given list
221      * 
222      * @param listIEntryToRetrieveValueFrom
223      *            The list of entry to retrieve the record field value from
224      * @return the list of RecordField which belong to an entry of the list of Entry which we want to gather values
225      */
226     private static List<RecordField> getRecordFieldList( List<IEntry> listIEntryToRetrieveValueFrom )
227     {
228         List<RecordField> listRecordFieldResult = new ArrayList<>( );
229 
230         if ( listIEntryToRetrieveValueFrom != null && !listIEntryToRetrieveValueFrom.isEmpty( ) )
231         {
232             for ( IEntry entry : listIEntryToRetrieveValueFrom )
233             {
234                 RecordFieldFilter recordFieldFilter = new RecordFieldFilter( );
235                 recordFieldFilter.setIdEntry( entry.getIdEntry( ) );
236 
237                 listRecordFieldResult.addAll( RecordFieldHome.getRecordFieldList( recordFieldFilter, false, false, false, DirectoryMultiviewPlugin.getPlugin( ) ) );
238             }
239         }
240 
241         return listRecordFieldResult;
242     }
243 
244     /**
245      * Clean the given list of RecordField by removing all of those which have empty value
246      * 
247      * @param listRecordField
248      *            The list to remove the RecordField with empty value
249      */
250     private static void cleanListRecordField( List<RecordField> listRecordField )
251     {
252         if ( !CollectionUtils.isEmpty( listRecordField ) )
253         {
254             Iterator<RecordField> iteratorRecordField = listRecordField.iterator( );
255             while ( iteratorRecordField.hasNext( ) )
256             {
257                 RecordField recordField = iteratorRecordField.next( );
258                 if ( recordField != null && StringUtils.isBlank( recordField.getValue( ) ) )
259                 {
260                     iteratorRecordField.remove( );
261                 }
262             }
263         }
264     }
265 
266     /**
267      * Return the name of the element build from the given pattern and the position of the filter
268      * 
269      * @param strPatternElementName
270      *            The pattern of the name of the element
271      * @return the property key name for the current EntryRecordField filter for the entry to map
272      */
273     private String buildElementName( String strPatternElementName )
274     {
275         int nPosition = NumberUtils.INTEGER_MINUS_ONE;
276 
277         IRecordFilter recordFilter = getRecordFilter( );
278         if ( recordFilter != null && recordFilter.getRecordFilterConfiguration( ) != null )
279         {
280             nPosition = recordFilter.getRecordFilterConfiguration( ).getPosition( );
281         }
282 
283         return String.format( strPatternElementName, nPosition );
284     }
285 
286     /**
287      * Return the IRecordColumn from the RecordFilterConfiguration of the RecordFilter of the RecordFilterDisplay
288      * 
289      * @return the IRecordColumn from the RecordFilterConfiguration of the RecordFilter of the RecordFilterDisplay or null if not exist
290      */
291     private IRecordColumn retrieveRecordColumn( )
292     {
293         IRecordColumn recordColumnResult = null;
294 
295         IRecordFilter recordFilter = getRecordFilter( );
296         if ( recordFilter != null )
297         {
298             RecordFilterConfiguration recordFilterConfiguration = recordFilter.getRecordFilterConfiguration( );
299             if ( recordFilterConfiguration instanceof RecordFilterEntryConfiguration )
300             {
301                 RecordFilterEntryConfiguration recordFilterEntryRecordFieldConfiguration = (RecordFilterEntryConfiguration) recordFilterConfiguration;
302                 recordColumnResult = recordFilterEntryRecordFieldConfiguration.getRecordColumn( );
303             }
304         }
305 
306         return recordColumnResult;
307     }
308 
309     /**
310      * Process a filtration of the record field; must be overrided
311      * 
312      * @param listRecordField
313      *            The list of recordField
314      * @param request
315      */
316     protected void filterListRecordField( List<RecordField> listRecordField, HttpServletRequest request )
317     {
318         // Do nothing
319     }
320 }