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.service.vote;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  import java.util.Locale;
39  import java.util.regex.Matcher;
40  import java.util.regex.Pattern;
41  
42  import javax.inject.Inject;
43  import javax.inject.Named;
44  import javax.servlet.http.HttpServletRequest;
45  
46  import org.apache.commons.lang.StringUtils;
47  
48  import fr.paris.lutece.plugins.document.business.Document;
49  import fr.paris.lutece.plugins.document.business.DocumentFilter;
50  import fr.paris.lutece.plugins.document.business.DocumentHome;
51  import fr.paris.lutece.plugins.document.business.spaces.DocumentSpace;
52  import fr.paris.lutece.plugins.document.business.spaces.DocumentSpaceHome;
53  import fr.paris.lutece.plugins.extend.business.extender.history.ResourceExtenderHistory;
54  import fr.paris.lutece.plugins.extend.business.extender.history.ResourceExtenderHistoryFilter;
55  import fr.paris.lutece.plugins.extend.modules.rating.business.Rating;
56  import fr.paris.lutece.plugins.extend.modules.rating.service.IRatingService;
57  import fr.paris.lutece.plugins.extend.modules.rating.service.RatingAddOnService;
58  import fr.paris.lutece.plugins.extend.modules.rating.service.extender.RatingResourceExtender;
59  import fr.paris.lutece.plugins.extend.modules.rating.service.security.IRatingSecurityService;
60  import fr.paris.lutece.plugins.extend.modules.rating.util.constants.RatingConstants;
61  import fr.paris.lutece.plugins.extend.service.extender.config.IResourceExtenderConfigService;
62  import fr.paris.lutece.plugins.extend.service.extender.history.IResourceExtenderHistoryService;
63  import fr.paris.lutece.plugins.participatorybudget.business.vote.MyVote;
64  import fr.paris.lutece.plugins.participatorybudget.business.vote.Vote;
65  import fr.paris.lutece.plugins.participatorybudget.business.vote.VoteHome;
66  import fr.paris.lutece.plugins.participatorybudget.service.NbProjetArrCacheService;
67  import fr.paris.lutece.plugins.participatorybudget.service.campaign.CampaignService;
68  import fr.paris.lutece.plugins.participatorybudget.service.rating.BudgetRatingService;
69  import fr.paris.lutece.plugins.participatorybudget.util.ParticipatoryBudgetConstants;
70  import fr.paris.lutece.portal.service.cache.AbstractCacheableService;
71  import fr.paris.lutece.portal.service.message.SiteMessage;
72  import fr.paris.lutece.portal.service.message.SiteMessageException;
73  import fr.paris.lutece.portal.service.message.SiteMessageService;
74  import fr.paris.lutece.portal.service.security.LuteceUser;
75  import fr.paris.lutece.portal.service.security.SecurityService;
76  import fr.paris.lutece.portal.service.util.AppPathService;
77  import fr.paris.lutece.util.json.ErrorJsonResponse;
78  import fr.paris.lutece.util.json.JsonUtil;
79  
80  /**
81   * Service to get ratings of users
82   */
83  public class MyVoteService
84  {
85      /**
86       * Name of the bean of this service
87       */
88      public static final String BEAN_NAME = "participatorybudget.myVoteService";
89      @Inject
90      private IResourceExtenderHistoryService _resourceExtenderHistoryService;
91      @Inject
92      private IRatingService _ratingService;
93      @Inject
94      @Named( RatingConstants.BEAN_CONFIG_SERVICE )
95      private IResourceExtenderConfigService _configService;
96      @Inject
97      private IRatingSecurityService _ratingSecurityService;
98  
99      private static AbstractCacheableService _nbProjetcache = new NbProjetArrCacheService( );
100 
101     /**
102      * Get the list of ratings of a user
103      * 
104      * @param user
105      *            The user to get the list of ratings of
106      * @return The list of ratings of the user
107      */
108     public List<MyVote> getUserVote( LuteceUser user )
109     {
110         ResourceExtenderHistoryFilter filter = new ResourceExtenderHistoryFilter( );
111         filter.setExtenderType( RatingResourceExtender.RESOURCE_EXTENDER );
112         filter.setExtendableResourceType( RatingAddOnService.PROPERTY_RESOURCE_TYPE );
113         filter.setUserGuid( user.getName( ) );
114 
115         List<ResourceExtenderHistory> listResourceHistory = _resourceExtenderHistoryService.findByFilter( filter );
116         List<MyVote> listVotes = new ArrayList<MyVote>( listResourceHistory.size( ) );
117 
118         for ( ResourceExtenderHistory history : listResourceHistory )
119         {
120             if ( StringUtils.isNumeric( history.getIdExtendableResource( ) ) )
121             {
122                 int nIdDocument = Integer.parseInt( history.getIdExtendableResource( ) );
123                 Document document = DocumentHome.findByPrimaryKeyWithoutBinaries( nIdDocument );
124                 Rating rating = _ratingService.findByResource( history.getIdExtendableResource( ), history.getExtendableResourceType( ) );
125                 MyVotegins/participatorybudget/business/vote/MyVote.html#MyVote">MyVote myVote = new MyVote( document, history.getDateCreation( ), rating.getVoteCount( ) );
126                 listVotes.add( myVote );
127             }
128         }
129 
130         return listVotes;
131     }
132 
133     /**
134      * Get the list of ratings of a user
135      * 
136      * @param user
137      *            The user to get the list of ratings of
138      * @return The list of ratings of the user
139      */
140     public int getNbUserVote( LuteceUser user )
141     {
142 
143         int nbCount = 0;
144         ResourceExtenderHistoryFilter filter = new ResourceExtenderHistoryFilter( );
145         filter.setExtenderType( RatingResourceExtender.RESOURCE_EXTENDER );
146         filter.setExtendableResourceType( RatingAddOnService.PROPERTY_RESOURCE_TYPE );
147         filter.setUserGuid( user.getName( ) );
148 
149         List<ResourceExtenderHistory> listResourceHistory = _resourceExtenderHistoryService.findByFilter( filter );
150 
151         if ( listResourceHistory != null )
152         {
153             nbCount = listResourceHistory.size( );
154         }
155         return nbCount;
156     }
157 
158     /**
159      * 
160      * @param idUser
161      * @return
162      */
163 
164     public MyVote getUserVote( String idUser )
165     {
166         MyVotegins/participatorybudget/business/vote/MyVote.html#MyVote">MyVote myVote = new MyVote( );
167         int nbrVoteArrdt = VoteHome.getVoteUserNotLocation( idUser, 75000 );
168         int nbrVoteParis = VoteHome.getVoteUserArrondissement( idUser, 75000 );
169         List<Vote> listvote = VoteHome.getVoteUser( idUser );
170         for ( Vote vote : listvote )
171         {
172             if ( vote.getArrondissement( ) != 75000 )
173             {
174 
175                 myVote.setArrondissementUser( getArrondissement( String.valueOf( vote.getArrondissement( ) ) ) );
176 
177             }
178         }
179 
180         int nbrVoteTotal = nbrVoteArrdt + nbrVoteParis;
181 
182         myVote.setNbTotVotes( nbrVoteTotal );
183         myVote.setNbTotVotesArrondissement( nbrVoteArrdt );
184         myVote.setNbTotVotesToutParis( nbrVoteParis );
185 
186         return myVote;
187     }
188 
189     /**
190      * GET ARRONDISSEMENT
191      * 
192      * @param arrondissement
193      * @return
194      */
195     private static String getArrondissement( String arrondissement )
196     {
197 
198         String strArrondissement = "";
199 
200         Pattern p = Pattern.compile( "75(020|00[1-9]|116|01[0-9])$" );
201         Matcher m = p.matcher( arrondissement );
202         if ( m.matches( ) )
203             strArrondissement = Integer.valueOf( arrondissement.substring( 2 ) ) + "e " + "arrondissement";
204 
205         return strArrondissement;
206     }
207 
208     /**
209      * 
210      * @param nSpaceId
211      * @return
212      */
213     public int getNumberDocumentSpace( int nSpaceId )
214     {
215 
216         int nbrDoc = 0;
217         String strSpaceCacheKey = "" + nSpaceId;
218         Integer nbDocInSpaceCache = (Integer) _nbProjetcache.getFromCache( strSpaceCacheKey );
219         if ( nbDocInSpaceCache != null )
220         {
221 
222             return nbDocInSpaceCache;
223         }
224         else
225         {
226             List<DocumentSpace> docSpace = DocumentSpaceHome.findChilds( nSpaceId );
227             DocumentFilter filter = new DocumentFilter( );
228             filter.setIsPublished( true );
229             filter.setCodeDocumentType( "project_2015" );
230             for ( DocumentSpace ds : docSpace )
231             {
232 
233                 filter.setIdSpace( ds.getId( ) );
234                 nbrDoc = nbrDoc + DocumentHome.findByFilter( filter, new Locale( "fr", "FR" ) ).size( );
235             }
236 
237             _nbProjetcache.putInCache( strSpaceCacheKey, nbrDoc );
238         }
239 
240         return nbrDoc;
241     }
242 
243     /**
244      * Valdate Votes
245      * 
246      * @param userId
247      * @param status
248      */
249     public void validateVotes( String userId, int status )
250     {
251 
252         VoteHome.validateVote( userId, status );
253 
254     }
255 
256     /**
257      * 
258      * @param userId
259      * @return
260      */
261     public boolean isUserVoteValidated( String userId )
262     {
263 
264         List<Vote> listvote = VoteHome.getVoteUser( userId, Vote.Status.STATUS_VALIDATED.getValeur( ) );
265         if ( listvote == null || listvote.isEmpty( ) )
266         {
267 
268             return false;
269         }
270 
271         return true;
272     }
273 
274     public String cancelVote( HttpServletRequest request )
275     {
276 
277         String strExtendableResourceType = request.getParameter( RatingConstants.PARAMETER_EXTENDABLE_RESOURCE_TYPE );
278         LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
279 
280         if ( !CampaignService.getInstance( ).isDuring( "VOTE" ) || isUserVoteValidated( user.getName( ) ) )
281         {
282             try
283             {
284                 SiteMessageService.setMessage( request, RatingConstants.MESSAGE_CANNOT_VOTE, SiteMessage.TYPE_STOP );
285             }
286             catch( SiteMessageException e )
287             {
288                 return JsonUtil.buildJsonResponse( new ErrorJsonResponse( AppPathService.getSiteMessageUrl( request ) ) );
289             }
290         }
291 
292         List<Vote> listVote = VoteHome.getVoteUser( user.getName( ) );
293 
294         for ( Vote vote : listVote )
295         {
296             // cancel all vote for stat export, and after recreate vote TP
297             _ratingService.doCancelVote( user, String.valueOf( vote.getProjetId( ) ), strExtendableResourceType );
298             if ( vote.getLocation( ) == "whole_city" )
299             {
300                 request.setAttribute( ParticipatoryBudgetConstants.PROJECT_THEME, vote.getTheme( ) );
301                 request.setAttribute( ParticipatoryBudgetConstants.PROJECT_TITLE, vote.getTitle( ) );
302                 request.setAttribute( ParticipatoryBudgetConstants.PROJECT_LOCATION, ParticipatoryBudgetConstants.LOCATION_WHOLE_CITY );
303                 _ratingService.doVote( String.valueOf( vote.getProjetId( ) ), strExtendableResourceType, BudgetRatingService.VOTE_VALUE, request );
304             }
305         }
306 
307         // return MyInfosXPage.getMyInfosPanelForAjax(request);
308         return "";
309     }
310 }