-
Notifications
You must be signed in to change notification settings - Fork 133
Lab reports APIs #17116
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
Draft
ImeshRanawellaSG
wants to merge
3
commits into
development
Choose a base branch
from
lab-reports-api
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Lab reports APIs #17116
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| ); | ||
|
|
||
| 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; | ||
| } | ||
| } | ||
87 changes: 87 additions & 0 deletions
87
src/main/java/com/divudi/ws/report/ViewInvestigationResponseDTO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.