1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
82
83 public class MyVoteService
84 {
85
86
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
103
104
105
106
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
135
136
137
138
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
161
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
191
192
193
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
211
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
245
246
247
248
249 public void validateVotes( String userId, int status )
250 {
251
252 VoteHome.validateVote( userId, status );
253
254 }
255
256
257
258
259
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
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
308 return "";
309 }
310 }