|
2 | 2 |
|
3 | 3 | import edu.harvard.dbmi.avillach.dictionary.concept.model.CategoricalConcept;
|
4 | 4 | import edu.harvard.dbmi.avillach.dictionary.concept.model.ContinuousConcept;
|
5 |
| -import org.json.JSONArray; |
6 |
| -import org.json.JSONException; |
| 5 | +import edu.harvard.dbmi.avillach.dictionary.util.JsonBlobParser; |
7 | 6 | import org.slf4j.Logger;
|
8 | 7 | import org.slf4j.LoggerFactory;
|
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
9 | 9 | import org.springframework.stereotype.Component;
|
10 | 10 |
|
11 |
| -import java.math.BigDecimal; |
12 |
| -import java.math.BigInteger; |
13 | 11 | import java.sql.ResultSet;
|
14 | 12 | import java.sql.SQLException;
|
15 |
| -import java.util.ArrayList; |
16 | 13 | import java.util.List;
|
17 |
| -import java.util.stream.Collectors; |
18 | 14 |
|
19 | 15 | @Component
|
20 | 16 | public class ConceptResultSetUtil {
|
21 | 17 |
|
22 | 18 | private static final Logger log = LoggerFactory.getLogger(ConceptResultSetUtil.class);
|
| 19 | + private final JsonBlobParser jsonBlobParser; |
| 20 | + |
| 21 | + @Autowired |
| 22 | + public ConceptResultSetUtil(JsonBlobParser jsonBlobParser) { |
| 23 | + this.jsonBlobParser = jsonBlobParser; |
| 24 | + } |
23 | 25 |
|
24 | 26 | public CategoricalConcept mapCategorical(ResultSet rs) throws SQLException {
|
25 | 27 | return new CategoricalConcept(
|
26 | 28 | rs.getString("concept_path"), rs.getString("name"), rs.getString("display"), rs.getString("dataset"),
|
27 |
| - rs.getString("description"), rs.getString("values") == null ? List.of() : parseValues(rs.getString("values")), |
| 29 | + rs.getString("description"), rs.getString("values") == null ? List.of() : jsonBlobParser.parseValues(rs.getString("values")), |
28 | 30 | rs.getBoolean("allowFiltering"), rs.getString("studyAcronym"), null, null
|
29 | 31 | );
|
30 | 32 | }
|
31 | 33 |
|
32 | 34 | public ContinuousConcept mapContinuous(ResultSet rs) throws SQLException {
|
33 | 35 | return new ContinuousConcept(
|
34 | 36 | rs.getString("concept_path"), rs.getString("name"), rs.getString("display"), rs.getString("dataset"),
|
35 |
| - rs.getString("description"), rs.getBoolean("allowFiltering"), parseMin(rs.getString("values")), |
36 |
| - parseMax(rs.getString("values")), rs.getString("studyAcronym"), null |
| 37 | + rs.getString("description"), rs.getBoolean("allowFiltering"), jsonBlobParser.parseMin(rs.getString("values")), |
| 38 | + jsonBlobParser.parseMax(rs.getString("values")), rs.getString("studyAcronym"), null |
37 | 39 | );
|
38 | 40 | }
|
39 | 41 |
|
40 |
| - public List<String> parseValues(String valuesArr) { |
41 |
| - try { |
42 |
| - ArrayList<String> vals = new ArrayList<>(); |
43 |
| - JSONArray arr = new JSONArray(valuesArr); |
44 |
| - for (int i = 0; i < arr.length(); i++) { |
45 |
| - vals.add(arr.getString(i)); |
46 |
| - } |
47 |
| - return vals; |
48 |
| - } catch (JSONException ex) { |
49 |
| - return List.of(); |
50 |
| - } |
51 |
| - } |
52 | 42 |
|
53 |
| - public Float parseMin(String valuesArr) { |
54 |
| - return parseFromIndex(valuesArr, 0); |
55 |
| - } |
56 | 43 |
|
57 |
| - private Float parseFromIndex(String valuesArr, int index) { |
58 |
| - try { |
59 |
| - JSONArray arr = new JSONArray(valuesArr); |
60 |
| - if (arr.length() != 2) { |
61 |
| - return 0F; |
62 |
| - } |
63 |
| - Object raw = arr.get(index); |
64 |
| - return switch (raw) { |
65 |
| - case Double d -> d.floatValue(); |
66 |
| - case Integer i -> i.floatValue(); |
67 |
| - case String s -> Double.valueOf(s).floatValue(); |
68 |
| - case BigDecimal d -> d.floatValue(); |
69 |
| - case BigInteger i -> i.floatValue(); |
70 |
| - default -> 0f; |
71 |
| - }; |
72 |
| - } catch (JSONException ex) { |
73 |
| - log.warn("Invalid json array for values: ", ex); |
74 |
| - return 0F; |
75 |
| - } catch (NumberFormatException ex) { |
76 |
| - log.warn("Valid json array but invalid val within: ", ex); |
77 |
| - return 0F; |
78 |
| - } |
79 |
| - } |
80 |
| - |
81 |
| - public Float parseMax(String valuesArr) { |
82 |
| - return parseFromIndex(valuesArr, 1); |
83 |
| - } |
84 | 44 | }
|
0 commit comments