-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show recently added series on index page.
Started to use Spring's NamedParameterJdbcTemplate for querying database. Fix GH #5
- Loading branch information
Showing
15 changed files
with
399 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
64
src/main/java/ru/mystamps/web/dao/impl/JdbcSeriesDaoImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
56
src/main/java/ru/mystamps/web/dao/impl/SeriesInfoDtoRowMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.