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.service;
35  
36  import java.io.IOException;
37  import java.util.ArrayList;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  import javax.servlet.http.HttpServletRequest;
43  
44  import fr.paris.lutece.plugins.leaflet.business.GeolocItem;
45  import fr.paris.lutece.plugins.leaflet.service.IconService;
46  import fr.paris.lutece.plugins.participatorybudget.service.campaign.CampaignService;
47  import fr.paris.lutece.plugins.participatorybudget.service.vote.MyVoteService;
48  import fr.paris.lutece.plugins.participatorybudget.util.BudgetUtils;
49  import fr.paris.lutece.plugins.search.solr.business.SolrSearchEngine;
50  import fr.paris.lutece.plugins.search.solr.business.SolrSearchResult;
51  import fr.paris.lutece.plugins.search.solr.indexer.SolrItem;
52  import fr.paris.lutece.plugins.search.solr.service.ISolrSearchAppAddOn;
53  import fr.paris.lutece.portal.service.security.LuteceUser;
54  import fr.paris.lutece.portal.service.security.SecurityService;
55  import fr.paris.lutece.portal.service.spring.SpringContextService;
56  import fr.paris.lutece.portal.service.util.AppLogService;
57  import fr.paris.lutece.portal.service.util.AppPropertiesService;
58  
59  public class BudgetSolrAddon implements ISolrSearchAppAddOn
60  {
61  
62      // Parameters copied from SolrSearchApp
63      private static final String SOLRSEARCHAPP_PARAMETER_CONF = "conf";
64      private static final String SOLRSEARCHAPP_MARK_POINTS_GEOJSON = "geojson";
65      private static final String SOLRSEARCHAPP_MARK_POINTS_ID = "id";
66      private static final String SOLRSEARCHAPP_MARK_POINTS_FIELDCODE = "code";
67      private static final String SOLRSEARCHAPP_MARK_POINTS_TYPE = "type";
68      private static final String SOLRSEARCHAPP_PROPERTY_SOLR_RESPONSE_MAX = "solr.reponse.max";
69      private static final int SOLRSEARCHAPP_SOLR_RESPONSE_MAX = Integer
70              .parseInt( AppPropertiesService.getProperty( SOLRSEARCHAPP_PROPERTY_SOLR_RESPONSE_MAX, "50" ) );
71  
72      private static final String PARAMETER_CONF_MAP_PROJETS = "projects_submitted_map"; // Vue des projets soumis au vote
73  
74      private static final String SOLR_QUERY_ALL = "*:*";
75      private static final String PROPERTY_OLDPROJECTS_FQ = "participatorybudget.oldprojects.fq";
76      private static final String [ ] SOLR_FQ_OLDPROJECTS = {
77              AppPropertiesService.getProperty( PROPERTY_OLDPROJECTS_FQ, "(statut_project_text:GAGNANT' OR statut_project_text:'gagnant') AND type:'PB Project" )
78      };
79      private static final String MARK_OLDPROJECTS_POINTS = "oldprojects_points";
80  
81      private static final String MARK_ARRONDISSEMENT_VOTE_USER = "arrondissementVote";
82      private static final String PARAMETER_REMOVE_ARR_FILTER = "remove_arr";
83      private static final String MARK_REMOVE_ARR_FILTER = "remove_arr";
84  
85      /**
86       * Ajoute les projets des années précédentes.
87       */
88      public void buildPageAddOn( Map<String, Object> model, HttpServletRequest request )
89      {
90          if ( PARAMETER_CONF_MAP_PROJETS.equals( request.getParameter( SOLRSEARCHAPP_PARAMETER_CONF ) ) )
91          {
92              SolrSearchEngine engine = SolrSearchEngine.getInstance( );
93              List<SolrSearchResult> listResultsGeoloc = engine.getGeolocSearchResults( SOLR_QUERY_ALL, SOLR_FQ_OLDPROJECTS, SOLRSEARCHAPP_SOLR_RESPONSE_MAX );
94              List<HashMap<String, Object>> points = getGeolocModel( listResultsGeoloc );
95              model.put( MARK_OLDPROJECTS_POINTS, points );
96          }
97  
98          MyVoteService _myVoteService = SpringContextService.getBean( MyVoteService.BEAN_NAME );
99          String arrondissement = null;
100 
101         boolean isValidated = false;
102         LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
103         if ( user != null )
104         {
105             arrondissement = BudgetUtils.getArrondissementDisplay( user );
106             isValidated = _myVoteService.isUserVoteValidated( user.getName( ) );
107         }
108 
109         if ( arrondissement != null )
110         {
111             model.put( MARK_ARRONDISSEMENT_VOTE_USER, arrondissement );
112         }
113         else
114         {
115             model.put( MARK_ARRONDISSEMENT_VOTE_USER, "notConnected" );
116         }
117 
118         model.put( BudgetUtils.MARK_VOTE_VALIDATED, isValidated );
119         model.put( MARK_REMOVE_ARR_FILTER, request.getParameter( PARAMETER_REMOVE_ARR_FILTER ) );
120 
121         model.put( BudgetUtils.MARK_CAMPAIGN_SERVICE, CampaignService.getInstance( ) );
122     }
123 
124     /**
125      * CopyPasted from SolrSearchApp to have the same freemarkers as if it was a search
126      */
127     private static List<HashMap<String, Object>> getGeolocModel( List<SolrSearchResult> listResultsGeoloc )
128     {
129         List<HashMap<String, Object>> points = new ArrayList<HashMap<String, Object>>( listResultsGeoloc.size( ) );
130         HashMap<String, String> iconKeysCache = new HashMap<String, String>( );
131 
132         for ( SolrSearchResult result : listResultsGeoloc )
133         {
134             Map<String, Object> dynamicFields = result.getDynamicFields( );
135 
136             for ( String key : dynamicFields.keySet( ) )
137             {
138                 if ( key.endsWith( SolrItem.DYNAMIC_GEOJSON_FIELD_SUFFIX ) )
139                 {
140                     HashMap<String, Object> h = new HashMap<String, Object>( );
141                     String strJson = (String) dynamicFields.get( key );
142                     GeolocItem geolocItem = null;
143 
144                     try
145                     {
146                         geolocItem = GeolocItem.fromJSON( strJson );
147                     }
148                     catch( IOException e )
149                     {
150                         AppLogService.error( "SolrSearchApp: error parsing geoloc JSON: " + strJson + ", exception " + e );
151                     }
152 
153                     if ( geolocItem != null )
154                     {
155                         String strType = result.getId( ).substring( result.getId( ).lastIndexOf( "_" ) + 1 );
156                         String strIcon;
157 
158                         if ( iconKeysCache.containsKey( geolocItem.getIcon( ) ) )
159                         {
160                             strIcon = iconKeysCache.get( geolocItem.getIcon( ) );
161                         }
162                         else
163                         {
164                             strIcon = IconService.getIcon( strType, geolocItem.getIcon( ) );
165                             iconKeysCache.put( geolocItem.getIcon( ), strIcon );
166                         }
167 
168                         geolocItem.setIcon( strIcon );
169                         h.put( SOLRSEARCHAPP_MARK_POINTS_GEOJSON, geolocItem.toJSON( ) );
170                         h.put( SOLRSEARCHAPP_MARK_POINTS_ID,
171                                 result.getId( ).substring( result.getId( ).indexOf( "_" ) + 1, result.getId( ).lastIndexOf( "_" ) ) );
172                         h.put( SOLRSEARCHAPP_MARK_POINTS_FIELDCODE, key.substring( 0, key.lastIndexOf( "_" ) ) );
173                         if ( strType.equals( "doc" ) )
174                         {
175 
176                             h.put( SOLRSEARCHAPP_MARK_POINTS_TYPE, "gagnant" );
177 
178                         }
179                         points.add( h );
180                     }
181                 }
182             }
183         }
184         return points;
185     }
186 }