Picker.java 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package org.dbsyncer.parser.model;
  2. import org.dbsyncer.common.util.CollectionUtils;
  3. import org.dbsyncer.common.util.StringUtil;
  4. import org.dbsyncer.sdk.model.Field;
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7. import java.util.HashMap;
  8. import java.util.HashSet;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.Set;
  12. import java.util.stream.Collectors;
  13. public class Picker {
  14. private List<Field> sourceFields = new ArrayList<>();
  15. private List<Field> targetFields = new ArrayList<>();
  16. public Picker(List<FieldMapping> fieldMapping) {
  17. if (!CollectionUtils.isEmpty(fieldMapping)) {
  18. fieldMapping.forEach(m -> {
  19. sourceFields.add(m.getSource());
  20. targetFields.add(m.getTarget());
  21. });
  22. }
  23. }
  24. public List<Map> pickTargetData(List<Map> source) {
  25. List<Map> targetMapList = new ArrayList<>();
  26. if (!CollectionUtils.isEmpty(source)) {
  27. final int size = source.size();
  28. final int sFieldSize = sourceFields.size();
  29. final int tFieldSize = targetFields.size();
  30. Map<String, Object> target = null;
  31. for (int i = 0; i < size; i++) {
  32. target = new HashMap<>();
  33. exchange(sFieldSize, tFieldSize, sourceFields, targetFields, source.get(i), target);
  34. targetMapList.add(target);
  35. }
  36. }
  37. return targetMapList;
  38. }
  39. public Map pickSourceData(Map target) {
  40. Map<String, Object> source = new HashMap<>();
  41. if (!CollectionUtils.isEmpty(target)) {
  42. exchange(targetFields.size(), sourceFields.size(), targetFields, sourceFields, target, source);
  43. }
  44. return source;
  45. }
  46. private void exchange(int sFieldSize, int tFieldSize, List<Field> sFields, List<Field> tFields, Map<String, Object> source, Map<String, Object> target) {
  47. Field sField = null;
  48. Field tField = null;
  49. Object v = null;
  50. String tFieldName = null;
  51. for (int k = 0; k < sFieldSize; k++) {
  52. sField = sFields.get(k);
  53. if (k < tFieldSize) {
  54. tField = tFields.get(k);
  55. }
  56. if (null != sField && null != tField) {
  57. v = source.get(sField.isUnmodifiabled() ? sField.getLabelName() : sField.getName());
  58. tFieldName = tField.getName();
  59. // 映射值
  60. if (!target.containsKey(tFieldName)) {
  61. target.put(tFieldName, v);
  62. continue;
  63. }
  64. // 合并值
  65. String mergedValue = new StringBuilder(StringUtil.toString(target.get(tFieldName))).append(StringUtil.toString(v)).toString();
  66. target.put(tFieldName, mergedValue);
  67. }
  68. }
  69. }
  70. public List<Field> getTargetFields() {
  71. List<Field> fields = new ArrayList<>();
  72. Set<String> keys = new HashSet<>();
  73. targetFields.stream().forEach(f -> {
  74. if (f != null && !keys.contains(f.getName())) {
  75. fields.add(f);
  76. keys.add(f.getName());
  77. }
  78. });
  79. keys.clear();
  80. return Collections.unmodifiableList(fields);
  81. }
  82. public Map<String, Field> getTargetFieldMap() {
  83. return targetFields.stream().filter(f -> null != f).collect(Collectors.toMap(Field::getName, f -> f, (k1, k2) -> k1));
  84. }
  85. }