View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.participatorybudget.web.vote;
35  
36  import java.io.IOException;
37  import java.util.List;
38  import java.util.Map;
39  
40  import javax.servlet.http.HttpServletRequest;
41  
42  import org.apache.commons.lang.StringUtils;
43  
44  import fr.paris.lutece.plugins.participatorybudget.business.vote.VotePerLocation;
45  import fr.paris.lutece.plugins.participatorybudget.business.vote.VotePerLocationHome;
46  import fr.paris.lutece.portal.service.security.UserNotSignedException;
47  import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
48  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
49  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
50  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
51  
52  /**
53   * This class provides the user interface to manage the launching of the indexing of the site pages
54   */
55  
56  @Controller( controllerJsp = "VoteParArrandissement.jsp", controllerPath = "jsp/admin/plugins/participatorybudget/", right = "VOTE_PAR_ARRANDISSEMENT" )
57  public class VoteParArrandJspBean extends MVCAdminJspBean
58  {
59      /**
60       * 
61       */
62      private static final long serialVersionUID = 1L;
63      // //////////////////////////////////////////////////////////////////////////
64      // Constantes
65      /**
66       * Right to manage indexation
67       */
68      public static final String RIGHT_INDEXER = "/";
69      private static final String TEMPLATE_MANAGE_VOTES_PAR_ARRAND = "admin/plugins/participatorybudget/vote_par_arrandissement.html";
70  
71      // Properties for page titles
72      private static final String PROPERTY_PAGE_TITLE_VOTE_PAR_ARRAND = "participatorybudget.vote_par_arrd.pageTitle";
73  
74      // Parameters
75      private static final String PARAMETER_NB = "nb_";
76  
77      // Mark
78      private static final String MARK_LIST_VOTES = "listVotes";
79  
80      // views
81      private static final String VIEW_MANAGE_VOTE_PAR_ARRAND = "manageVoteParArrand";
82  
83      // Actions
84      private static final String ACTION_DO_UPDATE = "update";
85  
86      /**
87       * Displays the indexing parameters
88       *
89       * @param request
90       *            the http request
91       * @return the html code which displays the parameters page
92       * @throws UserNotSignedException
93       */
94      @View( value = VIEW_MANAGE_VOTE_PAR_ARRAND, defaultView = true )
95      public String getManageVoteParArrand( HttpServletRequest request ) throws UserNotSignedException
96      {
97  
98          List<VotePerLocation> listVotes = VotePerLocationHome.getListVotesPerLocation( );
99  
100         Map<String, Object> model = getModel( );
101 
102         model.put( MARK_LIST_VOTES, listVotes );
103 
104         return getPage( PROPERTY_PAGE_TITLE_VOTE_PAR_ARRAND, TEMPLATE_MANAGE_VOTES_PAR_ARRAND, model );
105     }
106 
107     /**
108      * Calls the indexing process
109      *
110      * @param request
111      *            the http request
112      * @return the result of the indexing process
113      * @throws IOException
114      */
115     @Action( ACTION_DO_UPDATE )
116     public String doUpdate( HttpServletRequest request ) throws IOException
117     {
118         List<VotePerLocation> listVotes = VotePerLocationHome.getListVotesPerLocation( );
119 
120         for ( int i = 1; i <= listVotes.size( ); i++ )
121         {
122             VotePerLocation vote = null;
123             String strVal = request.getParameter( PARAMETER_NB + i ) == null ? StringUtils.EMPTY : request.getParameter( PARAMETER_NB + i );
124             if ( StringUtils.isEmpty( strVal ) || !StringUtils.isNumeric( strVal ) )
125             {
126                 if ( i == 21 )
127                 {
128                     addError( "Champs Tout Paris n'est pas remplis" );
129                     return redirectView( request, VIEW_MANAGE_VOTE_PAR_ARRAND );
130                 }
131                 else
132                 {
133                     addError( "Champs Paris " + i + " Arrandissement n'est pas remplis" );
134                     return redirectView( request, VIEW_MANAGE_VOTE_PAR_ARRAND );
135                 }
136             }
137             vote = listVotes.get( i - 1 );
138             vote.setNbVotes( Integer.parseInt( strVal ) );
139         }
140 
141         for ( VotePerLocation vpl : listVotes )
142         {
143             VotePerLocationHome.updateVotesPerLocation( vpl );
144         }
145         addInfo( "Votes par Arrandissemente modifié" );
146         return redirectView( request, VIEW_MANAGE_VOTE_PAR_ARRAND );
147     }
148 }