View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.participatorybudget.web;
35  
36  import java.io.IOException;
37  import java.util.regex.Matcher;
38  import java.util.regex.Pattern;
39  
40  import javax.servlet.ServletException;
41  import javax.servlet.http.HttpServletRequest;
42  import javax.servlet.http.HttpServletResponse;
43  
44  import org.apache.commons.lang.StringUtils;
45  import org.apache.log4j.Logger;
46  import org.springframework.web.util.UriComponents;
47  import org.springframework.web.util.UriComponentsBuilder;
48  
49  import fr.paris.lutece.plugins.participatorybudget.business.MyInfosForm;
50  import fr.paris.lutece.plugins.participatorybudget.service.MyInfosService;
51  import fr.paris.lutece.plugins.participatorybudget.service.campaign.CampaignService;
52  import fr.paris.lutece.portal.service.security.LuteceUser;
53  import fr.paris.lutece.portal.service.security.SecurityService;
54  import fr.paris.lutece.portal.service.util.AppPropertiesService;
55  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
56  import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
57  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
58  import fr.paris.lutece.portal.web.LocalVariables;
59  import fr.paris.lutece.portal.web.xpages.XPage;
60  
61  /**
62   * This class provides the user interface to manage ParisConnectUser xpages ( manage, create, modify, remove )
63   */
64  @Controller( xpageName = ProjectsXpage.PAGE_SOLR_PROJECT_SEARCH, pageTitleI18nKey = "participatorybudget.xpage.solrProjectSearch.pageTitle", pagePathI18nKey = "participatorybudget.xpage.solrProjectSearch.pagePathLabel" )
65  public class ProjectsXpage extends MVCApplication
66  {
67  
68      private static final long serialVersionUID = -4316691400124512416L;
69      private static final Logger LOGGER = Logger.getLogger( ProjectsXpage.class );
70  
71      // Properties
72      public static final String PROJECT = AppPropertiesService.getProperty( "participatorybudget.type.project" );
73      public static final String LOCATION = AppPropertiesService.getProperty( "participatorybudget.name.location_text" );
74  
75      // Views
76      public static final String PAGE_SOLR_PROJECT_SEARCH = "solrProjectSearch";
77  
78      // Parameters
79      private static final String PARAMETER_REMOVE_ARR_FILTER = "remove_arr";
80      private static final String PARAMETER_FACET_QUERY = "fq";
81      private static final String PARAMETER_SORT_ORDER = "sort_order";
82      private static final String PARAMETER_SORT_NAME = "sort_name";
83      private static final String PARAMETER_QUERY = "query";
84  
85      // Marks
86      private static final String MARK_CONF = "conf";
87  
88      // Constants
89      private static final String strSearchSolrUrl = "Portal.jsp?page=search-solr";
90      private static final String strArrondissementPattern = "75(020|00[1-9]|116|01[0-9])$";
91      private static final String strUserRemoveArrFilterSelected = "on";
92      private static final String strAmpSymbol = "&";
93      private static final String strEqualSymbol = "=";
94      private static final String strDoublePointSymbol = ":";
95  
96      @View( value = PAGE_SOLR_PROJECT_SEARCH, defaultView = true )
97      public XPage solrProjectSearch( HttpServletRequest request ) throws ServletException, IOException
98      {
99          StringBuilder sbReq = new StringBuilder( strSearchSolrUrl );
100 
101         // Retrieve the search configuration
102         String strConf = request.getParameter( MARK_CONF );
103         if ( StringUtils.isNotBlank( strConf ) )
104         {
105             sbReq.append( strAmpSymbol + MARK_CONF + strEqualSymbol + strConf );
106         }
107 
108         // Retrieve facet query from request
109         String [ ] facetQuery = request.getParameterValues( PARAMETER_FACET_QUERY );
110         boolean locationFilterActivated = false;
111         if ( facetQuery != null && facetQuery.length > 0 )
112         {
113             for ( String strFacet : facetQuery )
114             {
115                 if ( !locationFilterActivated )
116                 {
117                     locationFilterActivated = ( StringUtils.isNotBlank( strFacet ) && strFacet.contains( LOCATION ) ) ? true : false;
118                 }
119                 sbReq.append( strAmpSymbol + "fq" + strEqualSymbol + strFacet );
120             }
121         }
122 
123         // Check if the user has remove the filter on his borough
124         String strRemoveUserArrFilter = request.getParameter( PARAMETER_REMOVE_ARR_FILTER );
125         boolean removeUserArrFilterSelected = ( StringUtils.isNotBlank( strRemoveUserArrFilter )
126                 && strUserRemoveArrFilterSelected.equals( strRemoveUserArrFilter ) ) ? Boolean.TRUE : Boolean.FALSE;
127         if ( !locationFilterActivated && removeUserArrFilterSelected )
128         {
129             sbReq.append( strAmpSymbol + PARAMETER_REMOVE_ARR_FILTER + strEqualSymbol + strRemoveUserArrFilter );
130         }
131 
132         // Check if user is connected and which project location he wants to see.
133         // Filtering projects only :
134         // - User connected
135         // - User do not clicked on "remove arrdt filter" case
136         // - User do not use "location" SOLR facet
137         // - Campaign is in SUBMIT / VOTE phase.
138         // - SOLR page is "projects_submitted_list"
139         // - User has a vote arrdt
140         LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
141         if ( user != null && !removeUserArrFilterSelected && !locationFilterActivated && "projects_submitted_list".equals( strConf )
142                 && CampaignService.getInstance( ).isAfterBeginning( "SUBMIT" ) && CampaignService.getInstance( ).isBeforeEnd( "VOTE" ) )
143         {
144             String strArrt = getArrondissement( user );
145             if ( StringUtils.isNotBlank( strArrt ) )
146             {
147                 sbReq.append( "&fq=" + LOCATION + strDoublePointSymbol + strArrt + "\" " + "OR" + " " + LOCATION + strDoublePointSymbol + "\"" + "Whole city" );
148             }
149 
150         }
151 
152         // Mange the sort of the query
153         String strSortOrder = request.getParameter( PARAMETER_SORT_ORDER );
154         if ( StringUtils.isNotBlank( strSortOrder ) )
155         {
156             sbReq.append( "&sort_order=" + strSortOrder );
157         }
158         else
159         {
160             sbReq.append( "&sort_order=asc" );
161         }
162 
163         String strSortName = request.getParameter( PARAMETER_SORT_NAME );
164         if ( StringUtils.isNotBlank( strSortName ) )
165         {
166             sbReq.append( "&sort_name=" + strSortName );
167         }
168         else
169         {
170             sbReq.append( "&sort_name=title" );
171         }
172 
173         // Manage the query
174         String strQuery = request.getParameter( PARAMETER_QUERY );
175         if ( StringUtils.isNotBlank( strQuery ) )
176         {
177             sbReq.append( "&query=" + strQuery );
178         }
179         else
180         {
181             sbReq.append( "&query=*:*" );
182         }
183 
184         LOGGER.debug( "RequĂȘte SOLR de date, redirection vers " + sbReq.toString( ) );
185 
186         UriComponents uriComponents = UriComponentsBuilder.fromUriString( sbReq.toString( ) ).build( );
187         String strEncodedUri = uriComponents.encode( "UTF-8" ).toUriString( );
188 
189         // Make the redirection
190         HttpServletResponse response = LocalVariables.getResponse( );
191         response.sendRedirect( strEncodedUri );
192 
193         return new XPage( );
194     }
195 
196     /**
197      * Get arrondissement of a user
198      * 
199      * @param request
200      * @return
201      */
202     private static String getArrondissement( LuteceUser user )
203     {
204         if ( user != null )
205         {
206             MyInfosForm myInfo = MyInfosService.loadUserInfos( user );
207             Pattern p = Pattern.compile( strArrondissementPattern );
208             Matcher m = p.matcher( myInfo.getArrondissement( ) );
209             if ( m.matches( ) )
210             {
211                 return Integer.valueOf( myInfo.getArrondissement( ).substring( 2 ) ) + "e arrondissement";
212             }
213         }
214         return StringUtils.EMPTY;
215     }
216 
217 }