Skip to content
Draft
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 @@ -7,6 +7,7 @@

import com.divudi.core.data.BillType;
import com.divudi.core.data.PaymentMethod;
import com.divudi.core.data.lab.PatientInvestigationStatus;
import com.divudi.core.entity.Department;
import com.divudi.core.entity.Institution;
import com.divudi.core.entity.Item;
Expand Down Expand Up @@ -59,6 +60,7 @@ public class SearchKeyword {
private Department itemDepartment;
private Item item;
private Investigation investigation;
private PatientInvestigationStatus patientInvestigationStatus;
private List<BillType> billTypes;
private Long id;
// Removed legacy Institution distributor; use String fromInstitution in JSF filters
Expand Down Expand Up @@ -450,6 +452,14 @@ public void setInvestigation(Investigation investigation) {
this.investigation = investigation;
}

public PatientInvestigationStatus getPatientInvestigationStatus() {
return patientInvestigationStatus;
}

public void setPatientInvestigationStatus(PatientInvestigationStatus patientInvestigationStatus) {
this.patientInvestigationStatus = patientInvestigationStatus;
}

public Long getId() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public List<PatientInvestigation> fetchPatientInvestigations(
jpql += " and pi.encounter=:en";
params.put("en", searchKeyword.getPatientEncounter());
}

if (searchKeyword.getPatientInvestigationStatus() != null) {
jpql += " and pi.status=:pis ";
params.put("pis", searchKeyword.getPatientInvestigationStatus());
}
}

jpql += " order by pi.id desc";
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/divudi/ws/common/ApplicationConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(com.divudi.ws.lims.Lims.class);
resources.add(com.divudi.ws.lims.LimsMiddlewareController.class);
resources.add(com.divudi.ws.lims.MiddlewareController.class);
resources.add(com.divudi.ws.report.ReportApi.class);
}

}
181 changes: 181 additions & 0 deletions src/main/java/com/divudi/ws/report/ReportApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package com.divudi.ws.report;

import com.divudi.bean.common.ApiKeyController;
import com.divudi.bean.lab.PatientInvestigationController;
import com.divudi.core.data.dataStructure.SearchKeyword;
import com.divudi.core.data.lab.PatientInvestigationStatus;
import com.divudi.core.entity.ApiKey;
import com.divudi.core.entity.lab.PatientInvestigation;
import com.divudi.service.PatientInvestigationService;
import org.json.JSONObject;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

