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.controller.front;
35  
36  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACTION_ABANDON;
37  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACTION_COMPLETE_LATER;
38  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACTION_DOWNLOAD;
39  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACTION_NEXT;
40  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ACTION_SAVE;
41  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_CHANTIER;
42  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.ETAPE_EXECUTANT_TRAVAUX;
43  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.PAGE_PORTAL;
44  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.PARAMETER_NEXT_STEP;
45  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.SESSION_NUM_DEMANDE_ID;
46  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.TEMPLATE_CHANTIER;
47  import static fr.paris.lutece.plugins.ctv.constants.CtvConstants.XPAGE_ACCUEIL;
48  
49  import java.util.Map;
50  
51  import javax.servlet.http.HttpServletRequest;
52  
53  import org.apache.commons.fileupload.FileItem;
54  
55  import com.mysql.jdbc.StringUtils;
56  
57  import fr.paris.lutece.plugins.ctv.bo.EntiteDemande;
58  import fr.paris.lutece.plugins.ctv.business.IEntiteDemandeService;
59  import fr.paris.lutece.plugins.ctv.service.ChantierControllerService;
60  import fr.paris.lutece.plugins.ctv.service.FormulaireDemandeOccupationServiceImpl;
61  import fr.paris.lutece.plugins.ctv.service.IChantierControllerService;
62  import fr.paris.lutece.plugins.ctv.service.IFormulaire;
63  import fr.paris.lutece.plugins.ctv.util.Utils;
64  import fr.paris.lutece.portal.service.security.UserNotSignedException;
65  import fr.paris.lutece.portal.service.spring.SpringContextService;
66  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
67  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
68  import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
69  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
70  import fr.paris.lutece.portal.web.xpages.XPage;
71  
72  @Controller( xpageName = ETAPE_CHANTIER )
73  public class ChantierApp extends MVCApplication
74  {
75  
76      /**
77       * 
78       */
79      private static final long serialVersionUID = 1L;
80      private IChantierControllerService chantierControllerService = ChantierControllerService.getInstance( );
81      private IFormulaire formulaireDemandeOccupationServiceImpl = FormulaireDemandeOccupationServiceImpl.getInstance( );
82      private IEntiteDemandeService entiteDemandeService = SpringContextService.getBean( "entiteDemandeService" );
83      public static final String COMPLETE_TRUE = "&complete=true";
84  
85      @View( value = ETAPE_CHANTIER, defaultView = true )
86      public XPage getChantier( HttpServletRequest request ) throws UserNotSignedException
87      {
88          Utils.getUser( request );
89          Map<String, Object> model = getModel( );
90          chantierControllerService.initModel( model );
91          chantierControllerService.alimenterModel( request, model );
92          return getXPage( TEMPLATE_CHANTIER, request.getLocale( ), model );
93      }
94  
95      @Action( ACTION_NEXT )
96      public XPage nextPage( HttpServletRequest request )
97      {
98          chantierControllerService.registerConverters( );
99          chantierControllerService.updateFromRequest( request );
100         return redirectToPageExecutant( request );
101     }
102 
103     private XPage redirectToPageExecutant( HttpServletRequest request )
104     {
105         String numDemande = request.getSession( ).getAttribute( SESSION_NUM_DEMANDE_ID ).toString( );
106         if ( numDemande != null )
107         {
108             return returnExecutantPage( request, numDemande );
109         }
110         else
111         {
112             return redirect( request, PAGE_PORTAL + ETAPE_EXECUTANT_TRAVAUX );
113         }
114     }
115 
116     @Action( ACTION_COMPLETE_LATER )
117     public XPage completeLater( HttpServletRequest request )
118     {
119         chantierControllerService.updateFromRequest( request );
120         return redirect( request, PAGE_PORTAL + XPAGE_ACCUEIL );
121     }
122 
123     @Action( ACTION_SAVE )
124     public XPage doSaveAction( HttpServletRequest request )
125     {
126         chantierControllerService.registerConverters( );
127         chantierControllerService.updateFromRequest( request );
128         // Récupération de la redirection
129         String nextStepPath = request.getParameter( PARAMETER_NEXT_STEP );
130         if ( StringUtils.isNullOrEmpty( nextStepPath ) )
131         {
132             return redirect( request, PAGE_PORTAL + XPAGE_ACCUEIL );
133         }
134         return redirect( request, PAGE_PORTAL + nextStepPath );
135     }
136 
137     @Action( ACTION_ABANDON )
138     public XPage doAbandonAction( HttpServletRequest request )
139     {
140         formulaireDemandeOccupationServiceImpl.removeFilesFromSession( request );
141         // Récupération de la redirection
142         String nextStepPath = request.getParameter( PARAMETER_NEXT_STEP );
143         if ( StringUtils.isNullOrEmpty( nextStepPath ) )
144         {
145             return redirect( request, PAGE_PORTAL + XPAGE_ACCUEIL );
146         }
147         return redirect( request, PAGE_PORTAL + nextStepPath );
148     }
149 
150     @Action( ACTION_DOWNLOAD )
151     public XPage download( HttpServletRequest request )
152     {
153         FileItem fileItem = chantierControllerService.getFileItemFromRequest( request );
154         if ( fileItem != null )
155         {
156             return download( fileItem.get( ), fileItem.getName( ), fileItem.getName( ) );
157         }
158         return null;
159     }
160 
161     private XPage returnExecutantPage( HttpServletRequest request, String numDemande )
162     {
163         int demandeId = Integer.parseInt( numDemande );
164         EntiteDemande entiteExecutantDemande = entiteDemandeService.getExecutantByNumDemande( demandeId );
165         if ( entiteExecutantDemande != null )
166         {
167             return redirect( request, PAGE_PORTAL + ETAPE_EXECUTANT_TRAVAUX + COMPLETE_TRUE );
168         }
169         else
170         {
171             return redirect( request, PAGE_PORTAL + ETAPE_EXECUTANT_TRAVAUX );
172         }
173     }
174 
175 }