Skip to content

Commit 9137e6f

Browse files
committed
refactor(CategoryService.getStatisticsOf): change return value to Map.
Prerequisite for #1143
1 parent f1790eb commit 9137e6f

File tree

11 files changed

+96
-16
lines changed

11 files changed

+96
-16
lines changed

src/main/java/ru/mystamps/web/feature/category/CategoryDao.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.util.Date;
2424
import java.util.List;
25+
import java.util.Map;
2526

2627
@SuppressWarnings("PMD.TooManyMethods")
2728
public interface CategoryDao {
@@ -33,7 +34,7 @@ public interface CategoryDao {
3334
long countCategoriesOfCollection(Integer collectionId);
3435
long countAddedSince(Date date);
3536
long countUntranslatedNamesSince(Date date);
36-
List<Object[]> getStatisticsOf(Integer collectionId, String lang);
37+
Map<String, Integer> getStatisticsOf(Integer collectionId, String lang);
3738
List<Integer> findIdsByNames(List<String> names);
3839
List<Integer> findIdsByNamePattern(String pattern);
3940
List<LinkEntityDto> findAllAsLinkEntities(String lang);

src/main/java/ru/mystamps/web/feature/category/CategoryService.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.util.Date;
2424
import java.util.List;
25+
import java.util.Map;
2526

2627
@SuppressWarnings("PMD.TooManyMethods")
2728
public interface CategoryService {
@@ -38,6 +39,6 @@ public interface CategoryService {
3839
long countByNameRu(String name);
3940
long countAddedSince(Date date);
4041
long countUntranslatedNamesSince(Date date);
41-
List<Object[]> getStatisticsOf(Integer collectionId, String lang);
42+
Map<String, Integer> getStatisticsOf(Integer collectionId, String lang);
4243
String suggestCategoryForUser(Integer userId);
4344
}

src/main/java/ru/mystamps/web/feature/category/CategoryServiceImpl.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.Date;
3434
import java.util.List;
3535
import java.util.Locale;
36+
import java.util.Map;
3637
import java.util.stream.Collectors;
3738

3839
@SuppressWarnings("PMD.TooManyMethods")
@@ -193,7 +194,7 @@ public long countUntranslatedNamesSince(Date date) {
193194

194195
@Override
195196
@Transactional(readOnly = true)
196-
public List<Object[]> getStatisticsOf(Integer collectionId, String lang) {
197+
public Map<String, Integer> getStatisticsOf(Integer collectionId, String lang) {
197198
Validate.isTrue(collectionId != null, "Collection id must be non null");
198199

199200
return categoryDao.getStatisticsOf(collectionId, lang);

src/main/java/ru/mystamps/web/feature/category/JdbcCategoryDao.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.commons.lang3.Validate;
2222
import org.springframework.beans.factory.annotation.Value;
2323
import org.springframework.dao.EmptyResultDataAccessException;
24+
import org.springframework.jdbc.core.ResultSetExtractor;
2425
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
2526
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
2627
import org.springframework.jdbc.support.GeneratedKeyHolder;
@@ -29,6 +30,7 @@
2930
import ru.mystamps.web.common.JdbcUtils;
3031
import ru.mystamps.web.common.LinkEntityDto;
3132
import ru.mystamps.web.common.RowMappers;
33+
import ru.mystamps.web.support.spring.jdbc.MapStringIntegerResultSetExtractor;
3234

3335
import java.util.Collections;
3436
import java.util.Date;
@@ -40,6 +42,9 @@
4042
@SuppressWarnings({ "PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods", "PMD.TooManyFields" })
4143
public class JdbcCategoryDao implements CategoryDao {
4244

45+
private static final ResultSetExtractor<Map<String, Integer>> NAME_COUNTER_EXTRACTOR =
46+
new MapStringIntegerResultSetExtractor("name", "counter");
47+
4348
private final NamedParameterJdbcTemplate jdbcTemplate;
4449

4550
@Value("${category.create}")
@@ -178,15 +183,15 @@ public long countUntranslatedNamesSince(Date date) {
178183
}
179184

180185
@Override
181-
public List<Object[]> getStatisticsOf(Integer collectionId, String lang) {
186+
public Map<String, Integer> getStatisticsOf(Integer collectionId, String lang) {
182187
Map<String, Object> params = new HashMap<>();
183188
params.put("collection_id", collectionId);
184189
params.put("lang", lang);
185190

186191
return jdbcTemplate.query(
187192
countStampsByCategoriesSql,
188193
params,
189-
RowMappers::forNameAndCounter
194+
NAME_COUNTER_EXTRACTOR
190195
);
191196
}
192197

src/main/java/ru/mystamps/web/feature/collection/CollectionController.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.io.IOException;
3232
import java.util.List;
3333
import java.util.Locale;
34+
import java.util.Map;
3435

3536
@Controller
3637
@RequiredArgsConstructor
@@ -75,7 +76,8 @@ public String showInfoBySlug(
7576
long seriesCounter = collectionService.countSeriesOf(collectionId);
7677
long stampsCounter = collectionService.countStampsOf(collectionId);
7778

78-
List<Object[]> categoriesStat = categoryService.getStatisticsOf(collectionId, lang);
79+
Map<String, Integer> categoriesStat =
80+
categoryService.getStatisticsOf(collectionId, lang);
7981
List<Object[]> countriesStat = getCountriesStatistics(collectionId, lang);
8082

8183
model.addAttribute("categoryCounter", categoryCounter);

src/main/java/ru/mystamps/web/feature/site/ResourceUrl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public final class ResourceUrl {
3232
public static final String STATIC_RESOURCES_URL = "https://stamps.filezz.ru";
3333

3434
// MUST be updated when any of our resources were modified
35-
public static final String RESOURCES_VERSION = "v0.4.1";
35+
public static final String RESOURCES_VERSION = "v0.4.1.1";
3636

3737
// CheckStyle: ignore LineLength for next 10 lines
3838
private static final String CATALOG_UTILS_JS = "/public/js/" + RESOURCES_VERSION + "/CatalogUtils.min.js";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (C) 2009-2019 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.support.spring.jdbc;
19+
20+
import lombok.RequiredArgsConstructor;
21+
import org.springframework.dao.DataAccessException;
22+
import org.springframework.jdbc.core.ResultSetExtractor;
23+
24+
import java.sql.ResultSet;
25+
import java.sql.SQLException;
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
29+
@RequiredArgsConstructor
30+
public class MapStringIntegerResultSetExtractor
31+
implements ResultSetExtractor<Map<String, Integer>> {
32+
33+
private final String keyFieldName;
34+
private final String valueFieldName;
35+
36+
@Override
37+
public Map<String, Integer> extractData(ResultSet rs)
38+
throws SQLException, DataAccessException {
39+
40+
Map<String, Integer> result = new HashMap<>();
41+
42+
while (rs.next()) {
43+
String key = rs.getString(keyFieldName);
44+
// NOTE: when value is NULL then 0 is returned
45+
Integer value = rs.getInt(valueFieldName);
46+
result.put(key, value);
47+
}
48+
49+
return result;
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* Helpers and utilities to work with JDBC through Spring's JdbcTemplate.
3+
*/
4+
package ru.mystamps.web.support.spring.jdbc;

src/main/javascript/collection/info.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ function createDataTable(stat) {
2424
var table = new google.visualization.DataTable();
2525
table.addColumn('string', 'Category/Country');
2626
table.addColumn('number', 'Quantity of stamps');
27-
table.addRows(stat);
27+
28+
if (Array.isArray(stat)) {
29+
table.addRows(stat);
30+
} else {
31+
// {a: 5} => [a, 5]
32+
Object.keys(stat).forEach(function transformToList(key) {
33+
var value = stat[key];
34+
table.addRow([key, value]);
35+
});
36+
}
2837
return table;
2938
}

src/main/webapp/WEB-INF/views/collection/info.html

+5-5
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,11 @@ <h4 class="panel-title" th:text="#{t_stamps_by_categories}">Stamps by categories
183183
+]*/
184184

185185
/*[- */
186-
initPage([
187-
['Animals', 94],
188-
['Prehistoric animals', 37],
189-
['Sport', 1]
190-
], [
186+
initPage({
187+
'Animals': 94,
188+
'Prehistoric animals': 37,
189+
'Sport': 1
190+
}, [
191191
['Russia', 5],
192192
['USA', 2],
193193
['Australia', 11],

src/test/groovy/ru/mystamps/web/feature/category/CategoryServiceImplTest.groovy

+8-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import spock.lang.Unroll
2929

3030
import static io.qala.datagen.RandomShortApi.nullOr
3131
import static io.qala.datagen.RandomShortApi.nullOrBlank
32+
import static io.qala.datagen.RandomShortApi.positiveInteger
3233
import static io.qala.datagen.RandomValue.between
3334
import static io.qala.datagen.StringModifier.Impls.oneOf
3435

@@ -479,15 +480,19 @@ class CategoryServiceImplTest extends Specification {
479480
ex.message == 'Collection id must be non null'
480481
}
481482

482-
def 'getStatisticsOf() should pass arguments to dao'() {
483+
def 'getStatisticsOf() should invoke dao, pass arguments and return result from dao'() {
483484
given:
484485
Integer expectedCollectionId = Random.id()
485486
and:
486487
String expectedLang = Random.lang()
488+
and:
489+
Map<String, Integer> expectedResult = [ (Random.categoryName()) : positiveInteger() ]
487490
when:
488-
service.getStatisticsOf(expectedCollectionId, expectedLang)
491+
Map<String, Integer> result = service.getStatisticsOf(expectedCollectionId, expectedLang)
489492
then:
490-
1 * categoryDao.getStatisticsOf(expectedCollectionId, expectedLang) >> null
493+
1 * categoryDao.getStatisticsOf(expectedCollectionId, expectedLang) >> expectedResult
494+
and:
495+
result == expectedResult
491496
}
492497

493498
//

0 commit comments

Comments
 (0)