Skip to content

Commit b3f64d2

Browse files
committed
refactor(CountryService.getStatisticsOf): change return value to Map.
Prerequisite for #1143
1 parent 2423b54 commit b3f64d2

File tree

10 files changed

+50
-46
lines changed

10 files changed

+50
-46
lines changed

src/main/java/ru/mystamps/web/common/RowMappers.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,6 @@ public static LinkEntityDto forLinkEntityDto(ResultSet rs, int unused) throws SQ
2929
return createLinkEntityDto(rs, "id", "slug", "name");
3030
}
3131

32-
public static Object[] forNameAndCounter(ResultSet rs, int unused) throws SQLException {
33-
return new Object[]{
34-
rs.getString("name"),
35-
JdbcUtils.getInteger(rs, "counter")
36-
};
37-
}
38-
3932
public static EntityWithParentDto forEntityWithParentDto(ResultSet rs, int unused)
4033
throws SQLException {
4134

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ public String showInfoBySlug(
7878

7979
Map<String, Integer> categoriesStat =
8080
categoryService.getStatisticsOf(collectionId, lang);
81-
List<Object[]> countriesStat = getCountriesStatistics(collectionId, lang);
81+
82+
Map<String, Integer> countriesStat = getCountriesStatistics(collectionId, lang);
8283

8384
model.addAttribute("categoryCounter", categoryCounter);
8485
model.addAttribute("countryCounter", countryCounter);
@@ -123,16 +124,15 @@ public String showPrices(
123124
return "collection/estimation";
124125
}
125126

126-
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
127-
private List<Object[]> getCountriesStatistics(Integer collectionId, String lang) {
128-
List<Object[]> countriesStat = countryService.getStatisticsOf(collectionId, lang);
127+
private Map<String, Integer> getCountriesStatistics(Integer collectionId, String lang) {
128+
Map<String, Integer> countriesStat = countryService.getStatisticsOf(collectionId, lang);
129129

130-
for (Object[] countryStat : countriesStat) {
131-
// manually localize "Unknown" country's name
132-
Object name = countryStat[0];
133-
if ("Unknown".equals(name)) {
134-
countryStat[0] = messageSource.getMessage("t_unspecified", null, new Locale(lang));
135-
}
130+
// manually localize "Unknown" country's name
131+
Integer unknownCounter = countriesStat.get("Unknown");
132+
if (unknownCounter != null) {
133+
String localizedValue = messageSource.getMessage("t_unspecified", null, new Locale(lang));
134+
countriesStat.put(localizedValue, unknownCounter);
135+
countriesStat.remove("Unknown");
136136
}
137137

138138
return countriesStat;

src/main/java/ru/mystamps/web/feature/country/CountryDao.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.util.Date;
2323
import java.util.List;
24+
import java.util.Map;
2425

2526
@SuppressWarnings("PMD.TooManyMethods")
2627
public interface CountryDao {
@@ -32,7 +33,7 @@ public interface CountryDao {
3233
long countCountriesOfCollection(Integer collectionId);
3334
long countAddedSince(Date date);
3435
long countUntranslatedNamesSince(Date date);
35-
List<Object[]> getStatisticsOf(Integer collectionId, String lang);
36+
Map<String, Integer> getStatisticsOf(Integer collectionId, String lang);
3637
List<Integer> findIdsByNames(List<String> names);
3738
List<Integer> findIdsByNamePattern(String pattern);
3839
List<LinkEntityDto> findAllAsLinkEntities(String lang);

src/main/java/ru/mystamps/web/feature/country/CountryService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.util.Date;
2323
import java.util.List;
24+
import java.util.Map;
2425

2526
@SuppressWarnings("PMD.TooManyMethods")
2627
public interface CountryService {
@@ -36,6 +37,6 @@ public interface CountryService {
3637
long countByNameRu(String name);
3738
long countAddedSince(Date date);
3839
long countUntranslatedNamesSince(Date date);
39-
List<Object[]> getStatisticsOf(Integer collectionId, String lang);
40+
Map<String, Integer> getStatisticsOf(Integer collectionId, String lang);
4041
String suggestCountryForUser(Integer userId);
4142
}

src/main/java/ru/mystamps/web/feature/country/CountryServiceImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Date;
3333
import java.util.List;
3434
import java.util.Locale;
35+
import java.util.Map;
3536
import java.util.stream.Collectors;
3637

3738
@RequiredArgsConstructor
@@ -186,7 +187,7 @@ public long countUntranslatedNamesSince(Date date) {
186187

187188
@Override
188189
@Transactional(readOnly = true)
189-
public List<Object[]> getStatisticsOf(Integer collectionId, String lang) {
190+
public Map<String, Integer> getStatisticsOf(Integer collectionId, String lang) {
190191
Validate.isTrue(collectionId != null, "Collection id must be non null");
191192

192193
return countryDao.getStatisticsOf(collectionId, lang);

src/main/java/ru/mystamps/web/feature/country/JdbcCountryDao.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
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;
2728
import org.springframework.jdbc.support.KeyHolder;
2829
import ru.mystamps.web.common.JdbcUtils;
2930
import ru.mystamps.web.common.LinkEntityDto;
3031
import ru.mystamps.web.common.RowMappers;
32+
import ru.mystamps.web.support.spring.jdbc.MapStringIntegerResultSetExtractor;
3133

3234
import java.util.Collections;
3335
import java.util.Date;
@@ -39,6 +41,9 @@
3941
@SuppressWarnings({ "PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods", "PMD.TooManyFields" })
4042
public class JdbcCountryDao implements CountryDao {
4143

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

4449
@Value("${country.create}")
@@ -181,15 +186,15 @@ public long countUntranslatedNamesSince(Date date) {
181186
}
182187

183188
@Override
184-
public List<Object[]> getStatisticsOf(Integer collectionId, String lang) {
189+
public Map<String, Integer> getStatisticsOf(Integer collectionId, String lang) {
185190
Map<String, Object> params = new HashMap<>();
186191
params.put("collection_id", collectionId);
187192
params.put("lang", lang);
188193

189194
return jdbcTemplate.query(
190195
countStampsByCountriesSql,
191196
params,
192-
RowMappers::forNameAndCounter
197+
NAME_COUNTER_EXTRACTOR
193198
);
194199
}
195200

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

Lines changed: 1 addition & 1 deletion
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.1";
35+
public static final String RESOURCES_VERSION = "v0.4.1.2";
3636

3737
// CheckStyle: ignore LineLength for next 10 lines
3838
private static final String CATALOG_UTILS_JS = "/public/js/" + RESOURCES_VERSION + "/CatalogUtils.min.js";

src/main/javascript/collection/info.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,11 @@ function createDataTable(stat) {
2525
table.addColumn('string', 'Category/Country');
2626
table.addColumn('number', 'Quantity of stamps');
2727

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-
}
28+
// {a: 5} => [a, 5]
29+
Object.keys(stat).forEach(function transformToList(key) {
30+
var value = stat[key];
31+
table.addRow([key, value]);
32+
});
33+
3734
return table;
3835
}

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,18 @@ <h4 class="panel-title" th:text="#{t_stamps_by_categories}">Stamps by categories
187187
'Animals': 94,
188188
'Prehistoric animals': 37,
189189
'Sport': 1
190-
}, [
191-
['Russia', 5],
192-
['USA', 2],
193-
['Australia', 11],
194-
['Canada', 3],
195-
['Sahrawi Arab Democratic Republic', 2],
196-
['China', 1],
197-
['Congo', 20],
198-
['Brazil', 4],
199-
['Korea', 2]
200-
]);
190+
}, {
191+
'Russia': 5,
192+
'USA': 2,
193+
'Australia': 11,
194+
'Canada': 3,
195+
'Sahrawi Arab Democratic Republic': 2,
196+
'China': 1,
197+
'Congo': 20,
198+
'Brazil': 4,
199+
'Korea': 2
200+
}
201+
);
201202
/* -]*/
202203
});
203204
</script>

src/test/groovy/ru/mystamps/web/feature/country/CountryServiceImplTest.groovy

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import spock.lang.Unroll
2828

2929
import static io.qala.datagen.RandomShortApi.nullOr
3030
import static io.qala.datagen.RandomShortApi.nullOrBlank
31+
import static io.qala.datagen.RandomShortApi.positiveInteger
3132
import static io.qala.datagen.RandomValue.between
3233
import static io.qala.datagen.StringModifier.Impls.oneOf
3334

@@ -462,15 +463,19 @@ class CountryServiceImplTest extends Specification {
462463
ex.message == 'Collection id must be non null'
463464
}
464465

465-
def 'getStatisticsOf() should pass arguments to dao'() {
466+
def 'getStatisticsOf() should invoke dao, pass arguments and return result from dao'() {
466467
given:
467468
Integer expectedCollectionId = Random.id()
468469
and:
469470
String expectedLang = Random.lang()
471+
and:
472+
Map<String, Integer> expectedResult = [ (Random.countryName()):positiveInteger() ]
470473
when:
471-
service.getStatisticsOf(expectedCollectionId, expectedLang)
474+
Map<String, Integer> result = service.getStatisticsOf(expectedCollectionId, expectedLang)
472475
then:
473-
1 * countryDao.getStatisticsOf(expectedCollectionId, expectedLang) >> null
476+
1 * countryDao.getStatisticsOf(expectedCollectionId, expectedLang) >> expectedResult
477+
and:
478+
result == expectedResult
474479
}
475480

476481
//

0 commit comments

Comments
 (0)