code refactoring
This commit is contained in:
parent
4d9cdf9c9f
commit
b73ecd8635
9 changed files with 73 additions and 77 deletions
|
|
@ -16,7 +16,7 @@ public class Settings {
|
|||
public Settings() {
|
||||
}
|
||||
|
||||
public Settings(Long id, Long version, String name, String value) {
|
||||
public Settings(Long id, @Nullable Long version, @Nullable String name, @Nullable String value) {
|
||||
this.id = id;
|
||||
this.version = version;
|
||||
this.name = name;
|
||||
|
|
@ -31,27 +31,28 @@ public class Settings {
|
|||
this.id = id;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(Long version) {
|
||||
public void setVersion(@Nullable Long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
public @Nullable String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
public void setName(@Nullable String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
public @Nullable String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
public void setValue(@Nullable String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,11 +7,7 @@ import ru.nbch.indicsearchservice.model.LocalizationObject;
|
|||
|
||||
@Mapper
|
||||
public interface LocalizationObjectMapper {
|
||||
List<LocalizationObject> findByCode(String code);
|
||||
|
||||
List<LocalizationObject> findAllByCodes(@Param("codes") List<String> codes);
|
||||
|
||||
void insert(LocalizationObject localization);
|
||||
|
||||
void insertBatch(@Param("list") List<LocalizationObject> list);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
package ru.nbch.indicsearchservice.repository.bank;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
|
@ -13,9 +12,5 @@ public interface SettingsMapper {
|
|||
|
||||
List<Settings> findAllByNames(@Param("names") List<String> names);
|
||||
|
||||
Collection<Settings> findAll();
|
||||
|
||||
void insert(@Param("settings") Settings settings);
|
||||
|
||||
void insertBatch(@Param("list") List<Settings> list);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -199,7 +199,7 @@ public class IndicSearchService {
|
|||
|
||||
for (String key : columnKeys) {
|
||||
String table = keyToTable.get(key);
|
||||
Optional<String> valueOpt = settingsValues.getOrDefault(key, Optional.empty());
|
||||
Optional<String> valueOpt = settingsValues.get(key);
|
||||
|
||||
if (valueOpt.isPresent() && StringUtils.isNotBlank(valueOpt.get())) {
|
||||
String[] fields = valueOpt.get().split(";");
|
||||
|
|
@ -272,7 +272,7 @@ public class IndicSearchService {
|
|||
String tableKey = "db.viewer.table." + tableUpper;
|
||||
|
||||
// Локализация для таблиц
|
||||
Optional<String> tableLoc = localizationValues.getOrDefault(tableKey, Optional.empty());
|
||||
Optional<String> tableLoc = localizationValues.get(tableKey);
|
||||
if (tableLoc.isPresent() && StringUtils.isNotBlank(tableLoc.get())) {
|
||||
metaData.setTableNameLocalization(tableLoc.get());
|
||||
} else {
|
||||
|
|
@ -291,12 +291,12 @@ public class IndicSearchService {
|
|||
String primaryKey = "db.viewer." + tableUpper + ".field." + fieldUpper;
|
||||
String fallbackKey = "db.viewer.field." + fieldUpper;
|
||||
|
||||
Optional<String> primaryLoc = localizationValues.getOrDefault(primaryKey, Optional.empty());
|
||||
Optional<String> primaryLoc = localizationValues.get(primaryKey);
|
||||
|
||||
if (primaryLoc.isPresent() && StringUtils.isNotBlank(primaryLoc.get())) {
|
||||
metaData.addLocalization(field, primaryLoc.get());
|
||||
} else {
|
||||
Optional<String> fallbackLoc = localizationValues.getOrDefault(fallbackKey, Optional.empty());
|
||||
Optional<String> fallbackLoc = localizationValues.get(fallbackKey);
|
||||
if (fallbackLoc.isPresent() && StringUtils.isNotBlank(fallbackLoc.get())) {
|
||||
metaData.addLocalization(field, fallbackLoc.get());
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -4,10 +4,10 @@ import java.io.File;
|
|||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.nbch.indicsearchservice.config.settings.ApplicationSettings;
|
||||
|
|
@ -41,12 +41,25 @@ public class SettingsService {
|
|||
return Map.of();
|
||||
}
|
||||
|
||||
List<Settings> foundValues = settingsMapper.findAllByNames(names);
|
||||
if (foundValues == null || foundValues.isEmpty()) {
|
||||
return Map.of();
|
||||
Map<String, Optional<String>> resultMap = new HashMap<>();
|
||||
for (String name : names) {
|
||||
if (name != null) {
|
||||
resultMap.put(name, Optional.empty());
|
||||
}
|
||||
}
|
||||
|
||||
return foundValues.stream().collect(Collectors.toMap(Settings::getName, s -> Optional.ofNullable(s.getValue())));
|
||||
List<Settings> foundValues = settingsMapper.findAllByNames(names);
|
||||
if (foundValues == null || foundValues.isEmpty()) {
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
for (Settings setting : foundValues) {
|
||||
if (setting != null && setting.getName() != null) {
|
||||
resultMap.put(setting.getName(), Optional.ofNullable(setting.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
public Optional<String> getOptionalSettingsValueByName(String name) {
|
||||
|
|
|
|||
|
|
@ -4,8 +4,10 @@ import com.zaxxer.hikari.HikariConfig;
|
|||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Consumer;
|
||||
|
|
@ -201,18 +203,31 @@ public class DataSourceUpdater {
|
|||
case INDIC -> "indic";
|
||||
};
|
||||
|
||||
DataSourceSettings dataSourceSettings = new DataSourceSettings();
|
||||
dataSourceSettings.setDriver(settingsService.getSettingsValueByName("datasource." + key + ".driver"));
|
||||
dataSourceSettings.setUrl(settingsService.getSettingsValueByName("datasource." + key + ".url"));
|
||||
dataSourceSettings.setUsername(settingsService.getSettingsValueByName("datasource." + key + ".username"));
|
||||
dataSourceSettings.setPassword(settingsService.getSettingsValueByName("datasource." + key + ".password"));
|
||||
Map<String, Optional<String>> fetchedSettings = settingsService.getSettingsValuesByNames(List.of(
|
||||
"datasource." + key + ".driver",
|
||||
"datasource." + key + ".url",
|
||||
"datasource." + key + ".username",
|
||||
"datasource." + key + ".password",
|
||||
"datasource." + key + ".connection-timeout",
|
||||
"datasource." + key + ".validation-timeout",
|
||||
"datasource." + key + ".idle-timeout",
|
||||
"datasource." + key + ".max-pool-size",
|
||||
"datasource." + key + ".max-lifetime",
|
||||
"datasource." + key + ".keep-alive-time"
|
||||
));
|
||||
|
||||
dataSourceSettings.setConnectionTimeout(Long.valueOf(settingsService.getOptionalSettingsValueByName("datasource." + key + ".connection-timeout").orElse("30000")));
|
||||
dataSourceSettings.setValidationTimeout(Long.valueOf(settingsService.getOptionalSettingsValueByName("datasource." + key + ".validation-timeout").orElse("5000")));
|
||||
dataSourceSettings.setIdleTimeout(Long.valueOf(settingsService.getOptionalSettingsValueByName("datasource." + key + ".idle-timeout").orElse("600000")));
|
||||
dataSourceSettings.setMaxPoolSize(Integer.valueOf(settingsService.getOptionalSettingsValueByName("datasource." + key + ".max-pool-size").orElse("100")));
|
||||
dataSourceSettings.setMaxLifetime(Long.valueOf(settingsService.getOptionalSettingsValueByName("datasource." + key + ".max-lifetime").orElse("1800000")));
|
||||
dataSourceSettings.setKeepAliveTime(Long.valueOf(settingsService.getOptionalSettingsValueByName("datasource." + key + ".keep-alive-time").orElse("120000")));
|
||||
DataSourceSettings dataSourceSettings = new DataSourceSettings();
|
||||
dataSourceSettings.setDriver(fetchedSettings.get("datasource." + key + ".driver").orElseThrow(() -> new RuntimeException("Required SETTINGS not found!")));
|
||||
dataSourceSettings.setUrl(fetchedSettings.get("datasource." + key + ".url").orElseThrow(() -> new RuntimeException("Required SETTINGS not found!")));
|
||||
dataSourceSettings.setUsername(fetchedSettings.get("datasource." + key + ".username").orElseThrow(() -> new RuntimeException("Required SETTINGS not found!")));
|
||||
dataSourceSettings.setPassword(fetchedSettings.get("datasource." + key + ".password").orElseThrow(() -> new RuntimeException("Required SETTINGS not found!")));
|
||||
|
||||
dataSourceSettings.setConnectionTimeout(Long.valueOf(fetchedSettings.get("datasource." + key + ".connection-timeout").orElse("30000")));
|
||||
dataSourceSettings.setValidationTimeout(Long.valueOf(fetchedSettings.get("datasource." + key + ".validation-timeout").orElse("5000")));
|
||||
dataSourceSettings.setIdleTimeout(Long.valueOf(fetchedSettings.get("datasource." + key + ".idle-timeout").orElse("600000")));
|
||||
dataSourceSettings.setMaxPoolSize(Integer.valueOf(fetchedSettings.get("datasource." + key + ".max-pool-size").orElse("100")));
|
||||
dataSourceSettings.setMaxLifetime(Long.valueOf(fetchedSettings.get("datasource." + key + ".max-lifetime").orElse("1800000")));
|
||||
dataSourceSettings.setKeepAliveTime(Long.valueOf(fetchedSettings.get("datasource." + key + ".keep-alive-time").orElse("120000")));
|
||||
|
||||
settingsConsumer.accept(dataSourceSettings);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import java.io.IOException;
|
|||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
|
@ -12,7 +13,6 @@ import java.util.Map;
|
|||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
import org.apache.ibatis.cursor.Cursor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.nbch.indicsearchservice.config.settings.ApplicationSettings;
|
||||
|
|
@ -105,11 +105,24 @@ public class IndicSearchUtils {
|
|||
return Map.of();
|
||||
}
|
||||
|
||||
List<LocalizationObject> foundValues = localizationObjectMapper.findAllByCodes(localizationCodes);
|
||||
if (foundValues == null || foundValues.isEmpty()) {
|
||||
return Map.of();
|
||||
Map<String, Optional<String>> resultMap = new HashMap<>();
|
||||
for (String code : localizationCodes) {
|
||||
if (code != null) {
|
||||
resultMap.put(code, Optional.empty());
|
||||
}
|
||||
}
|
||||
|
||||
return foundValues.stream().collect(Collectors.toMap(LocalizationObject::getCode, s -> Optional.ofNullable(s.getText())));
|
||||
List<LocalizationObject> foundValues = localizationObjectMapper.findAllByCodes(localizationCodes);
|
||||
if (foundValues == null || foundValues.isEmpty()) {
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
for (LocalizationObject localization : foundValues) {
|
||||
if (localization != null && localization.getCode() != null) {
|
||||
resultMap.put(localization.getCode(), Optional.ofNullable(localization.getText()));
|
||||
}
|
||||
}
|
||||
|
||||
return resultMap;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,12 +28,6 @@
|
|||
FROM LOCALIZATION_OBJECT
|
||||
</sql>
|
||||
|
||||
<select id="findByCode" resultMap="localizationObjectResultMap">
|
||||
<include refid="baseSelect"/>
|
||||
WHERE CODE = #{code}
|
||||
WITH UR
|
||||
</select>
|
||||
|
||||
<select id="findAllByCodes" resultMap="localizationObjectResultMap">
|
||||
<include refid="baseSelect"/>
|
||||
WHERE CODE IN
|
||||
|
|
@ -41,23 +35,6 @@
|
|||
WITH UR
|
||||
</select>
|
||||
|
||||
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
|
||||
INSERT INTO LOCALIZATION_OBJECT (VERSION,
|
||||
CODE,
|
||||
DATE_CREATED,
|
||||
LAST_UPDATED,
|
||||
LOC,
|
||||
RELEVANCE,
|
||||
TEXT)
|
||||
VALUES (0,
|
||||
#{code},
|
||||
CURRENT_TIMESTAMP,
|
||||
CURRENT_TIMESTAMP,
|
||||
#{loc},
|
||||
#{relevance},
|
||||
#{text})
|
||||
</insert>
|
||||
|
||||
<insert id="insertBatch" useGeneratedKeys="true" keyProperty="id">
|
||||
INSERT INTO LOCALIZATION_OBJECT (VERSION,
|
||||
CODE,
|
||||
|
|
|
|||
|
|
@ -30,20 +30,6 @@
|
|||
WITH UR
|
||||
</select>
|
||||
|
||||
<select id="findAll" resultMap="settingsResultMap">
|
||||
<include refid="baseSelect"/>
|
||||
WITH UR
|
||||
</select>
|
||||
|
||||
<insert id="insert" useGeneratedKeys="true" keyProperty="settings.id" keyColumn="ID">
|
||||
insert into SETTINGS (VERSION,
|
||||
NAME,
|
||||
"VALUE")
|
||||
values (0,
|
||||
#{settings.name},
|
||||
#{settings.value})
|
||||
</insert>
|
||||
|
||||
<insert id="insertBatch">
|
||||
insert into SETTINGS (VERSION, NAME, "VALUE")
|
||||
values
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue