Skip to content

Commit

Permalink
refactor: move inclusion of category_dao_queries.properties to the ap…
Browse files Browse the repository at this point in the history
…propriate package config

We also switched to using Environment.getRequiredProperty() because @value doesn't work
without PropertySourcesPlaceholderConfigurer bean (that we are going to remove soon).

Relate to #927
  • Loading branch information
php-coder committed Feb 13, 2021
1 parent 59c9c83 commit 26c9f9f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholder
PropertySourcesPlaceholderConfigurer configurer =
new PropertySourcesPlaceholderConfigurer();
configurer.setLocations(
new ClassPathResource("sql/category_dao_queries.properties"),
new ClassPathResource("sql/country_dao_queries.properties"),
new ClassPathResource("sql/collection_dao_queries.properties"),
new ClassPathResource("sql/image_dao_queries.properties"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

Expand Down Expand Up @@ -73,13 +74,15 @@ public CategoryService categoryService() {
}

@RequiredArgsConstructor
@PropertySource("classpath:sql/category_dao_queries.properties")
public static class Daos {

private final NamedParameterJdbcTemplate jdbcTemplate;
private final Environment env;

@Bean
public CategoryDao categoryDao() {
return new JdbcCategoryDao(jdbcTemplate);
return new JdbcCategoryDao(env, jdbcTemplate);
}

}
Expand Down
84 changes: 36 additions & 48 deletions src/main/java/ru/mystamps/web/feature/category/JdbcCategoryDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
*/
package ru.mystamps.web.feature.category;

import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
Expand All @@ -38,61 +37,50 @@
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@SuppressWarnings({ "PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods", "PMD.TooManyFields" })
public class JdbcCategoryDao implements CategoryDao {

private static final ResultSetExtractor<Map<String, Integer>> NAME_COUNTER_EXTRACTOR =
new MapStringIntegerResultSetExtractor("name", "counter");

private final NamedParameterJdbcTemplate jdbcTemplate;

@Value("${category.create}")
private String addCategorySql;

@Value("${category.count_all_categories}")
private String countAllSql;

@Value("${category.count_categories_by_slug}")
private String countBySlugSql;

@Value("${category.count_categories_by_name}")
private String countByNameSql;

@Value("${category.count_categories_by_name_ru}")
private String countByNameRuSql;

@Value("${category.count_categories_of_collection}")
private String countCategoriesOfCollectionSql;

@Value("${category.count_categories_added_since}")
private String countCategoriesAddedSinceSql;

@Value("${category.count_untranslated_names_since}")
private String countUntranslatedNamesSinceSql;

@Value("${category.count_stamps_by_categories}")
private String countStampsByCategoriesSql;

@Value("${category.find_ids_by_names}")
private String findIdsByNamesSql;

@Value("${category.find_ids_by_name_pattern}")
private String findIdsByNamePatternSql;

@Value("${category.find_all_categories_names_with_slug}")
private String findCategoriesNamesWithSlugSql;

@Value("${category.find_category_link_info_by_slug}")
private String findLinkEntityBySlugSql;

private final String addCategorySql;
private final String countAllSql;
private final String countBySlugSql;
private final String countByNameSql;
private final String countByNameRuSql;
private final String countCategoriesOfCollectionSql;
private final String countCategoriesAddedSinceSql;
private final String countUntranslatedNamesSinceSql;
private final String countStampsByCategoriesSql;
private final String findIdsByNamesSql;
private final String findIdsByNamePatternSql;
private final String findCategoriesNamesWithSlugSql;
private final String findLinkEntityBySlugSql;
@SuppressWarnings("PMD.LongVariable")
@Value("${category.find_categories_with_parent_names}")
private String findCategoriesWithParentNamesSql;

private final String findCategoriesWithParentNamesSql;
@SuppressWarnings("PMD.LongVariable")
@Value("${category.find_from_last_created_series_by_user}")
private String findFromLastCreatedSeriesByUserSql;
private final String findFromLastCreatedSeriesByUserSql;

@SuppressWarnings("checkstyle:linelength")
public JdbcCategoryDao(Environment env, NamedParameterJdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
this.addCategorySql = env.getRequiredProperty("category.create");
this.countAllSql = env.getRequiredProperty("category.count_all_categories");
this.countBySlugSql = env.getRequiredProperty("category.count_categories_by_slug");
this.countByNameSql = env.getRequiredProperty("category.count_categories_by_name");
this.countByNameRuSql = env.getRequiredProperty("category.count_categories_by_name_ru");
this.countCategoriesOfCollectionSql = env.getRequiredProperty("category.count_categories_of_collection");
this.countCategoriesAddedSinceSql = env.getRequiredProperty("category.count_categories_added_since");
this.countUntranslatedNamesSinceSql = env.getRequiredProperty("category.count_untranslated_names_since");
this.countStampsByCategoriesSql = env.getRequiredProperty("category.count_stamps_by_categories");
this.findIdsByNamesSql = env.getRequiredProperty("category.find_ids_by_names");
this.findIdsByNamePatternSql = env.getRequiredProperty("category.find_ids_by_name_pattern");
this.findCategoriesNamesWithSlugSql = env.getRequiredProperty("category.find_all_categories_names_with_slug");
this.findLinkEntityBySlugSql = env.getRequiredProperty("category.find_category_link_info_by_slug");
this.findCategoriesWithParentNamesSql = env.getRequiredProperty("category.find_categories_with_parent_names");
this.findFromLastCreatedSeriesByUserSql = env.getRequiredProperty("category.find_from_last_created_series_by_user");
}

@Override
public Integer add(AddCategoryDbDto category) {
Expand Down

0 comments on commit 26c9f9f

Please sign in to comment.