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.rating;
35
36 import java.sql.Timestamp;
37 import java.text.ParseException;
38 import java.util.Calendar;
39 import java.util.Date;
40
41 import javax.servlet.http.HttpServletRequest;
42
43 import org.apache.commons.lang.StringUtils;
44 import org.springframework.transaction.annotation.Transactional;
45
46 import fr.paris.lutece.plugins.document.business.Document;
47 import fr.paris.lutece.plugins.extend.modules.rating.service.RatingPlugin;
48 import fr.paris.lutece.plugins.extend.modules.rating.service.RatingService;
49 import fr.paris.lutece.plugins.participatorybudget.business.MyInfosForm;
50 import fr.paris.lutece.plugins.participatorybudget.business.vote.Vote;
51 import fr.paris.lutece.plugins.participatorybudget.business.vote.VoteHistoryHome;
52 import fr.paris.lutece.plugins.participatorybudget.business.vote.VoteHome;
53 import fr.paris.lutece.plugins.participatorybudget.service.MyInfosService;
54 import fr.paris.lutece.plugins.participatorybudget.util.ParticipatoryBudgetConstants;
55 import fr.paris.lutece.portal.service.security.LuteceUser;
56 import fr.paris.lutece.portal.service.security.SecurityService;
57 import fr.paris.lutece.portal.service.util.AppLogService;
58 import fr.paris.lutece.portal.service.util.AppPropertiesService;
59
60 public class BudgetRatingService extends RatingService
61 {
62 public static final int VOTE_VALUE = AppPropertiesService.getPropertyInt( "participatorybudget.defaultVoteValue", 0 );
63
64
65
66
67 @Override
68 @Transactional( RatingPlugin.TRANSACTION_MANAGER )
69 public synchronized void doVote( String strIdExtendableResource, String strExtendableResourceType, double dVoteValue, HttpServletRequest request )
70 {
71 super.doVote( strIdExtendableResource, strExtendableResourceType, dVoteValue, request );
72
73 if ( !isBudgetResource( strIdExtendableResource, strExtendableResourceType ) )
74 {
75 return;
76 }
77
78 LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
79 doVote( user, strIdExtendableResource, request );
80 doVoteHistory( user, strIdExtendableResource, request, 1 );
81 }
82
83
84
85
86 @Override
87 @Transactional( RatingPlugin.TRANSACTION_MANAGER )
88 public synchronized void doCancelVote( LuteceUser user, String strIdExtendableResource, String strExtendableResourceType )
89 {
90 super.doCancelVote( user, strIdExtendableResource, strExtendableResourceType );
91
92 if ( !isBudgetResource( strIdExtendableResource, strExtendableResourceType ) )
93 {
94 return;
95 }
96
97 doCancelHistoryVote( user, strIdExtendableResource );
98 doCancelVote( user, strIdExtendableResource );
99 }
100
101
102
103
104
105
106
107
108
109
110
111 private void doVote( LuteceUser user, String strProjectId, HttpServletRequest request )
112 {
113
114 Vote/plugins/participatorybudget/business/vote/Vote.html#Vote">Vote vote = new Vote( );
115 vote.setUserId( user.getName( ) );
116 vote.setProjetId( Integer.parseInt( strProjectId ) );
117 vote.setIpAddress( request.getRemoteAddr( ) );
118
119 String strTheme = request.getParameter( ParticipatoryBudgetConstants.PROJECT_THEME );
120 if ( strTheme == null )
121 {
122 strTheme = (String) request.getAttribute( ParticipatoryBudgetConstants.PROJECT_THEME );
123 }
124 vote.setTheme( strTheme );
125
126 String strTitle = request.getParameter( ParticipatoryBudgetConstants.PROJECT_TITLE );
127 if ( strTitle == null )
128 {
129 strTitle = (String) request.getAttribute( ParticipatoryBudgetConstants.PROJECT_TITLE );
130 }
131 vote.setTitle( strTitle );
132
133 String strLocation = request.getParameter( ParticipatoryBudgetConstants.PROJECT_LOCATION );
134 if ( strLocation == null )
135 {
136 strLocation = (String) request.getAttribute( ParticipatoryBudgetConstants.PROJECT_LOCATION );
137 }
138 vote.setLocation( strLocation );
139
140 MyInfosForm myInfos = MyInfosService.loadUserInfos( user );
141 String strBirthDate = null;
142 String strArrondissement = null;
143 if ( myInfos != null )
144 {
145 strBirthDate = myInfos.getBirthdate( );
146 strArrondissement = myInfos.getArrondissement( );
147 }
148
149 if ( StringUtils.isNotEmpty( strBirthDate ) )
150 {
151 vote.setBirthDate( strBirthDate );
152 try
153 {
154 vote.setAge( MyInfosService.getAge( strBirthDate ) );
155 }
156 catch( ParseException ex )
157 {
158 AppLogService.error( "Error storing vote " + ex.getMessage( ), ex );
159 }
160 }
161
162 if ( StringUtils.isNotEmpty( strArrondissement ) )
163 {
164 try
165 {
166 vote.setArrondissement( Integer.parseInt( strArrondissement ) );
167 }
168 catch( NumberFormatException ex )
169 {
170 AppLogService.error( "Error storing vote " + ex.getMessage( ), ex );
171 }
172
173 }
174 VoteHome.create( vote );
175 }
176
177
178
179
180
181
182
183
184
185 private void doCancelVote( LuteceUser user, String strProjetId )
186 {
187 int nProjetId = 0;
188
189 try
190 {
191 nProjetId = Integer.parseInt( strProjetId );
192 VoteHome.remove( user.getName( ), nProjetId );
193 }
194 catch( NumberFormatException ex )
195 {
196 AppLogService.error( "Error canceling vote userId=" + user.getName( ) + " - projetId=" + nProjetId + " Error : " + ex.getMessage( ), ex );
197 }
198 }
199
200
201
202
203
204
205
206
207
208
209
210
211 private void doVoteHistory( LuteceUser user, String strProjectId, HttpServletRequest request, int status )
212 {
213 Vote/plugins/participatorybudget/business/vote/Vote.html#Vote">Vote vote = new Vote( );
214 vote.setUserId( user.getName( ) );
215 vote.setProjetId( Integer.parseInt( strProjectId ) );
216 vote.setIpAddress( request.getRemoteAddr( ) );
217
218 String strTheme = request.getParameter( ParticipatoryBudgetConstants.PROJECT_THEME );
219 if ( strTheme == null )
220 {
221 strTheme = (String) request.getAttribute( ParticipatoryBudgetConstants.PROJECT_THEME );
222 }
223 vote.setTheme( strTheme );
224
225 String strTitle = request.getParameter( ParticipatoryBudgetConstants.PROJECT_TITLE );
226 if ( strTitle == null )
227 {
228 strTitle = (String) request.getAttribute( ParticipatoryBudgetConstants.PROJECT_TITLE );
229 }
230 vote.setTitle( strTitle );
231
232 String strLocation = request.getParameter( ParticipatoryBudgetConstants.PROJECT_LOCATION );
233 if ( strLocation == null )
234 {
235 strLocation = (String) request.getAttribute( ParticipatoryBudgetConstants.PROJECT_LOCATION );
236 }
237 vote.setLocation( strLocation );
238
239 vote.setStatus( status );
240
241 MyInfosForm myInfos = MyInfosService.loadUserInfos( user );
242 String strBirthDate = null;
243 String strArrondissement = null;
244 if ( myInfos != null )
245 {
246 strBirthDate = myInfos.getBirthdate( );
247 strArrondissement = myInfos.getArrondissement( );
248 }
249
250 if ( StringUtils.isNotEmpty( strBirthDate ) )
251 {
252 vote.setBirthDate( strBirthDate );
253 try
254 {
255 vote.setAge( MyInfosService.getAge( strBirthDate ) );
256 }
257 catch( ParseException ex )
258 {
259 AppLogService.error( "Error storing vote " + ex.getMessage( ), ex );
260 }
261 }
262
263 if ( StringUtils.isNotEmpty( strArrondissement ) )
264 {
265 try
266 {
267 vote.setArrondissement( Integer.parseInt( strArrondissement ) );
268 }
269 catch( NumberFormatException ex )
270 {
271 AppLogService.error( "Error storing vote " + ex.getMessage( ), ex );
272 }
273
274 }
275 VoteHistoryHome.create( vote );
276 }
277
278
279
280
281
282
283
284
285
286 private void doCancelHistoryVote( LuteceUser user, String strProjetId )
287 {
288 int nUserId = 0;
289 int nProjetId = 0;
290
291 try
292 {
293
294 nProjetId = Integer.parseInt( strProjetId );
295 Vote vote = VoteHome.getVote( user.getName( ), nProjetId );
296 Vote vte = VoteHistoryHome.getVoteUser( user.getName( ), nProjetId );
297
298 if ( vote != null && vte != null )
299 {
300 vote.setStatus( -1 );
301 Calendar c = Calendar.getInstance( );
302 Date date = c.getTime( );
303 vote.setDateVote( new Timestamp( date.getTime( ) ) );
304 VoteHistoryHome.create( vote );
305 }
306 }
307 catch( NumberFormatException ex )
308 {
309 AppLogService.error( "Error canceling vote userId=" + nUserId + " - projetId=" + nProjetId + " Error : " + ex.getMessage( ), ex );
310 }
311 }
312
313 public static boolean isBudgetResource( String strIdExtendableResource, String strExtendableResourceType )
314 {
315
316 return Document.PROPERTY_RESOURCE_TYPE.equals( strExtendableResourceType );
317 }
318 }