Skip to content

Commit 4da78f7

Browse files
committed
Show charts on collection page.
Fix GH #49
1 parent 221b8e5 commit 4da78f7

18 files changed

+336
-3
lines changed

src/main/java/ru/mystamps/web/config/ControllersConfig.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import javax.inject.Inject;
2121

22+
import org.springframework.context.MessageSource;
2223
import org.springframework.context.annotation.Bean;
2324
import org.springframework.context.annotation.Configuration;
2425

@@ -30,6 +31,9 @@ public class ControllersConfig {
3031
@Inject
3132
private ServicesConfig servicesConfig;
3233

34+
@Inject
35+
private MessageSource messageSource;
36+
3337
@Bean
3438
public AccountController getAccountController() {
3539
return new AccountController(servicesConfig.getUserService());
@@ -56,7 +60,9 @@ public CollectionController getCollectionController() {
5660
return new CollectionController(
5761
servicesConfig.getCategoryService(),
5862
servicesConfig.getCountryService(),
59-
servicesConfig.getSeriesService());
63+
servicesConfig.getSeriesService(),
64+
messageSource
65+
);
6066
}
6167

6268
@Bean

src/main/java/ru/mystamps/web/controller/CollectionController.java

+26-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
package ru.mystamps.web.controller;
1919

2020
import java.util.Locale;
21+
import java.util.Map;
2122

23+
import org.springframework.context.MessageSource;
2224
import org.springframework.stereotype.Controller;
2325
import org.springframework.ui.Model;
2426
import org.springframework.web.bind.annotation.PathVariable;
@@ -42,6 +44,7 @@ public class CollectionController {
4244
private final CategoryService categoryService;
4345
private final CountryService countryService;
4446
private final SeriesService seriesService;
47+
private final MessageSource messageSource;
4548

4649
@RequestMapping(value = Url.INFO_COLLECTION_PAGE, method = RequestMethod.GET)
4750
public String showInfo(
@@ -64,9 +67,31 @@ public String showInfo(
6467
model.addAttribute("countryCounter", countryService.countCountriesOf(collection));
6568
model.addAttribute("seriesCounter", seriesService.countSeriesOf(collection));
6669
model.addAttribute("stampsCounter", seriesService.countStampsOf(collection));
70+
71+
model.addAttribute(
72+
"statOfCollectionByCategories",
73+
categoryService.getStatisticsOf(collection, lang)
74+
);
75+
model.addAttribute(
76+
"statOfCollectionByCountries",
77+
getCountriesStatistics(collection, lang)
78+
);
6779
}
6880

6981
return "collection/info";
7082
}
71-
83+
84+
private Map<String, Integer> getCountriesStatistics(Collection collection, String lang) {
85+
Map<String, Integer> countriesStat = countryService.getStatisticsOf(collection, lang);
86+
87+
// manually localize "Unknown" country's name
88+
if (countriesStat.containsKey("Unknown")) {
89+
String message = messageSource.getMessage("t_unspecified", null, new Locale(lang));
90+
countriesStat.put(message, countriesStat.get("Unknown"));
91+
countriesStat.remove("Unknown");
92+
}
93+
94+
return countriesStat;
95+
}
96+
7297
}

src/main/java/ru/mystamps/web/dao/JdbcCategoryDao.java

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
*/
1818
package ru.mystamps.web.dao;
1919

20+
import java.util.Map;
21+
2022
public interface JdbcCategoryDao {
2123
long countCategoriesOfCollection(Integer collectionId);
24+
Map<String, Integer> getStatisticsOf(Integer collectionId, String lang);
2225
}

src/main/java/ru/mystamps/web/dao/JdbcCountryDao.java

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
*/
1818
package ru.mystamps.web.dao;
1919

20+
import java.util.Map;
21+
2022
public interface JdbcCountryDao {
2123
long countCountriesOfCollection(Integer collectionId);
24+
Map<String, Integer> getStatisticsOf(Integer collectionId, String lang);
2225
}

src/main/java/ru/mystamps/web/dao/impl/JdbcCategoryDaoImpl.java

+31
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,31 @@
1818
package ru.mystamps.web.dao.impl;
1919

2020
import java.util.Collections;
21+
import java.util.HashMap;
22+
import java.util.List;
23+
import java.util.Map;
2124

2225
import javax.sql.DataSource;
2326

2427
import org.springframework.beans.factory.annotation.Value;
28+
import org.springframework.jdbc.core.RowMapper;
2529
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
2630

2731
import ru.mystamps.web.dao.JdbcCategoryDao;
2832

2933
public class JdbcCategoryDaoImpl implements JdbcCategoryDao {
3034

35+
private static final RowMapper<Pair<String, Integer>> STRING_INTEGER_PAIR_ROW_MAPPER =
36+
new StringIntegerPairRowMapper("name", "counter");
37+
3138
private final NamedParameterJdbcTemplate jdbcTemplate;
3239

3340
@Value("${category.count_categories_of_collection}")
3441
private String countCategoriesOfCollectionSql;
3542

43+
@Value("${category.count_stamps_by_categories}")
44+
private String countStampsByCategoriesSql;
45+
3646
public JdbcCategoryDaoImpl(DataSource dataSource) {
3747
jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
3848
}
@@ -46,4 +56,25 @@ public long countCategoriesOfCollection(Integer collectionId) {
4656
);
4757
}
4858

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+
4980
}

