View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.business.form.filter.querypart.impl;
35  
36  import java.util.Arrays;
37  import java.util.List;
38  import java.util.Map;
39  import java.util.Set;
40  import java.util.stream.Collectors;
41  
42  import org.apache.commons.collections.CollectionUtils;
43  import org.apache.commons.lang3.StringUtils;
44  import org.apache.lucene.index.Term;
45  import org.apache.lucene.search.BooleanClause;
46  import org.apache.lucene.search.BooleanQuery;
47  import org.apache.lucene.search.Query;
48  import org.apache.lucene.search.TermQuery;
49  
50  import fr.paris.lutece.plugins.forms.business.Question;
51  import fr.paris.lutece.plugins.forms.business.QuestionHome;
52  import fr.paris.lutece.plugins.forms.business.form.FormParameters;
53  import fr.paris.lutece.plugins.forms.business.form.search.FormResponseSearchItem;
54  import fr.paris.lutece.plugins.forms.service.entrytype.EntryTypeCheckBox;
55  import fr.paris.lutece.plugins.forms.service.entrytype.EntryTypeRadioButton;
56  import fr.paris.lutece.plugins.forms.service.entrytype.EntryTypeSelect;
57  import fr.paris.lutece.plugins.forms.service.search.IFormSearchEngine;
58  import fr.paris.lutece.plugins.forms.util.FormsConstants;
59  import fr.paris.lutece.plugins.genericattributes.business.Field;
60  import fr.paris.lutece.plugins.genericattributes.business.FieldHome;
61  import fr.paris.lutece.plugins.genericattributes.service.entrytype.EntryTypeServiceManager;
62  import fr.paris.lutece.plugins.genericattributes.service.entrytype.IEntryTypeService;
63  
64  /**
65   * Implementation of the IFormFilterQueryPart for an Entry filter
66   */
67  public class FormFilterEntryLuceneQueryPart extends AbstractFormFilterLuceneQueryPart
68  {
69      IFormSearchEngine _formSearchEngine;
70  
71      @Override
72      public void buildFormFilterQuery( FormParameters formParameters )
73      {
74          BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder( );
75          if ( formParameters.getFormParametersMap( ).isEmpty( ) )
76          {
77              setFormFilterQuery( null );
78              return;
79          }
80          
81          Set<Map.Entry<String, Object>> setFormParameters = formParameters.getFormParametersMap( ).entrySet( );
82  
83          boolean bEmptyQuery = true;
84          for ( Map.Entry<String, Object> formParam : setFormParameters )
85          {
86  
87              String params = String.valueOf( formParam.getValue( ) );
88              List<String> valueList = Arrays.asList( params.split( ";" ) ).stream( )
89                      .filter( StringUtils::isNotEmpty )
90                      .filter( s -> !String.valueOf( FormsConstants.DEFAULT_ID_VALUE ).equals( s ) )
91                      .collect( Collectors.toList( ) );
92              
93              
94              if ( CollectionUtils.isNotEmpty( valueList ) )
95              {
96                  bEmptyQuery = false;
97                  String strQuestionCode = formParam.getKey( );
98                  List<Question> questionList = QuestionHome.findByCode( strQuestionCode );
99                  for ( Question question : questionList )
100                 {
101                     List<Field> listFields = FieldHome.getFieldListByIdEntry( question.getEntry( ).getIdEntry( ) );
102                     IEntryTypeService entryTypeService = EntryTypeServiceManager.getEntryTypeService( question.getEntry( ) );
103                     boolean isSelect = entryTypeService instanceof EntryTypeSelect || entryTypeService instanceof EntryTypeRadioButton || entryTypeService instanceof EntryTypeCheckBox;
104                     
105                     for ( String value : valueList )
106                     {
107                         Query query = new TermQuery( new Term(
108                                 FormResponseSearchItem.FIELD_ENTRY_CODE_SUFFIX + strQuestionCode + FormResponseSearchItem.FIELD_RESPONSE_FIELD_ITER + "0",
109                                 value ) );
110                         booleanQueryBuilder.add( query, BooleanClause.Occur.SHOULD );
111                         for ( Field field : listFields )
112                         {
113                             String strFieldName = getFieldName( field );
114                             query = new TermQuery(
115                                     new Term(
116                                             FormResponseSearchItem.FIELD_ENTRY_CODE_SUFFIX + strQuestionCode + FormResponseSearchItem.FIELD_RESPONSE_FIELD_ITER
117                                                     + "0" + FormResponseSearchItem.FIELD_RESPONSE_FIELD_SEPARATOR + strFieldName + ( isSelect ? FormResponseSearchItem.FIELD_SELECT_SUFFIX : "" ),
118                                                     value ) );
119                                 booleanQueryBuilder.add( query, BooleanClause.Occur.SHOULD );
120                         }
121                         
122                     }
123                 }
124             }
125         }
126         if ( !bEmptyQuery )
127         {
128             setFormFilterQuery( booleanQueryBuilder.build( ) );
129         }
130         else
131         {
132             setFormFilterQuery( null );
133         }
134     }
135 
136     /**
137      * Get the field name
138      * 
139      * @param responseField
140      * @param response
141      * @return the field name
142      */
143     private String getFieldName( Field responseField )
144     {
145         if ( responseField.getIdField( ) > 0 )
146         {
147             return String.valueOf( responseField.getIdField( ) );
148         }
149         if ( !StringUtils.isEmpty( responseField.getCode( ) ) )
150         {
151             return responseField.getCode( );
152         }
153         if ( !StringUtils.isEmpty( responseField.getTitle( ) ) )
154         {
155             return responseField.getTitle( );
156         }
157         return StringUtils.EMPTY;
158     }
159 }