View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.ctv.service;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Map;
40  
41  import javax.servlet.http.HttpServletRequest;
42  
43  import com.google.gson.Gson;
44  import com.google.gson.GsonBuilder;
45  
46  import fr.paris.lutece.plugins.ctv.bo.Emprise;
47  import fr.paris.lutece.plugins.ctv.bo.Emprise.TypeEmprise;
48  import fr.paris.lutece.plugins.ctv.bo.TypeElementGeoCtv;
49  import fr.paris.lutece.plugins.ctv.business.GeometryTypeAdapter;
50  import fr.paris.lutece.plugins.ctv.business.IEmpriseService;
51  import fr.paris.lutece.plugins.ctv.geotools.Feature;
52  import fr.paris.lutece.plugins.ctv.geotools.Geometry;
53  import fr.paris.lutece.plugins.ctv.geotools.Properties;
54  import fr.paris.lutece.plugins.ctv.web.bean.BeanGisEntry;
55  import fr.paris.lutece.plugins.gismap.business.ViewHome;
56  import fr.paris.lutece.plugins.sitelabels.service.LabelService;
57  import fr.paris.lutece.plugins.unittree.service.unit.IUnitService;
58  import fr.paris.lutece.portal.service.spring.SpringContextService;
59  import fr.paris.lutece.portal.service.template.AppTemplateService;
60  import fr.paris.lutece.util.html.HtmlTemplate;
61  
62  /**
63   *
64   * GismapService
65   *
66   */
67  public class CtvMapService
68  {
69  
70      private static final String SITELABEL_INSTALLATION_TYPE = "sitelabels.site_property.installation.type.";
71      private static final String SITELABEL_INSTALLATION_COLOR = "sitelabels.site_property.installation.color.";
72  
73      public static final String GEOM_GEO_JSON = "GeomGeoJson";
74  
75      private static final IEmpriseService empriseService = SpringContextService.getBean( "empriseService" );
76  
77      private static final IFormulaire formulaireService = FormulaireDemandeOccupationServiceImpl.getInstance( );
78  
79      private IEmpriseControllerService empriseControllerService = EmpriseControllerService.getInstance( );
80  
81      private IUnitService unitService = SpringContextService.getBean( IUnitService.BEAN_UNIT_SERVICE );
82  
83      private static final String MAP_CTV_TEMPLATE = "/site/plugins/ctv/ctv_map.html";
84  
85      // Markers
86      private static final String PARAMETER_MAP_PARAMETER = "map_parameter";
87      private static final String PARAMETER_ADD = "add_parameter";
88      private static final String FIELD_PARAM = "fieldParam";
89      private static final String GIS_ENTRY = "entry";
90      private static final String FIELD_VALUES = "fieldValues";
91      private static final String PARAMETER_OTHER_EMPRISES = "other_emprises";
92      private static final String PARAMETER_SECTEURS = "secteurs";
93      private static final String PARAMETER_EDIT_CARTO = "isCartoModifiable";
94      /**
95       * Id de la map view dans gismap.properties (gismap.view.X.parameter.MapClass='map' ou X est l'id de la map view)
96       */
97  
98      public static final int MAP_VIEW_EDITION_ID = 1;
99      public static final int MAP_VIEW_RECHERCHE_ID = 2;
100 
101     // Templates
102     private static CtvMapServicece/CtvMapService.html#CtvMapService">CtvMapService _singleton = new CtvMapService( );
103 
104     /**
105      * Returns the instance of the singleton
106      *
107      * @return The instance of the singleton
108      */
109     public static CtvMapService getInstance( )
110     {
111         return _singleton;
112     }
113 
114     /**
115      * Gets the XPage view based on the given GIS code.
116      *
117      * @param strGisCode
118      * @param parameters
119      * @param request
120      * @return The HTML page
121      */
122     public String getMapTemplate( HttpServletRequest request, int gismapViewId )
123     {
124         Map<String, Object> model = new HashMap<String, Object>( );
125 
126         fr.paris.lutece.plugins.gismap.business.View view = ViewHome.findByPrimaryKey( gismapViewId );
127 
128         Boolean isCartoModifiable = false;
129         if (( request.getParameter( "edit" )!= null ) && "true".equals( request.getParameter( "edit" ))) {
130             isCartoModifiable = true;
131         }
132 
133         model.put( PARAMETER_MAP_PARAMETER, view.getMapParameter( ) );
134 
135         model.put( PARAMETER_ADD, view.getAddressParam( ) );
136 
137         model.put( GIS_ENTRY, new BeanGisEntry( gismapViewId ) );
138 
139         model.put( FIELD_PARAM, getFieldsNames( ) );
140 
141         model.put( FIELD_VALUES, getFieldsValues( request ) );
142 
143         model.put( PARAMETER_EDIT_CARTO, isCartoModifiable );
144 
145         if ( request.getParameter( PARAMETER_OTHER_EMPRISES ) != null )
146         {
147             int empriseIdToExclude = empriseControllerService.getCurrentEmpriseId( request );
148             int idDemande = formulaireService.getNumDemande( request );
149             Map<String, String> otherEmprises = empriseService.getOtherEmprisesGeoJson( idDemande, empriseIdToExclude );
150             model.put( "otherEmprises", otherEmprises );
151 
152             // Get other emprises installations
153             List<Emprise> emprises = empriseService.findEmprisesByDemandeId( idDemande );
154             List<String> otherInstallations = new ArrayList<>( );
155             for ( Emprise emprise : emprises )
156             {
157                 if ( empriseIdToExclude != emprise.getEmpriseId( ) )
158                 {
159                     List<String> empriseInstallation = getOtherFieldsValues( emprise.getEmpriseId( ) );
160                     if ( empriseInstallation != null )
161                     {
162                         otherInstallations.addAll( empriseInstallation );
163                     }
164                 }
165             }
166             model.put( "otherInstallations", otherInstallations );
167         }
168 
169         if ( request.getParameter( PARAMETER_SECTEURS ) != null )
170         {
171             List<String> secteurs = unitService.findAllSectorsGeo( );
172             model.put( "secteurs", secteurs );
173         }
174 
175         String empriseIdStr = request.getParameter( "emprise_id" );
176         if ( ( request.getParameter( "edit" ) != null ) && ( empriseIdStr != null ) )
177         {
178             int empriseId = Integer.parseInt( empriseIdStr );
179             Emprise emprise = empriseService.findEmpriseById( empriseId );
180             if ( TypeEmprise.BARRIERE.equals( emprise.getEmpriseType( ) ) )
181             {
182                 model.put( "installations", initTypeElementsGeo( ) );
183             }
184             else
185             {
186                 String colorCode = getColorCode( TypeElementGeoCtv.ECHAFAUDAGE );
187                 model.put( "echafaudageColor", colorCode );
188             }
189         }
190         else
191         {
192             if ( gismapViewId == MAP_VIEW_EDITION_ID )
193             {
194                 model.put( "installations", initTypeElementsGeo( ) );
195             }
196         }
197 
198         HtmlTemplate templateList = AppTemplateService.getTemplate( MAP_CTV_TEMPLATE, request.getLocale( ), model );
199 
200         return templateList.getHtml( );
201     }
202 
203     private TypeElementGeoCtv [ ] initTypeElementsGeo( )
204     {
205         TypeElementGeoCtv [ ] types = TypeElementGeoCtv.values( );
206         for ( TypeElementGeoCtv type : types )
207         {
208             String colorCode = getColorCode( type );
209             if ( ( colorCode != null ) && !"".equals( colorCode ) )
210             {
211                 type.setCodeColor( colorCode );
212             }
213         }
214         return types;
215     }
216 
217     private String getColorCode( TypeElementGeoCtv type )
218     {
219         String colorCode = LabelService.findByPrimaryKey( SITELABEL_INSTALLATION_COLOR + type.name( ).toLowerCase( ).replaceAll( "_", "\\." ) ).getValue( );
220         return colorCode;
221     }
222 
223     private Map<String, String> getFieldsNames( )
224     {
225         Map<String, String> fieldParam = new HashMap<String, String>( );
226         fieldParam.put( "TypeEdit", "Polygon" );
227         fieldParam.put( GEOM_GEO_JSON, GEOM_GEO_JSON );
228         fieldParam.put( "GeomCentroidXStockage", "modifyProjectX" );
229         fieldParam.put( "GeomCentroidYStockage", "modifyProjectY" );
230         fieldParam.put( "GeomCentroidXGeocodage", "GeomCentroidXGeocodage" );
231         fieldParam.put( "GeomCentroidYGeocodage", "GeomCentroidYGeocodage" );
232         fieldParam.put( "GeomState", "GeomState" );
233         fieldParam.put( "ExtentCurrent", "ExtentCurrent" );
234         fieldParam.put( "VisibleLayer", "VisibleLayer" );
235         return fieldParam;
236     }
237 
238     private List<String> getFieldsValues( HttpServletRequest request )
239     {
240         String empriseIdStr = request.getParameter( "emprise_id" );
241         if ( ( request.getParameter( "edit" ) != null ) && ( empriseIdStr != null ) )
242         {
243             int empriseId = Integer.parseInt( empriseIdStr );
244 
245             Emprise emprise = empriseService.findEmpriseById( empriseId );
246             List<Feature> features = empriseService.getGeolocalisedElements( empriseId );
247             for ( Feature feature : features )
248             {
249                 Properties prop = feature.getProperties( );
250                 if ( prop != null )
251                 {
252                     String colorCode = getColorCode( prop.getType( ) );
253                     if ( TypeEmprise.ECHAFAUDAGE.equals( emprise.getEmpriseType( ) ) && TypeElementGeoCtv.EMPRISE.equals( prop.getType( ) ) )
254                     {
255                         colorCode = getColorCode( TypeElementGeoCtv.ECHAFAUDAGE );
256                     }
257                     if ( colorCode != null )
258                     {
259                         prop.setColor( colorCode );
260                     }
261                 }
262             }
263             return addLabelsAndConvertToGeoJson( features );
264         }
265         return null;
266     }
267 
268     /**
269      * Get GeoJson of other features attached to the non consulted emprise
270      *
271      * @param empriseId
272      * @param editStatut
273      * @return
274      */
275     private List<String> getOtherFieldsValues( Integer empriseId )
276     {
277         if ( ( empriseId != null ) )
278         {
279             List<Feature> features = empriseService.getGeolocalisedElements( empriseId );
280             List<Feature> featuresToRemove = new ArrayList<>( );
281             for ( Feature feature : features )
282             {
283                 Properties prop = feature.getProperties( );
284                 if ( prop != null )
285                 {
286                     prop.setColor( "#808080" );
287 
288                     // Put emprises away from the list
289                     if ( TypeElementGeoCtv.EMPRISE.equals( prop.getType( ) ) )
290                     {
291                         featuresToRemove.add( feature );
292                     }
293                 }
294             }
295 
296             // Remove emprises from features
297             for ( Feature featureToRemove : featuresToRemove )
298             {
299                 features.remove( featureToRemove );
300             }
301 
302             return addLabelsAndConvertToGeoJson( features );
303         }
304         return null;
305     }
306 
307     private List<String> addLabelsAndConvertToGeoJson( List<Feature> features )
308     {
309         List<String> geoJsonList = new ArrayList<>( );
310         GsonBuilder gsonBuilder = new GsonBuilder( );
311         gsonBuilder.registerTypeAdapter( Geometry.class, new GeometryTypeAdapter( ) );
312         Gson gson = gsonBuilder.create( );
313         for ( Feature feature : features )
314         {
315             Properties properties = feature.getProperties( );
316             String code = properties.getCode( );
317             properties.setLabel( LabelService.findByPrimaryKey( SITELABEL_INSTALLATION_TYPE + code.toLowerCase( ) ).getValue( ) );
318             String geoJson = gson.toJson( feature ).replace( '"', '\'' );
319             geoJsonList.add( geoJson );
320         }
321 
322         return geoJsonList;
323     }
324 
325 }