src/main/java/ru/mystamps/web/dao/impl/JdbcCountryDaoImpl.java

+31
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,31 @@
1818
package ru.mystamps.web.dao.impl;
1919

2020
import java.util.Collections;
21+
import java.util.HashMap;
22+
import java.util.List;
23+
import java.util.Map;
2124

2225
import javax.sql.DataSource;
2326

2427
import org.springframework.beans.factory.annotation.Value;
28+
import org.springframework.jdbc.core.RowMapper;
2529
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
2630

2731
import ru.mystamps.web.dao.JdbcCountryDao;
2832

2933
public class JdbcCountryDaoImpl implements JdbcCountryDao {
3034

35+
private static final RowMapper<Pair<String, Integer>> STRING_INTEGER_PAIR_ROW_MAPPER =
36+
new StringIntegerPairRowMapper("name", "counter");
37+
3138
private final NamedParameterJdbcTemplate jdbcTemplate;
3239

3340
@Value("${country.count_countries_of_collection}")
3441
private String countCountriesOfCollectionSql;
3542

43+
@Value("${country.count_stamps_by_countries}")
44+
private String countStampsByCountriesSql;
45+
3646
public JdbcCountryDaoImpl(DataSource dataSource) {
3747
jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
3848
}
@@ -46,4 +56,25 @@ public long countCountriesOfCollection(Integer collectionId) {
4656
);
4757
}
4858

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+
countStampsByCountriesSql,
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+
4980
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (C) 2009-2014 Slava Semushin <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.dao.impl;
19+
20+
import lombok.Getter;
21+
import lombok.RequiredArgsConstructor;
22+
23+
@Getter
24+
@RequiredArgsConstructor
25+
public class Pair<F, S> {
26+
private final F first;
27+
private final S second;
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (C) 2009-2014 Slava Semushin <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.dao.impl;
19+
20+
import java.sql.ResultSet;
21+
import java.sql.SQLException;
22+
23+
import org.springframework.jdbc.core.RowMapper;
24+
25+
import lombok.RequiredArgsConstructor;
26+
27+
@RequiredArgsConstructor
28+
class StringIntegerPairRowMapper implements RowMapper<Pair<String, Integer>> {
29+
30+
private final String firstFieldName;
31+
private final String secondFieldName;
32+
33+
@Override
34+
public Pair<String, Integer> mapRow(ResultSet resultSet, int i) throws SQLException {
35+
String firstValue = resultSet.getString(firstFieldName);
36+
Integer secondValue = JdbcUtils.getInteger(resultSet, secondFieldName);
37+
38+
return new Pair<>(firstValue, secondValue);
39+
}
40+
41+
}

src/main/java/ru/mystamps/web/service/CategoryService.java

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
package ru.mystamps.web.service;
1919

20+
import java.util.Map;
21+
2022
import ru.mystamps.web.entity.Category;
2123
import ru.mystamps.web.entity.Collection;
2224
import ru.mystamps.web.entity.User;
@@ -30,4 +32,5 @@ public interface CategoryService {
3032
long countCategoriesOf(Collection collection);
3133
int countByName(String name);
3234
int countByNameRu(String name);
35+
Map<String, Integer> getStatisticsOf(Collection collection, String lang);
3336
}

src/main/java/ru/mystamps/web/service/CategoryServiceImpl.java

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package ru.mystamps.web.service;
1919

2020
import java.util.Date;
21+
import java.util.Map;
2122

2223
import org.springframework.security.access.prepost.PreAuthorize;
2324
import org.springframework.transaction.annotation.Transactional;
@@ -110,4 +111,13 @@ public int countByNameRu(String name) {
110111
return categoryDao.countByNameRu(name);
111112
}
112113

114+
@Override
115+
@Transactional(readOnly = true)
116+
public Map<String, Integer> getStatisticsOf(Collection collection, String lang) {
117+
Validate.isTrue(collection != null, "Collection must be non null");
118+
Validate.isTrue(collection.getId() != null, "Collection id must be non null");
119+
120+
return jdbcCategoryDao.getStatisticsOf(collection.getId(), lang);
121+
}
122+
113123
}

