View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de 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.rating.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  
40  /**
41   *
42   * class ResourceStatDAO
43   *
44   */
45  public class ResourceStatDAO implements IResourceStatDAO
46  {
47      private static final String SQL_QUERY_SELECT = "SELECT resource_type, id_resource, vote_count, score_value, download_count, view_count FROM rating_resource_stat WHERE resource_type=? AND id_resource=?";
48      private static final String SQL_QUERY_INSERT = "INSERT INTO rating_resource_stat (resource_type, id_resource, vote_count, score_value, download_count, view_count )VALUES(?,?,?,?,?,?)";
49      private static final String SQL_QUERY_UPDATE = "UPDATE rating_resource_stat SET resource_type=?,id_resource=?, vote_count=?, score_value=?, download_count=?, view_count=? WHERE resource_type=? AND id_resource=?";
50      private static final String SQL_QUERY_DELETE = "DELETE FROM rating_resource_stat WHERE resource_type=? AND id_resource=?";
51      private static final String SQL_QUERY_GET_DOWNLOAD_COUNT = "SELECT download_count FROM rating_resource_stat WHERE resource_type=? AND id_resource=?";
52      private static final String SQL_QUERY_GET_SCORE = "SELECT vote_count, score_value FROM rating_resource_stat WHERE resource_type=? AND id_resource=?";
53  
54      /**
55       * Load the data of the resourceStat from the table
56       *
57       * @param strResourceType the type of the resource
58       * @param nIdResource the id of the resource
59       * @param plugin the Plugin
60       * @return an instance of ResourceStat
61       */
62      public ResourceStat load( String strResourceType, int nIdResource, Plugin plugin )
63      {
64          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
65          daoUtil.setString( 1, strResourceType );
66          daoUtil.setInt( 2, nIdResource );
67  
68          daoUtil.executeQuery(  );
69  
70          ResourceStat resourceStat = null;
71  
72          if ( daoUtil.next(  ) )
73          {
74              resourceStat = new ResourceStat(  );
75              resourceStat.setResourceType( daoUtil.getString( 1 ) );
76              resourceStat.setIdResource( daoUtil.getInt( 2 ) );
77              resourceStat.setVoteCount( daoUtil.getInt( 3 ) );
78              resourceStat.setScoreValue( daoUtil.getInt( 4 ) );
79              resourceStat.setDownloadCount( daoUtil.getInt( 5 ) );
80              resourceStat.setViewCount( daoUtil.getInt( 6 ) );
81          }
82  
83          daoUtil.free(  );
84  
85          return resourceStat;
86      }
87  
88      /**
89      * Insert a new record in the table.
90      *
91      * @param resourceStat instance of the ResourceStat object to insert
92      * @param plugin the plugin
93      */
94      public void insert( ResourceStat resourceStat, Plugin plugin )
95      {
96          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
97  
98          daoUtil.setString( 1, resourceStat.getResourceType(  ) );
99          daoUtil.setInt( 2, resourceStat.getIdResource(  ) );
100         daoUtil.setInt( 3, resourceStat.getVoteCount(  ) );
101         daoUtil.setInt( 4, resourceStat.getScoreValue(  ) );
102         daoUtil.setInt( 5, resourceStat.getDownloadCount(  ) );
103         daoUtil.setInt( 6, resourceStat.getViewCount(  ) );
104 
105         daoUtil.executeUpdate(  );
106         daoUtil.free(  );
107     }
108 
109     /**
110      * update record in the table.
111      *
112      * @param resourceStat instance of the ResourceStat object to update
113      * @param plugin the plugin
114      */
115     public void store( ResourceStat resourceStat, Plugin plugin )
116     {
117         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
118 
119         daoUtil.setString( 1, resourceStat.getResourceType(  ) );
120         daoUtil.setInt( 2, resourceStat.getIdResource(  ) );
121         daoUtil.setInt( 3, resourceStat.getVoteCount(  ) );
122         daoUtil.setInt( 4, resourceStat.getScoreValue(  ) );
123         daoUtil.setInt( 5, resourceStat.getDownloadCount(  ) );
124         daoUtil.setInt( 6, resourceStat.getViewCount(  ) );
125         daoUtil.setString( 7, resourceStat.getResourceType(  ) );
126         daoUtil.setInt( 8, resourceStat.getIdResource(  ) );
127 
128         daoUtil.executeUpdate(  );
129         daoUtil.free(  );
130     }
131 
132     /**
133      * Remove the ResourceStat whose identifier are specified in parameters
134      *
135      * @param strResourceType The type of the resource
136      * @param nIdResource  the id of the resource
137      * @param plugin the Plugin
138      */
139     public void delete( String strResourceType, int nIdResource, Plugin plugin )
140     {
141         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
142         daoUtil.setString( 1, strResourceType );
143         daoUtil.setInt( 2, nIdResource );
144         daoUtil.executeUpdate(  );
145         daoUtil.free(  );
146     }
147 
148     /**
149          * Returns the count of resource download whose resource identifier and resource type are specified in parameters
150          *
151          * @param strResourceType The type of the resource
152          * @param nIdResource the id of the resource
153          * @param plugin the Plugin
154          * @return the count of resource download
155          */
156     public int getDownloadCountByResource( String strResourceType, int nIdResource, Plugin plugin )
157     {
158         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_GET_DOWNLOAD_COUNT, plugin );
159         daoUtil.setString( 1, strResourceType );
160         daoUtil.setInt( 2, nIdResource );
161 
162         daoUtil.executeQuery(  );
163 
164         int nDocumentDownlodCount = 0;
165 
166         if ( daoUtil.next(  ) )
167         {
168             nDocumentDownlodCount = daoUtil.getInt( 1 );
169         }
170 
171         daoUtil.free(  );
172 
173         return nDocumentDownlodCount;
174     }
175 
176     /**
177      * Returns the score of a resource whose resource identifier and resource type are specified in parameters
178      *
179      * @param strResourceType The type of the resource
180      * @param nIdResource the id of the resource
181      * @param plugin the Plugin
182      * @return the score
183      */
184     public int getScore( String strResourceType, int nIdResource, Plugin plugin )
185     {
186         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_GET_SCORE, plugin );
187 
188         daoUtil.setString( 1, strResourceType );
189         daoUtil.setInt( 2, nIdResource );
190 
191         daoUtil.executeQuery(  );
192 
193         int nScore = 0;
194 
195         if ( daoUtil.next(  ) )
196         {
197             int nVoteCount = daoUtil.getInt( 1 );
198             int nScoreValue = daoUtil.getInt( 2 );
199 
200             if ( ( nVoteCount != 0 ) )
201             {
202                 nScore = Math.round( (float) nScoreValue / (float) nVoteCount );
203                 nScore = nScore + 2;
204             }
205         }
206 
207         daoUtil.free(  );
208 
209         return nScore;
210     }
211 }