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.conference.coordination;
35  
36  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.NATURES_CHANTIER;
37  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.TEMPLATE_COORDINATION_CONFERENCE;
38  
39  import java.util.ArrayList;
40  import java.util.Arrays;
41  import java.util.HashMap;
42  import java.util.List;
43  import java.util.Map;
44  
45  import javax.servlet.http.HttpServletRequest;
46  
47  import org.apache.commons.lang.ArrayUtils;
48  
49  import com.google.gson.Gson;
50  import com.google.gson.GsonBuilder;
51  
52  import fr.paris.lutece.plugins.ctv.bo.Emprise;
53  import fr.paris.lutece.plugins.ctv.bo.ItemReferentiel.TypeReferentiel;
54  import fr.paris.lutece.plugins.ctv.bo.SearchBeanForEmprises;
55  import fr.paris.lutece.plugins.ctv.bo.controller.demandes.AbstractCtvJspBean;
56  import fr.paris.lutece.plugins.ctv.business.IConferenceCoordinationService;
57  import fr.paris.lutece.plugins.ctv.business.IReferentielsService;
58  import fr.paris.lutece.plugins.ctv.constant.Constants;
59  import fr.paris.lutece.plugins.ctv.constant.StatutDemande;
60  import fr.paris.lutece.plugins.ctv.constant.StatutEmprise;
61  import fr.paris.lutece.plugins.ctv.constant.TypeDemande;
62  import fr.paris.lutece.plugins.ctv.constant.TypeProfileCtv;
63  import fr.paris.lutece.plugins.ctv.constants.CtvConstants;
64  import fr.paris.lutece.plugins.ctv.util.UnitTreeUtils;
65  import fr.paris.lutece.plugins.ctv.view.object.SearchEmprisesViewObject;
66  import fr.paris.lutece.plugins.unittree.business.unit.Unit;
67  import fr.paris.lutece.plugins.unittree.business.unit.Unit.TypeUnit;
68  import fr.paris.lutece.plugins.unittree.service.unit.IUnitService;
69  import fr.paris.lutece.portal.business.user.AdminUser;
70  import fr.paris.lutece.portal.service.datastore.DatastoreService;
71  import fr.paris.lutece.portal.service.spring.SpringContextService;
72  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
73  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
74  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
75  
76  @Controller( controllerJsp = "conferenceCoordination.jsp", controllerPath = "jsp/admin/plugins/ctv/conferenceCoordination", right = "CONFERENCE_DE_COORDINATION" )
77  public class ConferenceCoordinationJspBean extends AbstractCtvJspBean
78  {
79      private static final long serialVersionUID = 1L;
80      private IUnitService unitService = SpringContextService.getBean( IUnitService.BEAN_UNIT_SERVICE );
81      private static final String MARK_LISTE_DEMANDES = "list_demandes";
82      private static IConferenceCoordinationService conferenceService = SpringContextService.getBean( "conferenceCoordinationService" );
83      private static IReferentielsService referentielsService = SpringContextService.getBean( "referentielsService" );
84      private static final String MARK_EMPRISES = "emprises";
85      private static final String MARK_DVDSTV_UNIT_ID = "dvdStvUnitId";
86      private static final String MARK_CONCESSIONNAIRE_UNIT_ID = "concessionUnitId";
87      private static final String MARK_CONCESSIONNAIRE_UNIT_LABEL = "concessionUnitLabel";
88      private static final String MARK_IS_CONCESSION_DVD_STV = "idConcessionDvdStv";
89  
90      @View( value = "conferenceCoordination", defaultView = true )
91      public String getConferenceCoordination( HttpServletRequest request )
92      {
93          Map<String, Object> model = initModel( request );
94          return getPage( "", TEMPLATE_COORDINATION_CONFERENCE, model );
95      }
96  
97      @Action( "validerConference" )
98      public String validerConference( HttpServletRequest request )
99      {
100         String emprisesJson = request.getParameter( MARK_EMPRISES );
101 
102         if ( emprisesJson != null )
103         {
104             GsonBuilder gsonBuilder = new GsonBuilder( );
105             gsonBuilder.setDateFormat( Constants.DATE_FORMAT );
106             Gson gson = gsonBuilder.create( );
107             List<Emprise> emprises = Arrays.asList( gson.fromJson( emprisesJson, Emprise [ ].class ) );
108             int paramDelaiDateGel = Integer.parseInt( DatastoreService.getDataValue( "parametre.numeric.intervention.duree.periode.gel", "3" ) );
109             empriseService.updateEmprisesConference( emprises, paramDelaiDateGel );
110         }
111 
112         return "ok";
113     }
114 
115     private Map<String, Object> initModel( HttpServletRequest request )
116     {
117         AdminUser user = getUser( );
118 
119         SearchBeanForEmprises searchBean = generateSearchBean( request );
120 
121         boolean profilModifPopin = isProfilModifPopin( request, user );
122 
123         Map<String, Object> model = new HashMap<>( );
124         List<Unit> allUnits = unitService.getAllUnits( true );
125         String uniteOrgaParam = request.getParameter( "uniteOrga" );
126         Integer [ ] unitesIds = null;
127         if ( uniteOrgaParam != null )
128         {
129             String [ ] unites = uniteOrgaParam.split( ", " );
130             unitesIds = new Integer [ unites.length];
131             for ( int i = 0; i < unitesIds.length; i++ )
132             {
133                 unitesIds [i] = Integer.parseInt( unites [i] );
134             }
135         }
136 
137         // Initialisation des variables de recherche
138         if ( searchBean.getFiltreConflitEmprise( ) == null )
139         {
140             searchBean.setFiltreConflitEmprise( CtvConstants.SEARCH_CONF_COORD_FILTRE_CONFLIT );
141             // Initialisation de la valeurs de la check box 'Voir les demandesdes autres demandeurs'
142             searchBean.setShowEmprisesOfOtherUsers( true );
143         }
144         if ( searchBean.getNombreJoursAvantDateDepart( ) == null )
145         {
146             searchBean.setNombreJoursAvantDateDepart( CtvConstants.SEARCH_CONF_COORD_JOUR_DEPART );
147         }
148         if ( searchBean.getNombreJoursApresDateFin( ) == null )
149         {
150             searchBean.setNombreJoursApresDateFin( CtvConstants.SEARCH_CONF_COORD_JOUR_FIN );
151         }
152         if ( searchBean.getRayonExtentionEmprise( ) == null )
153         {
154             searchBean.setRayonExtentionEmprise( CtvConstants.SEARCH_CONF_COORD_EXT_RAYON );
155         }
156 
157         model.put( "liste_unite", UnitTreeUtils.getUnitTreeJson( allUnits, unitesIds ) );
158         model.put( "isConcessionnaire", isConcessionnaire( user, request ) );
159         model.put( "listeStatutsEmprise", getMapFromStatutEmpriseEnum( ) );
160         model.put( "isPopinModifiable", profilModifPopin );
161         model.put( "searchCriteria", searchBean );
162 
163         putDemandes( request, model );
164         return model;
165     }
166 
167     private List<String> getMapFromStatutEmpriseEnum( )
168     {
169         List<String> statuts = new ArrayList<>( );
170         StatutEmprise [ ] statutEmpriseEnums = getStatutEmpriseEnums( );
171         for ( StatutEmprise statutEmprise : statutEmpriseEnums )
172         {
173             statuts.add( statutEmprise.name( ) );
174         }
175         return statuts;
176     }
177 
178     private boolean isProfilModifPopin( HttpServletRequest request, AdminUser user )
179     {
180         boolean isModifiable = false;
181         boolean isInstructeur = ctvProfileService.hasUserProfile( user, TypeProfileCtv.INSTRUCTEUR, getPlugin( ), request );
182         boolean isSuperviseurOne = ctvProfileService.hasUserProfile( user, TypeProfileCtv.SUPERVISEUR_1, getPlugin( ), request );
183         boolean isSuperviseurTwo = ctvProfileService.hasUserProfile( user, TypeProfileCtv.SUPERVISEUR_2, getPlugin( ), request );
184 
185         if ( isInstructeur || isSuperviseurOne || isSuperviseurTwo )
186         {
187             isModifiable = true;
188         }
189 
190         return isModifiable;
191     }
192 
193     private void putDemandes( HttpServletRequest request, Map<String, Object> model )
194     {
195         if ( "displayDemandes".equals( request.getParameter( "action" ) ) )
196         {
197 
198             SearchBeanForEmprises searchBean = generateSearchBean( request );
199 
200             List<SearchEmprisesViewObject> listDemandes = new ArrayList<SearchEmprisesViewObject>( );
201             Integer dvdStvUnitId = getUnitByLabel( "DVD - STV" ).getIdUnit( );
202             AdminUser user = getUser( );
203 
204             Integer concessionUnitId = -1;
205             String concessionUnitLabel = "";
206             Integer idConcessionDvdStv = 0;
207 
208             if ( isConcessionnaire( user, request ) )
209             {
210                 Unit concession = unitService.getUnitUserByType( user, TypeUnit.CONCESSIONNAIRE );
211 
212                 concessionUnitId = concession.getIdUnit( );
213                 concessionUnitLabel = concession.getLabel( );
214                 idConcessionDvdStv = demandeService.concessionIsDvdStv( concessionUnitId );
215             }
216 
217             List<SearchEmprisesViewObject> basicList = demandeService.getListDemandesBySearchBeanForConference( 1, searchBean );
218             for ( SearchEmprisesViewObject viewObject : basicList )
219             {
220                 if ( viewObject.getEmpriseStatut( ) == StatutEmprise.A_COORDONNEE )
221                 {
222                     viewObject.setEmpriseColor( "red" );
223                 }
224                 else
225                     if ( viewObject.getEmpriseStatut( ) == StatutEmprise.COORDONNEE )
226                     {
227                         viewObject.setEmpriseColor( "green" );
228                     }
229                     else
230                         if ( viewObject.getEmpriseStatut( ) == StatutEmprise.A_MODIFIER )
231                         {
232                             viewObject.setEmpriseColor( "orange" );
233                         }
234 
235                 if ( viewObject.getEmpriseDateDeGel( ) == null )
236                 {
237                     viewObject.setHasGel( false );
238                 }
239                 else
240                 {
241                     viewObject.setHasGel( true );
242                 }
243 
244                 setUnitIdAndParentUnitId( viewObject );
245             }
246             listDemandes.addAll( basicList );
247 
248             if ( searchBean.getShowEmpriseInterventionOccupation( ) )
249             {
250                 List<SearchEmprisesViewObject> listInterventionOccupation = demandeService.getListDemandesBySearchBeanForConference( 2, searchBean );
251                 for ( SearchEmprisesViewObject viewObject : listInterventionOccupation )
252                 {
253 
254                     viewObject.setEmpriseColor( "gray" );
255 
256                     if ( viewObject.getEmpriseDateDeGel( ) == null )
257                     {
258                         viewObject.setHasGel( false );
259 
260                     }
261                     else
262                     {
263                         viewObject.setHasGel( true );
264                     }
265 
266                     setUnitIdAndParentUnitId( viewObject );
267 
268                 }
269 
270                 listDemandes.addAll( listInterventionOccupation );
271 
272             }
273 
274             if ( searchBean.getShowCoucheDeGel( ) )
275             {
276                 List<SearchEmprisesViewObject> listCoucheGel = demandeService.getListDemandesBySearchBeanForConference( 3, searchBean );
277 
278                 for ( SearchEmprisesViewObject viewObject : listCoucheGel )
279                 {
280                     setUnitIdAndParentUnitId( viewObject );
281 
282                     for ( SearchEmprisesViewObject viewObjectListDemande : listDemandes )
283                     {
284                         if ( !viewObject.getEmpriseId( ).equals( viewObjectListDemande.getEmpriseId( ) ) )
285                         {
286                             viewObject.setEmpriseColor( "blue" );
287                             viewObject.setHasGel( true );
288                             listDemandes.add( viewObject );
289                             break;
290                         }
291                     }
292                 }
293 
294             }
295 
296             listDemandes = conferenceService.setAllTypeConflitInList( listDemandes );
297 
298             model.put( NATURES_CHANTIER, referentielsService.getItemReferentielList( TypeReferentiel.NATURE_TRAVAUX ) );
299             model.put( MARK_LISTE_DEMANDES, listDemandes );
300             model.put( MARK_DVDSTV_UNIT_ID, dvdStvUnitId );
301             model.put( MARK_CONCESSIONNAIRE_UNIT_ID, concessionUnitId );
302             model.put( MARK_CONCESSIONNAIRE_UNIT_LABEL, concessionUnitLabel );
303             model.put( MARK_IS_CONCESSION_DVD_STV, idConcessionDvdStv );
304             model.put( "searchCriteria", searchBean );
305         }
306 
307     }
308 
309     private SearchBeanForEmprises generateSearchBean( HttpServletRequest request )
310     {
311         SearchBeanForEmprises searchBean = new SearchBeanForEmprises( );
312 
313         populate( searchBean, request );
314 
315         if ( searchBean.getShowEmpriseInterventionOccupation( ) || searchBean.getShowCoucheDeGel( ) )
316         {
317             TypeDemande [ ] TypeDemandeOccupationEtIntervention = new TypeDemande [ ] {
318                     TypeDemande.OCCUPATION, TypeDemande.INTERVENTION
319             };
320 
321             searchBean.setTypesDemande( getTypeDemandeEnums( TypeDemandeOccupationEtIntervention ) );
322 
323         }
324         else
325         {
326 
327             searchBean.setTypesDemande( getTypeDemandeEnums( null ) );
328         }
329 
330         searchBean.setStatuts( getStatutDemandesEnums( ) );
331         searchBean.setStatutEmpriseArray( getStatutEmpriseEnums( ) );
332 
333         AdminUser user = getUser( );
334         if ( isConcessionnaire( user, request ) && !searchBean.getShowEmprisesOfOtherUsers( ) )
335         {
336             Unit concession = unitService.getUnitUserByType( user, TypeUnit.CONCESSIONNAIRE );
337             List<Integer> allUnitId = unitService.getAllUnitsIdByUnit( concession );
338             searchBean.setIdsUnit( allUnitId );
339         }
340         return searchBean;
341 
342     }
343 
344     private Boolean isConcessionnaire( AdminUser user, HttpServletRequest request )
345     {
346         return ctvProfileService.hasUserProfile( user, TypeProfileCtv.CONCESSIONNAIRE, getPlugin( ), request );
347     }
348 
349     private Unit getUnitByLabel( String unitLabel )
350     {
351         Unit unit = null;
352         List<Unit> units = unitService.getUnitsByLabel( unitLabel );
353         if ( !units.isEmpty( ) )
354         {
355             unit = units.get( 0 );
356         }
357         return unit;
358     }
359 
360     private void setUnitIdAndParentUnitId( SearchEmprisesViewObject viewObject )
361     {
362         Unit unit = getUnitByLabel( viewObject.getBeneficiaireRaisonSociale( ) );
363 
364         if ( unit == null )
365         {
366             viewObject.setUnitId( -1 );
367             viewObject.setParentUnitId( -1 );
368         }
369         else
370         {
371             viewObject.setUnitId( unit.getIdUnit( ) );
372             viewObject.setParentUnitId( unit.getIdParent( ) );
373         }
374 
375     }
376 
377     private TypeDemande [ ] getTypeDemandeEnums( TypeDemande [ ] DemandeType )
378     {
379         TypeDemande [ ] typesDemandeBasic = new TypeDemande [ ] {
380                 TypeDemande.BARRAGE_US, TypeDemande.INTENTION_TRAVAUX, TypeDemande.TRAVAUX_IMPERATIFS_NON_PROGRAMMES
381         };
382 
383         TypeDemande [ ] typesDemande = (TypeDemande [ ]) ArrayUtils.addAll( typesDemandeBasic, DemandeType );
384 
385         return typesDemande;
386     }
387 
388     private StatutEmprise [ ] getStatutEmpriseEnums( )
389     {
390         StatutEmprise [ ] statutEmprise = new StatutEmprise [ ] {
391                 StatutEmprise.A_COORDONNEE, StatutEmprise.COORDONNEE, StatutEmprise.A_MODIFIER, StatutEmprise.A_TRAITER, StatutEmprise.A_COMPLETER,
392                 StatutEmprise.A_VALIDER, StatutEmprise.A_SIGNER, StatutEmprise.SIGNE, StatutEmprise.FIN_TRAVAUX_RESEAUX, StatutEmprise.FIN_TRAVAUX_VOIRIE,
393                 StatutEmprise.REMISE_EN_CIRCULATION, StatutEmprise.REFECTION_EN_COURS, StatutEmprise.REFECTION_TERMINEE, StatutEmprise.PROLONGATION,
394                 StatutEmprise.TRAVAUX_EN_COURS, StatutEmprise.GARANTIE
395         };
396 
397         return statutEmprise;
398     }
399 
400     private StatutDemande [ ] getStatutDemandesEnums( )
401     {
402         StatutDemande [ ] statutDemande = new StatutDemande [ ] {
403                 StatutDemande.A_TRAITER, StatutDemande.A_SIGNER, StatutDemande.A_COMPLETER, StatutDemande.SIGNE, StatutDemande.EN_COURS, StatutDemande.A_VALIDER
404         };
405         return statutDemande;
406     }
407 
408 }