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.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   * Manager for add on display TODO : move this class into a document specific class !
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          // Getting "PB Project OP" child documents from root "PB Project" document
86          List<HashMap<String, Object>> points = null;
87  
88          // Only for voted project !
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         // Putting them in freemarker model
106         model.put( MARK_PROJECTS_OP_POINTS, points );
107     }
108 
109     /**
110      * CopyPasted from ProjectOPSolrAddon to have the same freemarkers as if it was a search
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 }