Skip to content

Adding support for Mamba API pagination #193

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ public interface MambaReportService extends OpenmrsService {
*/
@Authorized({MambaReportsConstants.VIEW_MAMBA_REPORT})
List<MambaReportItem> getMambaReportByCriteria(MambaReportCriteria criteria);


@Authorized({MambaReportsConstants.VIEW_MAMBA_REPORT})
Integer getMambaReportSize(MambaReportCriteria criteria);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,11 @@ public interface MambaReportItemDao {
*/
List<MambaReportItem> getMambaReport(MambaReportCriteria criteria);

/**
* Get the Total records (size) of this report
*
* @param criteria to follow
* @return total records
*/
Integer getMambaReportSize(MambaReportCriteria criteria);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.openmrs.module.mambacore.api.model.MambaReportItem;
import org.openmrs.module.mambacore.api.model.MambaReportItemColumn;
import org.openmrs.module.mambacore.api.parameter.MambaReportCriteria;
import org.openmrs.module.mambacore.api.parameter.MambaReportSearchField;
import org.openmrs.module.mambacore.db.ConnectionPoolManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -34,11 +35,21 @@ public List<MambaReportItem> getMambaReport(String mambaReportId) {

@Override
public List<MambaReportItem> getMambaReport(MambaReportCriteria criteria) {

Integer pageNumber = criteria.getPageNumber();
Integer pageSize = criteria.getPageSize();

String argumentsJson = "";
try {

ObjectMapper objectMapper = new ObjectMapper();

criteria.getSearchFields().add(new MambaReportSearchField("page_number", String.valueOf(pageNumber)));
criteria.getSearchFields().add(new MambaReportSearchField("page_size", String.valueOf(pageSize)));

argumentsJson = objectMapper.writeValueAsString(criteria.getSearchFields());
log.debug("Query arguments {}", argumentsJson);

} catch (Exception exc) {
log.error("Failed to get MambaReport", exc);
}
Expand All @@ -50,6 +61,7 @@ public List<MambaReportItem> getMambaReport(MambaReportCriteria criteria) {
.getInstance()
.getEtlDataSource();


try (Connection connection = dataSource.getConnection();
CallableStatement statement = connection.prepareCall("{CALL sp_mamba_get_report_column_names(?)}")) {

Expand All @@ -72,6 +84,8 @@ public List<MambaReportItem> getMambaReport(MambaReportCriteria criteria) {
try (Connection connection = dataSource.getConnection();
CallableStatement statement = connection.prepareCall("{CALL sp_mamba_generate_report_wrapper(?, ?, ?)}")) {

System.out.println("Query arguments here: " + argumentsJson);

statement.setInt("generate_columns_flag", 0);
statement.setString("report_identifier", criteria.getReportId());
statement.setString("parameter_list", argumentsJson);
Expand Down Expand Up @@ -118,4 +132,43 @@ public List<MambaReportItem> getMambaReport(MambaReportCriteria criteria) {
}
return mambaReportItems;
}

@Override
public Integer getMambaReportSize(MambaReportCriteria criteria) {
String argumentsJson = "";
try {

ObjectMapper objectMapper = new ObjectMapper();

argumentsJson = objectMapper.writeValueAsString(criteria.getSearchFields());
log.debug("Query arguments {}", argumentsJson);

} catch (Exception exc) {
log.error("Failed to get MambaReport", exc);
}

DataSource dataSource = ConnectionPoolManager
.getInstance()
.getEtlDataSource();

try (Connection connection = dataSource.getConnection();
CallableStatement statement = connection.prepareCall("{CALL sp_mamba_generate_report_size_sp_wrapper(?, ?)}")) {

statement.setString("report_identifier", criteria.getReportId());
statement.setString("parameter_list", argumentsJson);

System.out.println("Query report_identifier: " + criteria.getReportId());
System.out.println("Query arguments: " + argumentsJson);

if (statement.execute()) {
ResultSet resultSet = statement.getResultSet();
if (resultSet.next()) { // Move to the first row
return resultSet.getInt(1);
}
}
} catch (SQLException e) {
log.error("Failed to get MambaReport", e);
}
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ public List<MambaReportItem> getMambaReport(String mambaReportId) {
public List<MambaReportItem> getMambaReportByCriteria(MambaReportCriteria criteria) {
return dao.getMambaReport(criteria);
}

@Override
public Integer getMambaReportSize(MambaReportCriteria criteria) {
return dao.getMambaReportSize(criteria);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
* <p>
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.mambacore.api.model;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.io.Serializable;

public class MambaReportPagination implements Serializable {

private static final long serialVersionUID = -5805274582315599409L;

@JsonProperty("page_number")
private Integer pageNumber;

@JsonProperty("page_size")
private Integer pageSize;

@JsonProperty("total_pages")
private Integer totalPages;

@JsonProperty("total_records")
private Integer totalRecords;

public MambaReportPagination() {
this.pageNumber = 1;
this.pageSize = 50;
}

public Integer getPageNumber() {
return pageNumber;
}

public void setPageNumber(Integer pageNumber) {
this.pageNumber = pageNumber;
}

public Integer getPageSize() {
return pageSize;
}

public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}

public Integer getTotalPages() {
return totalPages;
}

public void setTotalPages(Integer totalPages) {
this.totalPages = totalPages;
}

public Integer getTotalRecords() {
return totalRecords;
}

public void setTotalRecords(Integer totalRecords) {
this.totalRecords = totalRecords;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@

public class MambaReportCriteria implements Serializable {

private static final long serialVersionUID = 6717202824335189575L;
private static final long serialVersionUID = 6727202844335189575L;

@JsonProperty("report_id")
private String reportId;

@JsonProperty("page_number")
private Integer pageNumber = 1;

@JsonProperty("page_size")
private Integer pageSize = 50;

@JsonProperty("arguments")
private List<MambaReportSearchField> searchFields = new ArrayList<>();

Expand All @@ -48,4 +54,20 @@ public List<MambaReportSearchField> getSearchFields() {
public void setSearchFields(List<MambaReportSearchField> searchFields) {
this.searchFields = searchFields;
}
}

public Integer getPageNumber() {
return pageNumber;
}

public void setPageNumber(Integer pageNumber) {
this.pageNumber = pageNumber;
}

public Integer getPageSize() {
return pageSize;
}

public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
*/
package org.openmrs.module.mambacore.web.resource;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.openmrs.api.context.Context;
import org.openmrs.module.mambacore.api.MambaReportService;
import org.openmrs.module.mambacore.api.model.MambaReportItem;
import org.openmrs.module.mambacore.api.model.MambaReportPagination;
import org.openmrs.module.mambacore.api.parameter.MambaReportCriteria;
import org.openmrs.module.mambacore.api.parameter.MambaReportSearchField;
import org.openmrs.module.mambacore.web.controller.MambaReportRestController;
Expand Down Expand Up @@ -46,6 +49,8 @@ public SimpleObject search(RequestContext context) throws ResponseException {
context.setStartIndex(0);

MambaReportCriteria searchCriteria = new MambaReportCriteria();
Integer pageSize = searchCriteria.getPageSize(); //defaults
Integer pageNumber = searchCriteria.getPageNumber(); //defaults

Enumeration<String> parameterNames = context.getRequest().getParameterNames();
while (parameterNames.hasMoreElements()) {
Expand All @@ -55,19 +60,59 @@ public SimpleObject search(RequestContext context) throws ResponseException {

log.debug("search API hit with param: {} vale: {}", paramName, paramValue);

if (paramName.equals("report_id")) {
searchCriteria.setReportId(paramValue);
} else {
searchCriteria.getSearchFields().add(new MambaReportSearchField(paramName, paramValue));
switch (paramName) {
case "report_id":
searchCriteria.setReportId(paramValue);
break;
case "page_number":
pageNumber = Integer.parseInt(paramValue);
searchCriteria.setPageNumber(pageNumber);
break;
case "page_size":
pageSize = Integer.parseInt(paramValue);
searchCriteria.setPageSize(pageSize);
break;
default:
searchCriteria.getSearchFields().add(new MambaReportSearchField(paramName, paramValue));
break;
}
}

if (searchCriteria.getReportId() == null) {
return new EmptySearchResult().toSimpleObject(null);
}

List<MambaReportItem> mambaReportItems = getService().getMambaReportByCriteria(searchCriteria);
return new SimpleObject().add("results", mambaReportItems);
ObjectMapper objectMapper = new ObjectMapper();

//TODO: Delete after Testing
try {
String argumentsJson = objectMapper.writeValueAsString(searchCriteria);
log.debug("Mamba search criteria before passing it over: {}", argumentsJson);
System.out.println("Mamba search criteria before passing it over sysout: " + argumentsJson);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}

List<MambaReportItem> mambaReportItems = getService()
.getMambaReportByCriteria(searchCriteria);

Integer totalRecords = getService()
.getMambaReportSize(searchCriteria);

System.out.println("Total records: " + totalRecords);
System.out.println("fetched records: " + mambaReportItems.size());

Integer totalPages = (int) Math.ceil((double) totalRecords / pageSize);

MambaReportPagination pagination = new MambaReportPagination();
pagination.setPageNumber(pageNumber);
pagination.setPageSize(pageSize);
pagination.setTotalRecords(totalRecords);
pagination.setTotalPages(totalPages);

return new SimpleObject()
.add("results", mambaReportItems)
.add("pagination", pagination);
}

@Override
Expand Down
Loading