Skip to content

Commit 0298f41

Browse files
committed
RowMappers: move all RowMapper to separate class.
No functional changes.
1 parent 93ecb1b commit 0298f41

File tree

6 files changed

+87
-50
lines changed

6 files changed

+87
-50
lines changed

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Map;
2525

2626
import org.springframework.beans.factory.annotation.Value;
27-
import org.springframework.jdbc.core.RowMapper;
2827
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
2928

3029
import lombok.RequiredArgsConstructor;
@@ -33,19 +32,9 @@
3332
import ru.mystamps.web.service.dto.LinkEntityDto;
3433
import ru.mystamps.web.service.dto.SelectEntityDto;
3534

36-
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
3735
@RequiredArgsConstructor
3836
public class JdbcCategoryDaoImpl implements JdbcCategoryDao {
3937

40-
private static final RowMapper<LinkEntityDto> LINK_ENTITY_DTO_ROW_MAPPER =
41-
new LinkEntityDtoRowMapper();
42-
43-
private static final RowMapper<Pair<String, Integer>> NAME_AND_COUNTER_ROW_MAPPER =
44-
new StringIntegerPairRowMapper("name", "counter");
45-
46-
private static final RowMapper<Pair<String, Integer>> NAME_AND_ID_ROW_MAPPER =
47-
new StringIntegerPairRowMapper("name", "id");
48-
4938
private final NamedParameterJdbcTemplate jdbcTemplate;
5039

5140
@Value("${category.count_all_categories}")
@@ -115,7 +104,7 @@ public Map<String, Integer> getStatisticsOf(Integer collectionId, String lang) {
115104
List<Pair<String, Integer>> rawResult = jdbcTemplate.query(
116105
countStampsByCategoriesSql,
117106
params,
118-
NAME_AND_COUNTER_ROW_MAPPER
107+
RowMappers.forNameAndCounter()
119108
);
120109

121110
Map<String, Integer> result = new HashMap<>(rawResult.size(), 1.0f);
@@ -133,7 +122,7 @@ public Iterable<SelectEntityDto> findAllAsSelectEntries(String lang) {
133122
List<Pair<String, Integer>> rawResult = jdbcTemplate.query(
134123
findCategoriesNamesWithIdsSql,
135124
Collections.singletonMap("lang", lang),
136-
NAME_AND_ID_ROW_MAPPER
125+
RowMappers.forNameAndId()
137126
);
138127

139128
List<SelectEntityDto> result = new LinkedList<>();
@@ -149,7 +138,7 @@ public Iterable<LinkEntityDto> findAllAsLinkEntities(String lang) {
149138
return jdbcTemplate.query(
150139
findCategoriesNamesWithSlugSql,
151140
Collections.singletonMap("lang", lang),
152-
LINK_ENTITY_DTO_ROW_MAPPER
141+
RowMappers.forLinkEntityDto()
153142
);
154143
}
155144

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.apache.commons.lang3.Validate;
2525

2626
import org.springframework.beans.factory.annotation.Value;
27-
import org.springframework.jdbc.core.RowMapper;
2827
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
2928

3029
import lombok.RequiredArgsConstructor;
@@ -36,9 +35,6 @@
3635
@RequiredArgsConstructor
3736
public class JdbcCollectionDaoImpl implements JdbcCollectionDao {
3837

39-
private static final RowMapper<LinkEntityDto> LINK_ENTITY_DTO_ROW_MAPPER =
40-
new LinkEntityDtoRowMapper();
41-
4238
private final NamedParameterJdbcTemplate jdbcTemplate;
4339

4440
@Value("${collection.find_last_created}")
@@ -55,7 +51,7 @@ public Iterable<LinkEntityDto> findLastCreated(int quantity) {
5551
return jdbcTemplate.query(
5652
findLastCreatedCollectionsSql,
5753
Collections.singletonMap("quantity", quantity),
58-
LINK_ENTITY_DTO_ROW_MAPPER
54+
RowMappers.forLinkEntityDto()
5955
);
6056
}
6157

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Map;
2525

2626
import org.springframework.beans.factory.annotation.Value;
27-
import org.springframework.jdbc.core.RowMapper;
2827
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
2928

3029
import lombok.RequiredArgsConstructor;
@@ -33,19 +32,9 @@
3332
import ru.mystamps.web.service.dto.LinkEntityDto;
3433
import ru.mystamps.web.service.dto.SelectEntityDto;
3534

36-
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
3735
@RequiredArgsConstructor
3836
public class JdbcCountryDaoImpl implements JdbcCountryDao {
3937

40-
private static final RowMapper<LinkEntityDto> LINK_ENTITY_DTO_ROW_MAPPER =
41-
new LinkEntityDtoRowMapper();
42-
43-
private static final RowMapper<Pair<String, Integer>> NAME_AND_COUNTER_ROW_MAPPER =
44-
new StringIntegerPairRowMapper("name", "counter");
45-
46-
private static final RowMapper<Pair<String, Integer>> NAME_AND_ID_ROW_MAPPER =
47-
new StringIntegerPairRowMapper("name", "id");
48-
4938
private final NamedParameterJdbcTemplate jdbcTemplate;
5039

5140
@Value("${country.count_all_countries}")
@@ -115,7 +104,7 @@ public Map<String, Integer> getStatisticsOf(Integer collectionId, String lang) {
115104
List<Pair<String, Integer>> rawResult = jdbcTemplate.query(
116105
countStampsByCountriesSql,
117106
params,
118-
NAME_AND_COUNTER_ROW_MAPPER
107+
RowMappers.forNameAndCounter()
119108
);
120109

121110
Map<String, Integer> result = new HashMap<>(rawResult.size(), 1.0f);
@@ -133,7 +122,7 @@ public Iterable<SelectEntityDto> findAllAsSelectEntries(String lang) {
133122
List<Pair<String, Integer>> rawResult = jdbcTemplate.query(
134123
findCountriesNamesWithIdsSql,
135124
Collections.singletonMap("lang", lang),
136-
NAME_AND_ID_ROW_MAPPER
125+
RowMappers.forNameAndId()
137126
);
138127

139128
List<SelectEntityDto> result = new LinkedList<>();
@@ -149,7 +138,7 @@ public Iterable<LinkEntityDto> findAllAsLinkEntities(String lang) {
149138
return jdbcTemplate.query(
150139
findCountriesNamesWithSlugSql,
151140
Collections.singletonMap("lang", lang),
152-
LINK_ENTITY_DTO_ROW_MAPPER
141+
RowMappers.forLinkEntityDto()
153142
);
154143
}
155144

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.Optional;
2424

2525
import org.springframework.beans.factory.annotation.Value;
26-
import org.springframework.jdbc.core.RowMapper;
2726
import org.springframework.dao.EmptyResultDataAccessException;
2827
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
2928

@@ -38,12 +37,6 @@
3837
@RequiredArgsConstructor
3938
public class JdbcSeriesDaoImpl implements JdbcSeriesDao {
4039

41-
private static final RowMapper<SitemapInfoDto> SITEMAP_INFO_DTO_ROW_MAPPER =
42-
new SitemapInfoDtoRowMapper();
43-
44-
private static final RowMapper<SeriesInfoDto> SERIES_INFO_DTO_ROW_MAPPER =
45-
new SeriesInfoDtoRowMapper();
46-
4740
private final NamedParameterJdbcTemplate jdbcTemplate;
4841

4942
@Value("${series.find_all_for_sitemap}")
@@ -102,7 +95,7 @@ public Iterable<SitemapInfoDto> findAllForSitemap() {
10295
return jdbcTemplate.query(
10396
findAllForSitemapSql,
10497
Collections.<String, Object>emptyMap(),
105-
SITEMAP_INFO_DTO_ROW_MAPPER
98+
RowMappers.forSitemapInfoDto()
10699
);
107100
}
108101

@@ -112,7 +105,7 @@ public Iterable<SeriesInfoDto> findLastAdded(int quantity, String lang) {
112105
params.put("quantity", quantity);
113106
params.put("lang", lang);
114107

115-
return jdbcTemplate.query(findLastAddedSeriesSql, params, SERIES_INFO_DTO_ROW_MAPPER);
108+
return jdbcTemplate.query(findLastAddedSeriesSql, params, RowMappers.forSeriesInfoDto());
116109
}
117110

118111
@Override
@@ -121,7 +114,7 @@ public Iterable<SeriesInfoDto> findByCategoryIdAsSeriesInfo(Integer categoryId,
121114
params.put("category_id", categoryId);
122115
params.put("lang", lang);
123116

124-
return jdbcTemplate.query(findByCategoryIdSql, params, SERIES_INFO_DTO_ROW_MAPPER);
117+
return jdbcTemplate.query(findByCategoryIdSql, params, RowMappers.forSeriesInfoDto());
125118
}
126119

127120
@Override
@@ -130,7 +123,7 @@ public Iterable<SeriesInfoDto> findByCountryIdAsSeriesInfo(Integer countryId, St
130123
params.put("country_id", countryId);
131124
params.put("lang", lang);
132125

133-
return jdbcTemplate.query(findByCountryIdSql, params, SERIES_INFO_DTO_ROW_MAPPER);
126+
return jdbcTemplate.query(findByCountryIdSql, params, RowMappers.forSeriesInfoDto());
134127
}
135128

136129
@Override
@@ -140,7 +133,7 @@ public Iterable<SeriesInfoDto> findByCollectionIdAsSeriesInfo(Integer collection
140133
params.put("collection_id", collectionId);
141134
params.put("lang", lang);
142135

143-
return jdbcTemplate.query(findByCollectionIdSql, params, SERIES_INFO_DTO_ROW_MAPPER);
136+
return jdbcTemplate.query(findByCollectionIdSql, params, RowMappers.forSeriesInfoDto());
144137
}
145138

146139
@Override

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.apache.commons.lang3.Validate;
2525

2626
import org.springframework.beans.factory.annotation.Value;
27-
import org.springframework.jdbc.core.RowMapper;
2827
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
2928

3029
import lombok.RequiredArgsConstructor;
@@ -36,9 +35,6 @@
3635
@RequiredArgsConstructor
3736
public class JdbcUsersActivationDaoImpl implements JdbcUsersActivationDao {
3837

39-
private static final RowMapper<UsersActivation> USERS_ACTIVATION_ROW_MAPPER =
40-
new UsersActivationRowMapper();
41-
4238
private final NamedParameterJdbcTemplate jdbcTemplate;
4339

4440
@Value("${users_activation.find_by_activation_key}")
@@ -58,7 +54,7 @@ public UsersActivation findByActivationKey(String activationKey) {
5854
return jdbcTemplate.queryForObject(
5955
findByActivationKeySql,
6056
Collections.singletonMap("activation_key", activationKey),
61-
USERS_ACTIVATION_ROW_MAPPER
57+
RowMappers.forUsersActivation()
6258
);
6359
}
6460

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (C) 2009-2015 Slava Semushin <slava.semushin@gmail.com>
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 org.springframework.jdbc.core.RowMapper;
21+
22+
import ru.mystamps.web.entity.UsersActivation;
23+
import ru.mystamps.web.service.dto.LinkEntityDto;
24+
import ru.mystamps.web.service.dto.SeriesInfoDto;
25+
import ru.mystamps.web.service.dto.SitemapInfoDto;
26+
27+
final class RowMappers {
28+
29+
private static final RowMapper<LinkEntityDto> LINK_ENTITY_DTO_ROW_MAPPER =
30+
new LinkEntityDtoRowMapper();
31+
32+
private static final RowMapper<Pair<String, Integer>> NAME_AND_COUNTER_ROW_MAPPER =
33+
new StringIntegerPairRowMapper("name", "counter");
34+
35+
private static final RowMapper<Pair<String, Integer>> NAME_AND_ID_ROW_MAPPER =
36+
new StringIntegerPairRowMapper("name", "id");
37+
38+
private static final RowMapper<SitemapInfoDto> SITEMAP_INFO_DTO_ROW_MAPPER =
39+
new SitemapInfoDtoRowMapper();
40+
41+
private static final RowMapper<SeriesInfoDto> SERIES_INFO_DTO_ROW_MAPPER =
42+
new SeriesInfoDtoRowMapper();
43+
44+
private static final RowMapper<UsersActivation> USERS_ACTIVATION_ROW_MAPPER =
45+
new UsersActivationRowMapper();
46+
47+
private RowMappers() {
48+
}
49+
50+
public static RowMapper<LinkEntityDto> forLinkEntityDto() {
51+
return LINK_ENTITY_DTO_ROW_MAPPER;
52+
}
53+
54+
public static RowMapper<Pair<String, Integer>> forNameAndCounter() {
55+
return NAME_AND_COUNTER_ROW_MAPPER;
56+
}
57+
58+
public static RowMapper<Pair<String, Integer>> forNameAndId() {
59+
return NAME_AND_ID_ROW_MAPPER;
60+
}
61+
62+
public static RowMapper<SitemapInfoDto> forSitemapInfoDto() {
63+
return SITEMAP_INFO_DTO_ROW_MAPPER;
64+
}
65+
66+
public static RowMapper<SeriesInfoDto> forSeriesInfoDto() {
67+
return SERIES_INFO_DTO_ROW_MAPPER;
68+
}
69+
70+
public static RowMapper<UsersActivation> forUsersActivation() {
71+
return USERS_ACTIVATION_ROW_MAPPER;
72+
}
73+
74+
}

0 commit comments

Comments
 (0)