123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- package org.dbsyncer.parser.model;
- import org.dbsyncer.common.util.CollectionUtils;
- import org.dbsyncer.common.util.DateFormatUtil;
- import org.dbsyncer.common.util.StringUtil;
- import org.dbsyncer.sdk.enums.FilterEnum;
- import org.dbsyncer.sdk.enums.OperationEnum;
- import org.dbsyncer.sdk.filter.CompareFilter;
- import org.dbsyncer.sdk.model.Field;
- import org.dbsyncer.sdk.model.Filter;
- import java.sql.Date;
- import java.sql.Timestamp;
- import java.util.*;
- import java.util.stream.Collectors;
- public class Picker {
- private final List<Field> sourceFields = new ArrayList<>();
- private final List<Field> targetFields = new ArrayList<>();
- private final int sFieldSize;
- private final int tFieldSize;
- private final TableGroup tableGroup;
- private final boolean enabledFilter;
- private List<Filter> add;
- private List<Filter> or;
- public Picker(TableGroup tableGroup) {
- this.tableGroup = tableGroup;
- if (!CollectionUtils.isEmpty(tableGroup.getFieldMapping())) {
- tableGroup.getFieldMapping().forEach(m -> {
- sourceFields.add(m.getSource());
- targetFields.add(m.getTarget());
- });
- }
- this.sFieldSize = sourceFields.size();
- this.tFieldSize = targetFields.size();
- // 解析过滤条件
- List<Filter> filter = tableGroup.getFilter();
- enabledFilter = !CollectionUtils.isEmpty(filter);
- if (enabledFilter) {
- add = filter.stream().filter(f -> StringUtil.equals(f.getOperation(), OperationEnum.AND.getName())).collect(Collectors.toList());
- or = filter.stream().filter(f -> StringUtil.equals(f.getOperation(), OperationEnum.OR.getName())).collect(Collectors.toList());
- }
- }
- public List<Map> pickTargetData(List<Map> source) {
- List<Map> targetMapList = new ArrayList<>();
- if (!CollectionUtils.isEmpty(source)) {
- Map<String, Object> target = null;
- for (Map row : source) {
- target = new HashMap<>();
- exchange(sFieldSize, tFieldSize, sourceFields, targetFields, row, target);
- targetMapList.add(target);
- }
- }
- return targetMapList;
- }
- public List<Map> pickTargetData(List<List<Object>> rows, List<Map> sourceMapList) {
- List<Map> targetMapList = new ArrayList<>();
- if (CollectionUtils.isEmpty(rows)) {
- return targetMapList;
- }
- Map<String, Object> source = null;
- Map<String, Object> target = null;
- for (List<Object> row : rows) {
- // 排除下标不一致的数据
- if (row.size() != getOriginalFields().size()) {
- continue;
- }
- source = new HashMap<>();
- for (int j = 0; j < getOriginalFields().size(); j++) {
- source.put(getOriginalFields().get(j).getName(), row.get(j));
- }
- target = new HashMap<>();
- exchange(sFieldSize, tFieldSize, sourceFields, targetFields, source, target);
- // 根据条件过滤数据
- if (filter(target)) {
- sourceMapList.add(source);
- targetMapList.add(target);
- }
- }
- return targetMapList;
- }
- public List<Object> pickSourceData(Map target) {
- Map<String, Object> source = new HashMap<>();
- if (!CollectionUtils.isEmpty(target)) {
- exchange(tFieldSize, sFieldSize, targetFields, sourceFields, target, source);
- }
- return getFields(sourceFields).stream().map(field -> source.get(field.getName())).collect(Collectors.toList());
- }
- public List<Field> getSourceFields() {
- return getFields(sourceFields);
- }
- public List<Field> getTargetFields() {
- return getFields(targetFields);
- }
- public Map<String, Field> getTargetFieldMap() {
- return targetFields.stream().filter(Objects::nonNull).collect(Collectors.toMap(Field::getName, f -> f, (k1, k2) -> k1));
- }
- private boolean filter(Map<String, Object> row) {
- if (!enabledFilter) {
- return true;
- }
- // where (id > 1 and id < 100) or (id = 100 or id =101)
- // 或 关系(成立任意条件)
- Object value = null;
- for (Filter f : or) {
- value = row.get(f.getName());
- if (null == value) {
- continue;
- }
- if (compareValueWithFilter(f, value)) {
- return true;
- }
- }
- boolean pass = false;
- // 并 关系(成立所有条件)
- for (Filter f : add) {
- value = row.get(f.getName());
- if (null == value) {
- continue;
- }
- if (!compareValueWithFilter(f, value)) {
- return false;
- }
- pass = true;
- }
- return pass;
- }
- /**
- * 比较值是否满足过滤条件
- *
- * @param filter 过滤器
- * @param comparedValue 比较值
- * @return
- */
- private boolean compareValueWithFilter(Filter filter, Object comparedValue) {
- CompareFilter compareFilter = FilterEnum.getCompareFilter(filter.getFilter());
- // 支持时间比较
- if (comparedValue instanceof Timestamp) {
- Timestamp comparedTimestamp = (Timestamp) comparedValue;
- Timestamp filterTimestamp = DateFormatUtil.stringToTimestamp(filter.getValue());
- return compareFilter.compare(String.valueOf(comparedTimestamp.getTime()), String.valueOf(filterTimestamp.getTime()));
- }
- if (comparedValue instanceof java.sql.Date) {
- java.sql.Date comparedDate = (java.sql.Date) comparedValue;
- Date filterDate = DateFormatUtil.stringToDate(filter.getValue());
- return compareFilter.compare(String.valueOf(comparedDate.getTime()), String.valueOf(filterDate.getTime()));
- }
- return compareFilter.compare(String.valueOf(comparedValue), filter.getValue());
- }
- private void exchange(int sFieldSize, int tFieldSize, List<Field> sFields, List<Field> tFields, Map<String, Object> source, Map<String, Object> target) {
- Field sField = null;
- Field tField = null;
- Object v = null;
- String tFieldName = null;
- for (int k = 0; k < sFieldSize; k++) {
- sField = sFields.get(k);
- if (k < tFieldSize) {
- tField = tFields.get(k);
- }
- if (null != sField && null != tField) {
- v = source.get(sField.getName());
- tFieldName = tField.getName();
- // 映射值
- if (!target.containsKey(tFieldName)) {
- target.put(tFieldName, v);
- continue;
- }
- // 合并值
- target.put(tFieldName, StringUtil.toString(target.get(tFieldName)) + StringUtil.toString(v));
- }
- }
- }
- private List<Field> getFields(List<Field> list) {
- List<Field> fields = new ArrayList<>();
- Set<String> keys = new HashSet<>();
- list.forEach(f -> {
- if (f != null && !keys.contains(f.getName())) {
- fields.add(f);
- keys.add(f.getName());
- }
- });
- keys.clear();
- return Collections.unmodifiableList(fields);
- }
- private List<Field> getOriginalFields() {
- return tableGroup.getSourceTable().getColumn();
- }
- }
|