1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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";
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
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
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 }