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 static fr.paris.lutece.plugins.ctv.constants.CtvConstants.EMPRISE_ID;
37  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.EMPRISE_ID_PARAM_REQUEST;
38  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_EMPRISE;
39  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.FORMES_EMRPISE;
40  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.PARAMETER_ADD;
41  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.TYPES_CONFIG;
42  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.TYPES_ECHAFAUDAGE;
43  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.TYPES_LOCALISATION;
44  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.TYPES_MATERIEL;
45  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.TYPES_PRECISIONS_LOCALISATION;
46  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.TYPES_ZONE_STATIONNEMENT;
47  
48  import java.util.Date;
49  import java.util.List;
50  import java.util.Map;
51  import java.util.Set;
52  
53  import javax.servlet.http.HttpServletRequest;
54  
55  import org.apache.commons.beanutils.ConvertUtils;
56  import org.apache.commons.beanutils.Converter;
57  import org.apache.commons.lang3.StringUtils;
58  
59  import com.google.gson.Gson;
60  import com.google.gson.GsonBuilder;
61  
62  import fr.paris.lutece.plugins.ctv.bo.ChantierOccupation;
63  import fr.paris.lutece.plugins.ctv.bo.DetailsEmprise;
64  import fr.paris.lutece.plugins.ctv.bo.DetailsEmprise.TypeLocalisation;
65  import fr.paris.lutece.plugins.ctv.bo.Emprise;
66  import fr.paris.lutece.plugins.ctv.bo.Installation;
67  import fr.paris.lutece.plugins.ctv.bo.ItemReferentiel.TypeReferentiel;
68  import fr.paris.lutece.plugins.ctv.bo.TypeElementGeoCtv;
69  import fr.paris.lutece.plugins.ctv.business.GeometryTypeAdapter;
70  import fr.paris.lutece.plugins.ctv.business.IChantierService;
71  import fr.paris.lutece.plugins.ctv.business.IEmpriseService;
72  import fr.paris.lutece.plugins.ctv.business.IReferentielsService;
73  import fr.paris.lutece.plugins.ctv.business.IStockageImageService;
74  import fr.paris.lutece.plugins.ctv.constant.OuiNonEnum;
75  import fr.paris.lutece.plugins.ctv.constant.StatutEmprise;
76  import fr.paris.lutece.plugins.ctv.converter.CustomDateConverter;
77  import fr.paris.lutece.plugins.ctv.converter.DoubleConverter;
78  import fr.paris.lutece.plugins.ctv.converter.EnumConverter;
79  import fr.paris.lutece.plugins.ctv.geotools.CtvCartoFeatures;
80  import fr.paris.lutece.plugins.ctv.geotools.Feature;
81  import fr.paris.lutece.plugins.ctv.geotools.Geometry;
82  import fr.paris.lutece.plugins.ctv.geotools.Properties;
83  import fr.paris.lutece.plugins.ctv.handler.CtvUploadEmpriseDocumentHandler;
84  import fr.paris.lutece.plugins.ctv.util.EmpriseMappingUtils;
85  import fr.paris.lutece.plugins.gismap.business.AddressParamHome;
86  import fr.paris.lutece.portal.service.datastore.DatastoreService;
87  import fr.paris.lutece.portal.service.spring.SpringContextService;
88  import fr.paris.lutece.portal.service.util.AppPropertiesService;
89  import fr.paris.lutece.util.bean.BeanUtil;
90  
91  public abstract class AbstractEmpriseControllerService implements IEmpriseControllerService
92  {
93  
94      private static String PATH_FOLDER_IMAGE_EMPRISE = AppPropertiesService.getProperty( "ctv.directory.upload" );
95  
96      protected static final IEmpriseService empriseService = SpringContextService.getBean( "empriseService" );
97      private static final IReferentielsService referentielsService = SpringContextService.getBean( "referentielsService" );
98      private static final IFormulaire formulaireService = FormulaireDemandeIntentionServiceImpl.getInstance( );
99      private static final IChantierService chantierService = SpringContextService.getBean( "chantierService" );
100     private CtvUploadEmpriseDocumentHandler ctvUploadEmpriseDocumentHandler = SpringContextService
101             .getBean( CtvUploadEmpriseDocumentHandler.CTV_EMPRISE_UPLOAD_HANDLER );
102     private static final IStockageImageService stockageService = SpringContextService.getBean( "stockageImageService" );
103 
104     @Override
105     public void updateEmpriseFromRequest( HttpServletRequest request, boolean withGeometry )
106     {
107         registerConverters( );
108         Emprise emprise = new Emprise( );
109         BeanUtil.populate( emprise, request );
110         int empriseId = getCurrentEmpriseId( request );
111         if ( withGeometry )
112         {
113             populateGeometry( request, emprise );
114         }
115         empriseService.updateEmprise( empriseId, emprise, withGeometry );
116 
117         // Fait 71985 enregistrer l'apercu sur le serveur
118         // Attention EmpriseDAO m├ęthode mapEmpriseCommon enregistre null en base pour map_preview_data_url
119         Emprise empriseFromDB = empriseService.findEmpriseById( empriseId );
120         String pathFile = PATH_FOLDER_IMAGE_EMPRISE;
121         stockageService.saveImageOnServer( emprise.getMapPreviewDataUrl( ), empriseFromDB.getEmpriseDateCreation( ), pathFile, empriseId );
122 
123         int paramDelaiDateGel = Integer.parseInt( DatastoreService.getDataValue( "parametre.numeric.intervention.duree.periode.gel", "0" ) );
124         populateDetailsEmprise( request, paramDelaiDateGel );
125     }
126 
127     private void populateDetailsEmprise( HttpServletRequest request, int paramDelaiDateGel )
128     {
129         DetailsEmprise details = new DetailsEmprise( );
130         EmpriseMappingUtils.map( request, details );
131         if ( details.getLocalisationsEmprise( ) != null && details.getLocalisationsEmprise( ).containsKey( TypeLocalisation.EMPRISE_PISTE_CYCLABLE ) )
132         {
133             details.getLocalisationsEmprise( ).put( TypeLocalisation.EMPRISE_PISTE_CYCLABLE, 1.0 );
134         }
135         int empriseId = getCurrentEmpriseId( request );
136         details.prepare( );
137         empriseService.createDetails( empriseId, details, paramDelaiDateGel );
138     }
139 
140     protected boolean populateGeometry( HttpServletRequest request, Emprise emprise )
141     {
142         String featuresGeoJSON = request.getParameter( "featuresGeoJSON" );
143         if ( StringUtils.isNoneBlank( featuresGeoJSON ) )
144         {
145 
146             GsonBuilder builder = new GsonBuilder( );
147             builder.registerTypeAdapter( Geometry.class, new GeometryTypeAdapter( ) );
148             Gson gson = builder.create( );
149 
150             CtvCartoFeatures features = gson.fromJson( featuresGeoJSON.replaceAll( "'", "\"" ), CtvCartoFeatures.class );
151             for ( Feature feature : features.getFeatures( ) )
152             {
153                 Geometry geometry = feature.getGeometry( );
154                 Properties properties = feature.getProperties( );
155                 TypeElementGeoCtv type = properties.getType( );
156                 String geoJson = gson.toJson( geometry );
157                 if ( TypeElementGeoCtv.EMPRISE.equals( type ) )
158                 {
159                     emprise.setGeomGeoJson( geoJson );
160                 }
161                 else
162                 {
163                     Installation installation = new Installation( );
164                     installation.setInstallationType( type );
165                     installation.setGeomGeoJson( geoJson );
166                     String longueur = properties.getLongueur( );
167                     if ( StringUtils.isNoneBlank( longueur ) )
168                     {
169                         installation.setInstallationLongueur( Double.parseDouble( longueur.replaceAll( ",", "." ) ) );
170                     }
171                     String largeur = properties.getLargeur( );
172                     if ( StringUtils.isNoneBlank( largeur ) )
173                     {
174                         installation.setInstallationLargeur( Double.parseDouble( largeur.replaceAll( ",", "." ) ) );
175                     }
176                     installation.setInstallationCommentaire( properties.getCommentaire( ) );
177                     installation.setInstallationNumero( Integer.parseInt( properties.getNumero( ) ) );
178                     emprise.getInstallationsGeoJson( ).add( installation );
179                 }
180             }
181             return true;
182         }
183         return false;
184     }
185 
186     @Override
187     public int getCurrentEmpriseId( HttpServletRequest request )
188     {
189         int empriseId = -1;
190         if ( null != request.getParameter( EMPRISE_ID_PARAM_REQUEST ) )
191         {
192             empriseId = Integer.parseInt( request.getParameter( EMPRISE_ID_PARAM_REQUEST ) );
193             request.getSession( ).setAttribute( EMPRISE_ID, empriseId );
194         }
195         else
196         {
197             if ( request.getSession( ).getAttribute( EMPRISE_ID ) != null )
198             {
199                 empriseId = (int) request.getSession( ).getAttribute( EMPRISE_ID );
200             }
201         }
202         return empriseId;
203     }
204 
205     protected Converter escapeConversion( )
206     {
207         return new Converter( )
208         {
209             @Override
210             public <T> T convert( Class<T> arg0, Object arg1 )
211             {
212                 return null;
213             }
214         };
215     }
216 
217     @Override
218     public void initModel( Map<String, Object> model, HttpServletRequest request )
219     {
220         model.put( TYPES_LOCALISATION, DetailsEmprise.TypeLocalisation.values( ) );
221         model.put( TYPES_PRECISIONS_LOCALISATION, referentielsService.getItemReferentielList( TypeReferentiel.PRECISION_LOCALISATION ) );
222         model.put( TYPES_ZONE_STATIONNEMENT, DetailsEmprise.TypeZoneStationnement.values( ) );
223         model.put( TYPES_CONFIG, DetailsEmprise.TypeConfigurationSpecifique.values( ) );
224         model.put( TYPES_MATERIEL, DetailsEmprise.TypeMateriel.values( ) );
225         model.put( TYPES_ECHAFAUDAGE, DetailsEmprise.TypeEchafaudage.values( ) );
226         model.put( FORMES_EMRPISE, referentielsService.getItemReferentielList( TypeReferentiel.FORME_EMPRISE ) );
227         model.put( PARAMETER_ADD, AddressParamHome.getAddressParameters( ) );
228         model.put( CtvUploadEmpriseDocumentHandler.CTV_EMPRISE_UPLOAD_HANDLER, ctvUploadEmpriseDocumentHandler );
229         int numDemande = formulaireService.getNumDemande( request );
230         ChantierOccupation chantier = chantierService.getChantierOccupationByNumDemande( numDemande );
231         Emprise emprise = new Emprise( );
232         if ( chantier != null )
233         {
234             emprise.setEmpriseAdresseDebut( chantier.getChantierAdresse( ) );
235         }
236 
237         model.put( "emprise", emprise );
238     }
239 
240     @Override
241     public void registerConverters( )
242     {
243         ConvertUtils.register( new CustomDateConverter( ), Date.class );
244         ConvertUtils.register( new EnumConverter( ), Emprise.TypeEmprise.class );
245         ConvertUtils.register( new EnumConverter( ), StatutEmprise.class );
246         ConvertUtils.register( new EnumConverter( ), OuiNonEnum.class );
247         ConvertUtils.register( escapeConversion( ), Map.class );
248         ConvertUtils.register( escapeConversion( ), Set.class );
249         ConvertUtils.register( new DoubleConverter( ), Double.class );
250     }
251 
252     @Override
253     public void alimenterModel( HttpServletRequest request, Map<String, Object> model )
254     {
255         getFormulaireService( ).alimentationModel( request, model );
256         getFormulaireService( ).sauvegardeEtape( request, model, ETAPE_EMPRISE );
257     }
258 
259     @Override
260     public void fillModelForDetailEmprise( HttpServletRequest request, Map<String, Object> model )
261     {
262         initModel( model, request );
263         int empriseId = getCurrentEmpriseId( request );
264         List<Installation> installations = empriseService.getInfosInstallations( empriseId );
265         Emprise emprise = empriseService.findEmpriseById( empriseId );
266 
267         // chercher l'apercu de l'image encode depuis le serveur
268         if ( null != emprise )
269         {
270             String pathFolder = PATH_FOLDER_IMAGE_EMPRISE;
271             String imageEncoded = stockageService.getImageFromServer( pathFolder, emprise.getEmpriseDateCreation( ), empriseId );
272             emprise.setMapPreviewDataUrl( imageEncoded );
273         }
274 
275         DetailsEmprise details = empriseService.findDetailsEmpriseById( empriseId );
276         model.put( "emprise", emprise );
277         model.put( "details", details );
278         model.put( "installations", installations );
279         alimenterModel( request, model );
280 
281     }
282 
283     protected void initSpecificModel( Map<String, Object> model )
284     {
285         TypeLocalisation [ ] tabLoc = {
286                 DetailsEmprise.TypeLocalisation.EMPRISE_CHAUSSEE, DetailsEmprise.TypeLocalisation.EMPRISE_TROTTOIR
287         };
288         model.put( "localisations", tabLoc );
289         model.put( "ouinonenum", OuiNonEnum.values( ) );
290     }
291 
292     public abstract IFormulaire getFormulaireService( );
293 
294 }