Skip to content

Commit

Permalink
feat: add SystemConfiguration repo (#167)
Browse files Browse the repository at this point in the history
* feat: add SystemConfiguration repo

* feat: add SystemConfiguration repo

---------

Co-authored-by: wildeslam <[email protected]>
  • Loading branch information
wildeslam and wildeslam authored Feb 22, 2024
1 parent b13f318 commit 2ef734e
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 4 deletions.
2 changes: 1 addition & 1 deletion arex-storage-config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<parent>
<artifactId>arex-storage-service</artifactId>
<groupId>com.arextest</groupId>
<version>1.1.11</version>
<version>1.1.12</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.arextest.config.mapper;

import com.arextest.config.model.dao.config.SystemConfigurationCollection;
import com.arextest.config.model.dto.SystemConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

/**
* @author wildeslam.
* @create 2024/2/21 19:53
*/
@Mapper
public interface SystemConfigurationMapper {

SystemConfigurationMapper INSTANCE = Mappers.getMapper(SystemConfigurationMapper.class);

SystemConfiguration dtoFromDao(SystemConfigurationCollection dao);

@Mappings({@Mapping(target = "id", expression = "java(null)"),
@Mapping(target = "dataChangeCreateTime", expression = "java(System.currentTimeMillis())"),
@Mapping(target = "dataChangeUpdateTime", expression = "java(System.currentTimeMillis())")})
SystemConfigurationCollection daoFromDto(SystemConfiguration dto);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.arextest.config.model.dao.config;

import com.arextest.config.model.dao.BaseEntity;
import com.arextest.config.model.dto.DesensitizationJar;
import java.util.Map;
import lombok.Data;
import lombok.experimental.FieldNameConstants;

/**
* @author wildeslam.
* @create 2024/2/21 19:43
*/
@Data
@FieldNameConstants
public class SystemConfigurationCollection extends BaseEntity {

public static final String DOCUMENT_NAME = "SystemConfiguration";

/**
* The problem of prohibiting concurrent repeated insertions, the key is unique the function of
* this record
*/
private String key;
private Map<String, Integer> refreshTaskMark;
private DesensitizationJar desensitizationJar;
private String callbackUrl;


public interface KeySummary {
String REFRESH_DATA = "refresh_data";
String DESERIALIZATION_JAR = "deserialization_jar";
String CALLBACK_URL = "callback_url";

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.arextest.config.model.dto;

import lombok.Data;

/**
* @author wildeslam.
* @create 2024/2/20 20:11
*/
@Data
public class DesensitizationJar {
private String jarUrl;
private String remark;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.arextest.config.model.dto;

import java.util.List;
import java.util.Map;
import lombok.Data;

/**
* @author wildeslam.
* @create 2024/2/21 19:54
*/
@Data
public class SystemConfiguration {
/**
* The problem of prohibiting concurrent repeated insertions, the key is unique the function of
* this record
*/
private String key;
private Map<String, Integer> refreshTaskMark;
private DesensitizationJar desensitizationJar;
private String callbackUrl;

public static SystemConfiguration mergeConfigs(List<SystemConfiguration> systemConfigurations) {
if (systemConfigurations == null || systemConfigurations.isEmpty()) {
return new SystemConfiguration();
}
SystemConfiguration result = new SystemConfiguration();
for (SystemConfiguration systemConfiguration : systemConfigurations) {
if (systemConfiguration == null) {
continue;
}
if (systemConfiguration.getRefreshTaskMark() != null) {
result.setRefreshTaskMark(systemConfiguration.getRefreshTaskMark());
}
if (systemConfiguration.getDesensitizationJar() != null) {
result.setDesensitizationJar(systemConfiguration.getDesensitizationJar());
}
if (systemConfiguration.getCallbackUrl() != null) {
result.setCallbackUrl(systemConfiguration.getCallbackUrl());
}
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.arextest.config.repository;

import com.arextest.config.model.dto.SystemConfiguration;
import java.util.List;

/**
* @author wildeslam.
* @create 2024/2/21 19:57
*/
public interface SystemConfigurationRepository {
boolean saveConfig(SystemConfiguration systemConfig);

List<SystemConfiguration> getAllSystemConfigList();

SystemConfiguration getSystemConfigByKey(String key);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.arextest.config.repository.impl;

import com.arextest.config.mapper.SystemConfigurationMapper;
import com.arextest.config.model.dao.config.SystemConfigurationCollection;
import com.arextest.config.model.dao.config.SystemConfigurationCollection.Fields;
import com.arextest.config.model.dto.SystemConfiguration;
import com.arextest.config.repository.SystemConfigurationRepository;
import com.arextest.config.utils.MongoHelper;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.model.Updates;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.PostConstruct;
import org.bson.conversions.Bson;

/**
* @author wildeslam.
* @create 2024/2/21 19:58
*/
public class SystemConfigurationRepositoryImpl implements SystemConfigurationRepository {

MongoCollection<SystemConfigurationCollection> mongoCollection;
private MongoDatabase mongoDatabase;

public SystemConfigurationRepositoryImpl(MongoDatabase mongoDatabase) {
this.mongoDatabase = mongoDatabase;
}

@PostConstruct
public void init() {
this.mongoCollection = mongoDatabase.getCollection(SystemConfigurationCollection.DOCUMENT_NAME,
SystemConfigurationCollection.class);
}

@Override
public boolean saveConfig(SystemConfiguration systemConfig) {
Bson filter = Filters.eq(Fields.key, systemConfig.getKey());
List<Bson> updateList = Arrays.asList(MongoHelper.getUpdate(),
MongoHelper.getFullProperties(systemConfig));
Bson updateCombine = Updates.combine(updateList);
return mongoCollection.updateOne(filter, updateCombine, new UpdateOptions().upsert(true))
.getModifiedCount() > 0;
}

@Override
public List<SystemConfiguration> getAllSystemConfigList() {
List<SystemConfiguration> systemConfigurations = new ArrayList<>();
try (MongoCursor<SystemConfigurationCollection> cursor = mongoCollection.find().iterator()) {
while (cursor.hasNext()) {
SystemConfigurationCollection document = cursor.next();
SystemConfiguration dto = SystemConfigurationMapper.INSTANCE.dtoFromDao(document);
systemConfigurations.add(dto);
}
}
return systemConfigurations;
}

@Override
public SystemConfiguration getSystemConfigByKey(String key) {
Bson filter = Filters.eq(Fields.key, key);
SystemConfigurationCollection collection = mongoCollection.find(filter).first();
return collection == null ? null : SystemConfigurationMapper.INSTANCE.dtoFromDao(collection);
}
}
2 changes: 1 addition & 1 deletion arex-storage-model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<artifactId>arex-storage-service</artifactId>
<groupId>com.arextest</groupId>
<version>1.1.11</version>
<version>1.1.12</version>
</parent>

<profiles>
Expand Down
2 changes: 1 addition & 1 deletion arex-storage-web-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
<parent>
<artifactId>arex-storage-service</artifactId>
<groupId>com.arextest</groupId>
<version>1.1.11</version>
<version>1.1.12</version>
</parent>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.arextest.config.repository.impl.DynamicClassConfigurationRepositoryImpl;
import com.arextest.config.repository.impl.InstancesConfigurationRepositoryImpl;
import com.arextest.config.repository.impl.ServiceCollectConfigurationRepositoryImpl;
import com.arextest.config.repository.impl.SystemConfigurationRepositoryImpl;
import com.mongodb.client.MongoDatabase;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -49,4 +50,10 @@ public InstancesConfigurationRepositoryImpl instancesConfigurationRepositoryImpl
dynamicClassConfigurationRepositoryImpl(MongoDatabase mongoDatabase) {
return new DynamicClassConfigurationRepositoryImpl(mongoDatabase);
}

@Bean
public SystemConfigurationRepositoryImpl systemConfigurationRepositoryImpl(
MongoDatabase mongoDatabase) {
return new SystemConfigurationRepositoryImpl(mongoDatabase);
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -408,5 +408,5 @@
<url>https://github.com/arextest/arex-storage</url>


<version>1.1.11</version>
<version>1.1.12</version>
</project>

0 comments on commit 2ef734e

Please sign in to comment.