forked from Luke-Sikina/picsure-search-refinement
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add to service, repo layer for new query - Make new mapper - Add children to all concepts - Tests!
- Loading branch information
Luke Sikina
committed
Sep 5, 2024
1 parent
3f25818
commit 93cad46
Showing
11 changed files
with
330 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/edu/harvard/dbmi/avillach/dictionary/concept/ConceptResultSetExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package edu.harvard.dbmi.avillach.dictionary.concept; | ||
|
||
import edu.harvard.dbmi.avillach.dictionary.concept.model.Concept; | ||
import edu.harvard.dbmi.avillach.dictionary.concept.model.ConceptType; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.core.ResultSetExtractor; | ||
import org.springframework.stereotype.Component; | ||
import software.amazon.awssdk.services.secretsmanager.endpoints.internal.Value; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.*; | ||
|
||
@Component | ||
public class ConceptResultSetExtractor implements ResultSetExtractor<Concept> { | ||
@Autowired | ||
private ConceptResultSetUtil conceptResultSetUtil; | ||
|
||
private record Wrapper(Concept c, int id) { | ||
@Override | ||
public boolean equals(Object object) { | ||
if (this == object) return true; | ||
if (object == null || getClass() != object.getClass()) return false; | ||
Wrapper wrapper = (Wrapper) object; | ||
return id == wrapper.id; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(id); | ||
} | ||
}; | ||
|
||
@Override | ||
public Concept extractData(ResultSet rs) throws SQLException, DataAccessException { | ||
Map<Integer, List<Wrapper>> conceptsByParentId = new HashMap<>(); | ||
Wrapper root = null; | ||
while (rs.next()) { | ||
Concept c = switch (ConceptType.toConcept(rs.getString("concept_type"))) { | ||
case Categorical -> conceptResultSetUtil.mapCategorical(rs); | ||
case Continuous -> conceptResultSetUtil.mapContinuous(rs); | ||
}; | ||
Wrapper wrapper = new Wrapper(c, rs.getInt("concept_node_id")); | ||
if (root == null) { root = wrapper; } | ||
|
||
int parentId = rs.getInt("parent_id"); | ||
// weirdness: null value for int is 0, so to check for missing parent value, you need the wasNull check | ||
if (!rs.wasNull()) { | ||
List<Wrapper> concepts = conceptsByParentId.getOrDefault(parentId, new ArrayList<>()); | ||
concepts.add(wrapper); | ||
conceptsByParentId.put(parentId, concepts); | ||
} | ||
} | ||
|
||
|
||
return root == null ? null : seedChildren(root, conceptsByParentId); | ||
} | ||
|
||
private Concept seedChildren(Wrapper root, Map<Integer, List<Wrapper>> conceptsByParentId) { | ||
List<Concept> children = conceptsByParentId.getOrDefault(root.id, List.of()).stream() | ||
.map(wrapper -> seedChildren(wrapper, conceptsByParentId)) | ||
.toList(); | ||
return root.c.withChildren(children); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/edu/harvard/dbmi/avillach/dictionary/concept/ConceptResultSetUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package edu.harvard.dbmi.avillach.dictionary.concept; | ||
|
||
import edu.harvard.dbmi.avillach.dictionary.concept.model.CategoricalConcept; | ||
import edu.harvard.dbmi.avillach.dictionary.concept.model.ContinuousConcept; | ||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.List; | ||
|
||
@Component | ||
public class ConceptResultSetUtil { | ||
|
||
public CategoricalConcept mapCategorical(ResultSet rs) throws SQLException { | ||
return new CategoricalConcept( | ||
rs.getString("concept_path"), rs.getString("name"), | ||
rs.getString("display"), rs.getString("dataset"), rs.getString("description"), | ||
rs.getString("values") == null ? List.of() : List.of(rs.getString("values").split(",")), | ||
null, | ||
null | ||
); | ||
} | ||
|
||
public ContinuousConcept mapContinuous(ResultSet rs) throws SQLException { | ||
return new ContinuousConcept( | ||
rs.getString("concept_path"), rs.getString("name"), | ||
rs.getString("display"), rs.getString("dataset"), rs.getString("description"), | ||
parseMin(rs.getString("values")), parseMax(rs.getString("values")), | ||
null | ||
); | ||
} | ||
|
||
public Integer parseMin(String valuesArr) { | ||
try { | ||
JSONArray arr = new JSONArray(valuesArr); | ||
return arr.length() == 2 ? arr.getInt(0) : 0; | ||
} catch (JSONException ex) { | ||
return 0; | ||
} | ||
} | ||
|
||
public Integer parseMax(String valuesArr) { | ||
try { | ||
JSONArray arr = new JSONArray(valuesArr); | ||
return arr.length() == 2 ? arr.getInt(1) : 0; | ||
} catch (JSONException ex) { | ||
return 0; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.