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.HashMap;
38  import java.util.List;
39  import java.util.Map;
40  
41  import fr.paris.lutece.plugins.document.business.Document;
42  import fr.paris.lutece.plugins.document.business.DocumentHome;
43  import fr.paris.lutece.plugins.participatorybudget.business.vote.VoteHome;
44  import fr.paris.lutece.portal.service.spring.SpringContextService;
45  
46  public class VoteStatService
47  {
48  
49      // *********************************************************************************************
50      // * SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON *
51      // * SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON *
52      // *********************************************************************************************
53  
54      private static final String BEAN_VOTE_STAT = "participatorybudget.voteStatService";
55  
56      private static VoteStatService _singleton;
57  
58      public static VoteStatService getInstance( )
59      {
60          if ( _singleton == null )
61          {
62              _singleton = SpringContextService.getBean( BEAN_VOTE_STAT );
63          }
64          return _singleton;
65      }
66  
67      // *********************************************************************************************
68      // * STATS STATS STATS STATS STATS STATS STATS STATS STATS STATS STATS STATS STATS STATS STATS *
69      // * STATS STATS STATS STATS STATS STATS STATS STATS STATS STATS STATS STATS STATS STATS STATS *
70      // *********************************************************************************************
71  
72      /**
73       * Return number of votes by campaign
74       * 
75       * @return A map with the campaign code as key and number of votes as value
76       */
77      public Map<String, Integer> getNbVotesByCampaign( )
78      {
79          return VoteHome.countNbVotesByCampaignCode( );
80      }
81  
82      /**
83       * Return number of votes by campaign then by date
84       * 
85       * @return A map with the campaign code as key and a map as value containing the date as key and the number of votes as value
86       */
87      public Map<String, Map<String, Integer>> getNbVotesByDateAllCampaigns( )
88      {
89          return VoteHome.countNbVotesByDateAllCampaigns( );
90      }
91  
92      /**
93       * Return number of votes by theme for a campaign
94       * 
95       * @param campaignId
96       *            The id of the campaign
97       * @return A map with the theme as key and and the number of votes as value
98       */
99      public Map<String, Integer> getNbVotesByTheme( int campaignId )
100     {
101         return VoteHome.countNbVotesByTheme( campaignId );
102     }
103 
104     /**
105      * Return number of votes by location for a campaign
106      * 
107      * @param campaignId
108      *            The id of the campaign
109      * @return A map with the location as key and and the number of votes as value
110      */
111     public Map<String, Integer> getNbVotesByLocation( int campaignId )
112     {
113         return VoteHome.countNbVotesByLocation( campaignId );
114     }
115 
116     /**
117      * Return number of votes by project for a campaign
118      * 
119      * @param campaignId
120      *            The id of the campaign
121      * @return A list containing, for each project, a map of key/values
122      */
123     public List<Map<String, String>> getNbVotesByProject( int campaignId )
124     {
125         List<Map<String, String>> result = new ArrayList<>( );
126 
127         Map<Integer, Integer> votes = VoteHome.countNbVotesByProjectId( campaignId );
128         for ( int documentId : votes.keySet( ) )
129         {
130             Document project = DocumentHome.findByPrimaryKey( documentId );
131 
132             Map<String, String> values = new HashMap<>( );
133             values.put( "ref", "" + project.getId( ) );
134             values.put( "title", project.getTitle( ) );
135             values.put( "location", project.getAttribute( "location" ).getTextValue( ) );
136             values.put( "theme", project.getAttribute( "theme" ).getTextValue( ) );
137             values.put( "nb_votes", "" + votes.get( documentId ) );
138 
139             result.add( values );
140         }
141 
142         return result;
143     }
144 
145 }