Skip to content

Commit

Permalink
Show recently added series on index page.
Browse files Browse the repository at this point in the history
Started to use Spring's NamedParameterJdbcTemplate for querying database.

Fix GH #5
  • Loading branch information
php-coder committed Sep 14, 2014
1 parent 2b344e0 commit 5caa57c
Show file tree
Hide file tree
Showing 15 changed files with 399 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/main/java/ru/mystamps/web/config/ApplicationContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

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

Expand All @@ -37,6 +40,7 @@
LiquibaseConfig.class,
MailConfig.class,
SecurityConfig.class,
DaoConfig.class,
ServicesConfig.class,
StrategiesConfig.class
})
Expand All @@ -60,6 +64,16 @@ public MessageSource getMessageSource() {
return messageSource;
}

@Bean
public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer configurer =
new PropertySourcesPlaceholderConfigurer();
configurer.setLocations(new Resource[] {
new ClassPathResource("sql/series_dao_queries.properties")
});
return configurer;
}

@Bean
public TogglzConfig getTogglzConfig() {
return new FeatureConfig(dataSourceConfig.getDataSource());
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/ru/mystamps/web/config/DaoConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2009-2014 Slava Semushin <slava.semushin@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package ru.mystamps.web.config;

import javax.inject.Inject;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import ru.mystamps.web.dao.JdbcSeriesDao;
import ru.mystamps.web.dao.impl.JdbcSeriesDaoImpl;

@Configuration
public class DaoConfig {

@Inject
private DataSourceConfig dataSourceConfig;

@Bean
public JdbcSeriesDao getJdbcSeriesDao() {
return new JdbcSeriesDaoImpl(dataSourceConfig.getDataSource());
}

}
5 changes: 4 additions & 1 deletion src/main/java/ru/mystamps/web/config/ServicesConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public class ServicesConfig {
@Inject
private CategoryDao categoryDao;

@Inject
private DaoConfig daoConfig;

@Inject
private SecurityConfig securityConfig;

Expand Down Expand Up @@ -112,7 +115,7 @@ public MailService getMailService() {

@Bean
public SeriesService getSeriesService() {
return new SeriesServiceImpl(seriesDao, getImageService());
return new SeriesServiceImpl(seriesDao, daoConfig.getJdbcSeriesDao(), getImageService());
}

@Bean
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/ru/mystamps/web/controller/SiteController.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package ru.mystamps.web.controller;

import java.util.Locale;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -29,23 +31,33 @@
import ru.mystamps.web.service.CollectionService;
import ru.mystamps.web.service.CountryService;
import ru.mystamps.web.service.SeriesService;
import ru.mystamps.web.util.LocaleUtils;

@Controller
@RequiredArgsConstructor
public class SiteController {

private static final int AMOUNT_OF_RECENTLY_ADDED_SERIES = 10; // NOPMD: LongVariable

private final CategoryService categoryService;
private final CollectionService collectionService;
private final CountryService countryService;
private final SeriesService seriesService;

@RequestMapping(value = Url.INDEX_PAGE, method = RequestMethod.GET)
public String showIndexPage(Model model) {
public String showIndexPage(Model model, Locale userLocale) {
model.addAttribute("categoryCounter", categoryService.countAll());
model.addAttribute("countryCounter", countryService.countAll());
model.addAttribute("seriesCounter", seriesService.countAll());
model.addAttribute("stampsCounter", seriesService.countAllStamps());
model.addAttribute("collectionsCounter", collectionService.countCollectionsOfUsers());

String lang = LocaleUtils.getLanguageOrNull(userLocale);
model.addAttribute(
"recentlyAddedSeries",
seriesService.findRecentlyAdded(AMOUNT_OF_RECENTLY_ADDED_SERIES, lang)
);

return "site/index";
}

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/ru/mystamps/web/dao/JdbcSeriesDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2009-2014 Slava Semushin <slava.semushin@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package ru.mystamps.web.dao;

import ru.mystamps.web.service.dto.SeriesInfoDto;

public interface JdbcSeriesDao {
Iterable<SeriesInfoDto> findLastAdded(int quantity, String lang);
}
64 changes: 64 additions & 0 deletions src/main/java/ru/mystamps/web/dao/impl/JdbcSeriesDaoImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2009-2014 Slava Semushin <slava.semushin@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package ru.mystamps.web.dao.impl;

import java.util.Collections;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import ru.mystamps.web.dao.JdbcSeriesDao;
import ru.mystamps.web.service.dto.SeriesInfoDto;

public class JdbcSeriesDaoImpl implements JdbcSeriesDao {

private static final RowMapper<SeriesInfoDto> SERIES_INFO_DTO_ROW_MAPPER =
new SeriesInfoDtoRowMapper();

private final NamedParameterJdbcTemplate jdbcTemplate;

@Value("${series.find_last_added_sql}")
private String findLastAddedSeriesSql;

@Value("${series.find_last_added_ru_sql}")
private String findLastAddedSeriesRuSql;

public JdbcSeriesDaoImpl(DataSource dataSource) {
jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}

@Override
public Iterable<SeriesInfoDto> findLastAdded(int quantity, String lang) {
String sql;
if ("ru".equals(lang)) {
sql = findLastAddedSeriesRuSql;
} else {
sql = findLastAddedSeriesSql;
}

return jdbcTemplate.query(
sql,
Collections.singletonMap("quantity", quantity),
SERIES_INFO_DTO_ROW_MAPPER
);
}

}
39 changes: 39 additions & 0 deletions src/main/java/ru/mystamps/web/dao/impl/JdbcUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2009-2014 Slava Semushin <slava.semushin@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package ru.mystamps.web.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

public final class JdbcUtils {

private JdbcUtils() {
}

// @see http://stackoverflow.com/q/2920364/checking-for-a-null-int-value-from-a-java-resultset
@SuppressWarnings("PMD.PrematureDeclaration")
public static Integer getInteger(ResultSet resultSet, String fieldName) throws SQLException {
int value = resultSet.getInt(fieldName);
if (resultSet.wasNull()) {
return null;
}

return Integer.valueOf(value);
}

}
56 changes: 56 additions & 0 deletions src/main/java/ru/mystamps/web/dao/impl/SeriesInfoDtoRowMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2009-2014 Slava Semushin <slava.semushin@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package ru.mystamps.web.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import ru.mystamps.web.service.dto.SeriesInfoDto;

class SeriesInfoDtoRowMapper implements RowMapper<SeriesInfoDto> {

@Override
public SeriesInfoDto mapRow(ResultSet resultSet, int i) throws SQLException {
Integer seriesId = resultSet.getInt("id");
Integer releaseDay = JdbcUtils.getInteger(resultSet, "release_day");
Integer releaseMonth = JdbcUtils.getInteger(resultSet, "release_month");
Integer releaseYear = JdbcUtils.getInteger(resultSet, "release_year");
Integer quantity = resultSet.getInt("quantity");
Boolean perforated = resultSet.getBoolean("perforated");
Integer categoryId = resultSet.getInt("category_id");
String categoryName = resultSet.getString("category_name");
Integer countryId = JdbcUtils.getInteger(resultSet, "country_id");
String countryName = resultSet.getString("country_name");

return new SeriesInfoDto(
seriesId,
categoryId,
categoryName,
countryId,
countryName,
releaseDay,
releaseMonth,
releaseYear,
quantity,
perforated
);
}

}
1 change: 1 addition & 0 deletions src/main/java/ru/mystamps/web/service/SeriesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ public interface SeriesService {
Iterable<SeriesInfoDto> findBy(Category category, String lang);
Iterable<SeriesInfoDto> findBy(Country country, String lang);
Iterable<SeriesInfoDto> findBy(Collection collection, String lang);
Iterable<SeriesInfoDto> findRecentlyAdded(int quantity, String lang);
}
10 changes: 10 additions & 0 deletions src/main/java/ru/mystamps/web/service/SeriesServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import lombok.RequiredArgsConstructor;

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

private final SeriesDao seriesDao;
private final JdbcSeriesDao jdbcSeriesDao;
private final ImageService imageService;

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

@Override
@Transactional(readOnly = true)
public Iterable<SeriesInfoDto> findRecentlyAdded(int quantity, String lang) {
Validate.isTrue(quantity > 0, "Quantity of recently added series must be greater than 0");

return jdbcSeriesDao.findLastAdded(quantity, lang);
}

private static void setDateOfReleaseIfProvided(AddSeriesDto dto, Series series) {
if (dto.getYear() == null) {
return;
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/ru/mystamps/i18n/Messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ t_countries_amount = Amount of countries
t_series_amount = Amount of series
t_stamps_amount = Amount of stamps
t_collections_amount = Amount of collections
t_recently_added_series = Recently added series

# account/register.html
t_registration_on_site = Register on site
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/ru/mystamps/i18n/Messages_ru.properties
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ t_countries_amount = Стран
t_series_amount = Серий
t_stamps_amount = Марок
t_collections_amount = Коллекций
t_recently_added_series = Недавно добавленные серии

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

0 comments on commit 5caa57c

Please sign in to comment.