Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Custom Location Mapping feature: it allows to run automated reports for multiples Location/OrgUni mapping. #11

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/
6 changes: 6 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.0.1-jre</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,24 @@
*/
package org.openmrs.module.dhisconnector;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.module.ModuleActivator;
import org.openmrs.util.OpenmrsUtil;

/**
* This class contains the logic that is run every time this module is either started or stopped.
*/
public class DHISConnectorActivator implements ModuleActivator {

protected Log log = LogFactory.getLog(getClass());


public static final String DHISCONNECTOR_MAPPINGS_FOLDER = File.separator + "dhisconnector" + File.separator
+ "mappings";

/**
* @see ModuleActivator#willRefreshContext()
*/
Expand All @@ -45,6 +52,25 @@ public void willStart() {
* @see ModuleActivator#started()
*/
public void started() {
String mappingsDirecoryPath = OpenmrsUtil.getApplicationDataDirectory() + DHISCONNECTOR_MAPPINGS_FOLDER;
File mappingsDirecory = new File(mappingsDirecoryPath);

if (!mappingsDirecory.exists()) {
try {
if (!mappingsDirecory.mkdirs()) {
log.warn("Not able to create resource folder");
}else{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some right code format needed here too, '}else'

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed - please see my last commit

File directory = new File(getClass().getClassLoader().getResource("mappings").getFile());
FileUtils.copyDirectory(directory, mappingsDirecory);
log.debug("Copiyed all the mapping files to:"+mappingsDirecory);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed - please see my last commit

}

}
catch (Exception e) {
e.printStackTrace();
log.error("Error while creating "+DHISCONNECTOR_MAPPINGS_FOLDER+ "Directory");
}
}
log.info("DHIS Connector started");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public class ReportToDataSetMapping extends BaseOpenmrsObject {
@Column(name = "org_unit_uid", nullable = false)
private String orgUnitUid;

@Column(name = "is_full_mapping")
private String isFullMapping;

@Column(name = "last_run")
private Date lastRun;

Expand All @@ -61,10 +64,11 @@ public class ReportToDataSetMapping extends BaseOpenmrsObject {
public ReportToDataSetMapping() {
}

public ReportToDataSetMapping(String mapping, Location location, String orgUnitId) {
public ReportToDataSetMapping(String mapping, Location location, String orgUnitId, String isFullMapping) {
setMapping(mapping);
setLocation(location);
setOrgUnitUid(orgUnitId);
setIsFullMapping(isFullMapping);
}

public Integer getId() {
Expand All @@ -75,6 +79,14 @@ public void setId(Integer id) {
this.id = id;
}

public String getIsFullMapping() {
return isFullMapping;
}

public void setIsFullMapping(String isFullMapping) {
this.isFullMapping = isFullMapping;
}

public String getOrgUnitUid() {
return orgUnitUid;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Date;
import java.util.List;

import org.openmrs.Location;
import org.openmrs.api.OpenmrsService;
import org.openmrs.module.dhisconnector.ReportToDataSetMapping;
import org.openmrs.module.dhisconnector.api.model.DHISDataValueSet;
Expand Down Expand Up @@ -101,9 +102,11 @@ public interface DHISConnectorService extends OpenmrsService {

void deleteReportToDataSetMapping(Integer reportToDataSetMappingId);

String runAndPushReportToDHIS(ReportToDataSetMapping reportToDatasetMapping);
String runAndPushReportToDHIS(ReportToDataSetMapping reportToDatasetMapping, Location location, String orgUnitUid, boolean isEndOfFullMapping);

String runAllAutomatedReportsAndPostToDHIS();

String runReportsAndPostToDHIS(List<ReportToDataSetMapping> mps);

String transformToDHISPeriod(Calendar startDate, Calendar endDate, String periodType, Date lastRun);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
import java.util.Date;
import java.util.Enumeration;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
Expand Down Expand Up @@ -79,6 +81,7 @@
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.openmrs.Location;
import org.openmrs.api.AdministrationService;
import org.openmrs.api.context.Context;
import org.openmrs.api.impl.BaseOpenmrsService;
import org.openmrs.module.dhisconnector.Configurations;
Expand Down Expand Up @@ -122,6 +125,8 @@
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.google.common.base.Splitter;

/**
* It is a default implementation of {@link DHISConnectorService}.
*/
Expand All @@ -148,6 +153,8 @@ public class DHISConnectorServiceImpl extends BaseOpenmrsService implements DHIS

public static final String DATASETS_PATH = "/api/dataSets/";

public static final String GLOBAL_PROPERTY_FULL_MAPPING = "dhisconnector.fullmappingvalues";

public static final String ORGUNITS_PATH = "/api/organisationUnits/";

public static String JSON_POST_FIX = ".json?paging=false";
Expand Down Expand Up @@ -1290,32 +1297,34 @@ public void deleteReportToDataSetMapping(Integer reportToDataSetMappingId) {
}

@Override
public String runAndPushReportToDHIS(ReportToDataSetMapping reportToDatasetMapping) {
public String runAndPushReportToDHIS(ReportToDataSetMapping reportToDatasetMapping, Location location, String orgUnitUid, boolean isEndOfFullMapping) {
if (reportToDatasetMapping != null) {
Calendar startDate = Calendar.getInstance(Context.getLocale());
Calendar endDate = Calendar.getInstance(Context.getLocale());
DHISMapping mapping = getMapping(reportToDatasetMapping.getMapping());

if (mapping != null) {
Location location = reportToDatasetMapping.getLocation();
//Location location = reportToDatasetMapping.getLocation();
String periodType = mapping.getPeriodType();
PeriodIndicatorReportDefinition ranReportDef = (PeriodIndicatorReportDefinition) Context
.getService(ReportDefinitionService.class)
.getDefinitionByUuid(mapping.getPeriodIndicatorReportGUID());
String orgUnitUid = reportToDatasetMapping.getOrgUnitUid();
//String orgUnitUid = reportToDatasetMapping.getOrgUnitUid();
Date lastRun = reportToDatasetMapping.getLastRun();
if (location != null && ranReportDef != null) {
String period = transformToDHISPeriod(startDate, endDate, periodType, lastRun);

if (StringUtils.isNotBlank(period)) {
if (StringUtils.isNotBlank(period) ) {
Report ranReport = runPeriodIndicatorReport(ranReportDef, startDate.getTime(), endDate.getTime(),
location);
if (ranReport != null) {
Object response = sendReportDataToDHIS(ranReport, mapping, period, orgUnitUid);

if (response != null) {
reportToDatasetMapping.setLastRun(endDate.getTime());
saveReportToDataSetMapping(reportToDatasetMapping);
if (response != null ) {
if ( isEndOfFullMapping ) {
reportToDatasetMapping.setLastRun(endDate.getTime());
saveReportToDataSetMapping(reportToDatasetMapping);
}

return getPostSummary(response);
}
Expand Down Expand Up @@ -1459,15 +1468,50 @@ public Report runPeriodIndicatorReport(PeriodIndicatorReportDefinition reportDef

@Override
public String runAllAutomatedReportsAndPostToDHIS() {
String responses = "";

List<ReportToDataSetMapping> mps = getAllReportToDataSetMappings();

return runReportsAndPostToDHIS(mps);
}

@Override
public String runReportsAndPostToDHIS(List<ReportToDataSetMapping> mps) {
String responses = "";

/////////////
AdministrationService as = Context.getAdministrationService();
String fullMappingProperty = as.getGlobalProperty(GLOBAL_PROPERTY_FULL_MAPPING);
Map<String, String> locOrgUnits = null;
if (StringUtils.isNotBlank ( fullMappingProperty ) && fullMappingProperty.contains("=") )
locOrgUnits = Splitter.on(",").withKeyValueSeparator("=").split((CharSequence) fullMappingProperty) ;
else
locOrgUnits = new HashMap<String, String>() ;
///////////////

if (mps != null) {
for (ReportToDataSetMapping m : mps) {
String resp = runAndPushReportToDHIS(m);
if (StringUtils.isNotBlank( m.getIsFullMapping()) && m.getIsFullMapping().equals( "Y" ) ) {
int counting = 0;
boolean isEndOfFullMapping = false;
for (Map.Entry mapentry : locOrgUnits.entrySet()) {
if ( StringUtils.isNotBlank((String)mapentry.getKey())
&& StringUtils.isNotBlank((String)mapentry.getValue()) ) {
if (locOrgUnits.size() - 1 == counting )
isEndOfFullMapping = true;
String resp = runAndPushReportToDHIS(m, Context.getLocationService().getLocationByUuid((String)mapentry.getKey()), (String)mapentry.getValue(), isEndOfFullMapping );

if (StringUtils.isNotBlank(resp))
responses += " => " + resp;
counting ++;
}
}

if (StringUtils.isNotBlank(resp))
responses += " => " + resp;
} else {
String resp = runAndPushReportToDHIS(m, m.getLocation(), m.getOrgUnitUid(), true);

if (StringUtils.isNotBlank(resp))
responses += " => " + resp;
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions api/src/main/resources/liquibase.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,30 @@
<column name="location" type="int">
<constraints nullable="false" />
</column>

</createTable>
<addForeignKeyConstraint constraintName="dhisconnector_report_to_dataset_location_fk"
baseTableName="dhisconnector_report_to_dataset" baseColumnNames="location"
referencedTableName="location" referencedColumnNames="location_id" />
</changeSet>

<changeSet id="20181221-1519" author="a-hammi">
<preConditions onFail="MARK_RAN">
<tableExists tableName="dhisconnector_report_to_dataset" />
<not><columnExists columnName="is_full_mapping" tableName="dhisconnector_report_to_dataset"/></not>

</preConditions>
<comment>
Add new column to the table dhisconnector_report_to_dataset table
</comment>
<addColumn
tableName="dhisconnector_report_to_dataset">
<column name="is_full_mapping" type="char(1)">
<constraints nullable="true" />
</column>
</addColumn>
</changeSet>

<changeSet id="20171128-2333" author="k-joseph">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">SELECT COUNT(*) FROM
Expand Down
1 change: 1 addition & 0 deletions api/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ${project.parent.artifactId}.runReports=Run Reports
${project.parent.artifactId}.url=URL
${project.parent.artifactId}.user=Username
${project.parent.artifactId}.pass=Password
${project.parent.artifactId}.fullMapping=Custom Location Mapping
${project.parent.artifactId}.save=Save
${project.parent.artifactId}.saveSuccess=DHIS Server Successfully Configured
${project.parent.artifactId}.saveFailure=DHIS Server Configuration Failed
Expand Down
1 change: 1 addition & 0 deletions omod/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/
22 changes: 22 additions & 0 deletions omod/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@
<artifactId>reportingrest-api</artifactId>
</dependency>

<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest-omod</artifactId>
<version>${webservicesRestVersion}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest-omod</artifactId>
<version>${webservicesRestVersion}</version>
<scope>provided</scope>
</dependency>


<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest-omod-common</artifactId>
Expand Down Expand Up @@ -93,6 +108,12 @@
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.0.1-jre</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -222,6 +243,7 @@
</plugins>
</build>


<profiles>
<profile>
<id>deploy-web</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public Map<String, String> getLinks() {
map.put("/module/dhisconnector/dhis2BackupImport.form", "dhisconnector.dhis2Backup.import");
map.put("/module/dhisconnector/dhis2BackupExport.form", "dhisconnector.dhis2Backup.export");
map.put("/module/dhisconnector/uploadMapping.form", "dhisconnector.uploadMapping");
map.put("/module/dhisconnector/fullMapping.form", "dhisconnector.fullMapping");
map.put("/module/dhisconnector/exportMappings.form", "dhisconnector.exportMapping");
map.put("/module/dhisconnector/createMapping.form", "dhisconnector.createMapping");
map.put("/module/dhisconnector/manageMappings.form", "dhisconnector.manageMappings");
Expand Down
Loading