View Javadoc
1   /*
2    * Copyright (c) 2002-2025, City of 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.forms.web.form.filter.display.impl;
35  
36  import java.util.ArrayList;
37  import java.util.Arrays;
38  import java.util.LinkedHashMap;
39  import java.util.List;
40  import java.util.Locale;
41  import java.util.Map;
42  import java.util.stream.Collectors;
43  
44  import javax.servlet.http.HttpServletRequest;
45  
46  import org.apache.commons.collections.CollectionUtils;
47  import org.apache.commons.lang3.ArrayUtils;
48  import org.apache.commons.lang3.StringUtils;
49  import org.apache.commons.lang3.math.NumberUtils;
50  
51  import fr.paris.lutece.plugins.forms.business.Form;
52  import fr.paris.lutece.plugins.forms.business.FormHome;
53  import fr.paris.lutece.plugins.forms.business.form.column.IFormColumn;
54  import fr.paris.lutece.plugins.forms.business.form.column.impl.FormColumnEntry;
55  import fr.paris.lutece.plugins.forms.business.form.filter.FormFilter;
56  import fr.paris.lutece.plugins.forms.business.form.filter.configuration.FormFilterEntryConfiguration;
57  import fr.paris.lutece.plugins.forms.business.form.filter.configuration.IFormFilterConfiguration;
58  import fr.paris.lutece.plugins.forms.util.ReferenceListFactory;
59  import fr.paris.lutece.plugins.genericattributes.business.Entry;
60  import fr.paris.lutece.plugins.genericattributes.business.EntryFilter;
61  import fr.paris.lutece.plugins.genericattributes.business.EntryHome;
62  import fr.paris.lutece.plugins.genericattributes.business.Field;
63  import fr.paris.lutece.plugins.genericattributes.business.FieldHome;
64  import fr.paris.lutece.plugins.genericattributes.service.entrytype.IEntryTypeService;
65  import fr.paris.lutece.util.ReferenceList;
66  
67  /**
68   * Implementation of the IFormFilterDisplay interface for the filter on the Entry column
69   */
70  public class FormFilterDisplayEntry extends AbstractFormFilterDisplay
71  {
72      // Constants
73      private static final String PARAMETER_ENTRY_VALUE_PATTERN = "multiview_entry_value_%s";
74      private static final String FORM_RESOURCE_TYPE = Form.RESOURCE_TYPE;
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public String getParameterName( )
81      {
82          return buildElementName( PARAMETER_ENTRY_VALUE_PATTERN );
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      @Override
89      public Map<String, Object> getFilterDisplayMapValues( HttpServletRequest request )
90      {
91          Map<String, Object> mapFilterNameValues = new LinkedHashMap<>( );
92  
93          String strParameterName = buildElementName( PARAMETER_ENTRY_VALUE_PATTERN );
94          String[] strEntryParameterArray = request.getParameterValues( strParameterName );
95          
96          
97          if ( ArrayUtils.isNotEmpty( strEntryParameterArray ) )
98          {
99              String value = Arrays.asList( strEntryParameterArray ).stream( ).collect( Collectors.joining( ";" ) );
100             setValue( value );
101             
102             IFormColumn formColumn = retrieveFormColumn( );
103             String strEntryValueColumnName = StringUtils.EMPTY;
104             if ( formColumn != null )
105             {
106                 FormColumnEntry../../../../fr/paris/lutece/plugins/forms/business/form/column/impl/FormColumnEntry.html#FormColumnEntry">FormColumnEntry formColumnEntry = (FormColumnEntry) formColumn;
107                 List<String> listEntryCode = formColumnEntry.getListEntryCode( );
108                 strEntryValueColumnName = listEntryCode.get( 0 );
109             }
110             mapFilterNameValues.put( strEntryValueColumnName, value );
111         }
112         else
113         {
114             setValue( StringUtils.EMPTY );
115         }
116         return mapFilterNameValues;
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public void buildTemplate(HttpServletRequest request, Locale locale)
124     {
125         String strParameterName = buildElementName( PARAMETER_ENTRY_VALUE_PATTERN );
126         manageFilterTemplate( request, createReferenceList( locale ), strParameterName );
127     }
128 
129     /**
130      * Create the ReferenceList based on the value of the Entry for an Entry column
131      * 
132      * @return the ReferenceList with all values of the Entry for an Entry column
133      */
134     private ReferenceList createReferenceList( Locale locale )
135     {
136         List<Entry> listIEntryToRetrieveValueFrom = new ArrayList<>( );
137 
138         IFormColumn formColumn = retrieveFormColumn( );
139         if ( formColumn instanceof FormColumnEntry )
140         {
141             FormColumnEntry../../../../fr/paris/lutece/plugins/forms/business/form/column/impl/FormColumnEntry.html#FormColumnEntry">FormColumnEntry formColumnEntry = (FormColumnEntry) formColumn;
142             List<String> listEntryCode = formColumnEntry.getListEntryCode( );
143             listIEntryToRetrieveValueFrom = getEntryListFromCode( listEntryCode );
144         }
145         ReferenceList referenceListResult = new ReferenceList( );
146         // Add the default ReferenceItem if necessary
147         referenceListResult.addItem( ReferenceListFactory.DEFAULT_CODE, getFormFilterDisplayLabel( locale ) );
148 
149         FieldHome.getFieldListByListIdEntry( listIEntryToRetrieveValueFrom.stream( ).map( Entry::getIdEntry ).distinct( ).collect( Collectors.toList( ) ) )
150                 .stream( ).filter( field -> IEntryTypeService.FIELD_ANSWER_CHOICE.equals( field.getCode( ) ) )
151                 .forEach( ( Field field ) -> referenceListResult.addItem( field.getValue( ), field.getTitle( ) ) );
152 
153         return referenceListResult;
154 
155     }
156 
157     /**
158      * Return the list of entry built from the name of the entry which are stored in configuration of the column
159      * 
160      * @param listEntryCode
161      *            The list of code of entry to retrieve the value from
162      * @return the list of entry built from the given list of entry code
163      */
164     private static List<Entry> getEntryListFromCode( List<String> listEntryCode )
165     {
166         List<Entry> listEntry = new ArrayList<>( );
167 
168         if ( !CollectionUtils.isEmpty( listEntryCode ) )
169         {
170             // Retrieve the list of forms
171             List<Form> listForms = FormHome.getFormList( );
172 
173             if ( !CollectionUtils.isEmpty( listForms ) )
174             {
175                 for ( Form form : listForms )
176                 {
177                     listEntry.addAll( fillEntryListFromCode( form.getId( ), listEntryCode ) );
178                 }
179             }
180         }
181 
182         return listEntry;
183     }
184 
185     /**
186      * Return the list of all the Entry of the specified form which have the same code than the value in the given list.
187      * 
188      * @param nIdForm
189      *            The identifier of the form to retrieve the Entry from
190      * @param listEntryCode
191      *            The list of code of entry to retrieve the value from
192      * @return the list of entry retrieve from the form from the given list of entry code
193      */
194     private static List<Entry> fillEntryListFromCode( int nIdForm, List<String> listEntryCode )
195     {
196         List<Entry> listEntry = new ArrayList<>( );
197 
198         EntryFilter entryFilter = new EntryFilter( );
199         entryFilter.setIdResource( nIdForm );
200         entryFilter.setResourceType( FORM_RESOURCE_TYPE );
201         entryFilter.setIdIsGroup( EntryFilter.FILTER_FALSE );
202         entryFilter.setIdIsComment( EntryFilter.FILTER_FALSE );
203 
204         List<Entry> entryList = EntryHome.getEntryList( entryFilter );
205 
206         for ( String strEntryCodeToFind : listEntryCode )
207         {
208             for ( Entry entry : entryList )
209             {
210                 if ( strEntryCodeToFind.equals( entry.getCode( ) ) )
211                 {
212                     listEntry.add( entry );
213                 }
214             }
215         }
216 
217         return listEntry;
218     }
219 
220     /**
221      * Return the name of the element build from the given pattern and the position of the filter
222      * 
223      * @param strPatternElementName
224      *            The pattern of the name of the element
225      * @return the property key name for the current Entry filter for the entry to map
226      */
227     private String buildElementName( String strPatternElementName )
228     {
229         int nPosition = NumberUtils.INTEGER_MINUS_ONE;
230 
231         FormFilter formFilter = getFormFilter( );
232         if ( formFilter != null && formFilter.getFormFilterConfiguration( ) != null )
233         {
234             nPosition = formFilter.getFormFilterConfiguration( ).getPosition( );
235         }
236 
237         return String.format( strPatternElementName, nPosition );
238     }
239 
240     /**
241      * Return the IFormColumn from the FormFilterConfiguration of the FormFilter of the FormFilterDisplay
242      * 
243      * @return the IFormColumn from the FormFilterConfiguration of the FormFilter of the FormFilterDisplay or null if not exist
244      */
245     private IFormColumn retrieveFormColumn( )
246     {
247         IFormColumn formColumnResult = null;
248 
249         FormFilter formFilter = getFormFilter( );
250         if ( formFilter != null )
251         {
252             IFormFilterConfiguration formFilterConfiguration = formFilter.getFormFilterConfiguration( );
253             if ( formFilterConfiguration instanceof FormFilterEntryConfiguration )
254             {
255                 FormFilterEntryConfiguratione/plugins/forms/business/form/filter/configuration/FormFilterEntryConfiguration.html#FormFilterEntryConfiguration">FormFilterEntryConfiguration formFilterEntryConfiguration = (FormFilterEntryConfiguration) formFilterConfiguration;
256                 formColumnResult = formFilterEntryConfiguration.getFormColumn( );
257             }
258         }
259 
260         return formColumnResult;
261     }
262 }