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.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
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
56
57
58
59
60
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
90
91
92
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
111
112
113
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
134
135
136
137
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
150
151
152
153
154
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
178
179
180
181
182
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 }