From 58c57f8d391ff9c14a12a7eba2f9004c1837a2d0 Mon Sep 17 00:00:00 2001 From: mgoodrich Date: Fri, 4 Aug 2017 16:50:35 -0400 Subject: [PATCH] ED-94: Customize the exports functionality of endTB Bahmni for non-study patients --- scripts/rpm/bahmni-batch | 4 +++ .../bahmni/batch/exports/BaseExportStep.java | 29 ++++++++++++------- .../MetaDataCodeDictionaryExportStep.java | 3 +- .../exports/NonTBDrugOrderBaseExportStep.java | 3 +- .../batch/exports/ObservationExportStep.java | 11 +++++-- .../exports/TBDrugOrderBaseExportStep.java | 3 +- .../TreatmentRegistrationBaseExportStep.java | 3 +- .../metaDataCodeDictionary.sql.ftl} | 0 .../nonTbDrugOrder.sql.ftl} | 6 ++++ .../resources/templates/obsWithParentSql.ftl | 19 ++++++++---- .../tbDrugOrder.sql.ftl} | 7 +++++ .../treatmentRegistration.sql.ftl} | 7 +++++ 12 files changed, 69 insertions(+), 26 deletions(-) rename src/main/resources/{sql/metaDataCodeDictionary.sql => templates/metaDataCodeDictionary.sql.ftl} (100%) rename src/main/resources/{sql/nonTbDrugOrder.sql => templates/nonTbDrugOrder.sql.ftl} (95%) rename src/main/resources/{sql/tbDrugOrder.sql => templates/tbDrugOrder.sql.ftl} (96%) rename src/main/resources/{sql/treatmentRegistration.sql => templates/treatmentRegistration.sql.ftl} (90%) diff --git a/scripts/rpm/bahmni-batch b/scripts/rpm/bahmni-batch index 107ce71..b6f4043 100644 --- a/scripts/rpm/bahmni-batch +++ b/scripts/rpm/bahmni-batch @@ -2,6 +2,10 @@ . /etc/bahmni-installer/bahmni.conf +# change to this directory explicitly so that any properties set up in applicatin.properties here or the config subdirectory will get picked up +# (see: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html) +cd /opt/bahmni-batch + echo "Running bahmni-batch..." java -DOPENMRS_DB_SERVER=${OPENMRS_DB_SERVER} -DOPENMRS_DB_USER=${OPENMRS_DB_USERNAME} -DOPENMRS_DB_PASSWORD=${OPENMRS_DB_PASSWORD} -jar /opt/bahmni-batch/libs/bahmni-batch-*.jar >> /opt/bahmni-batch/log/bahmni-batch.log echo "Done" diff --git a/src/main/java/org/bahmni/batch/exports/BaseExportStep.java b/src/main/java/org/bahmni/batch/exports/BaseExportStep.java index 747f38d..cab12da 100644 --- a/src/main/java/org/bahmni/batch/exports/BaseExportStep.java +++ b/src/main/java/org/bahmni/batch/exports/BaseExportStep.java @@ -1,6 +1,6 @@ package org.bahmni.batch.exports; -import org.bahmni.batch.BatchUtils; +import org.bahmni.batch.helper.FreeMarkerEvaluator; import org.springframework.batch.core.Step; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; import org.springframework.batch.item.database.JdbcCursorItemReader; @@ -8,21 +8,28 @@ import org.springframework.batch.item.file.FlatFileItemWriter; import org.springframework.batch.item.file.transform.DelimitedLineAggregator; import org.springframework.batch.item.file.transform.PassThroughFieldExtractor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.Resource; import org.springframework.jdbc.core.ColumnMapRowMapper; -import javax.annotation.PostConstruct; import javax.sql.DataSource; import java.io.IOException; import java.io.Writer; +import java.util.HashMap; +import java.util.Map; public class BaseExportStep { + + @Value("${restrictByTreatmentInitiationCohort:false}") + public Boolean restrictByTreatmentInitiationCohort; + private DataSource dataSource; private StepBuilderFactory stepBuilderFactory; - private Resource sqlResource; + private String ftlFilename; private Resource outputFolder; @@ -30,12 +37,13 @@ public class BaseExportStep { private String headers; - private String sql; + @Autowired + private FreeMarkerEvaluator> freeMarkerEvaluator; - public BaseExportStep(StepBuilderFactory stepBuilderFactory, DataSource dataSource, Resource sqlResource, Resource outputFolder, String exportName, String headers) { + public BaseExportStep(StepBuilderFactory stepBuilderFactory, DataSource dataSource, String ftlFilename, Resource outputFolder, String exportName, String headers) { this.dataSource = dataSource; this.stepBuilderFactory = stepBuilderFactory; - this.sqlResource = sqlResource; + this.ftlFilename = ftlFilename; this.outputFolder = outputFolder; this.exportName = exportName; this.headers = headers; @@ -51,6 +59,11 @@ public Step getStep() { } private JdbcCursorItemReader jdbcItemReader() { + + Map input = new HashMap(); + input.put("restrictByTreatmentInitiationCohort", restrictByTreatmentInitiationCohort); + String sql = freeMarkerEvaluator.evaluate(ftlFilename, input); + JdbcCursorItemReader reader = new JdbcCursorItemReader(); reader.setDataSource(dataSource); reader.setSql(sql); @@ -78,8 +91,4 @@ public String getHeaders() { return headers; } - @PostConstruct - public void postConstruct(){ - this.sql = BatchUtils.convertResourceOutputToString(sqlResource); - } } diff --git a/src/main/java/org/bahmni/batch/exports/MetaDataCodeDictionaryExportStep.java b/src/main/java/org/bahmni/batch/exports/MetaDataCodeDictionaryExportStep.java index 4d6e713..1eec94a 100644 --- a/src/main/java/org/bahmni/batch/exports/MetaDataCodeDictionaryExportStep.java +++ b/src/main/java/org/bahmni/batch/exports/MetaDataCodeDictionaryExportStep.java @@ -13,10 +13,9 @@ public class MetaDataCodeDictionaryExportStep extends BaseExportStep { @Autowired public MetaDataCodeDictionaryExportStep(StepBuilderFactory stepBuilderFactory, DataSource dataSource, - @Value("classpath:sql/metaDataCodeDictionary.sql") Resource sqlResource, @Value("${outputFolder}/metaDataCodeDictionary.csv") Resource outputFolder, @Value("${metaDataCodeDictionaryHeaders}") String headers) { - super(stepBuilderFactory, dataSource, sqlResource, outputFolder, "metaDataCodeDictionary", headers); + super(stepBuilderFactory, dataSource, "metaDataCodeDictionary.sql.ftl", outputFolder, "metaDataCodeDictionary", headers); } } diff --git a/src/main/java/org/bahmni/batch/exports/NonTBDrugOrderBaseExportStep.java b/src/main/java/org/bahmni/batch/exports/NonTBDrugOrderBaseExportStep.java index d701abb..eec1e2f 100644 --- a/src/main/java/org/bahmni/batch/exports/NonTBDrugOrderBaseExportStep.java +++ b/src/main/java/org/bahmni/batch/exports/NonTBDrugOrderBaseExportStep.java @@ -14,9 +14,8 @@ public class NonTBDrugOrderBaseExportStep extends BaseExportStep { @Autowired public NonTBDrugOrderBaseExportStep(StepBuilderFactory stepBuilderFactory, DataSource dataSource, - @Value("classpath:sql/nonTbDrugOrder.sql") Resource sqlResource, @Value("${outputFolder}/nonTbDrugOrder.csv") Resource outputFolder, @Value("${nonTbDrugOrderHeaders}") String headers ) { - super(stepBuilderFactory, dataSource, sqlResource, outputFolder, "nonTbDrugOrder", headers); + super(stepBuilderFactory, dataSource, "nonTbDrugOrder.sql.ftl", outputFolder, "nonTbDrugOrder", headers); } } diff --git a/src/main/java/org/bahmni/batch/exports/ObservationExportStep.java b/src/main/java/org/bahmni/batch/exports/ObservationExportStep.java index c742ada..d629565 100644 --- a/src/main/java/org/bahmni/batch/exports/ObservationExportStep.java +++ b/src/main/java/org/bahmni/batch/exports/ObservationExportStep.java @@ -26,6 +26,7 @@ import java.io.File; import java.io.IOException; import java.io.Writer; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -44,8 +45,11 @@ public class ObservationExportStep { @Value("${outputFolder}") public Resource outputFolder; + @Value("${restrictByTreatmentInitiationCohort:false}") + public Boolean restrictByTreatmentInitiationCohort; + @Autowired - private FreeMarkerEvaluator freeMarkerEvaluator; + private FreeMarkerEvaluator> freeMarkerEvaluator; private BahmniForm form; @@ -66,7 +70,10 @@ public Step getStep() { } private JdbcCursorItemReader> obsReader() { - String sql = freeMarkerEvaluator.evaluate("obsWithParentSql.ftl",form); + Map input = new HashMap(); + input.put("form", form); + input.put("restrictByTreatmentInitiationCohort", restrictByTreatmentInitiationCohort); + String sql = freeMarkerEvaluator.evaluate("obsWithParentSql.ftl", input); JdbcCursorItemReader> reader = new JdbcCursorItemReader<>(); reader.setDataSource(dataSource); reader.setSql(sql); diff --git a/src/main/java/org/bahmni/batch/exports/TBDrugOrderBaseExportStep.java b/src/main/java/org/bahmni/batch/exports/TBDrugOrderBaseExportStep.java index fee812f..d3a914f 100644 --- a/src/main/java/org/bahmni/batch/exports/TBDrugOrderBaseExportStep.java +++ b/src/main/java/org/bahmni/batch/exports/TBDrugOrderBaseExportStep.java @@ -15,10 +15,9 @@ public class TBDrugOrderBaseExportStep extends BaseExportStep{ @Autowired public TBDrugOrderBaseExportStep(StepBuilderFactory stepBuilderFactory, DataSource dataSource, - @Value("classpath:sql/tbDrugOrder.sql") Resource sqlResource, @Value("${outputFolder}/tbDrugOrder.csv") Resource outputFolder, @Value("${tbDrugOrderHeaders}")String headers) { - super(stepBuilderFactory, dataSource, sqlResource, outputFolder, "tbDrugOrder", headers); + super(stepBuilderFactory, dataSource, "tbDrugOrder.sql.ftl", outputFolder, "tbDrugOrder", headers); } diff --git a/src/main/java/org/bahmni/batch/exports/TreatmentRegistrationBaseExportStep.java b/src/main/java/org/bahmni/batch/exports/TreatmentRegistrationBaseExportStep.java index 2fd3bf9..e3c1c49 100644 --- a/src/main/java/org/bahmni/batch/exports/TreatmentRegistrationBaseExportStep.java +++ b/src/main/java/org/bahmni/batch/exports/TreatmentRegistrationBaseExportStep.java @@ -14,9 +14,8 @@ public class TreatmentRegistrationBaseExportStep extends BaseExportStep { @Autowired public TreatmentRegistrationBaseExportStep(StepBuilderFactory stepBuilderFactory, DataSource dataSource, - @Value("classpath:sql/treatmentRegistration.sql") Resource sqlResource, @Value("${outputFolder}/treatmentRegistration.csv") Resource outputFolder, @Value("${treatmentRegistrationHeaders}")String headers ) { - super(stepBuilderFactory, dataSource, sqlResource, outputFolder, "treatmentRegistration", headers); + super(stepBuilderFactory, dataSource, "treatmentRegistration.sql.ftl", outputFolder, "treatmentRegistration", headers); } } diff --git a/src/main/resources/sql/metaDataCodeDictionary.sql b/src/main/resources/templates/metaDataCodeDictionary.sql.ftl similarity index 100% rename from src/main/resources/sql/metaDataCodeDictionary.sql rename to src/main/resources/templates/metaDataCodeDictionary.sql.ftl diff --git a/src/main/resources/sql/nonTbDrugOrder.sql b/src/main/resources/templates/nonTbDrugOrder.sql.ftl similarity index 95% rename from src/main/resources/sql/nonTbDrugOrder.sql rename to src/main/resources/templates/nonTbDrugOrder.sql.ftl index 30e9529..3667bc0 100644 --- a/src/main/resources/sql/nonTbDrugOrder.sql +++ b/src/main/resources/templates/nonTbDrugOrder.sql.ftl @@ -64,5 +64,11 @@ FROM LEFT OUTER JOIN program_attribute_type pat ON o.attribute_type_id = pat.program_attribute_type_id LEFT JOIN concept_name cn on cn.name=o.reason_for_administration and cn.voided = 0 LEFT JOIN concept_reference_term_map_view reason_admin on reason_admin.concept_id=cn.concept_id AND reason_admin.concept_reference_source_name='EndTB-Export' and reason_admin.concept_map_type_name= 'SAME-AS' + +<#if input.restrictByTreatmentInitiationCohort> +WHERE patient_id in +(select distinct person_id from obs where concept_id in (SELECT concept_id from concept_name where name='Treatment Initiation') and obs_group_id IS NULL and voided=0) + + GROUP BY patient_id, program_id, order_id ORDER BY patient_id, date_enrolled; \ No newline at end of file diff --git a/src/main/resources/templates/obsWithParentSql.ftl b/src/main/resources/templates/obsWithParentSql.ftl index e32c2eb..a7373c1 100644 --- a/src/main/resources/templates/obsWithParentSql.ftl +++ b/src/main/resources/templates/obsWithParentSql.ftl @@ -1,12 +1,19 @@ -SELECT obs0.obs_id,obs${input.depthToParent}.obs_id as parent_obs_id +SELECT obs0.obs_id,obs${input.form.depthToParent}.obs_id as parent_obs_id FROM obs obs0 -<#if input.depthToParent > 0> -<#list 1..input.depthToParent as x> +<#if input.form.depthToParent > 0> +<#list 1..input.form.depthToParent as x> INNER JOIN obs obs${x} on ( obs${x}.obs_id=obs${x-1}.obs_group_id and obs${x}.voided=0 ) -WHERE obs0.concept_id=${input.formName.id?c} +WHERE obs0.concept_id=${input.form.formName.id?c} AND obs0.voided = 0 -<#if input.parent?has_content> -AND obs${input.depthToParent}.concept_id=${input.parent.formName.id?c} +<#if input.form.parent?has_content> +AND obs${input.form.depthToParent}.concept_id=${input.form.parent.formName.id?c} +<#if input.restrictByTreatmentInitiationCohort> +AND obs0.person_id in +(select distinct person_id from obs where concept_id in (SELECT concept_id from concept_name where name='Treatment Initiation') and obs_group_id IS NULL and voided=0) + + + + diff --git a/src/main/resources/sql/tbDrugOrder.sql b/src/main/resources/templates/tbDrugOrder.sql.ftl similarity index 96% rename from src/main/resources/sql/tbDrugOrder.sql rename to src/main/resources/templates/tbDrugOrder.sql.ftl index 03a76d3..d21a33a 100644 --- a/src/main/resources/sql/tbDrugOrder.sql +++ b/src/main/resources/templates/tbDrugOrder.sql.ftl @@ -68,5 +68,12 @@ FROM LEFT JOIN concept_reference_term_map_view stopped_reason ON stopped_order.order_reason = stopped_reason.concept_id and stopped_reason.concept_reference_source_name='EndTB-Export' and stopped_reason.concept_map_type_name= 'SAME-AS' ) o LEFT OUTER JOIN program_attribute_type pat ON o.attribute_type_id = pat.program_attribute_type_id + +<#if input.restrictByTreatmentInitiationCohort> +WHERE patient_id in +(select distinct person_id from obs where concept_id in (SELECT concept_id from concept_name where name='Treatment Initiation') and obs_group_id IS NULL and voided=0) + + + GROUP BY patient_id, program_id, order_id ORDER BY patient_id, date_enrolled; \ No newline at end of file diff --git a/src/main/resources/sql/treatmentRegistration.sql b/src/main/resources/templates/treatmentRegistration.sql.ftl similarity index 90% rename from src/main/resources/sql/treatmentRegistration.sql rename to src/main/resources/templates/treatmentRegistration.sql.ftl index 19392ab..6a19f26 100644 --- a/src/main/resources/sql/treatmentRegistration.sql +++ b/src/main/resources/templates/treatmentRegistration.sql.ftl @@ -45,5 +45,12 @@ FROM LEFT OUTER JOIN concept_name outcome_concept ON outcome_concept.concept_id = pp.outcome_concept_id and outcome_concept.concept_name_type='FULLY_SPECIFIED' AND outcome_concept.voided = 0 ) o LEFT OUTER JOIN program_attribute_type pat ON o.attribute_type_id = pat.program_attribute_type_id + + +<#if input.restrictByTreatmentInitiationCohort> +WHERE patient_id in +(select distinct person_id from obs where concept_id in (SELECT concept_id from concept_name where name='Treatment Initiation') and obs_group_id IS NULL and voided=0) + + GROUP BY patient_id, patient_program_id ORDER BY patient_id, date_enrolled;