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.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       * {@inheritDoc}
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       * {@inheritDoc}
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      * Save the vote
103      * 
104      * @param user
105      *            The User
106      * @param strProjectId
107      *            The project Id
108      * @param strIpAddress
109      *            The user Ip
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      * Cancel a vote
179      * 
180      * @param user
181      *            The User
182      * @param strProjetId
183      *            The project ID
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      * Save the vote
202      * 
203      * @param user
204      *            The User
205      * @param strProjectId
206      *            The project Id
207      * @param strIpAddress
208      *            The user Ip
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      * add a vote in history
280      * 
281      * @param user
282      *            The User
283      * @param strProjetId
284      *            The project ID
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         // We may also check in the document for normal documents vs budget documents ?
316         return Document.PROPERTY_RESOURCE_TYPE.equals( strExtendableResourceType );
317     }
318 }