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.NEW_EMPRISE;
38  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.PARAMETER_ADD;
39  
40  import java.util.Calendar;
41  import java.util.Date;
42  import java.util.Map;
43  
44  import javax.servlet.http.HttpServletRequest;
45  
46  import fr.paris.lutece.plugins.ctv.bo.Emprise;
47  import fr.paris.lutece.plugins.ctv.business.IReferentielsService;
48  import fr.paris.lutece.plugins.ctv.business.IStockageImageService;
49  import fr.paris.lutece.plugins.ctv.constant.StatutEmprise;
50  import fr.paris.lutece.plugins.gismap.business.AddressParam;
51  import fr.paris.lutece.plugins.gismap.business.AddressParamHome;
52  import fr.paris.lutece.portal.service.datastore.DatastoreService;
53  import fr.paris.lutece.portal.service.spring.SpringContextService;
54  import fr.paris.lutece.portal.service.util.AppPropertiesService;
55  import fr.paris.lutece.util.bean.BeanUtil;
56  
57  public class EmpriseControllerService extends AbstractEmpriseControllerService
58  {
59  
60      private static String PATH_FOLDER_IMAGE_EMPRISE = AppPropertiesService.getProperty( "ctv.directory.upload" );
61      private static final IFormulaire formulaireService = FormulaireDemandeOccupationServiceImpl.getInstance( );
62      private static final IEmpriseControllerService empriseCtrlService = new EmpriseControllerService( );
63      private static IReferentielsService referentielsService = SpringContextService.getBean( "referentielsService" );
64      private static final IStockageImageService stockageService = SpringContextService.getBean( "stockageImageService" );
65  
66      public static IEmpriseControllerService getInstance( )
67      {
68          return empriseCtrlService;
69      }
70  
71      @Override
72      public IFormulaire getFormulaireService( )
73      {
74          return formulaireService;
75      }
76  
77      @Override
78      public void populateEmprise( HttpServletRequest request )
79      {
80          registerConverters( );
81          Emprise emprise = new Emprise( );
82          BeanUtil.populate( emprise, request );
83          populateGeometry( request, emprise );
84          addInfosToEmprise( emprise );
85          int numDemande = formulaireService.getNumDemande( request );
86          int empriseId = empriseService.createEmprise( numDemande, emprise );
87  
88          // Fait 71985 enregistrer l'apercu sur le serveur
89          // Attention EmpriseDAO m├ęthode mapEmpriseCommon enregistre null en base pour map_preview_data_url
90          String pathFile = PATH_FOLDER_IMAGE_EMPRISE;
91          stockageService.saveImageOnServer( emprise.getMapPreviewDataUrl( ), new Date( ), pathFile, empriseId );
92  
93          request.getSession( ).setAttribute( EMPRISE_ID, empriseId );
94          request.getSession( ).setAttribute( NEW_EMPRISE, true );
95      }
96  
97      private void addInfosToEmprise( Emprise emprise )
98      {
99          int nbJourTravauxReseau = Integer
100                 .parseInt( DatastoreService.getDataValue( "parametre.numeric.occupation.fin.travaux.reseaux.remise.circulation", "0" ) );
101         int nbJourTravauxVoirie = Integer
102                 .parseInt( DatastoreService.getDataValue( "parametre.numeric.occupation.fin.travaux.voirie.remise.circulation", "0" ) );
103 
104         Date dateOperation = substractDays( emprise.getEmpriseDateFin( ), nbJourTravauxReseau );
105         emprise.setEmpriseDateFinTravauxReseau( dateOperation );
106         Date dateOperation2 = substractDays( emprise.getEmpriseDateFin( ), nbJourTravauxVoirie );
107         emprise.setEmpriseDateFinTravauxVoirie( dateOperation2 );
108 
109         emprise.setEmpriseStatut( StatutEmprise.BROUILLON );
110     }
111 
112     private Date substractDays( Date initial, int number )
113     {
114         Calendar calendar = Calendar.getInstance( );
115         calendar.setTime( initial );
116         calendar.add( Calendar.DATE, number * ( -1 ) );
117         return calendar.getTime( );
118     }
119 
120     @Override
121     public void initModel( Map<String, Object> model, HttpServletRequest request )
122     {
123         super.initModel( model, request );
124         AddressParam addressParam = AddressParamHome.getAddressParameters( );
125         addressParam.setListType( "ADR" );
126         model.put( PARAMETER_ADD, addressParam );
127     }
128 
129 }