• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pkiraly / metadata-qa-api / #677

07 May 2025 08:08PM UTC coverage: 87.245% (+0.01%) from 87.231%
#677

push

pkiraly
Implement SonarCloud quality suggestions #159: fix blockers

17 of 24 new or added lines in 9 files covered. (70.83%)

2 existing lines in 1 file now uncovered.

5520 of 6327 relevant lines covered (87.25%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.1
/src/main/java/de/gwdg/metadataqa/api/calculator/FieldExtractor.java
1
package de.gwdg.metadataqa.api.calculator;
2

3
import com.jayway.jsonpath.InvalidJsonException;
4
import de.gwdg.metadataqa.api.counter.FieldCounter;
5
import de.gwdg.metadataqa.api.interfaces.Calculator;
6
import de.gwdg.metadataqa.api.interfaces.MetricResult;
7
import de.gwdg.metadataqa.api.json.DataElement;
8
import de.gwdg.metadataqa.api.model.EdmFieldInstance;
9
import de.gwdg.metadataqa.api.model.selector.Selector;
10
import de.gwdg.metadataqa.api.model.XmlFieldInstance;
11
import de.gwdg.metadataqa.api.problemcatalog.FieldCounterBasedResult;
12
import de.gwdg.metadataqa.api.schema.Schema;
13
import de.gwdg.metadataqa.api.util.FileUtils;
14
import de.gwdg.metadataqa.api.util.IdentifierGenerator;
15
import org.apache.commons.lang3.StringUtils;
16

17
import java.io.Serializable;
18
import java.util.ArrayList;
19
import java.util.LinkedHashSet;
20
import java.util.List;
21
import java.util.Set;
22
import java.util.logging.Logger;
23

24
/**
25
 * Field extractor
26
 *
27
 * @author Péter Király <peter.kiraly at gwdg.de>
28
 */
29
public class FieldExtractor implements Calculator, Serializable {
30

31
  private static final Logger LOGGER = Logger.getLogger(FieldExtractor.class.getCanonicalName());
1✔
32

33
  public static final String CALCULATOR_NAME = "fieldExtractor";
34
  public static final String FIELD_NAME = "recordId";
35

36
  private String idPath;
37
  protected String nullValue = "";
1✔
38
  protected Schema schema;
39
  private boolean generatedIdentifierEnabled;
40

41
  public FieldExtractor() {
×
42
  }
×
43

44
  public FieldExtractor(Schema schema) {
1✔
45
    this.schema = schema;
1✔
46
    if (schema.getExtractableFields().get(FIELD_NAME) != null)
1✔
47
      setIdPath(schema.getExtractableFields().get(FIELD_NAME));
1✔
48
  }
1✔
49

50
  public FieldExtractor(String idPath) {
1✔
51
    this.idPath = idPath;
1✔
52
  }
1✔
53

54
  @Override
55
  public String getCalculatorName() {
56
    return CALCULATOR_NAME;
1✔
57
  }
58

59
  @Override
60
  public List<MetricResult> measure(Selector cache)
61
      throws InvalidJsonException {
62
    FieldCounter<String> resultMap = new FieldCounter<>();
1✔
63
    if (idPath != null)
1✔
64
      extractSingleField(cache, resultMap, idPath, FIELD_NAME);
1✔
65

66
    if (schema != null) {
1✔
67
      String path;
68
      DataElement dataELement;
69
      for (String fieldName : schema.getExtractableFields().keySet()) {
1✔
70
        if (idPath == null || !fieldName.equals(FIELD_NAME)) {
1✔
71
          dataELement = schema.getPathByLabel(fieldName);
1✔
72
          path = schema.getExtractableFields().get(fieldName);
1✔
73
          extractSingleField(cache, resultMap, path, fieldName, dataELement);
1✔
74
        }
75
      }
1✔
76
    }
77
    return List.of(new FieldCounterBasedResult<>(getCalculatorName(), resultMap).withNoCompression());
1✔
78
  }
79

80
  private void extractSingleField(Selector cache,
81
                                  FieldCounter<String> resultMap,
82
                                  String path,
83
                                  String fieldName) {
84
    extractSingleField(cache, resultMap, path, fieldName,null);
1✔
85
  }
1✔
86

87
  private void extractSingleField(Selector cache,
88
                                    FieldCounter<String> resultMap,
89
                                    String path,
90
                                    String fieldName,
91
                                    DataElement dataELement) {
92
    List<XmlFieldInstance> fieldInstances;
93
    if (dataELement != null) {
1✔
94
      fieldInstances = cache.get(dataELement);
1✔
95
    } else {
96
      fieldInstances = cache.get(path);
1✔
97
    }
98
    String value = null;
1✔
99
    if (fieldInstances == null || fieldInstances.isEmpty() || fieldInstances.get(0) == null) {
1✔
100
      value = nullValue;
1✔
101
    } else {
102
      Set<String> values = new LinkedHashSet<>();
1✔
103
      for (XmlFieldInstance instance : fieldInstances) {
1✔
104
        boolean isEdm = instance instanceof EdmFieldInstance;
1✔
105
        if (isEdm && ((EdmFieldInstance)instance).getResource() != null) {
1✔
106
          value = ((EdmFieldInstance) instance).getResource();
×
107
        } else if (instance.getValue() != null) {
1✔
108
          value = instance.getValue();
1✔
109
        }
110
        // if (!values.contains(values))
111
        if (StringUtils.isNotBlank(value))
1✔
112
          values.add(value);
1✔
113
      }
1✔
114
      value = StringUtils.join(values, " --- ");
1✔
115
    }
116
    if (fieldName.equals(FIELD_NAME) && StringUtils.isBlank(value)) {
1✔
117
      value = cache.getRecordId() != null ?
×
NEW
118
        IdentifierGenerator.prefix + cache.getRecordId() : IdentifierGenerator.generate();
×
119
    }
120

121
    // LOGGER.info(String.format("fieldName: %s, value: %s", fieldName, value));
122
    resultMap.put(fieldName, value);
1✔
123
    cache.setRecordId(value);
1✔
124
  }
1✔
125

126
  public String getIdPath() {
127
    return idPath;
×
128
  }
129

130
  public void setIdPath(String idPath) {
131
    this.idPath = idPath;
1✔
132
  }
1✔
133

134
  @Override
135
  public List<String> getHeader() {
136
    List<String> headers = new ArrayList<>();
1✔
137
    if (idPath != null)
1✔
138
      headers.add(FIELD_NAME);
1✔
139

140
    if (schema != null)
1✔
141
      for (String fieldName : schema.getExtractableFields().keySet())
1✔
142
        if (idPath == null || !fieldName.equals(FIELD_NAME))
1✔
143
          headers.add(FileUtils.escape(fieldName));
1✔
144

145
    return headers;
1✔
146
  }
147

148
  public void enableGeneratedIdentifier() {
149
    generatedIdentifierEnabled = true;
×
150
  }
×
151
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc