mirror of
https://github.com/vran-dev/databasir.git
synced 2025-08-08 18:10:26 +08:00
Feature: support custom document table header (#60)
* feat: template property jooq generate * feat: add template property api * feat: update export logic * feat: update frontend resources
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package com.databasir.core.domain.document.converter;
|
||||
|
||||
import com.databasir.core.domain.document.data.DocumentTemplatePropertiesUpdateRequest;
|
||||
import com.databasir.dao.enums.DocumentTemplatePropertyType;
|
||||
import com.databasir.dao.tables.pojos.DocumentTemplatePropertyPojo;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Mapper(componentModel = "spring")
|
||||
public interface DocumentTemplatePropertiesUpdateRequestConverter {
|
||||
|
||||
@Mapping(target = "defaultValue", constant = "")
|
||||
DocumentTemplatePropertyPojo toPojo(DocumentTemplatePropertiesUpdateRequest.PropertyRequest property,
|
||||
DocumentTemplatePropertyType type);
|
||||
|
||||
default List<DocumentTemplatePropertyPojo> toPojo(DocumentTemplatePropertiesUpdateRequest request) {
|
||||
return request.getProperties().stream()
|
||||
.map(prop -> toPojo(prop, request.getType()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
package com.databasir.core.domain.document.converter;
|
||||
|
||||
import com.databasir.core.domain.document.data.DocumentTemplatePropertiesResponse;
|
||||
import com.databasir.dao.tables.pojos.DocumentTemplatePropertyPojo;
|
||||
import org.mapstruct.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper(componentModel = "spring")
|
||||
public interface DocumentTemplatePropertyResponseConverter {
|
||||
|
||||
List<DocumentTemplatePropertiesResponse.DocumentTemplatePropertyResponse> of(
|
||||
List<DocumentTemplatePropertyPojo> pojoList);
|
||||
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
package com.databasir.core.domain.document.data;
|
||||
|
||||
import com.databasir.dao.enums.DocumentTemplatePropertyType;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class DocumentTemplatePropertiesResponse {
|
||||
|
||||
private List<DocumentTemplatePropertyResponse> columnFieldNameProperties = Collections.emptyList();
|
||||
|
||||
private List<DocumentTemplatePropertyResponse> indexFieldNameProperties = Collections.emptyList();
|
||||
|
||||
private List<DocumentTemplatePropertyResponse> triggerFieldNameProperties = Collections.emptyList();
|
||||
|
||||
private List<DocumentTemplatePropertyResponse> foreignKeyFieldNameProperties = Collections.emptyList();
|
||||
|
||||
@Data
|
||||
public static class DocumentTemplatePropertyResponse {
|
||||
|
||||
private String key;
|
||||
|
||||
private String value;
|
||||
|
||||
private String defaultValue;
|
||||
|
||||
private DocumentTemplatePropertyType type;
|
||||
|
||||
private LocalDateTime createAt;
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
package com.databasir.core.domain.document.data;
|
||||
|
||||
import com.databasir.dao.enums.DocumentTemplatePropertyType;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class DocumentTemplatePropertiesUpdateRequest {
|
||||
|
||||
@NotNull
|
||||
private DocumentTemplatePropertyType type;
|
||||
|
||||
@NotEmpty
|
||||
private List<PropertyRequest> properties = Collections.emptyList();
|
||||
|
||||
@Data
|
||||
public static class PropertyRequest {
|
||||
|
||||
private String key;
|
||||
|
||||
private String value;
|
||||
|
||||
}
|
||||
}
|
@@ -2,8 +2,11 @@ package com.databasir.core.domain.document.generator;
|
||||
|
||||
import com.databasir.common.SystemException;
|
||||
import com.databasir.core.domain.document.data.DatabaseDocumentResponse;
|
||||
import com.databasir.core.domain.document.data.DocumentTemplatePropertiesResponse;
|
||||
import com.databasir.core.domain.document.data.TableDocumentResponse;
|
||||
import com.databasir.core.domain.document.service.DocumentTemplateService;
|
||||
import com.databasir.core.render.markdown.MarkdownBuilder;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -12,16 +15,17 @@ import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class MarkdownDocumentFileGenerator implements DocumentFileGenerator {
|
||||
|
||||
private final DocumentTemplateService documentTemplateService;
|
||||
|
||||
@Override
|
||||
public boolean support(DocumentFileType type) {
|
||||
return type == DocumentFileType.MARKDOWN;
|
||||
@@ -29,8 +33,9 @@ public class MarkdownDocumentFileGenerator implements DocumentFileGenerator {
|
||||
|
||||
@Override
|
||||
public void generate(DocumentFileGenerateContext context, OutputStream outputStream) {
|
||||
DocumentTemplatePropertiesResponse templateProperties = documentTemplateService.getAllProperties();
|
||||
String fileName = context.getDatabaseDocument().getDatabaseName() + "-" + UUID.randomUUID().toString();
|
||||
String data = markdownData(context);
|
||||
String data = markdownData(context, templateProperties);
|
||||
Path tempFile = null;
|
||||
try {
|
||||
tempFile = Files.createTempFile(fileName, ".md");
|
||||
@@ -49,14 +54,37 @@ public class MarkdownDocumentFileGenerator implements DocumentFileGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
private String markdownData(DocumentFileGenerateContext context) {
|
||||
private String markdownData(DocumentFileGenerateContext context,
|
||||
DocumentTemplatePropertiesResponse properties) {
|
||||
DatabaseDocumentResponse doc = context.getDatabaseDocument();
|
||||
MarkdownBuilder builder = MarkdownBuilder.builder();
|
||||
builder.primaryTitle(doc.getDatabaseName());
|
||||
// overview
|
||||
overviewBuild(builder, doc);
|
||||
// tables
|
||||
doc.getTables().forEach(table -> tableBuild(builder, table));
|
||||
Map<String, String> columnTitleMap = properties.getColumnFieldNameProperties()
|
||||
.stream()
|
||||
.collect(Collectors.toMap(d -> d.getKey(),
|
||||
d -> Objects.requireNonNullElse(d.getValue(), d.getDefaultValue())));
|
||||
Map<String, String> indexTitleMap = properties.getIndexFieldNameProperties()
|
||||
.stream()
|
||||
.collect(Collectors.toMap(d -> d.getKey(),
|
||||
d -> Objects.requireNonNullElse(d.getValue(), d.getDefaultValue())));
|
||||
Map<String, String> triggerTitleMap = properties.getTriggerFieldNameProperties()
|
||||
.stream()
|
||||
.collect(Collectors.toMap(d -> d.getKey(),
|
||||
d -> Objects.requireNonNullElse(d.getValue(), d.getDefaultValue())));
|
||||
Map<String, String> foreignKeyTitleMap = properties.getForeignKeyFieldNameProperties()
|
||||
.stream()
|
||||
.collect(Collectors.toMap(d -> d.getKey(),
|
||||
d -> Objects.requireNonNullElse(d.getValue(), d.getDefaultValue())));
|
||||
doc.getTables().forEach(table -> {
|
||||
builder.secondTitle(table.getName());
|
||||
columnBuild(builder, table, columnTitleMap);
|
||||
indexBuild(builder, table, indexTitleMap);
|
||||
foreignKeyBuild(builder, table, foreignKeyTitleMap);
|
||||
triggerBuild(builder, table, triggerTitleMap);
|
||||
});
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@@ -71,15 +99,9 @@ public class MarkdownDocumentFileGenerator implements DocumentFileGenerator {
|
||||
builder.table(List.of("", "表名", "类型", "备注"), overviewContent);
|
||||
}
|
||||
|
||||
private void tableBuild(MarkdownBuilder builder, TableDocumentResponse table) {
|
||||
builder.secondTitle(table.getName());
|
||||
columnBuild(builder, table);
|
||||
indexBuild(builder, table);
|
||||
foreignKeyBuild(builder, table);
|
||||
triggerBuild(builder, table);
|
||||
}
|
||||
|
||||
private void columnBuild(MarkdownBuilder builder, TableDocumentResponse table) {
|
||||
private void columnBuild(MarkdownBuilder builder,
|
||||
TableDocumentResponse table,
|
||||
Map<String, String> titleMap) {
|
||||
Function<TableDocumentResponse.ColumnDocumentResponse, String>
|
||||
columnDefaultValueMapping = column -> {
|
||||
if (Objects.equals(column.getNullable(), "YES")) {
|
||||
@@ -107,11 +129,24 @@ public class MarkdownDocumentFileGenerator implements DocumentFileGenerator {
|
||||
columnDefaultValueMapping.apply(column),
|
||||
column.getComment()));
|
||||
}
|
||||
builder.table(List.of("", "名称", "类型", "是否为主键", "可为空", "自增", "默认值", "备注"),
|
||||
columnContent);
|
||||
builder.table(
|
||||
List.of(
|
||||
"",
|
||||
titleMap.getOrDefault("name", "name"),
|
||||
titleMap.getOrDefault("type", "type"),
|
||||
titleMap.getOrDefault("isPrimaryKey", "isPrimaryKey"),
|
||||
titleMap.getOrDefault("nullable", "nullable"),
|
||||
titleMap.getOrDefault("autoIncrement", "autoIncrement"),
|
||||
titleMap.getOrDefault("defaultValue", "defaultValue"),
|
||||
titleMap.getOrDefault("comment", "comment")
|
||||
),
|
||||
columnContent
|
||||
);
|
||||
}
|
||||
|
||||
private void indexBuild(MarkdownBuilder builder, TableDocumentResponse table) {
|
||||
private void indexBuild(MarkdownBuilder builder,
|
||||
TableDocumentResponse table,
|
||||
Map<String, String> titleMap) {
|
||||
builder.thirdTitle("Indexes");
|
||||
List<List<String>> indexContent = new ArrayList<>();
|
||||
for (int i = 0; i < table.getIndexes().size(); i++) {
|
||||
@@ -120,11 +155,20 @@ public class MarkdownDocumentFileGenerator implements DocumentFileGenerator {
|
||||
String isUnique = index.getIsUnique() ? "YES" : "NO";
|
||||
indexContent.add(List.of((i + 1) + "", index.getName(), isUnique, columnNames));
|
||||
}
|
||||
builder.table(List.of("", "名称", "是否唯一", "关联列"), indexContent);
|
||||
|
||||
builder.table(
|
||||
List.of(
|
||||
"",
|
||||
titleMap.getOrDefault("name", "name"),
|
||||
titleMap.getOrDefault("isUnique", "isUnique"),
|
||||
titleMap.getOrDefault("columnNames", "columnNames")
|
||||
),
|
||||
indexContent
|
||||
);
|
||||
}
|
||||
|
||||
private void foreignKeyBuild(MarkdownBuilder builder, TableDocumentResponse table) {
|
||||
private void foreignKeyBuild(MarkdownBuilder builder,
|
||||
TableDocumentResponse table,
|
||||
Map<String, String> titleMap) {
|
||||
if (!table.getForeignKeys().isEmpty()) {
|
||||
List<List<String>> foreignKeys = new ArrayList<>();
|
||||
builder.thirdTitle("Foreign Keys");
|
||||
@@ -139,14 +183,24 @@ public class MarkdownDocumentFileGenerator implements DocumentFileGenerator {
|
||||
foreignKeys.add(item);
|
||||
}
|
||||
builder.table(
|
||||
List.of("", "FK Name", "FK Column", "PK Name", "PK Table", "PK Column",
|
||||
"Update Rule", "Delete Rule"),
|
||||
List.of(
|
||||
"",
|
||||
titleMap.getOrDefault("fkName", "fkName"),
|
||||
titleMap.getOrDefault("fkColumnName", "fkColumnName"),
|
||||
titleMap.getOrDefault("pkName", "pkName"),
|
||||
titleMap.getOrDefault("pkTableName", "pkTableName"),
|
||||
titleMap.getOrDefault("pkColumnName", "pkColumnName"),
|
||||
titleMap.getOrDefault("updateRule", "updateRule"),
|
||||
titleMap.getOrDefault("deleteRule", "deleteRule")
|
||||
),
|
||||
foreignKeys
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private void triggerBuild(MarkdownBuilder builder, TableDocumentResponse table) {
|
||||
private void triggerBuild(MarkdownBuilder builder,
|
||||
TableDocumentResponse table,
|
||||
Map<String, String> titleMap) {
|
||||
if (!table.getTriggers().isEmpty()) {
|
||||
List<List<String>> triggerContent = new ArrayList<>();
|
||||
for (int i = 0; i < table.getTriggers().size(); i++) {
|
||||
@@ -158,7 +212,16 @@ public class MarkdownDocumentFileGenerator implements DocumentFileGenerator {
|
||||
trigger.getStatement()));
|
||||
}
|
||||
builder.thirdTitle("Triggers");
|
||||
builder.table(List.of("", "名称", "timing", "manipulation", "statement"), triggerContent);
|
||||
builder.table(
|
||||
List.of(
|
||||
"",
|
||||
titleMap.getOrDefault("name", "name"),
|
||||
titleMap.getOrDefault("timing", "timing"),
|
||||
titleMap.getOrDefault("manipulation", "manipulation"),
|
||||
titleMap.getOrDefault("statement", "statement")
|
||||
),
|
||||
triggerContent
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,46 @@
|
||||
package com.databasir.core.domain.document.service;
|
||||
|
||||
import com.databasir.core.domain.document.converter.DocumentTemplatePropertiesUpdateRequestConverter;
|
||||
import com.databasir.core.domain.document.converter.DocumentTemplatePropertyResponseConverter;
|
||||
import com.databasir.core.domain.document.data.DocumentTemplatePropertiesResponse;
|
||||
import com.databasir.core.domain.document.data.DocumentTemplatePropertiesUpdateRequest;
|
||||
import com.databasir.dao.impl.DocumentTemplatePropertyDao;
|
||||
import com.databasir.dao.tables.pojos.DocumentTemplatePropertyPojo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.databasir.dao.enums.DocumentTemplatePropertyType.*;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class DocumentTemplateService {
|
||||
|
||||
private final DocumentTemplatePropertyDao documentTemplatePropertyDao;
|
||||
|
||||
private final DocumentTemplatePropertyResponseConverter documentTemplatePropertyResponseConverter;
|
||||
|
||||
private final DocumentTemplatePropertiesUpdateRequestConverter documentTemplatePropertiesUpdateRequestConverter;
|
||||
|
||||
public DocumentTemplatePropertiesResponse getAllProperties() {
|
||||
List<DocumentTemplatePropertyPojo> properties = documentTemplatePropertyDao.selectAll();
|
||||
var propertiesGroupByType = documentTemplatePropertyResponseConverter.of(properties)
|
||||
.stream()
|
||||
.collect(Collectors.groupingBy(d -> d.getType()));
|
||||
return DocumentTemplatePropertiesResponse.builder()
|
||||
.columnFieldNameProperties(propertiesGroupByType.get(COLUMN_FIELD_NAME))
|
||||
.foreignKeyFieldNameProperties(propertiesGroupByType.get(FOREIGN_KEY_FIELD_NAME))
|
||||
.indexFieldNameProperties(propertiesGroupByType.get(INDEX_FIELD_NAME))
|
||||
.triggerFieldNameProperties(propertiesGroupByType.get(TRIGGER_FIELD_NAME))
|
||||
.build();
|
||||
}
|
||||
|
||||
public void updateByType(DocumentTemplatePropertiesUpdateRequest request) {
|
||||
List<DocumentTemplatePropertyPojo> pojoList = documentTemplatePropertiesUpdateRequestConverter.toPojo(request);
|
||||
documentTemplatePropertyDao.batchInsertOnDuplicateKeyUpdate(pojoList);
|
||||
}
|
||||
}
|
@@ -1,72 +1,22 @@
|
||||
package com.databasir.core.domain.system.service;
|
||||
|
||||
import com.databasir.common.codec.Aes;
|
||||
import com.databasir.common.codec.Rsa;
|
||||
import com.databasir.core.domain.DomainErrors;
|
||||
import com.databasir.core.domain.system.data.SystemEmailResponse;
|
||||
import com.databasir.core.domain.system.data.SystemEmailUpdateRequest;
|
||||
import com.databasir.dao.impl.SysKeyDao;
|
||||
import com.databasir.dao.impl.SysMailDao;
|
||||
import com.databasir.dao.impl.UserDao;
|
||||
import com.databasir.dao.impl.UserRoleDao;
|
||||
import com.databasir.dao.tables.pojos.SysKeyPojo;
|
||||
import com.databasir.dao.tables.pojos.SysMailPojo;
|
||||
import com.databasir.dao.tables.pojos.UserPojo;
|
||||
import com.databasir.dao.tables.pojos.UserRolePojo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class SystemService {
|
||||
|
||||
private final SysKeyDao sysKeyDao;
|
||||
|
||||
private final SysMailDao sysMailDao;
|
||||
|
||||
private final UserDao userDao;
|
||||
|
||||
private final UserRoleDao userRoleDao;
|
||||
|
||||
@SuppressWarnings("checkstyle:all")
|
||||
private BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
|
||||
|
||||
@PostConstruct
|
||||
public void postInit() {
|
||||
sysKeyDao.selectOptionTopOne()
|
||||
.orElseGet(() -> {
|
||||
SysKeyPojo pojo = new SysKeyPojo();
|
||||
pojo.setAesKey(Aes.randomBase64Key());
|
||||
Rsa.RsaBase64Key key = Rsa.generateBase64Key();
|
||||
pojo.setRsaPublicKey(key.getPublicBase64Key());
|
||||
pojo.setRsaPrivateKey(key.getPrivateBase64Key());
|
||||
sysKeyDao.insertAndReturnId(pojo);
|
||||
return pojo;
|
||||
});
|
||||
|
||||
String email = "N/A";
|
||||
String username = "databasir";
|
||||
Optional<UserPojo> userOpt = userDao.selectByEmailOrUsername(username);
|
||||
if (!userOpt.isPresent()) {
|
||||
UserPojo admin = new UserPojo();
|
||||
admin.setEmail(email);
|
||||
admin.setUsername(username);
|
||||
admin.setPassword(bCryptPasswordEncoder.encode(username));
|
||||
admin.setEnabled(true);
|
||||
admin.setNickname("Databasir Admin");
|
||||
Integer userId = userDao.insertAndReturnId(admin);
|
||||
UserRolePojo role = new UserRolePojo();
|
||||
role.setUserId(userId);
|
||||
role.setRole("SYS_OWNER");
|
||||
userRoleDao.insertAndReturnId(role);
|
||||
}
|
||||
}
|
||||
|
||||
public void renewKey() {
|
||||
// TODO
|
||||
}
|
||||
|
@@ -7,7 +7,6 @@ import com.databasir.core.infrastructure.mail.MailSender;
|
||||
import com.databasir.dao.impl.ProjectDao;
|
||||
import com.databasir.dao.impl.SysMailDao;
|
||||
import com.databasir.dao.impl.UserDao;
|
||||
import com.databasir.dao.impl.UserRoleDao;
|
||||
import com.databasir.dao.tables.pojos.ProjectPojo;
|
||||
import com.databasir.dao.tables.pojos.UserPojo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -27,8 +26,6 @@ public class DocumentEventSubscriber {
|
||||
|
||||
private final MailSender mailSender;
|
||||
|
||||
private final UserRoleDao userRoleDao;
|
||||
|
||||
private final UserDao userDao;
|
||||
|
||||
private final SysMailDao sysMailDao;
|
||||
|
@@ -0,0 +1,137 @@
|
||||
package com.databasir.core.infrastructure.event.subscriber;
|
||||
|
||||
import com.databasir.common.codec.Aes;
|
||||
import com.databasir.common.codec.Rsa;
|
||||
import com.databasir.core.domain.document.data.TableDocumentResponse;
|
||||
import com.databasir.dao.enums.DocumentTemplatePropertyType;
|
||||
import com.databasir.dao.impl.DocumentTemplatePropertyDao;
|
||||
import com.databasir.dao.impl.SysKeyDao;
|
||||
import com.databasir.dao.impl.UserDao;
|
||||
import com.databasir.dao.impl.UserRoleDao;
|
||||
import com.databasir.dao.tables.pojos.DocumentTemplatePropertyPojo;
|
||||
import com.databasir.dao.tables.pojos.SysKeyPojo;
|
||||
import com.databasir.dao.tables.pojos.UserPojo;
|
||||
import com.databasir.dao.tables.pojos.UserRolePojo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class SystemStartedEventSubscriber {
|
||||
|
||||
private final SysKeyDao sysKeyDao;
|
||||
|
||||
private final UserDao userDao;
|
||||
|
||||
private final UserRoleDao userRoleDao;
|
||||
|
||||
private final DocumentTemplatePropertyDao documentTemplatePropertyDao;
|
||||
|
||||
@SuppressWarnings("checkstyle:all")
|
||||
private BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
|
||||
|
||||
@EventListener(classes = ContextRefreshedEvent.class)
|
||||
public void onStarted(ContextRefreshedEvent event) {
|
||||
log.info("begin to init system data");
|
||||
initSysOwnerIfNecessary();
|
||||
initDatabaseTypesIfNecessary();
|
||||
initTemplatePropertiesIfNecessary();
|
||||
log.info("system data init finished");
|
||||
}
|
||||
|
||||
private void initTemplatePropertiesIfNecessary() {
|
||||
List<String> ignoreFields = List.of("createAt", "discussionCount", "id");
|
||||
BiFunction<Field, DocumentTemplatePropertyType, DocumentTemplatePropertyPojo> mapping = (field, type) -> {
|
||||
String key = field.getName();
|
||||
String def = field.getName();
|
||||
DocumentTemplatePropertyPojo pojo = new DocumentTemplatePropertyPojo();
|
||||
pojo.setType(type);
|
||||
pojo.setKey(key);
|
||||
pojo.setDefaultValue(def);
|
||||
return pojo;
|
||||
};
|
||||
// column field name;
|
||||
Field[] columnFields = TableDocumentResponse.ColumnDocumentResponse.class.getDeclaredFields();
|
||||
List<DocumentTemplatePropertyPojo> columnProperties = Arrays.stream(columnFields)
|
||||
.filter(f -> !ignoreFields.contains(f.getName()))
|
||||
.map(field -> mapping.apply(field, DocumentTemplatePropertyType.COLUMN_FIELD_NAME))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// index field name;
|
||||
Field[] indexFields = TableDocumentResponse.IndexDocumentResponse.class.getDeclaredFields();
|
||||
List<DocumentTemplatePropertyPojo> indexProperties = Arrays.stream(indexFields)
|
||||
.filter(f -> !ignoreFields.contains(f.getName()))
|
||||
.map(field -> mapping.apply(field, DocumentTemplatePropertyType.INDEX_FIELD_NAME))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// foreign key field name;
|
||||
Field[] fkFields = TableDocumentResponse.ForeignKeyDocumentResponse.class.getDeclaredFields();
|
||||
List<DocumentTemplatePropertyPojo> fkProperties = Arrays.stream(fkFields)
|
||||
.filter(f -> !ignoreFields.contains(f.getName()))
|
||||
.map(field -> mapping.apply(field, DocumentTemplatePropertyType.FOREIGN_KEY_FIELD_NAME))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// trigger field name;
|
||||
Field[] triggerFields = TableDocumentResponse.TriggerDocumentResponse.class.getDeclaredFields();
|
||||
List<DocumentTemplatePropertyPojo> triggerProperties = Arrays.stream(triggerFields)
|
||||
.filter(f -> !ignoreFields.contains(f.getName()))
|
||||
.map(field -> mapping.apply(field, DocumentTemplatePropertyType.TRIGGER_FIELD_NAME))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<DocumentTemplatePropertyPojo> properties = new ArrayList<>();
|
||||
properties.addAll(columnProperties);
|
||||
properties.addAll(indexProperties);
|
||||
properties.addAll(fkProperties);
|
||||
properties.addAll(triggerProperties);
|
||||
documentTemplatePropertyDao.batchInsertOnDuplicateIgnore(properties);
|
||||
|
||||
}
|
||||
|
||||
private void initDatabaseTypesIfNecessary() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
private void initSysOwnerIfNecessary() {
|
||||
sysKeyDao.selectOptionTopOne()
|
||||
.orElseGet(() -> {
|
||||
SysKeyPojo pojo = new SysKeyPojo();
|
||||
pojo.setAesKey(Aes.randomBase64Key());
|
||||
Rsa.RsaBase64Key key = Rsa.generateBase64Key();
|
||||
pojo.setRsaPublicKey(key.getPublicBase64Key());
|
||||
pojo.setRsaPrivateKey(key.getPrivateBase64Key());
|
||||
sysKeyDao.insertAndReturnId(pojo);
|
||||
return pojo;
|
||||
});
|
||||
|
||||
String email = "N/A";
|
||||
String username = "databasir";
|
||||
Optional<UserPojo> userOpt = userDao.selectByEmailOrUsername(username);
|
||||
if (!userOpt.isPresent()) {
|
||||
UserPojo admin = new UserPojo();
|
||||
admin.setEmail(email);
|
||||
admin.setUsername(username);
|
||||
admin.setPassword(bCryptPasswordEncoder.encode(username));
|
||||
admin.setEnabled(true);
|
||||
admin.setNickname("Databasir Admin");
|
||||
Integer userId = userDao.insertAndReturnId(admin);
|
||||
UserRolePojo role = new UserRolePojo();
|
||||
role.setUserId(userId);
|
||||
role.setRole("SYS_OWNER");
|
||||
userRoleDao.insertAndReturnId(role);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user