18
18
package ru .mystamps .web .dao .impl ;
19
19
20
20
import java .util .Collections ;
21
+ import java .util .HashMap ;
22
+ import java .util .List ;
23
+ import java .util .Map ;
21
24
22
25
import javax .sql .DataSource ;
23
26
24
27
import org .springframework .beans .factory .annotation .Value ;
28
+ import org .springframework .jdbc .core .RowMapper ;
25
29
import org .springframework .jdbc .core .namedparam .NamedParameterJdbcTemplate ;
26
30
27
31
import ru .mystamps .web .dao .JdbcCategoryDao ;
28
32
29
33
public class JdbcCategoryDaoImpl implements JdbcCategoryDao {
30
34
35
+ private static final RowMapper <Pair <String , Integer >> STRING_INTEGER_PAIR_ROW_MAPPER =
36
+ new StringIntegerPairRowMapper ("name" , "counter" );
37
+
31
38
private final NamedParameterJdbcTemplate jdbcTemplate ;
32
39
33
40
@ Value ("${category.count_categories_of_collection}" )
34
41
private String countCategoriesOfCollectionSql ;
35
42
43
+ @ Value ("${category.count_stamps_by_categories}" )
44
+ private String countStampsByCategoriesSql ;
45
+
36
46
public JdbcCategoryDaoImpl (DataSource dataSource ) {
37
47
jdbcTemplate = new NamedParameterJdbcTemplate (dataSource );
38
48
}
@@ -46,4 +56,25 @@ public long countCategoriesOfCollection(Integer collectionId) {
46
56
);
47
57
}
48
58
59
+ @ Override
60
+ public Map <String , Integer > getStatisticsOf (Integer collectionId , String lang ) {
61
+ Map <String , Object > params = new HashMap <>();
62
+ params .put ("collection_id" , collectionId );
63
+ params .put ("lang" , lang );
64
+
65
+ // TODO: find a better way of extracting results
66
+ List <Pair <String , Integer >> rawResult = jdbcTemplate .query (
67
+ countStampsByCategoriesSql ,
68
+ params ,
69
+ STRING_INTEGER_PAIR_ROW_MAPPER
70
+ );
71
+
72
+ Map <String , Integer > result = new HashMap <>(rawResult .size (), 1.0f );
73
+ for (Pair <String , Integer > pair : rawResult ) {
74
+ result .put (pair .getFirst (), pair .getSecond ());
75
+ }
76
+
77
+ return result ;
78
+ }
79
+
49
80
}
0 commit comments