Skip to content

Commit

Permalink
refactor: move inclusion of {user,s_activation}_dao_queries.propertie…
Browse files Browse the repository at this point in the history
…s to the appropriate 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 14, 2021
1 parent 6b01bf3 commit fd597d0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 38 deletions.
2 changes: 0 additions & 2 deletions src/main/java/ru/mystamps/web/config/ApplicationContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholder
PropertySourcesPlaceholderConfigurer configurer =
new PropertySourcesPlaceholderConfigurer();
configurer.setLocations(
new ClassPathResource("sql/user_dao_queries.properties"),
new ClassPathResource("sql/users_activation_dao_queries.properties"),
new ClassPathResource("sql/series_dao_queries.properties"),
new ClassPathResource("sql/series_sales_dao_queries.properties"),
new ClassPathResource("sql/suspicious_activity_dao_queries.properties"),
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/ru/mystamps/web/feature/account/AccountConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import ru.mystamps.web.feature.collection.CollectionService;
Expand Down Expand Up @@ -50,8 +52,13 @@ public AccountController accountController() {
}

@RequiredArgsConstructor
@PropertySource({
"classpath:sql/user_dao_queries.properties",
"classpath:sql/users_activation_dao_queries.properties"
})
public static class Services {

private final Environment env;
private final NamedParameterJdbcTemplate jdbcTemplate;

@Bean
Expand Down Expand Up @@ -84,12 +91,12 @@ public UsersActivationService usersActivationService(

@Bean
public UserDao userDao() {
return new JdbcUserDao(jdbcTemplate);
return new JdbcUserDao(env, jdbcTemplate);
}

@Bean
public UsersActivationDao usersActivationDao() {
return new JdbcUsersActivationDao(jdbcTemplate);
return new JdbcUsersActivationDao(env, jdbcTemplate);
}

}
Expand Down
26 changes: 12 additions & 14 deletions src/main/java/ru/mystamps/web/feature/account/JdbcUserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
*/
package ru.mystamps.web.feature.account;

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.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
Expand All @@ -32,22 +31,21 @@
import java.util.HashMap;
import java.util.Map;

@RequiredArgsConstructor
public class JdbcUserDao implements UserDao {

private final NamedParameterJdbcTemplate jdbcTemplate;
private final String countByLoginSql;
private final String countActivatedSinceSql;
private final String findUserDetailsByLoginSql;
private final String addUserSql;

@Value("${user.count_users_by_login}")
private String countByLoginSql;

@Value("${user.count_activated_since}")
private String countActivatedSinceSql;

@Value("${user.find_user_details_by_login}")
private String findUserDetailsByLoginSql;

@Value("${user.create}")
private String addUserSql;
public JdbcUserDao(Environment env, NamedParameterJdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
this.countByLoginSql = env.getRequiredProperty("user.count_users_by_login");
this.countActivatedSinceSql = env.getRequiredProperty("user.count_activated_since");
this.findUserDetailsByLoginSql = env.getRequiredProperty("user.find_user_details_by_login");
this.addUserSql = env.getRequiredProperty("user.create");
}

@Override
public long countByLogin(String login) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
*/
package ru.mystamps.web.feature.account;

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.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import java.util.Collections;
Expand All @@ -29,28 +28,26 @@
import java.util.Map;

@SuppressWarnings("PMD.AvoidDuplicateLiterals")
@RequiredArgsConstructor
public class JdbcUsersActivationDao implements UsersActivationDao {

private final NamedParameterJdbcTemplate jdbcTemplate;
private final String findByActivationKeySql;
private final String findOlderThanDateSql;
private final String countByActivationKeySql;
private final String countCreatedSinceSql;
private final String removeByActivationKeySql;
private final String addActivationKeySql;

@Value("${users_activation.find_by_activation_key}")
private String findByActivationKeySql;

@Value("${users_activation.find_older_than}")
private String findOlderThanDateSql;

@Value("${users_activation.count_by_activation_key}")
private String countByActivationKeySql;

@Value("${users_activation.count_created_since}")
private String countCreatedSinceSql;

@Value("${users_activation.remove_by_activation_key}")
private String removeByActivationKeySql;

@Value("${users_activation.create}")
private String addActivationKeySql;
@SuppressWarnings("checkstyle:linelength")
public JdbcUsersActivationDao(Environment env, NamedParameterJdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
this.findByActivationKeySql = env.getRequiredProperty("users_activation.find_by_activation_key");
this.findOlderThanDateSql = env.getRequiredProperty("users_activation.find_older_than");
this.countByActivationKeySql = env.getRequiredProperty("users_activation.count_by_activation_key");
this.countCreatedSinceSql = env.getRequiredProperty("users_activation.count_created_since");
this.removeByActivationKeySql = env.getRequiredProperty("users_activation.remove_by_activation_key");
this.addActivationKeySql = env.getRequiredProperty("users_activation.create");
}

@Override
public UsersActivationDto findByActivationKey(String activationKey) {
Expand Down

0 comments on commit fd597d0

Please sign in to comment.