View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.blog.business.portlet;
35  
36  import fr.paris.lutece.portal.service.util.AppPropertiesService;
37  
38  /**
39   * PortletFilter class
40   * 
41   * @author merlinfe
42   *
43   */
44  public class PortletFilter
45  {
46      public static final String CONSTANTE_SPACE_STRING = " ";
47      public static final String PAGE_NAME = AppPropertiesService.getProperty( "blog.filter.page_name", "page_name" );
48      public static final String PORTLET_NAME = AppPropertiesService.getProperty( "blog.filter.portlet_name", "portlet_name" );
49      public static final String PAGE_ID = AppPropertiesService.getProperty( "blog.filter.page_id", "page_id" );
50      public static final int PROPERTY_NUMBER_OF_MAX_LATEST_PORTLETS_DISPLAY = AppPropertiesService
51              .getPropertyInt( "blog.filter.number_of_max_latest_portlets_display", 10 );
52      private static final String SQL_FILTER_PAGE_NAME = "  f.name LIKE ? ";
53      private static final String SQL_FILTER_BY_PAGE_ID = "  a.id_page=? ";
54      private static final String SQL_FILTER_BY_PORTLET_NAME = " a.name LIKE ? ";
55      private boolean _bIsDisplayLatestPortlets;
56      private String [ ] _aPageName;
57      private String [ ] _aPortletName;
58      private Integer _nIdPage;
59      private String _portletFilterType;
60      private String _strSearchValue;
61  
62      /**
63       * Set the page name list
64       * 
65       * @param aPageTitle
66       *            The list of titles of pages
67       */
68      public void setPageName( String [ ] aPageTitle )
69      {
70          _aPageName = aPageTitle;
71      }
72  
73      /**
74       * Get the page name list
75       * 
76       * @return The page name list
77       */
78      public String [ ] getPageName( )
79      {
80          return _aPageName;
81      }
82  
83      /**
84       * Set the portlet title filter
85       * 
86       * @param aPortletTitle
87       *            The portlet title filter
88       */
89      public void setPortletName( String [ ] aPortletTitle )
90      {
91          _aPortletName = aPortletTitle;
92      }
93  
94      /**
95       * Get the portlet title filter
96       * 
97       * @return The portlet title filter
98       */
99      public String [ ] getPortletName( )
100     {
101         return _aPortletName;
102     }
103 
104     /**
105      * Set the id of the page
106      * 
107      * @param nIdPage
108      *            The id of the page
109      */
110     public void setIdPage( Integer nIdPage )
111     {
112         _nIdPage = nIdPage;
113     }
114 
115     /**
116      * Get the id of the page
117      * 
118      * @return The id of the page
119      */
120     public Integer getIdPage( )
121     {
122         return _nIdPage;
123     }
124 
125     /**
126      * Set the display latest portlets filter attribute
127      * 
128      * @param bIsDisplayLatestPortlets
129      *            the display latest portlets filter attribute
130      */
131     public void setDisplayLatestPortlets( boolean bIsDisplayLatestPortlets )
132     {
133         _bIsDisplayLatestPortlets = bIsDisplayLatestPortlets;
134     }
135 
136     /**
137      * Get the display latest portlets filter attribute
138      * 
139      * @return The display latest portlets filter attribute
140      */
141     public boolean isDisplayLatestPortlets( )
142     {
143         return _bIsDisplayLatestPortlets;
144     }
145 
146     /**
147      * Set the portlet type
148      * 
149      * @param portletFilterType
150      *            The portlet type
151      */
152     public void setPortletFilterType( String portletFilterType )
153     {
154         _portletFilterType = portletFilterType;
155     }
156 
157     /**
158      * Get the portlet type
159      * 
160      * @return The portlet type
161      */
162     public String getPortletFilterType( )
163     {
164         return _portletFilterType;
165     }
166 
167     /**
168      * Get the SQL query for searching
169      *
170      * @return the SQL query
171      */
172     public String getSQLFilter( )
173     {
174         if ( ( _portletFilterType != null ) && !_bIsDisplayLatestPortlets )
175         {
176             StringBuilder sbSQL = new StringBuilder( );
177 
178             if ( _portletFilterType.equals( PAGE_NAME ) && ( _aPageName != null ) )
179             {
180                 sbSQL.append( " ( " );
181 
182                 for ( int i = 0; i < _aPageName.length; i++ )
183                 {
184                     sbSQL.append( SQL_FILTER_PAGE_NAME );
185 
186                     if ( i != ( _aPageName.length - 1 ) )
187                     {
188                         sbSQL.append( " OR " );
189                     }
190                 }
191 
192                 sbSQL.append( " ) " );
193             }
194             else
195                 if ( _portletFilterType.equals( PORTLET_NAME ) && ( _aPortletName != null ) )
196                 {
197                     sbSQL.append( " ( " );
198 
199                     for ( int i = 0; i < _aPortletName.length; i++ )
200                     {
201                         sbSQL.append( SQL_FILTER_BY_PORTLET_NAME );
202 
203                         if ( i != ( _aPortletName.length - 1 ) )
204                         {
205                             sbSQL.append( " OR " );
206                         }
207                     }
208 
209                     sbSQL.append( " ) " );
210                 }
211                 else
212                     if ( _portletFilterType.equals( PAGE_ID ) && ( _nIdPage != null ) )
213                     {
214                         sbSQL.append( SQL_FILTER_BY_PAGE_ID );
215                     }
216 
217             return sbSQL.toString( );
218         }
219 
220         return null;
221     }
222 
223     /**
224      * Set the search value
225      * 
226      * @param strSearchValue
227      *            the search value
228      */
229     public void setSearchValue( String strSearchValue )
230     {
231         _strSearchValue = strSearchValue;
232     }
233 
234     /**
235      * Get the search value
236      * 
237      * @return the search value
238      */
239     public String getSearchValue( )
240     {
241         return _strSearchValue;
242     }
243 }