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.bo.controller.demandes;
35  
36  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_CHANTIER;
37  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_GESTION_CONFLITS;
38  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.NATURES_CHANTIER;
39  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.PARAMETER_ADD;
40  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.SESSION_NUM_DEMANDE_ID;
41  
42  import java.util.Calendar;
43  import java.util.Date;
44  import java.util.List;
45  import java.util.Map;
46  
47  import javax.servlet.http.HttpServletRequest;
48  
49  import fr.paris.lutece.plugins.ctv.bo.AbstractChantier;
50  import fr.paris.lutece.plugins.ctv.bo.Conflit;
51  import fr.paris.lutece.plugins.ctv.bo.EmpriseConflit;
52  import fr.paris.lutece.plugins.ctv.bo.ItemReferentiel.TypeReferentiel;
53  import fr.paris.lutece.plugins.ctv.business.IChantierService;
54  import fr.paris.lutece.plugins.ctv.business.IConflitService;
55  import fr.paris.lutece.plugins.ctv.business.IEmpriseService;
56  import fr.paris.lutece.plugins.ctv.business.IReferentielsService;
57  import fr.paris.lutece.plugins.ctv.form.GestionConflitsFormBean;
58  import fr.paris.lutece.plugins.ctv.service.IFormulaire;
59  import fr.paris.lutece.plugins.ctv.service.IFormulaireDemandeOccupationBoService;
60  import fr.paris.lutece.plugins.gismap.business.AddressParamHome;
61  import fr.paris.lutece.portal.service.spring.SpringContextService;
62  import fr.paris.lutece.portal.service.util.AppPropertiesService;
63  
64  public abstract class AbstractGestionConflitsJspBean extends AbstractCtvJspBean
65  {
66  
67      private static final long serialVersionUID = -374300118257976280L;
68  
69      // Title
70      protected static final String GESTION_CONFLITS_TITLE = "ctv.etape.gestion.conflits.title";
71  
72      // Views
73      protected static final String VIEW_GESTION_CONFLITS = "gestion_conflits";
74  
75      // Actions
76      protected static final String ACTION_SEARCH = "search";
77      protected static final String ACTION_DELETE_CONFLIT = "delete_conflit";
78      protected static final String ACTION_ADD_CONFLIT = "add_conflit";
79  
80      // Marks
81      private static final String MARK_CONFLITS_FORM = "conflitsForm";
82      private static final String MARK_EMPRISES_CONFLIT = "emprises_conflit";
83      private static final String MARK_HAS_SEARCHED = "hasSearched";
84  
85      // Service
86      private IEmpriseService empriseService = SpringContextService.getBean( "empriseService" );
87      private static final IConflitService conflitService = SpringContextService.getBean( "conflitService" );
88      protected IChantierService chantierService = SpringContextService.getBean( "chantierService" );
89  
90      // Properties
91      private static final String PROP_DEBUT_RECHERCHE = "gestion.conflits.recherche.debut";
92      private static final String PROP_FIN_RECHERCHE = "gestion.conflits.recherche.fin";
93      private static final String PROP_RAYON_RECHERCHE = "gestion.conflits.rayon";
94  
95      protected abstract IFormulaire getFormulaireService( );
96  
97      private static IReferentielsService referentielsService = SpringContextService.getBean( "referentielsService" );
98  
99      protected abstract String getAdresseChantier( int numDemande );
100 
101     private void fillConflits( HttpServletRequest request, GestionConflitsFormBean formBean, Map<String, Object> model )
102     {
103         int numDemande = getFormulaireService( ).getNumDemande( request );
104         String adresseChantier = getAdresseChantier( numDemande );
105 
106         model.put( "chantieradresse", adresseChantier );
107         if ( null != formBean.getDateDebutRecherche( ) && null != formBean.getDateFinRecherche( ) )
108         {
109             // Recherche des demandes en conflit
110             List<EmpriseConflit> emprisesConflit = empriseService.getEmprisesConflitByNumDemande( formBean.getDateDebutRecherche( ),
111                     formBean.getDateFinRecherche( ), formBean.getRayon( ), numDemande );
112             model.put( MARK_HAS_SEARCHED, true );
113             model.put( MARK_EMPRISES_CONFLIT, emprisesConflit );
114         }
115 
116     }
117 
118     private void initFormBeanFromModel( GestionConflitsFormBean formBean, Map<String, Object> model )
119     {
120         int debutNJours = AppPropertiesService.getPropertyInt( PROP_DEBUT_RECHERCHE, 0 );
121         int finNJours = AppPropertiesService.getPropertyInt( PROP_FIN_RECHERCHE, 0 );
122         double rayon = Double.parseDouble( AppPropertiesService.getProperty( PROP_RAYON_RECHERCHE ) );
123         int numDemande = (int) model.get( SESSION_NUM_DEMANDE_ID );
124 
125         Conflit conflit = conflitService.findConflitByNumDemande( numDemande );
126 
127         if ( null != conflit )
128         {
129             formBean.setHasConflits( true );
130         }
131 
132         AbstractChantier chantier = (AbstractChantier) model.get( ETAPE_CHANTIER );
133         if ( null == chantier )
134         {
135             return;
136         }
137 
138         Date dateDebut = chantier.getChantierStartDate( );
139         Date dateFin = chantier.getChantierEndDate( );
140         Calendar cal = Calendar.getInstance( );
141 
142         if ( null != dateDebut )
143         {
144             cal.setTime( dateDebut );
145             cal.add( Calendar.DAY_OF_MONTH, -debutNJours );
146             dateDebut = new Date( cal.getTimeInMillis( ) );
147         }
148 
149         if ( null != dateFin )
150         {
151             cal.setTime( dateFin );
152             cal.add( Calendar.DAY_OF_MONTH, finNJours );
153             dateFin = new Date( cal.getTimeInMillis( ) );
154         }
155 
156         formBean.setDateDebutRecherche( dateDebut );
157         formBean.setDateFinRecherche( dateFin );
158         formBean.setRayon( rayon );
159     }
160 
161     private void fillModel( HttpServletRequest request, Map<String, Object> model )
162     {
163         getFormulaireService( ).alimentationModel( request, model );
164         if ( getFormulaireServiceBo( ) != null )
165         {
166             getFormulaireServiceBo( ).alimentationModelBO( request, model );
167         }
168         getFormulaireService( ).sauvegardeEtape( request, model, ETAPE_GESTION_CONFLITS );
169         int numDemande = getFormulaireService( ).getNumDemande( request );
170         fillUserPossibleActions( request, model, numDemande );
171         model.put( PARAMETER_ADD, AddressParamHome.getAddressParameters( ) );
172         model.put( MARK_PATH_TABLEAU_DE_BORD, PATH_TABLEAU_DE_BORD );
173         model.put( MARK_PAGE_TITLE, TABLEAU_BORD_PAGE_TITLE );
174         model.put( "controlForm", true );
175         model.put( NATURES_CHANTIER, referentielsService.getItemReferentielList( TypeReferentiel.NATURE_TRAVAUX ) );
176     }
177 
178     protected Map<String, Object> getConflitsModel( HttpServletRequest request )
179     {
180         Map<String, Object> model = getModel( );
181         fillModel( request, model );
182         GestionConflitsFormBeanstionConflitsFormBean.html#GestionConflitsFormBean">GestionConflitsFormBean formBean = new GestionConflitsFormBean( );
183         initFormBeanFromModel( formBean, model );
184         model.put( MARK_CONFLITS_FORM, formBean );
185         return model;
186     }
187 
188     protected Map<String, Object> getSearchActionModel( HttpServletRequest request )
189     {
190         GestionConflitsFormBeanstionConflitsFormBean.html#GestionConflitsFormBean">GestionConflitsFormBean formBean = new GestionConflitsFormBean( );
191         populate( formBean, request );
192 
193         Map<String, Object> model = getModel( );
194 
195         // Récupération des critères de recherche
196         fillConflits( request, formBean, model );
197 
198         // Rechargement de la page avec les paramètres récupérés
199         fillModel( request, model );
200         model.put( MARK_CONFLITS_FORM, formBean );
201         return model;
202     }
203 
204     protected String deleteConflit( HttpServletRequest request )
205     {
206         int numDemande = getFormulaireService( ).getNumDemande( request );
207         fillLastUser( numDemande, request );
208         return String.valueOf( conflitService.delete( numDemande ) );
209     }
210 
211     protected String addConflit( HttpServletRequest request )
212     {
213         int numDemande = getFormulaireService( ).getNumDemande( request );
214         fillLastUser( numDemande, request );
215         return String.valueOf( conflitService.update( new Conflit( ), numDemande ) );
216     }
217 
218     protected abstract IFormulaireDemandeOccupationBoService getFormulaireServiceBo( );
219 }