Picker.java 3.1 KB

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