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.search.solr.business.SolrSearchEngine;
47 import fr.paris.lutece.plugins.search.solr.business.SolrSearchResult;
48 import fr.paris.lutece.plugins.search.solr.indexer.SolrItem;
49 import fr.paris.lutece.portal.business.resourceenhancer.IResourceDisplayManager;
50 import fr.paris.lutece.portal.service.util.AppLogService;
51 import fr.paris.lutece.portal.service.util.AppPropertiesService;
52
53
54
55
56 public class DocumentPageProjectOPService implements IResourceDisplayManager
57 {
58 private static final String SOLR_QUERY_ALL = "*:*";
59 private static final String SOLR_FQ_PROJECTS_OP_SUIVI_SYNT = "document_suivi_synt_text_text:";
60 private static final String SOLR_FQ_PROJECTS_OP_STATUT = "-statut_text:";
61
62 private static final String SOLRSEARCHAPP_PROPERTY_SOLR_RESPONSE_MAX = "solr.reponse.max";
63 private static final int SOLRSEARCHAPP_SOLR_RESPONSE_MAX = Integer
64 .parseInt( AppPropertiesService.getProperty( SOLRSEARCHAPP_PROPERTY_SOLR_RESPONSE_MAX, "50" ) );
65
66 private static final String MARK_STATUS = "status_document";
67 private static final String MARK_STATUS_VOTED = "SUIVI";
68
69 private static final String MARK_PROJECTS_OP_POINTS = "projects_op_points";
70
71 private static final String SOLRSEARCHAPP_MARK_POINTS_GEOJSON = "geojson";
72 private static final String SOLRSEARCHAPP_MARK_POINTS_ID = "id";
73 private static final String SOLRSEARCHAPP_MARK_POINTS_FIELDCODE = "code";
74 private static final String SOLRSEARCHAPP_MARK_POINTS_TYPE = "type";
75
76 @Override
77 public void getXmlAddOn( StringBuffer strXml, String strResourceType, int nResourceId )
78 {
79 return;
80 }
81
82 @Override
83 public void buildPageAddOn( Map<String, Object> model, String strResourceType, int nIdResource, String strPortletId, HttpServletRequest request )
84 {
85
86 List<HashMap<String, Object>> points = null;
87
88
89 Object isVotedProject = model.get( MARK_STATUS );
90 if ( MARK_STATUS_VOTED.equals( isVotedProject ) )
91 {
92 String [ ] strQuery = {
93 SOLR_FQ_PROJECTS_OP_SUIVI_SYNT + nIdResource, SOLR_FQ_PROJECTS_OP_STATUT + "Obsolète", SOLR_FQ_PROJECTS_OP_STATUT + "Abandonné",
94 SOLR_FQ_PROJECTS_OP_STATUT + "Suppression", SOLR_FQ_PROJECTS_OP_STATUT + "Reliquat"
95 };
96 SolrSearchEngine engine = SolrSearchEngine.getInstance( );
97 List<SolrSearchResult> results = engine.getGeolocSearchResults( SOLR_QUERY_ALL, strQuery, SOLRSEARCHAPP_SOLR_RESPONSE_MAX );
98 points = getGeolocModel( results );
99 }
100 else
101 {
102 points = new ArrayList<HashMap<String, Object>>( );
103 }
104
105
106 model.put( MARK_PROJECTS_OP_POINTS, points );
107 }
108
109
110
111
112 private static List<HashMap<String, Object>> getGeolocModel( List<SolrSearchResult> listResultsGeoloc )
113 {
114 List<HashMap<String, Object>> points = new ArrayList<HashMap<String, Object>>( listResultsGeoloc.size( ) );
115 HashMap<String, String> iconKeysCache = new HashMap<String, String>( );
116
117 for ( SolrSearchResult result : listResultsGeoloc )
118 {
119 Map<String, Object> dynamicFields = result.getDynamicFields( );
120
121 for ( String key : dynamicFields.keySet( ) )
122 {
123 if ( key.endsWith( SolrItem.DYNAMIC_GEOJSON_FIELD_SUFFIX ) )
124 {
125 HashMap<String, Object> h = new HashMap<String, Object>( );
126 String strJson = (String) dynamicFields.get( key );
127 GeolocItem geolocItem = null;
128
129 try
130 {
131 geolocItem = GeolocItem.fromJSON( strJson );
132 }
133 catch( IOException e )
134 {
135 AppLogService.error( "SolrSearchApp: error parsing geoloc JSON: " + strJson + ", exception " + e );
136 }
137
138 if ( geolocItem != null )
139 {
140 String strType = result.getId( ).substring( result.getId( ).lastIndexOf( "_" ) + 1 );
141 String strIcon;
142
143 if ( iconKeysCache.containsKey( geolocItem.getIcon( ) ) )
144 {
145 strIcon = iconKeysCache.get( geolocItem.getIcon( ) );
146 }
147 else
148 {
149 strIcon = IconService.getIcon( strType, geolocItem.getIcon( ) );
150 iconKeysCache.put( geolocItem.getIcon( ), strIcon );
151 }
152
153 geolocItem.setIcon( strIcon );
154 h.put( SOLRSEARCHAPP_MARK_POINTS_GEOJSON, geolocItem.toJSON( ) );
155 h.put( SOLRSEARCHAPP_MARK_POINTS_ID,
156 result.getId( ).substring( result.getId( ).indexOf( "_" ) + 1, result.getId( ).lastIndexOf( "_" ) ) );
157 h.put( SOLRSEARCHAPP_MARK_POINTS_FIELDCODE, key.substring( 0, key.lastIndexOf( "_" ) ) );
158 if ( strType.equals( "doc" ) )
159 {
160
161 h.put( SOLRSEARCHAPP_MARK_POINTS_TYPE, "gagnant" );
162
163 }
164 points.add( h );
165 }
166 }
167 }
168 }
169 return points;
170 }
171
172 }