|
@@ -1,16 +1,11 @@
|
|
|
package org.dbsyncer.biz.impl;
|
|
|
|
|
|
-import com.sun.org.apache.xpath.internal.operations.Bool;
|
|
|
-import org.apache.commons.lang.StringUtils;
|
|
|
-import org.dbsyncer.biz.BizException;
|
|
|
import org.dbsyncer.biz.TableGroupService;
|
|
|
import org.dbsyncer.biz.checker.Checker;
|
|
|
import org.dbsyncer.common.util.CollectionUtils;
|
|
|
-import org.dbsyncer.common.util.JsonUtil;
|
|
|
import org.dbsyncer.connector.config.Field;
|
|
|
-import org.dbsyncer.connector.config.MetaInfo;
|
|
|
-import org.dbsyncer.connector.config.Table;
|
|
|
import org.dbsyncer.manager.Manager;
|
|
|
+import org.dbsyncer.parser.model.ConfigModel;
|
|
|
import org.dbsyncer.parser.model.Mapping;
|
|
|
import org.dbsyncer.parser.model.TableGroup;
|
|
|
import org.slf4j.Logger;
|
|
@@ -19,10 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* @author AE86
|
|
@@ -42,37 +34,19 @@ public class TableGroupServiceImpl implements TableGroupService {
|
|
|
|
|
|
@Override
|
|
|
public String add(Map<String, String> params) {
|
|
|
+ ConfigModel model = tableGroupChecker.checkAddConfigModel(params);
|
|
|
+ String id = manager.addTableGroup(model);
|
|
|
+
|
|
|
String mappingId = params.get("mappingId");
|
|
|
- String sourceTable = params.get("sourceTable");
|
|
|
- String targetTable = params.get("targetTable");
|
|
|
Assert.hasText(mappingId, "tableGroup mappingId is empty.");
|
|
|
- Assert.hasText(sourceTable, "tableGroup sourceTable is empty.");
|
|
|
- Assert.hasText(targetTable, "tableGroup targetTable is empty.");
|
|
|
- Mapping mapping = manager.getMapping(mappingId);
|
|
|
- Assert.notNull(mapping, "mapping can not be null.");
|
|
|
-
|
|
|
- // 检查是否存在重复映射关系
|
|
|
- checkRepeatedTable(mappingId, sourceTable, targetTable);
|
|
|
-
|
|
|
- // 读取表信息
|
|
|
- TableGroup tableGroup = new TableGroup();
|
|
|
- tableGroup.setMappingId(mappingId);
|
|
|
- Table sTable = getTable(mapping.getSourceConnectorId(), sourceTable);
|
|
|
- Table tTable = getTable(mapping.getTargetConnectorId(), targetTable);
|
|
|
- tableGroup.setSourceTable(sTable);
|
|
|
- tableGroup.setTargetTable(tTable);
|
|
|
-
|
|
|
- // 合并驱动公共字段
|
|
|
- mergeMappingColumn(mapping, sTable.getColumn(), tTable.getColumn());
|
|
|
-
|
|
|
- String json = JsonUtil.objToJson(tableGroup);
|
|
|
- return manager.addTableGroup(json);
|
|
|
+ mergeMappingColumn(mappingId);
|
|
|
+ return id;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String edit(Map<String, String> params) {
|
|
|
- String json = tableGroupChecker.checkConfigModel(params);
|
|
|
- return manager.editTableGroup(json);
|
|
|
+ ConfigModel model = tableGroupChecker.checkEditConfigModel(params);
|
|
|
+ return manager.editTableGroup(model);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -81,14 +55,7 @@ public class TableGroupServiceImpl implements TableGroupService {
|
|
|
Assert.notNull(tableGroup, "tableGroup can not be null.");
|
|
|
|
|
|
manager.removeTableGroup(id);
|
|
|
- Mapping mapping = manager.getMapping(tableGroup.getMappingId());
|
|
|
- Assert.notNull(mapping, "mapping not exist.");
|
|
|
-
|
|
|
- // 合并驱动公共字段
|
|
|
- Table sTable = tableGroup.getSourceTable();
|
|
|
- Table tTable = tableGroup.getTargetTable();
|
|
|
- mergeMappingColumn(mapping, sTable.getColumn(), tTable.getColumn());
|
|
|
-
|
|
|
+ mergeMappingColumn(tableGroup.getMappingId());
|
|
|
return true;
|
|
|
}
|
|
|
|
|
@@ -104,42 +71,33 @@ public class TableGroupServiceImpl implements TableGroupService {
|
|
|
return manager.getTableGroupAll(mappingId);
|
|
|
}
|
|
|
|
|
|
- private Table getTable(String connectorId, String tableName) {
|
|
|
- MetaInfo metaInfo = manager.getMetaInfo(connectorId, tableName);
|
|
|
- Assert.notNull(metaInfo, "无法获取连接信息.");
|
|
|
- return new Table().setName(tableName).setColumn(metaInfo.getColumn());
|
|
|
- }
|
|
|
+ private void mergeMappingColumn(String mappingId) {
|
|
|
+ List<TableGroup> groups = manager.getTableGroupAll(mappingId);
|
|
|
|
|
|
- private void checkRepeatedTable(String mappingId, String sourceTable, String targetTable) {
|
|
|
- List<TableGroup> list = manager.getTableGroupAll(mappingId);
|
|
|
- if (!CollectionUtils.isEmpty(list)) {
|
|
|
- for (TableGroup g : list) {
|
|
|
- // 数据源表和目标表都存在
|
|
|
- if(StringUtils.equals(sourceTable, g.getSourceTable().getName()) && StringUtils.equals(targetTable, g.getTargetTable().getName())){
|
|
|
- final String error = String.format("映射关系[%s]>>[%s]已存在.", sourceTable, targetTable);
|
|
|
- logger.error(error);
|
|
|
- throw new BizException(error);
|
|
|
- }
|
|
|
- }
|
|
|
+ Mapping mapping = manager.getMapping(mappingId);
|
|
|
+ Assert.notNull(mapping, "mapping not exist.");
|
|
|
+
|
|
|
+ List<Field> sourceColumn = null;
|
|
|
+ List<Field> targetColumn = null;
|
|
|
+ for (TableGroup g : groups) {
|
|
|
+ sourceColumn = pickCommonFields(sourceColumn, g.getSourceTable().getColumn());
|
|
|
+ targetColumn = pickCommonFields(targetColumn, g.getTargetTable().getColumn());
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- private void mergeMappingColumn(Mapping mapping, List<Field> sColumn, List<Field> tColumn) {
|
|
|
- mapping.setSourceColumn(pickCommonFields(mapping.getSourceColumn(), sColumn));
|
|
|
- mapping.setTargetColumn(pickCommonFields(mapping.getTargetColumn(), tColumn));
|
|
|
- String json = JsonUtil.objToJson(mapping);
|
|
|
- manager.editMapping(json);
|
|
|
+ mapping.setSourceColumn(sourceColumn);
|
|
|
+ mapping.setTargetColumn(targetColumn);
|
|
|
+ manager.editMapping(mapping);
|
|
|
}
|
|
|
|
|
|
private List<Field> pickCommonFields(List<Field> column, List<Field> target) {
|
|
|
- if(CollectionUtils.isEmpty(column) || CollectionUtils.isEmpty(target)){
|
|
|
+ if (CollectionUtils.isEmpty(column) || CollectionUtils.isEmpty(target)) {
|
|
|
return target;
|
|
|
}
|
|
|
List<Field> list = new ArrayList<>();
|
|
|
- Map<String, Boolean> map = new HashMap<>(column.size());
|
|
|
- column.forEach(f -> map.putIfAbsent(f.getName(), true));
|
|
|
+ Set<String> keys = new HashSet<>(column.size());
|
|
|
+ column.forEach(f -> keys.add(f.getName()));
|
|
|
target.forEach(f -> {
|
|
|
- if(map.get(f.getName())){
|
|
|
+ if (keys.contains(f.getName())) {
|
|
|
list.add(f);
|
|
|
}
|
|
|
});
|