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.panel.initializer.querypart;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  
39  import org.apache.commons.collections.CollectionUtils;
40  import org.apache.commons.lang3.StringUtils;
41  
42  import fr.paris.lutece.plugins.directory.modules.multiview.business.record.filter.RecordFilterQueryConstants;
43  
44  /**
45   * Query builder utility class for RecordFilterPanel
46   */
47  public final class RecordPanelInitializerQueryBuilder
48  {
49      /**
50       * Constructor
51       */
52      private RecordPanelInitializerQueryBuilder( )
53      {
54  
55      }
56  
57      /**
58       * Build the select query part of the RecordPanelInitializer
59       * 
60       * @param listRecordPanelInitializerQueryPart
61       *            The list of RecordPanelInitializerQueryPart to use
62       * @return the list of all select query part of the given list of IRecordPanelInitializerQueryPart
63       */
64      public static List<String> buildPanelInitializerSelectQueryParts( List<IRecordPanelInitializerQueryPart> listRecordPanelInitializerQueryPart )
65      {
66          List<String> listPanelInitializerSelectQueryParts = new ArrayList<>( );
67  
68          if ( !CollectionUtils.isEmpty( listRecordPanelInitializerQueryPart ) )
69          {
70              for ( IRecordPanelInitializerQueryPart recordPanelInitializerQueryPart : listRecordPanelInitializerQueryPart )
71              {
72                  String strRecordPanelInitializerSelectQueryPart = recordPanelInitializerQueryPart.getRecordPanelInitializerSelectQuery( );
73                  if ( StringUtils.isNotBlank( strRecordPanelInitializerSelectQueryPart ) )
74                  {
75                      listPanelInitializerSelectQueryParts.add( strRecordPanelInitializerSelectQueryPart );
76                  }
77              }
78          }
79  
80          return listPanelInitializerSelectQueryParts;
81      }
82  
83      /**
84       * Build the from query part of the RecordPanelInitializer
85       * 
86       * @param listRecordPanelInitializerQueryPart
87       *            The list of RecordPanelInitializerQueryPart to use
88       * @return the list of all from query part of the given list of IRecordPanelInitializerQueryPart
89       */
90      public static List<String> buildPanelInitializerFromQueryParts( List<IRecordPanelInitializerQueryPart> listRecordPanelInitializerQueryPart )
91      {
92          List<String> listPanelInitializerFromQueryParts = new ArrayList<>( );
93  
94          if ( !CollectionUtils.isEmpty( listRecordPanelInitializerQueryPart ) )
95          {
96              for ( IRecordPanelInitializerQueryPart recordPanelInitializerQueryPart : listRecordPanelInitializerQueryPart )
97              {
98                  String strRecordPanelInitializerFromQueryPart = recordPanelInitializerQueryPart.getRecordPanelInitializerFromQuery( );
99                  if ( StringUtils.isNotBlank( strRecordPanelInitializerFromQueryPart ) )
100                 {
101                     listPanelInitializerFromQueryParts.add( strRecordPanelInitializerFromQueryPart );
102                 }
103             }
104         }
105 
106         return listPanelInitializerFromQueryParts;
107     }
108 
109     /**
110      * Populate the given string builder with all the join queries from the given RecordPanelInitializer list
111      * 
112      * @param stringBuilderJoinQueryPart
113      *            The string builder to populate with all join queries parts of the RecordPanelInitializer
114      * @param listRecordPanelInitializerQueryPart
115      *            The list of all RecordPanelInitializerQueryPart to retrieve the list of join query parts from
116      */
117     public static void buildRecordPanelInitializerJoinQueryParts( StringBuilder stringBuilderJoinQueryPart,
118             List<IRecordPanelInitializerQueryPart> listRecordPanelInitializerQueryPart )
119     {
120         for ( IRecordPanelInitializerQueryPart recordPanelInitializerQueryPart : listRecordPanelInitializerQueryPart )
121         {
122             List<String> listRecordPanelInitializerJoinQueries = recordPanelInitializerQueryPart.getRecordPanelInitializerJoinQueries( );
123             if ( listRecordPanelInitializerJoinQueries != null && !listRecordPanelInitializerJoinQueries.isEmpty( ) )
124             {
125                 for ( String strRecordPanelInitializerJoinQuery : listRecordPanelInitializerJoinQueries )
126                 {
127                     if ( StringUtils.isNotBlank( strRecordPanelInitializerJoinQuery ) )
128                     {
129                         stringBuilderJoinQueryPart.append( strRecordPanelInitializerJoinQuery );
130                         stringBuilderJoinQueryPart.append( RecordFilterQueryConstants.SPACE_SEPARATOR );
131                     }
132                 }
133             }
134         }
135     }
136 }