Skip to content

Commit

Permalink
Merge pull request #2 from endtb/ED-94
Browse files Browse the repository at this point in the history
ED-94: Customize the exports functionality of endTB Bahmni for non-st…
  • Loading branch information
mogoodrich authored Aug 8, 2017
2 parents 4e3ed45 + 58c57f8 commit ea5af17
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 26 deletions.
4 changes: 4 additions & 0 deletions scripts/rpm/bahmni-batch
Original file line number Diff line number Diff line change
Expand Up @@ -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"
29 changes: 19 additions & 10 deletions src/main/java/org/bahmni/batch/exports/BaseExportStep.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,49 @@
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;
import org.springframework.batch.item.file.FlatFileHeaderCallback;
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;

private String exportName;

private String headers;

private String sql;
@Autowired
private FreeMarkerEvaluator<Map<String, Object>> 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;
Expand All @@ -51,6 +59,11 @@ public Step getStep() {
}

private JdbcCursorItemReader jdbcItemReader() {

Map<String, Object> input = new HashMap<String,Object>();
input.put("restrictByTreatmentInitiationCohort", restrictByTreatmentInitiationCohort);
String sql = freeMarkerEvaluator.evaluate(ftlFilename, input);

JdbcCursorItemReader reader = new JdbcCursorItemReader();
reader.setDataSource(dataSource);
reader.setSql(sql);
Expand Down Expand Up @@ -78,8 +91,4 @@ public String getHeaders() {
return headers;
}

@PostConstruct
public void postConstruct(){
this.sql = BatchUtils.convertResourceOutputToString(sqlResource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -44,8 +45,11 @@ public class ObservationExportStep {
@Value("${outputFolder}")
public Resource outputFolder;

@Value("${restrictByTreatmentInitiationCohort:false}")
public Boolean restrictByTreatmentInitiationCohort;

@Autowired
private FreeMarkerEvaluator<BahmniForm> freeMarkerEvaluator;
private FreeMarkerEvaluator<Map<String, Object>> freeMarkerEvaluator;

private BahmniForm form;

Expand All @@ -66,7 +70,10 @@ public Step getStep() {
}

private JdbcCursorItemReader<Map<String, Object>> obsReader() {
String sql = freeMarkerEvaluator.evaluate("obsWithParentSql.ftl",form);
Map<String, Object> input = new HashMap<String,Object>();
input.put("form", form);
input.put("restrictByTreatmentInitiationCohort", restrictByTreatmentInitiationCohort);
String sql = freeMarkerEvaluator.evaluate("obsWithParentSql.ftl", input);
JdbcCursorItemReader<Map<String, Object>> reader = new JdbcCursorItemReader<>();
reader.setDataSource(dataSource);
reader.setSql(sql);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
</#if>

GROUP BY patient_id, program_id, order_id
ORDER BY patient_id, date_enrolled;
19 changes: 13 additions & 6 deletions src/main/resources/templates/obsWithParentSql.ftl
Original file line number Diff line number Diff line change
@@ -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 &gt; 0>
<#list 1..input.depthToParent as x>
<#if input.form.depthToParent &gt; 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 )
</#list>
</#if>
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>
<#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)
</#if>



Original file line number Diff line number Diff line change
Expand Up @@ -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)
</#if>


GROUP BY patient_id, program_id, order_id
ORDER BY patient_id, date_enrolled;
Original file line number Diff line number Diff line change
Expand Up @@ -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)
</#if>

GROUP BY patient_id, patient_program_id
ORDER BY patient_id, date_enrolled;

0 comments on commit ea5af17

Please sign in to comment.