Skip to content

Commit 5caa57c

Browse files
committed
Show recently added series on index page.
Started to use Spring's NamedParameterJdbcTemplate for querying database. Fix GH #5
1 parent 2b344e0 commit 5caa57c

File tree

15 files changed

+399
-4
lines changed

15 files changed

+399
-4
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
import org.springframework.context.annotation.Bean;
2424
import org.springframework.context.annotation.Configuration;
2525
import org.springframework.context.annotation.Import;
26+
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
2627
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
28+
import org.springframework.core.io.ClassPathResource;
29+
import org.springframework.core.io.Resource;
2730

2831
import ru.mystamps.web.support.spring.security.SecurityConfig;
2932

@@ -37,6 +40,7 @@
3740
LiquibaseConfig.class,
3841
MailConfig.class,
3942
SecurityConfig.class,
43+
DaoConfig.class,
4044
ServicesConfig.class,
4145
StrategiesConfig.class
4246
})
@@ -60,6 +64,16 @@ public MessageSource getMessageSource() {
6064
return messageSource;
6165
}
6266

67+
@Bean
68+
public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() {
69+
PropertySourcesPlaceholderConfigurer configurer =
70+
new PropertySourcesPlaceholderConfigurer();
71+
configurer.setLocations(new Resource[] {
72+
new ClassPathResource("sql/series_dao_queries.properties")
73+
});
74+
return configurer;
75+
}
76+
6377
@Bean
6478
public TogglzConfig getTogglzConfig() {
6579
return new FeatureConfig(dataSourceConfig.getDataSource());
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.config;
19+
20+
import javax.inject.Inject;
21+
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Configuration;
24+
25+
import ru.mystamps.web.dao.JdbcSeriesDao;
26+
import ru.mystamps.web.dao.impl.JdbcSeriesDaoImpl;
27+
28+
@Configuration
29+
public class DaoConfig {
30+
31+
@Inject
32+
private DataSourceConfig dataSourceConfig;
33+
34+
@Bean
35+
public JdbcSeriesDao getJdbcSeriesDao() {
36+
return new JdbcSeriesDaoImpl(dataSourceConfig.getDataSource());
37+
}
38+
39+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public class ServicesConfig {
3737
@Inject
3838
private CategoryDao categoryDao;
3939

40+
@Inject
41+
private DaoConfig daoConfig;
42+
4043
@Inject
4144
private SecurityConfig securityConfig;
4245

@@ -112,7 +115,7 @@ public MailService getMailService() {
112115

113116
@Bean
114117
public SeriesService getSeriesService() {
115-
return new SeriesServiceImpl(seriesDao, getImageService());
118+
return new SeriesServiceImpl(seriesDao, daoConfig.getJdbcSeriesDao(), getImageService());
116119
}
117120

118121
@Bean

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
package ru.mystamps.web.controller;
1919

20+
import java.util.Locale;
21+
2022
import org.springframework.stereotype.Controller;
2123
import org.springframework.ui.Model;
2224
import org.springframework.web.bind.annotation.RequestMapping;
@@ -29,23 +31,33 @@
2931
import ru.mystamps.web.service.CollectionService;
3032
import ru.mystamps.web.service.CountryService;
3133
import ru.mystamps.web.service.SeriesService;
34+
import ru.mystamps.web.util.LocaleUtils;
3235

3336
@Controller
3437
@RequiredArgsConstructor
3538
public class SiteController {
3639

40+
private static final int AMOUNT_OF_RECENTLY_ADDED_SERIES = 10; // NOPMD: LongVariable
41+
3742
private final CategoryService categoryService;
3843
private final CollectionService collectionService;
3944
private final CountryService countryService;
4045
private final SeriesService seriesService;
4146

4247
@RequestMapping(value = Url.INDEX_PAGE, method = RequestMethod.GET)
43-
public String showIndexPage(Model model) {
48+
public String showIndexPage(Model model, Locale userLocale) {
4449
model.addAttribute("categoryCounter", categoryService.countAll());
4550
model.addAttribute("countryCounter", countryService.countAll());
4651
model.addAttribute("seriesCounter", seriesService.countAll());
4752
model.addAttribute("stampsCounter", seriesService.countAllStamps());
4853
model.addAttribute("collectionsCounter", collectionService.countCollectionsOfUsers());
54+
55+
String lang = LocaleUtils.getLanguageOrNull(userLocale);
56+
model.addAttribute(
57+
"recentlyAddedSeries",
58+
seriesService.findRecentlyAdded(AMOUNT_OF_RECENTLY_ADDED_SERIES, lang)
59+
);
60+
4961
return "site/index";
5062
}
5163

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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;
19+
20+
import ru.mystamps.web.service.dto.SeriesInfoDto;
21+
22+
public interface JdbcSeriesDao {
23+
Iterable<SeriesInfoDto> findLastAdded(int quantity, String lang);
24+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.util.Collections;
21+
22+
import javax.sql.DataSource;
23+
24+
import org.springframework.beans.factory.annotation.Value;
25+
import org.springframework.jdbc.core.RowMapper;
26+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
27+
28+
import ru.mystamps.web.dao.JdbcSeriesDao;
29+
import ru.mystamps.web.service.dto.SeriesInfoDto;
30+
31+
public class JdbcSeriesDaoImpl implements JdbcSeriesDao {
32+
33+
private static final RowMapper<SeriesInfoDto> SERIES_INFO_DTO_ROW_MAPPER =
34+
new SeriesInfoDtoRowMapper();
35+
36+
private final NamedParameterJdbcTemplate jdbcTemplate;
37+
38+
@Value("${series.find_last_added_sql}")
39+
private String findLastAddedSeriesSql;
40+
41+
@Value("${series.find_last_added_ru_sql}")
42+
private String findLastAddedSeriesRuSql;
43+
44+
public JdbcSeriesDaoImpl(DataSource dataSource) {
45+
jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
46+
}
47+
48+
@Override
49+
public Iterable<SeriesInfoDto> findLastAdded(int quantity, String lang) {
50+
String sql;
51+
if ("ru".equals(lang)) {
52+
sql = findLastAddedSeriesRuSql;
53+
} else {
54+
sql = findLastAddedSeriesSql;
55+
}
56+
57+
return jdbcTemplate.query(
58+
sql,
59+
Collections.singletonMap("quantity", quantity),
60+
SERIES_INFO_DTO_ROW_MAPPER
61+
);
62+
}
63+
64+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
public final class JdbcUtils {
24+
25+
private JdbcUtils() {
26+
}
27+
28+
// @see http://stackoverflow.com/q/2920364/checking-for-a-null-int-value-from-a-java-resultset
29+
@SuppressWarnings("PMD.PrematureDeclaration")
30+
public static Integer getInteger(ResultSet resultSet, String fieldName) throws SQLException {
31+
int value = resultSet.getInt(fieldName);
32+
if (resultSet.wasNull()) {
33+
return null;
34+
}
35+
36+
return Integer.valueOf(value);
37+
}
38+
39+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 ru.mystamps.web.service.dto.SeriesInfoDto;
26+
27+
class SeriesInfoDtoRowMapper implements RowMapper<SeriesInfoDto> {
28+
29+
@Override
30+
public SeriesInfoDto mapRow(ResultSet resultSet, int i) throws SQLException {
31+
Integer seriesId = resultSet.getInt("id");
32+
Integer releaseDay = JdbcUtils.getInteger(resultSet, "release_day");
33+
Integer releaseMonth = JdbcUtils.getInteger(resultSet, "release_month");
34+
Integer releaseYear = JdbcUtils.getInteger(resultSet, "release_year");
35+
Integer quantity = resultSet.getInt("quantity");
36+
Boolean perforated = resultSet.getBoolean("perforated");
37+
Integer categoryId = resultSet.getInt("category_id");
38+
String categoryName = resultSet.getString("category_name");
39+
Integer countryId = JdbcUtils.getInteger(resultSet, "country_id");
40+
String countryName = resultSet.getString("country_name");
41+
42+
return new SeriesInfoDto(
43+
seriesId,
44+
categoryId,
45+
categoryName,
46+
countryId,
47+
countryName,
48+
releaseDay,
49+
releaseMonth,
50+
releaseYear,
51+
quantity,
52+
perforated
53+
);
54+
}
55+
56+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ public interface SeriesService {
3636
Iterable<SeriesInfoDto> findBy(Category category, String lang);
3737
Iterable<SeriesInfoDto> findBy(Country country, String lang);
3838
Iterable<SeriesInfoDto> findBy(Collection collection, String lang);
39+
Iterable<SeriesInfoDto> findRecentlyAdded(int quantity, String lang);
3940
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import lombok.RequiredArgsConstructor;
3232

33+
import ru.mystamps.web.dao.JdbcSeriesDao;
3334
import ru.mystamps.web.dao.SeriesDao;
3435
import ru.mystamps.web.entity.Category;
3536
import ru.mystamps.web.entity.Country;
@@ -51,6 +52,7 @@ public class SeriesServiceImpl implements SeriesService {
5152
private static final Logger LOG = LoggerFactory.getLogger(SeriesServiceImpl.class);
5253

5354
private final SeriesDao seriesDao;
55+
private final JdbcSeriesDao jdbcSeriesDao;
5456
private final ImageService imageService;
5557

5658
@Override
@@ -191,6 +193,14 @@ public Iterable<SeriesInfoDto> findBy(Collection collection, String lang) {
191193
return seriesDao.findByAsSeriesInfo(collection.getId(), lang);
192194
}
193195

196+
@Override
197+
@Transactional(readOnly = true)
198+
public Iterable<SeriesInfoDto> findRecentlyAdded(int quantity, String lang) {
199+
Validate.isTrue(quantity > 0, "Quantity of recently added series must be greater than 0");
200+
201+
return jdbcSeriesDao.findLastAdded(quantity, lang);
202+
}
203+
194204
private static void setDateOfReleaseIfProvided(AddSeriesDto dto, Series series) {
195205
if (dto.getYear() == null) {
196206
return;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ t_countries_amount = Amount of countries
3434
t_series_amount = Amount of series
3535
t_stamps_amount = Amount of stamps
3636
t_collections_amount = Amount of collections
37+
t_recently_added_series = Recently added series
3738

3839
# account/register.html
3940
t_registration_on_site = Register on site

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ t_countries_amount = Стран
3434
t_series_amount = Серий
3535
t_stamps_amount = Марок
3636
t_collections_amount = Коллекций
37+
t_recently_added_series = Недавно добавленные серии
3738

3839
# account/register.html
3940
t_registration_on_site = Регистрация на сайте

0 commit comments

Comments
 (0)