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.business.record.filter;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  import java.util.Map;
39  import java.util.Map.Entry;
40  import java.util.regex.Pattern;
41  
42  import org.apache.commons.lang3.StringUtils;
43  
44  import fr.paris.lutece.plugins.directory.modules.multiview.business.record.RecordParameters;
45  
46  /**
47   * Class used to build the query of the filter
48   */
49  public final class RecordFilterQueryBuilder
50  {
51      // Constants
52      private static final String DEFAULT_QUERY_VALUE = StringUtils.EMPTY;
53      private static final String KEY_NAME_SEPARATOR = "$";
54      private static final String DEFAULT_ITEM_VALUE = "-1";
55      private static final String PARAMETER_TO_REPLACE_SYMBOL = "?";
56  
57      /**
58       * Constructor
59       */
60      private RecordFilterQueryBuilder( )
61      {
62  
63      }
64  
65      /**
66       * Build the query of a RecordFilter from the specified pattern and the RecordFilterItem to retrieve the values from
67       * 
68       * @param strRecordFilterQueryPattern
69       *            The pattern to use for building the query of the RecordFilter
70       * @param recordParameters
71       *            The RecordParameters to retrieve the values from for format the pattern
72       * @return the query formatted with all parameter replace by their values or an empty String if a parameter is missing in the RecordFilterItem
73       */
74      public static String buildRecordFilterQuery( String strRecordFilterQueryPattern, RecordParameters recordParameters )
75      {
76          String strRecordFilterQuery = DEFAULT_QUERY_VALUE;
77          List<String> listParameterValuesToUse = new ArrayList<>( );
78  
79          if ( StringUtils.isNotBlank( strRecordFilterQueryPattern ) && recordParameters != null )
80          {
81              Map<String, Object> mapRecordParameterNameValue = recordParameters.getRecordParametersMap( );
82  
83              if ( mapRecordParameterNameValue != null && !mapRecordParameterNameValue.isEmpty( ) )
84              {
85                  strRecordFilterQuery = strRecordFilterQueryPattern;
86  
87                  for ( Entry<String, Object> entryRecordParameter : mapRecordParameterNameValue.entrySet( ) )
88                  {
89                      String strParameterName = entryRecordParameter.getKey( );
90                      Object objParameterValue = entryRecordParameter.getValue( );
91  
92                      // If a value is missing we will interrupt the processing for the current filter and
93                      // reset the current query to avoid SQL error
94                      if ( objParameterValue == null || String.valueOf( objParameterValue ).equals( DEFAULT_ITEM_VALUE ) )
95                      {
96                          strRecordFilterQuery = DEFAULT_QUERY_VALUE;
97                          break;
98                      }
99                      else
100                     {
101                         String strParameterValue = String.valueOf( objParameterValue );
102                         listParameterValuesToUse.add( strParameterValue );
103 
104                         String strParameterNameBuilt = buildParameterNameToReplace( strParameterName );
105                         strRecordFilterQuery = strRecordFilterQuery.replaceAll( Pattern.quote( strParameterNameBuilt ), PARAMETER_TO_REPLACE_SYMBOL );
106                     }
107                 }
108             }
109 
110             recordParameters.setListUsedParametersValue( listParameterValuesToUse );
111         }
112 
113         return strRecordFilterQuery;
114     }
115 
116     /**
117      * Format the parameter name to replace in the query pattern of the filter
118      * 
119      * @param strParameterName
120      *            The name of the parameter to format
121      * @return the formatted name of the parameter
122      */
123     private static String buildParameterNameToReplace( String strParameterName )
124     {
125         StringBuilder stringBuilderParameterName = new StringBuilder( );
126         stringBuilderParameterName.append( KEY_NAME_SEPARATOR ).append( strParameterName ).append( KEY_NAME_SEPARATOR );
127 
128         return stringBuilderParameterName.toString( );
129     }
130 }