src/main/java/ru/mystamps/web/service/CountryService.java

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
package ru.mystamps.web.service;
1919

20+
import java.util.Map;
21+
2022
import ru.mystamps.web.entity.Collection;
2123
import ru.mystamps.web.entity.Country;
2224
import ru.mystamps.web.entity.User;
@@ -30,4 +32,5 @@ public interface CountryService {
3032
long countCountriesOf(Collection collection);
3133
int countByName(String name);
3234
int countByNameRu(String name);
35+
Map<String, Integer> getStatisticsOf(Collection collection, String lang);
3336
}

src/main/java/ru/mystamps/web/service/CountryServiceImpl.java

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package ru.mystamps.web.service;
1919

2020
import java.util.Date;
21+
import java.util.Map;
2122

2223
import org.springframework.security.access.prepost.PreAuthorize;
2324
import org.springframework.transaction.annotation.Transactional;
@@ -110,4 +111,13 @@ public int countByNameRu(String name) {
110111
return countryDao.countByNameRu(name);
111112
}
112113

114+
@Override
115+
@Transactional(readOnly = true)
116+
public Map<String, Integer> getStatisticsOf(Collection collection, String lang) {
117+
Validate.isTrue(collection != null, "Collection must be non null");
118+
Validate.isTrue(collection.getId() != null, "Collection id must be non null");
119+
120+
return jdbcCountryDao.getStatisticsOf(collection.getId(), lang);
121+
}
122+
113123
}

src/main/java/ru/mystamps/web/support/togglz/Features.java

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public enum Features implements Feature {
3636
@EnabledByDefault
3737
SHOW_COLLECTION_STATISTICS,
3838

39+
@Label("Show charts on collection page")
40+
@EnabledByDefault
41+
SHOW_COLLECTION_CHARTS,
42+
3943
@Label("Possibility to user to add series to collection")
4044
@EnabledByDefault
4145
ADD_SERIES_TO_COLLECTION;

src/main/resources/ru/mystamps/i18n/Messages.properties

+3
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,6 @@ t_collection_just_added = Series has been added to your collection
117117
t_collection_just_removed = Series has been removed from your collection
118118
t_empty_collection = In this collection is no stamps
119119
t_in_collection = In this collection
120+
t_stamps_by_categories = Stamps by categories
121+
t_stamps_by_countries = Stamps by countries
122+
t_unspecified = Unspecified

src/main/resources/ru/mystamps/i18n/Messages_ru.properties

+3
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,6 @@ t_collection_just_added = Серия добавлена в вашу коллек
117117
t_collection_just_removed = Серия удалена из вашей коллекции
118118
t_empty_collection = В этой коллекции еще нет марок
119119
t_in_collection = В этой коллекции
120+
t_stamps_by_categories = Марки по категориям
121+
t_stamps_by_countries = Марки по странам
122+
t_unspecified = Не указана

0 commit comments

Comments
 (0)