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. 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> pickTargetData(List<Map> source) {
  29. List<Map> targetMapList = new ArrayList<>();
  30. if (!CollectionUtils.isEmpty(source)) {
  31. final int size = source.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, source.get(i), target);
  37. targetMapList.add(target);
  38. }
  39. }
  40. return targetMapList;
  41. }
  42. public Map pickSourceData(Map target) {
  43. Map<String, Object> source = new HashMap<>();
  44. if (!CollectionUtils.isEmpty(target)) {
  45. exchange(targetFields.size(), targetFields, sourceFields, target, source);
  46. }
  47. return source;
  48. }
  49. private void exchange(int fieldSize, List<Field> sFields, List<Field> tFields, Map<String, Object> source, Map<String, Object> target) {
  50. Field sField = null;
  51. Field tField = null;
  52. Object v = null;
  53. String tFieldName = null;
  54. for (int k = 0; k < fieldSize; k++) {
  55. sField = sFields.get(k);
  56. tField = tFields.get(k);
  57. if (null != sField && null != tField) {
  58. v = source.get(sField.isUnmodifiabled() ? sField.getLabelName() : sField.getName());
  59. tFieldName = tField.getName();
  60. // 映射值
  61. if (!target.containsKey(tFieldName)) {
  62. target.put(tFieldName, v);
  63. continue;
  64. }
  65. // 合并值
  66. String mergedValue = new StringBuilder(StringUtil.toString(target.get(tFieldName))).append(StringUtil.toString(v)).toString();
  67. target.put(tFieldName, mergedValue);
  68. }
  69. }
  70. }
  71. public List<Field> getTargetFields() {
  72. List<Field> fields = new ArrayList<>();
  73. Set<String> keys = new HashSet<>();
  74. targetFields.forEach(f -> {
  75. if (!keys.contains(f.getName())) {
  76. fields.add(f);
  77. keys.add(f.getName());
  78. }
  79. });
  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. }