Skip to content

Commit

Permalink
RODI-33 pushing, complete later
Browse files Browse the repository at this point in the history
  • Loading branch information
kaweesi committed Mar 13, 2017
1 parent 6d37143 commit ebcfeab
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public interface DHISConnectorService extends OpenmrsService {
*/
public String getDataFromDHISEndpoint(String endpoint);

public String postDataToDHISEndpoint(String endpoint, String jsonPayload, boolean useAdxNotDXF);
public String postDataToDHISEndpoint(String endpoint, String jsonPayload);

/**
* Tests to check if the given DHIS server details are correct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Enumeration;
import java.util.GregorianCalendar;
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 @@ -73,6 +74,7 @@
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.openmrs.Location;
import org.openmrs.api.context.Context;
import org.openmrs.api.impl.BaseOpenmrsService;
import org.openmrs.module.dhisconnector.Configurations;
Expand All @@ -91,9 +93,20 @@
import org.openmrs.module.dhisconnector.api.model.DHISImportSummary;
import org.openmrs.module.dhisconnector.api.model.DHISMapping;
import org.openmrs.module.dhisconnector.api.model.DHISOrganisationUnit;
import org.openmrs.module.reporting.dataset.DataSet;
import org.openmrs.module.reporting.dataset.DataSetColumn;
import org.openmrs.module.reporting.dataset.DataSetRow;
import org.openmrs.module.reporting.evaluation.parameter.Mapped;
import org.openmrs.module.reporting.report.Report;
import org.openmrs.module.reporting.report.ReportRequest;
import org.openmrs.module.reporting.report.ReportRequest.Priority;
import org.openmrs.module.reporting.report.ReportRequest.Status;
import org.openmrs.module.reporting.report.definition.PeriodIndicatorReportDefinition;
import org.openmrs.module.reporting.report.definition.ReportDefinition;
import org.openmrs.module.reporting.report.definition.service.ReportDefinitionService;
import org.openmrs.module.reporting.report.renderer.RenderingMode;
import org.openmrs.module.reporting.report.service.ReportService;
import org.openmrs.module.reporting.web.renderers.DefaultWebRenderer;
import org.openmrs.util.OpenmrsUtil;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.Document;
Expand Down Expand Up @@ -320,7 +333,7 @@ private AdxDataValueSet convertDHISDataValueSetToAdxDataValueSet(DHISDataValueSe
}

@Override
public String postDataToDHISEndpoint(String endpoint, String data, boolean useAdxNotDxf) {
public String postDataToDHISEndpoint(String endpoint, String data) {
String url = Context.getAdministrationService().getGlobalProperty("dhisconnector.url");
String user = Context.getAdministrationService().getGlobalProperty("dhisconnector.user");
String pass = Context.getAdministrationService().getGlobalProperty("dhisconnector.pass");
Expand All @@ -345,7 +358,7 @@ public String postDataToDHISEndpoint(String endpoint, String data, boolean useAd
Credentials creds = new UsernamePasswordCredentials(user, pass);
Header bs = new BasicScheme().authenticate(creds, httpPost, localcontext);
httpPost.addHeader("Authorization", bs.getValue());
if (useAdxNotDxf) {
if (configs.useAdxInsteadOfDxf()) {
httpPost.addHeader("Content-Type", "application/xml+adx");
httpPost.addHeader("Accept", "application/xml");
} else {
Expand Down Expand Up @@ -478,7 +491,7 @@ public Object postDataValueSet(DHISDataValueSet dataValueSet) {
jsonOrXmlString = configs.useAdxInsteadOfDxf()
? factory.translateAdxDataValueSetIntoString(convertDHISDataValueSetToAdxDataValueSet(dataValueSet))
: mapper.writeValueAsString(dataValueSet);
responseString = postDataToDHISEndpoint(DATAVALUESETS_PATH, jsonOrXmlString, configs.useAdxInsteadOfDxf());
responseString = postDataToDHISEndpoint(DATAVALUESETS_PATH, jsonOrXmlString);

if (configs.useAdxInsteadOfDxf()) {
JAXBContext jaxbImportSummaryContext = JAXBContext.newInstance(ImportSummaries.class);
Expand Down Expand Up @@ -1070,4 +1083,49 @@ private String beautifyXML(String xml) {
return xml;
}

private Report runPeriodIndicatorReport(PeriodIndicatorReportDefinition reportDef, Date startDate, Date endDate,
Location location) {
ReportRequest request = new ReportRequest(new Mapped<ReportDefinition>(reportDef, null), null,
new RenderingMode(new DefaultWebRenderer(), "Web", null, 100), Priority.HIGHEST, null);

request.getReportDefinition().addParameterMapping("startDate", startDate);
request.getReportDefinition().addParameterMapping("endDate", endDate);
request.getReportDefinition().addParameterMapping("location", location);
request.setStatus(Status.PROCESSING);
request = Context.getService(ReportService.class).saveReportRequest(request);

return Context.getService(ReportService.class).runReport(request);
}

//TODO investigate to support scheduling in the future: https://jembiprojects.jira.com/browse/RODI-33
public Object sendReportDataToDHIS(PeriodIndicatorReportDefinition reportDef, String period, String orgUnitId,
DHISMapping mapping, Date startDate, Date endDate, Location location) {
Report report = runPeriodIndicatorReport(reportDef, startDate, endDate, location);
Map<String, DataSet> dataSets = report.getReportData().getDataSets();
List<DHISDataValue> dataValues = new ArrayList<DHISDataValue>();

if (dataSets != null && dataSets.size() > 0 && StringUtils.isNotBlank(period) && StringUtils.isNotBlank(orgUnitId)) {
DHISDataValueSet dataValueSet = new DHISDataValueSet();
DataSet ds = dataSets.get("defaultDataSet");
List<DataSetColumn> columns = ds.getMetaData().getColumns();

DataSetRow row = ds.iterator().next();

for (int i = 0; i < columns.size(); i++) {
DHISDataValue dv = new DHISDataValue();
String column = columns.get(i).getName();

dv.setValue(row.getColumnValue(column).toString());
dv.setDataElement(mapping.getElements().get(i).getDataElement());//TODO debug
dataValues.add(dv);
}
dataValueSet.setDataValues(dataValues);
dataValueSet.setOrgUnit(orgUnitId);
dataValueSet.setPeriod(period);
dataValueSet.setDataSet(mapping.getDataSetUID());

return Context.getService(DHISConnectorService.class).postDataValueSet(dataValueSet);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"conflicts",
"dataSetComplete"
})
//TODO dhis upgrade
public class DHISImportSummary {

@JsonProperty("responseType")
Expand Down Expand Up @@ -166,12 +165,10 @@ public String getDataSetComplete() {
public void setDataSetComplete(String dataSetComplete) {
this.dataSetComplete = dataSetComplete;
}


public DHISImportSummaryImportOptions getImportOptions() {
return importOptions;
}


public void setImportOptions(DHISImportSummaryImportOptions importOptions) {
this.importOptions = importOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
@Generated("org.jsonschema2pojo")
public class DHISImportSummaryImportOptions {


@JsonProperty("dryRun")
private boolean dryRun;

Expand Down Expand Up @@ -55,152 +54,122 @@ public class DHISImportSummaryImportOptions {

@JsonProperty("idSchemes")
private JSONObject idSchemes;


public boolean isDryRun() {
return dryRun;
}


public void setDryRun(boolean dryRun) {
this.dryRun = dryRun;
}


public boolean isAsync() {
return async;
}


public void setAsync(boolean async) {
this.async = async;
}


public String getImportStrategy() {
return importStrategy;
}


public void setImportStrategy(String importStrategy) {
this.importStrategy = importStrategy;
}


public String getMergeMode() {
return mergeMode;
}


public void setMergeMode(String mergeMode) {
this.mergeMode = mergeMode;
}


public boolean isSkipExistingCheck() {
return skipExistingCheck;
}


public void setSkipExistingCheck(boolean skipExistingCheck) {
this.skipExistingCheck = skipExistingCheck;
}


public boolean isSharing() {
return sharing;
}


public void setSharing(boolean sharing) {
this.sharing = sharing;
}


public boolean isSkipNotifications() {
return skipNotifications;
}


public void setSkipNotifications(boolean skipNotifications) {
this.skipNotifications = skipNotifications;
}


public boolean isDatasetAllowsPeriods() {
return datasetAllowsPeriods;
}


public void setDatasetAllowsPeriods(boolean datasetAllowsPeriods) {
this.datasetAllowsPeriods = datasetAllowsPeriods;
}


public boolean isStrictPeriods() {
return strictPeriods;
}


public void setStrictPeriods(boolean strictPeriods) {
this.strictPeriods = strictPeriods;
}


public boolean isStrictCategoryOptionCombos() {
return strictCategoryOptionCombos;
}


public void setStrictCategoryOptionCombos(boolean strictCategoryOptionCombos) {
this.strictCategoryOptionCombos = strictCategoryOptionCombos;
}


public boolean isStrictAttributeOptionCombos() {
return strictAttributeOptionCombos;
}


public void setStrictAttributeOptionCombos(boolean strictAttributeOptionCombos) {
this.strictAttributeOptionCombos = strictAttributeOptionCombos;
}


public boolean isStrictOrganisationUnits() {
return strictOrganisationUnits;
}


public void setStrictOrganisationUnits(boolean strictOrganisationUnits) {
this.strictOrganisationUnits = strictOrganisationUnits;
}


public boolean isRequireCategoryOptionCombo() {
return requireCategoryOptionCombo;
}


public void setRequireCategoryOptionCombo(boolean requireCategoryOptionCombo) {
this.requireCategoryOptionCombo = requireCategoryOptionCombo;
}


public boolean isRequireAttributeOptionCombo() {
return requireAttributeOptionCombo;
}


public void setRequireAttributeOptionCombo(boolean requireAttributeOptionCombo) {
this.requireAttributeOptionCombo = requireAttributeOptionCombo;
}


public JSONObject getIdSchemes() {
return idSchemes;
}


public void setIdSchemes(JSONObject idSchemes) {
this.idSchemes = idSchemes;
Expand Down

0 comments on commit ebcfeab

Please sign in to comment.