/**
* REST Web Service
*
* @author Imesh Ranawella
*/
@Path("report")
@RequestScoped
public class ReportApi {
private static final Logger LOGGER = Logger.getLogger(ReportApi.class.getName());

@EJB
private PatientInvestigationService patientInvestigationService;
@Inject
private PatientInvestigationController patientInvestigationController;
@Inject
ApiKeyController apiKeyController;

private SearchKeyword searchKeyword;

public ReportApi() {
}

/**
* Get the reports by mobile number
* Endpoint: /report/by-mobile/{mobile}
*/
@GET
@Path("by-mobile/{mobile}")
@Produces(MediaType.APPLICATION_JSON)
public Response getReportsByMobile(
@Context HttpServletRequest requestContext,
@PathParam("mobile") String mobile,
@QueryParam("fromDate") @NotNull String fromDate,
@QueryParam("toDate") @NotNull String toDate) {
final String key = requestContext.getHeader("Token");

if (!isValidKey(key)) {
JSONObject responseError = errorMessageNotValidKey();
String json = responseError.toString();
return Response.status(Response.Status.UNAUTHORIZED).entity(json).build();
}

try {
validateDates(fromDate, toDate);
validateMobileNumber(mobile);
} catch (WebApplicationException ex) {
LOGGER.warning("Validation error: " + ex.getMessage());
return Response.status(ex.getResponse().getStatus())
.entity(ex.getMessage())
.build();
}

getSearchKeyword().setPatientPhone(mobile);
getSearchKeyword().setPatientInvestigationStatus(PatientInvestigationStatus.REPORT_APPROVED);

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime fromLocal = LocalDateTime.parse(fromDate.trim(), formatter);
LocalDateTime toLocal = LocalDateTime.parse(toDate.trim(), formatter);

List<PatientInvestigation> investigations = patientInvestigationService.fetchPatientInvestigations(
Date.from(fromLocal.atZone(ZoneId.systemDefault()).toInstant()),
Date.from(toLocal.atZone(ZoneId.systemDefault()).toInstant()),
getSearchKeyword()
);
Comment thread
coderabbitai[bot] marked this conversation as resolved.

ViewReportsResponseDTO responseDTO = new ViewReportsResponseDTO(
mobile,
fromDate,
toDate,
investigations
);

return Response.status(Response.Status.OK)
.entity(responseDTO)
.build();
}

private void validateDates(final String fromDate, final String toDate) {
if (fromDate == null || fromDate.isEmpty() || toDate == null || toDate.isEmpty()) {
throw new WebApplicationException("From date and To date must be provided", Response.Status.BAD_REQUEST);
}

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
try {
LocalDateTime fromLocal = LocalDateTime.parse(fromDate.trim(), formatter);
LocalDateTime toLocal = LocalDateTime.parse(toDate.trim(), formatter);
Date from = Date.from(fromLocal.atZone(ZoneId.systemDefault()).toInstant());
Date to = Date.from(toLocal.atZone(ZoneId.systemDefault()).toInstant());

if (from.after(to)) {
throw new WebApplicationException("From date must be before To date", Response.Status.BAD_REQUEST);
}

LocalDateTime oneMonthAfterFrom = fromLocal.plusMonths(1);
if (toLocal.isAfter(oneMonthAfterFrom)) {
throw new WebApplicationException("Date range cannot exceed 1 month", Response.Status.BAD_REQUEST);
}
} catch (DateTimeParseException ex) {
LOGGER.warning("Invalid date format: " + ex.getMessage());

throw new WebApplicationException("Invalid date format. Please use ISO format (yyyy-MM-dd HH:mm:ss).", Response.Status.BAD_REQUEST);
}
}

private void validateMobileNumber(final String mobile) {
if (mobile == null || mobile.isEmpty()) {
throw new WebApplicationException("Mobile number must be provided", Response.Status.BAD_REQUEST);
}
}

private boolean isValidKey(String key) {
if (key == null || key.trim().isEmpty()) {
return false;
}
ApiKey k = apiKeyController.findApiKey(key);
if (k == null) {
return false;
}
if (k.getWebUser() == null) {
return false;
}
if (k.getWebUser().isRetired()) {
return false;
}
if (!k.getWebUser().isActivated()) {
return false;
}
if (k.getDateOfExpiary().before(new Date())) {
return false;
}
return true;
}

private JSONObject errorMessageNotValidKey() {
JSONObject jSONObjectOut = new JSONObject();
jSONObjectOut.put("code", 401);
jSONObjectOut.put("type", "error");
String e = "Not a valid key.";
jSONObjectOut.put("message", e);
return jSONObjectOut;
}

public SearchKeyword getSearchKeyword() {
if (searchKeyword == null) {
searchKeyword = new SearchKeyword();
}
return searchKeyword;
}

public void setSearchKeyword(SearchKeyword searchKeyword) {
this.searchKeyword = searchKeyword;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.divudi.ws.report;

import com.divudi.core.entity.lab.Investigation;

public class ViewInvestigationResponseDTO {
private String investigationName;
private String investigationFullName;
private String sampleName;
private String reportType;
private String category;
private String reportFormat;

public ViewInvestigationResponseDTO(final Investigation investigation) {
if (investigation == null) {
this.investigationName = null;
this.investigationFullName = null;
this.sampleName = null;
this.reportType = null;
this.category = null;
this.reportFormat = null;
return;
}

this.investigationName = investigation.getName();
this.investigationFullName = investigation.getFullName();
this.sampleName = investigation.getSample() == null
? null
: investigation.getSample().getName();
this.reportType = investigation.getReportType() == null
? null
: investigation.getReportType().name();
this.category = investigation.getCategory() == null
? null
: investigation.getCategory().getName();
this.reportFormat = investigation.getReportFormat() == null
? null
: investigation.getReportFormat().getName();
}

public String getInvestigationName() {
return investigationName;
}

public void setInvestigationName(String investigationName) {
this.investigationName = investigationName;
}

public String getSampleName() {
return sampleName;
}

public void setSampleName(String sampleName) {
this.sampleName = sampleName;
}

public String getReportType() {
return reportType;
}

public void setReportType(String reportType) {
this.reportType = reportType;
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}

public String getReportFormat() {
return reportFormat;
}

public void setReportFormat(String reportFormat) {
this.reportFormat = reportFormat;
}

public String getInvestigationFullName() {
return investigationFullName;
}

public void setInvestigationFullName(String investigationFullName) {
this.investigationFullName = investigationFullName;
}
}
Loading
Loading