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