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.constant.TypeProfileCtv.INSTRUCTEUR;
37  import static fr.paris.lutece.plugins.ctv.constant.TypeProfileCtv.SUPERVISEUR_1;
38  import static fr.paris.lutece.plugins.ctv.constant.TypeProfileCtv.SUPERVISEUR_2;
39  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.NATURES_CHANTIER;
40  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.PARAMETER_ADD;
41  
42  import java.util.ArrayList;
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 org.apache.commons.beanutils.ConvertUtils;
50  
51  import fr.paris.lutece.plugins.ctv.bo.ItemReferentiel.TypeReferentiel;
52  import fr.paris.lutece.plugins.ctv.bo.SearchBeanForAlertes;
53  import fr.paris.lutece.plugins.ctv.bo.SearchBeanForAlertes.CategorieAlertes;
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.IReferentielsService;
57  import fr.paris.lutece.plugins.ctv.constant.Constants;
58  import fr.paris.lutece.plugins.ctv.constant.StatutDemande;
59  import fr.paris.lutece.plugins.ctv.constant.StatutEmprise;
60  import fr.paris.lutece.plugins.ctv.constant.TypeDemande;
61  import fr.paris.lutece.plugins.ctv.constant.TypeProfileCtv;
62  import fr.paris.lutece.plugins.ctv.converter.CustomDateConverter;
63  import fr.paris.lutece.plugins.ctv.converter.EnumConverter;
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.gismap.business.AddressParam;
67  import fr.paris.lutece.plugins.gismap.business.AddressParamHome;
68  import fr.paris.lutece.plugins.unittree.business.unit.Unit;
69  import fr.paris.lutece.plugins.unittree.business.unit.Unit.TypeUnit;
70  import fr.paris.lutece.plugins.unittree.service.unit.IUnitService;
71  import fr.paris.lutece.portal.business.user.AdminUser;
72  import fr.paris.lutece.portal.service.spring.SpringContextService;
73  
74  public abstract class AbstractTableauDeBordControllerService extends AbstractCtvJspBean implements ITableauDeBordControllerService
75  {
76  
77      private static final long serialVersionUID = -9119149650491996161L;
78  
79      private static IReferentielsService referentielsService = SpringContextService.getBean( "referentielsService" );
80      protected IUnitService unitService = SpringContextService.getBean( IUnitService.BEAN_UNIT_SERVICE );
81  
82      protected static final String BLOC_EN_COURS = "en_cours";
83      protected static final String BLOC_NOUVEAU = "nouveau";
84      protected static final String BLOC_A_TRAITER = "a_traiter";
85      protected static final String BLOC_BROUILLON = "brouillon";
86  
87      protected static final String PARAMETER_STATUT = "statut";
88  
89      private static final String MARK_LISTE_DEMANDES = "list_demandes";
90      private static final String MARK_LISTE_USER_UNITS_ID = "list_user_units_id";
91  
92      protected void displaySectorTree( Map<String, Object> model, HttpServletRequest request )
93      {
94          List<Unit> allUnits = unitService.getAllUnits( true );
95          String uniteOrgaParam = request.getParameter( "uniteOrga" );
96          Integer [ ] unitesIds = null;
97          if ( ( uniteOrgaParam != null ) && !"".equals( uniteOrgaParam ) )
98          {
99              String [ ] unites = uniteOrgaParam.split( ", " );
100             unitesIds = new Integer [ unites.length];
101             for ( int i = 0; i < unitesIds.length; i++ )
102             {
103                 unitesIds [i] = Integer.parseInt( unites [i] );
104             }
105         }
106         model.put( "liste_unite", UnitTreeUtils.getUnitTreeJson( allUnits, unitesIds ) );
107         model.put( "type_demandes", getTypesDemandes( ) );
108 
109     }
110 
111     @Override
112     public String getTableauDeBord( HttpServletRequest request, AdminUser user )
113     {
114         Map<String, Object> model = getModel( );
115         putDemandes( request, model, user );
116         putCompteurs( model, user, request );
117         AddressParam addressParam = AddressParamHome.getAddressParameters( );
118         addressParam.setListType( "VOIE" );
119         model.put( PARAMETER_ADD, addressParam );
120         if ( request.getSession( ).getAttribute( "profile_default" ) != null )
121         {
122             model.put( "profileInSession", ( (TypeProfileCtv) request.getSession( ).getAttribute( "profile_default" ) ).getKey( ) );
123         }
124 
125         if(ctvProfileService.hasUserProfile( user, SUPERVISEUR_1, getPlugin( ), request )
126                 || ctvProfileService.hasUserProfile( user, INSTRUCTEUR, getPlugin( ), request )
127                 || ctvProfileService.hasUserProfile( user, SUPERVISEUR_2, getPlugin( ), request ))
128         {
129             model.put( "instruction", true );
130             model.put(MARK_LISTE_USER_UNITS_ID, setUnitIds( user ));
131         }
132         else
133         {
134             model.put( "instruction", false );
135         }
136 
137 
138         return getPage( model );
139     }
140 
141     private void putDemandes( HttpServletRequest request, Map<String, Object> model, AdminUser user )
142     {
143         String typeAlerteParam = request.getParameter( "typeAlerte" );
144         List<SearchEmprisesViewObject> listeDemandes = new ArrayList<>( );
145         model.put( NATURES_CHANTIER, referentielsService.getItemReferentielList( TypeReferentiel.NATURE_TRAVAUX ) );
146         if ( typeAlerteParam != null )
147         {
148             listeDemandes = demandeService
149                     .getListDemandesWithEmpriseByAlertesCriteria( getSearchBeanForAlert( CategorieAlertes.valueOf( typeAlerteParam ), user, request ) );
150             putPathDemande( model );
151             model.put( MARK_LISTE_DEMANDES, listeDemandes );
152         }
153         else
154             if ( "displayDemandes".equals( request.getParameter( "action" ) ) )
155             {
156 
157                 SearchBeanForEmprises searchBean = initSearchBean( user );
158                 List<StatutDemande> statusDemande = new ArrayList<>( );
159 
160                 if ( request.getParameter( "statut" ) != null )
161                 {
162                     searchBean.setLibeleByBloc( request.getParameter( "statut" ).toUpperCase( ) );
163                     if ( request.getParameter( "typeDeDemande" ) != null )
164                     {
165                         String typeDeDemande = request.getParameter( "typeDeDemande" );
166                         if ( !( typeDeDemande.equals( Constants.FIN_TRAVAUX_VOIRIE ) ) && !( typeDeDemande.equals( Constants.FIN_TRAVAUX_RESEAUX ) )
167                                 && !( typeDeDemande.equals( Constants.REMISE_CIRCULATION ) ) && !( typeDeDemande.equals( Constants.TRAVAUX_EN_COURS ) )
168                                 && !( typeDeDemande.equals( Constants.PROLONGATION ) ) && !( typeDeDemande.equals( Constants.GARANTIE ) ) )
169                         {
170                             if (typeDeDemande.equals( Constants.INTERVENTION ) &&  request.getParameter( "statut" ).equals(BLOC_A_TRAITER) && (ctvProfileService.hasUserProfile( user, TypeProfileCtv.SUPERVISEUR_1, getPlugin( ), request ) || ctvProfileService.hasUserProfile( user, TypeProfileCtv.SUPERVISEUR_2, getPlugin( ), request ) ))
171                             {
172                                 searchBean.setTypesDemande( new TypeDemande [ ] {
173                                         ( TypeDemande.valueOf( typeDeDemande ) ) , ( TypeDemande.valueOf( Constants.BARRAGE_US ) )
174                                 } );
175                             }
176                             else
177                             {
178                                 searchBean.setTypesDemande( new TypeDemande [ ] {
179                                         ( TypeDemande.valueOf( typeDeDemande ) )
180                                 } );
181                             }
182                         }
183                         else
184                         {
185                             searchBean.setTypesDemande( getTypesDemandes( ) );
186                         }
187                         statusDemande = getStatusByBlocs( searchBean, request, statusDemande, user, typeDeDemande );
188                     }
189 
190                     StatutDemande [ ] array = new StatutDemande [ statusDemande.size( )];
191                     statusDemande.toArray( array );
192                     searchBean.setStatuts( array );
193 
194                     searchBean.setIdsUnit( setUnitIds( user ) );
195                     searchBean.setAlert( true );
196                 }
197                 else
198                 {
199                     if(( request.getParameter( "typeDemande" ) != null ) && !"".equals( request.getParameter( "typeDemande" ) ))
200                     {
201                         searchBean.setTypesDemande( new TypeDemande [ ] {
202                                 TypeDemande.valueOf( request.getParameter( "typeDemande" ) )
203                         } );
204                     }
205                     else
206                     {
207                         searchBean.setTypesDemande( getTypesDemandes( ) );
208                     }
209 
210                     statusDemande.addAll( getStatusBySearchCriteria( ) );
211 
212                     ConvertUtils.register( new CustomDateConverter( ), Date.class );
213                     ConvertUtils.register( new EnumConverter( ), StatutEmprise.class );
214 
215                     populate( searchBean, request );
216 
217                     if ( searchBean.getNomIntervenant( ) != null )
218                     {
219                         String nomIntervenant = searchBean.getNomIntervenant( );
220                         nomIntervenant = nomIntervenant.replace( "\"", "&quot;" );
221                         searchBean.setNomIntervenant( nomIntervenant );
222                     }
223 
224                     if ( searchBean.getNumProjetIntervenant( ) != null )
225                     {
226                         String numIntervenant = searchBean.getNumProjetIntervenant( );
227                         numIntervenant = numIntervenant.replace( "\"", "&quot;" );
228                         searchBean.setNumProjetIntervenant( numIntervenant );
229                     }
230 
231                     if ( searchBean.getNomVoie( ) != null )
232                     {
233                         String nomVoie = searchBean.getNomVoie( );
234                         nomVoie = nomVoie.replace( "\"", "&quot;" );
235                         searchBean.setNomVoie( nomVoie );
236                     }
237 
238                     model.put( "searchCriteria", searchBean );
239 
240                     StatutDemande [ ] array = new StatutDemande [ statusDemande.size( )];
241                     statusDemande.toArray( array );
242                     searchBean.setStatuts( array );
243                 }
244 
245                 // Si aucune subdivision n'est définie alors on implémente celles par défaut de l'utilisateur (sauf concessionnaire)
246                 String uniteOrga = searchBean.getUniteOrga( );
247                 if(( uniteOrga == null ) || "".equals( uniteOrga ))
248                 {
249                     searchBean.setUniteOrga((getDefaultUserUnits (request )));
250                 }
251 
252                 listeDemandes = demandeService.getListDemandesWithEmpriseByCriteria( searchBean );
253                 putPathDemande( model );
254                 model.put( NATURES_CHANTIER, referentielsService.getItemReferentielList( TypeReferentiel.NATURE_TRAVAUX ) );
255                 model.put( MARK_LISTE_DEMANDES, listeDemandes );
256             }
257         insertListDemandesForExcel( model, listeDemandes );
258 
259         displaySectorTree( model, request );
260         model.put( "statutEmprises", StatutEmprise.values( ) );
261 
262     }
263 
264     protected List<Integer> setUnitIds( AdminUser user )
265     {
266         List<Integer> idUnits = new ArrayList<Integer>( );
267         List<Unit> units = unitService.getUnitsByIdUser( user.getUserId( ), true );
268         for ( Unit unit : units )
269         {
270             if ( TypeUnit.VILLE.equals( unit.getTypeunit( ) ) )
271             {
272                 idUnits.addAll( unitService.getAllUnitsIdByUnit( unit ) );
273             }
274         }
275         return idUnits;
276     }
277 
278     protected abstract SearchBeanForAlertes getSearchBeanForAlert( CategorieAlertes catgeorie, AdminUser user, HttpServletRequest request );
279 
280     protected abstract TypeDemande [ ] getTypesDemandes( );
281 
282     protected abstract List<StatutDemande> getStatusByBlocs( SearchBeanForEmprises searchBean, HttpServletRequest request, List<StatutDemande> statusDemande,
283             AdminUser user, String typeDeDemande );
284 
285     protected abstract String getPage( Map<String, Object> model );
286 
287     protected abstract void putPathDemande( Map<String, Object> model );
288 
289     protected abstract void putCompteurs( Map<String, Object> model, AdminUser user, HttpServletRequest request );
290 
291     protected abstract List<StatutDemande> getStatusBySearchCriteria( );
292 
293     protected abstract SearchBeanForEmprises initSearchBean( AdminUser user );
294 
295     protected abstract String getDefaultUserUnits (HttpServletRequest request );
296 
297 }