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
1 parent
3f25818
commit a43a77d
Showing
11 changed files
with
325 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
65 changes: 65 additions & 0 deletions
65
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,65 @@ | ||
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 java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.*; | ||
|
||
@Component | ||
public class ConceptResultSetExtractor implements ResultSetExtractor<Concept> { | ||
@Autowired | ||
private ConceptResultSetUtil conceptResultSetUtil; | ||
|
||
private record ConceptWithId(Concept c, int id) { | ||
@Override | ||
public boolean equals(Object object) { | ||
if (this == object) return true; | ||
if (object == null || getClass() != object.getClass()) return false; | ||
ConceptWithId conceptWithId = (ConceptWithId) object; | ||
return id == conceptWithId.id; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(id); | ||
} | ||
}; | ||
|
||
@Override | ||
public Concept extractData(ResultSet rs) throws SQLException, DataAccessException { | ||
Map<Integer, List<ConceptWithId>> conceptsByParentId = new HashMap<>(); | ||
ConceptWithId root = null; | ||
while (rs.next()) { | ||
Concept c = switch (ConceptType.toConcept(rs.getString("concept_type"))) { | ||
case Categorical -> conceptResultSetUtil.mapCategorical(rs); | ||
case Continuous -> conceptResultSetUtil.mapContinuous(rs); | ||
}; | ||
ConceptWithId conceptWithId = new ConceptWithId(c, rs.getInt("concept_node_id")); | ||
if (root == null) { root = conceptWithId; } | ||
|
||
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<ConceptWithId> concepts = conceptsByParentId.getOrDefault(parentId, new ArrayList<>()); | ||
concepts.add(conceptWithId); | ||
conceptsByParentId.put(parentId, concepts); | ||
} | ||
} | ||
|
||
|
||
return root == null ? null : seedChildren(root, conceptsByParentId); | ||
} | ||
|
||
private Concept seedChildren(ConceptWithId root, Map<Integer, List<ConceptWithId>> conceptsByParentId) { | ||
List<Concept> children = conceptsByParentId.getOrDefault(root.id, List.of()).stream() | ||
.map(conceptWithId -> seedChildren(conceptWithId, 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.