24
24
*/
25
25
public class ConceptSetExpressionQueryBuilder {
26
26
27
- private final static String CONCEPT_SET_QUERY_TEMPLATE = ResourceHelper .GetResourceAsString ("/resources/vocabulary/sql/conceptSetExpression.sql" );
28
- private final static String CONCEPT_SET_EXCLUDE_TEMPLATE = ResourceHelper .GetResourceAsString ("/resources/vocabulary/sql/conceptSetExclude.sql" );
27
+ private final static String CONCEPT_SET_QUERY_TEMPLATE = ResourceHelper .GetResourceAsString ("/resources/vocabulary/sql/conceptSetQuery.sql" );
29
28
private final static String CONCEPT_SET_DESCENDANTS_TEMPLATE = ResourceHelper .GetResourceAsString ("/resources/vocabulary/sql/conceptSetDescendants.sql" );
30
29
private final static String CONCEPT_SET_MAPPED_TEMPLATE = ResourceHelper .GetResourceAsString ("/resources/vocabulary/sql/conceptSetMapped.sql" );
30
+ private final static String CONCEPT_SET_INCLUDE_TEMPLATE = ResourceHelper .GetResourceAsString ("/resources/vocabulary/sql/conceptSetInclude.sql" );
31
+ private final static String CONCEPT_SET_EXCLUDE_TEMPLATE = ResourceHelper .GetResourceAsString ("/resources/vocabulary/sql/conceptSetExclude.sql" );
31
32
32
33
33
34
private ArrayList <Long > getConceptIds (ArrayList <Concept > concepts )
@@ -39,35 +40,51 @@ private ArrayList<Long> getConceptIds(ArrayList<Concept> concepts)
39
40
return conceptIdList ;
40
41
}
41
42
43
+
44
+
45
+ private String buildConceptSetSubQuery (
46
+ ArrayList <Concept > concepts ,
47
+ ArrayList <Concept > descendantConcepts
48
+ )
49
+ {
50
+ ArrayList <String > queries = new ArrayList <>();
51
+ if (concepts .size () > 0 ) {
52
+ queries .add (StringUtils .replace (CONCEPT_SET_QUERY_TEMPLATE , "@conceptIds" , StringUtils .join (getConceptIds (concepts ), "," )));
53
+ }
54
+ if (descendantConcepts .size () > 0 ) {
55
+ queries .add (StringUtils .replace (CONCEPT_SET_DESCENDANTS_TEMPLATE , "@conceptIds" , StringUtils .join (getConceptIds (descendantConcepts ), "," )));
56
+ }
57
+
58
+ return StringUtils .join (queries , "UNION" );
59
+
60
+ }
61
+
62
+ private String buildConceptSetMappedQuery (
63
+ ArrayList <Concept > mappedConcepts ,
64
+ ArrayList <Concept > mappedDescendantConcepts
65
+ ) {
66
+ String conceptSetQuery = buildConceptSetSubQuery (mappedConcepts , mappedDescendantConcepts );
67
+ return StringUtils .replace (CONCEPT_SET_MAPPED_TEMPLATE , "@conceptsetQuery" , conceptSetQuery );
68
+ }
69
+
42
70
private String buildConceptSetQuery (
43
71
ArrayList <Concept > concepts ,
44
72
ArrayList <Concept > descendantConcepts ,
45
- ArrayList <Concept > excludeConcepts ,
46
- ArrayList <Concept > excludeDescendantConcepts )
73
+ ArrayList <Concept > mappedConcepts ,
74
+ ArrayList <Concept > mappedDesandantConcepts )
47
75
{
48
76
if (concepts .size () == 0 )
49
77
{
50
78
return "select concept_id from @cdm_database_schema.CONCEPT where 0=1" ;
51
79
}
52
- String conceptSetQuery = StringUtils .replace (CONCEPT_SET_QUERY_TEMPLATE , "@conceptIds" ,StringUtils .join (getConceptIds (concepts ), "," ));
53
- if (descendantConcepts .size () > 0 ) {
54
- String includeDescendantQuery = StringUtils .replace (CONCEPT_SET_DESCENDANTS_TEMPLATE , "@conceptIds" , StringUtils .join (getConceptIds (descendantConcepts ), "," ));
55
- conceptSetQuery = StringUtils .replace (conceptSetQuery ,"@descendantQuery" , includeDescendantQuery );
56
- } else {
57
- conceptSetQuery = StringUtils .replace (conceptSetQuery , "@descendantQuery" , "" );
58
- }
59
- if (excludeConcepts .size () > 0 )
80
+
81
+ String conceptSetQuery = buildConceptSetSubQuery (concepts , descendantConcepts );
82
+
83
+ if (mappedConcepts .size () > 0 || mappedDesandantConcepts .size () > 0 )
60
84
{
61
- String excludeClause = StringUtils .replace (CONCEPT_SET_EXCLUDE_TEMPLATE ,"@conceptIds" , StringUtils .join (getConceptIds (excludeConcepts ),"," ));
62
- if (excludeDescendantConcepts .size () > 0 ){
63
- String excludeClauseDescendantQuery = StringUtils .replace (CONCEPT_SET_DESCENDANTS_TEMPLATE , "@conceptIds" , StringUtils .join (getConceptIds (excludeDescendantConcepts ), "," ));
64
- excludeClause = StringUtils .replace (excludeClause , "@descendantQuery" , excludeClauseDescendantQuery );
65
- } else {
66
- excludeClause = StringUtils .replace (excludeClause , "@descendantQuery" , "" );
67
- }
68
- conceptSetQuery += excludeClause ;
85
+ buildConceptSetMappedQuery (mappedConcepts ,mappedDesandantConcepts );
86
+ conceptSetQuery += "UNION\n " + buildConceptSetMappedQuery (mappedConcepts ,mappedDesandantConcepts );
69
87
}
70
-
71
88
return conceptSetQuery ;
72
89
}
73
90
@@ -76,11 +93,11 @@ public String buildExpressionQuery(ConceptSetExpression expression)
76
93
// handle included concepts.
77
94
ArrayList <Concept > includeConcepts = new ArrayList <>();
78
95
ArrayList <Concept > includeDescendantConcepts = new ArrayList <>();
79
- ArrayList <Concept > excludeConcepts = new ArrayList <>();
80
- ArrayList <Concept > excludeDescendantConcepts = new ArrayList <>();
81
-
82
96
ArrayList <Concept > includeMappedConcepts = new ArrayList <>();
83
97
ArrayList <Concept > includeMappedDescendantConcepts = new ArrayList <>();
98
+
99
+ ArrayList <Concept > excludeConcepts = new ArrayList <>();
100
+ ArrayList <Concept > excludeDescendantConcepts = new ArrayList <>();
84
101
ArrayList <Concept > excludeMappedConcepts = new ArrayList <>();
85
102
ArrayList <Concept > excludeMappedDescendantConcepts = new ArrayList <>();
86
103
@@ -115,11 +132,11 @@ public String buildExpressionQuery(ConceptSetExpression expression)
115
132
116
133
// each ArrayList contains the concepts that are used in the sub-query of the codeset expression query
117
134
118
- String conceptSetQuery = buildConceptSetQuery (includeConcepts , includeDescendantConcepts , excludeConcepts , excludeDescendantConcepts );
135
+ String conceptSetQuery = StringUtils . replace ( CONCEPT_SET_INCLUDE_TEMPLATE , "@includeQuery" , buildConceptSetQuery (includeConcepts , includeDescendantConcepts , includeMappedConcepts , includeMappedDescendantConcepts ) );
119
136
120
- if (includeMappedConcepts .size () > 0 ){
121
- String mappedConceptsQuery = buildConceptSetQuery (includeMappedConcepts , includeMappedDescendantConcepts , excludeMappedConcepts , excludeMappedDescendantConcepts );
122
- conceptSetQuery += " \n UNION \n " + StringUtils . replace ( CONCEPT_SET_MAPPED_TEMPLATE , "@conceptsetQuery" , mappedConceptsQuery ) ;
137
+ if (excludeConcepts .size () > 0 ){
138
+ String excludeConceptsQuery = StringUtils . replace ( CONCEPT_SET_EXCLUDE_TEMPLATE , "@excludeQuery" , buildConceptSetQuery (excludeConcepts , excludeDescendantConcepts , excludeMappedConcepts , excludeMappedDescendantConcepts ) );
139
+ conceptSetQuery += excludeConceptsQuery ;
123
140
}
124
141
125
142
return conceptSetQuery ;
0 commit comments